This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdynamodb_feature_store.js
250 lines (219 loc) · 7.69 KB
/
dynamodb_feature_store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const { initState, batchWrite, queryHelper } = require('./dynamodb_helpers');
const CachingStoreWrapper = require('launchdarkly-node-server-sdk/caching_store_wrapper');
const defaultCacheTTLSeconds = 15;
// We won't try to store items whose total size exceeds this. The DynamoDB documentation says
// only "400KB", which probably means 400*1024, but to avoid any chance of trying to store a
// too-large item we are rounding it down.
const dynamoDbMaxItemSize = 400000;
// Note that the format of parameters in this implementation is a bit different than in the
// LD DynamoDB integrations for some other platforms, because we are using the
// AWS.DynamoDB.DocumentClient class, which represents values as simple types like
// string or number, rather than in the { S: stringValue } or { N: numericStringValue }
// format used by the basic AWS DynamoDB API.
function DynamoDBFeatureStore(tableName, maybeOptions) {
const options = maybeOptions || {};
const ttl = options.cacheTTL !== null && options.cacheTTL !== undefined
? options.cacheTTL
: defaultCacheTTLSeconds;
return config =>
new CachingStoreWrapper(
dynamoDBFeatureStoreInternal(tableName, options, config.logger),
ttl,
'DynamoDB'
);
}
function dynamoDBFeatureStoreInternal(tableName, options, logger) {
const state = initState(options);
const dynamoDBClient = state.client;
const prefix = state.prefix;
const store = {};
store.getInternal = function(kind, key, cb) {
dynamoDBClient.get({
TableName: tableName,
Key: {
namespace: namespaceForKind(kind),
key: key,
}
}, function(err, data) {
if (err || !data.Item) {
if (err) {
logger.error('failed to get:' + err);
}
cb(null);
} else {
cb(unmarshalItem(data.Item));
}
});
};
store.getAllInternal = function(kind, cb) {
var params = queryParamsForNamespace(kind.namespace);
queryHelper(dynamoDBClient, params).then(function (items) {
var results = {};
for (var i = 0; i < items.length; i++) {
var item = unmarshalItem(items[i]);
if (item) {
results[item.key] = item;
}
}
cb(results);
}).catch(function (err) {
logger.error('failed to get all ' + kind.namespace + ': ' + err);
cb(null);
});
};
store.initOrderedInternal = function(allData, cb) {
readExistingItems(allData)
.then(function(existingItems) {
var existingNamespaceKeys = {};
for (var i = 0; i < existingItems.length; i++) {
existingNamespaceKeys[makeNamespaceKey(existingItems[i])] = true;
}
delete existingNamespaceKeys[makeNamespaceKey(initializedToken())];
// Write all initial data (without version checks).
var ops = [];
allData.forEach(function(collection) {
collection.items.forEach(function(item) {
var key = item.key;
const dbItem = marshalItem(collection.kind, item);
if (checkSizeLimit(dbItem)) {
delete existingNamespaceKeys[namespaceForKind(collection.kind) + '$' + key];
ops.push({ PutRequest: { Item: dbItem } });
}
});
});
// Remove existing data that is not in the new list.
for (var namespaceKey in existingNamespaceKeys) {
var namespaceAndKey = namespaceKey.split('$');
ops.push({ DeleteRequest: { Key: { namespace: namespaceAndKey[0], key: namespaceAndKey[1] } } });
}
// Always write the initialized token when we initialize.
ops.push({ PutRequest: { Item: initializedToken() } });
var writePromises = batchWrite(dynamoDBClient, tableName, ops);
return Promise.all(writePromises);
})
.catch(function (err) {
logger.error('failed to initialize: ' + err);
})
.then(function() { cb && cb(); });
};
store.upsertInternal = function(kind, item, cb) {
var params = makeVersionedPutRequest(kind, item);
if (!checkSizeLimit(params.Item)) {
// We deliberately don't report this back to the SDK as an error, because we don't want to trigger any
// useless retry behavior. We just won't do the update.
cb(null, null);
return;
}
// testUpdateHook is instrumentation, used only by the unit tests
var prepare = store.testUpdateHook || function(prepareCb) { prepareCb(); };
prepare(function () {
dynamoDBClient.put(params, function(err) {
if (err) {
if (err.code !== 'ConditionalCheckFailedException') {
logger.error('failed to upsert: ' + err);
cb(err, null);
return;
}
store.getInternal(kind, item.key, function (existingItem) {
cb(null, existingItem);
});
return;
}
cb(null, item);
});
});
};
store.initializedInternal = function(cb) {
var token = initializedToken();
dynamoDBClient.get({
TableName: tableName,
Key: token,
}, function(err, data) {
if (err) {
logger.error(err);
cb(false);
return;
}
var inited = data.Item && data.Item.key === token.key;
cb(!!inited);
});
};
store.close = function() {
// The Node DynamoDB client is stateless, so close isn't a meaningful operation.
};
function queryParamsForNamespace(namespace) {
return {
TableName: tableName,
KeyConditionExpression: 'namespace = :namespace',
FilterExpression: 'attribute_not_exists(deleted) OR deleted = :deleted',
ExpressionAttributeValues: { ':namespace': prefix + namespace, ':deleted': false }
};
}
function readExistingItems(allData) {
var p = Promise.resolve([]);
allData.forEach(function(collection) {
var namespace = collection.kind.namespace;
p = p.then(function(previousItems) {
var params = queryParamsForNamespace(namespace);
return queryHelper(dynamoDBClient, params).then(function (items) {
return previousItems.concat(items);
});
});
});
return p;
}
function namespaceForKind(kind) {
return prefix + kind.namespace;
}
function initializedToken() {
var value = prefix + '$inited';
return { namespace: value, key: value };
}
function marshalItem(kind, item) {
return {
namespace: namespaceForKind(kind),
key: item.key,
version: item.version,
item: JSON.stringify(item)
};
}
function unmarshalItem(dbItem) {
var itemJson = dbItem.item;
if (itemJson) {
try {
return JSON.parse(itemJson);
} catch(e) {
logger.error('database item did not contain a valid JSON object');
}
}
return null;
}
function makeVersionedPutRequest(kind, item) {
return {
TableName: tableName,
Item: marshalItem(kind, item),
ConditionExpression: 'attribute_not_exists(version) OR version < :new_version',
ExpressionAttributeValues: {':new_version': item.version }
};
}
function makeNamespaceKey(item) {
return item.namespace + '$' + item.key;
}
function checkSizeLimit(item) {
// see: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/CapacityUnitCalculations.html
let size = 100; // fixed overhead for index data
for (const [key, value] of Object.entries(item)) {
size += key.length + Buffer.byteLength(value.toString());
}
if (size <= dynamoDbMaxItemSize) {
return true;
}
logSizeLimitError(item.namespace, item.key);
return false;
}
function logSizeLimitError(namespace, key) {
logger.error(`The item "${key}" in "${namespace}" was too large to store in DynamoDB and was dropped`);
}
return store;
}
module.exports = DynamoDBFeatureStore;