-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSplitsCacheInRedis.ts
More file actions
285 lines (248 loc) · 10.5 KB
/
SplitsCacheInRedis.ts
File metadata and controls
285 lines (248 loc) · 10.5 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
import { KeyBuilderSS } from '../KeyBuilderSS';
import { ILogger } from '../../logger/types';
import { LOG_PREFIX } from './constants';
import { ISplit, ISplitFiltersValidation } from '../../dtos/types';
import { AbstractSplitsCacheAsync } from '../AbstractSplitsCacheAsync';
import { returnDifference } from '../../utils/lang/sets';
import type { RedisAdapter } from './RedisAdapter';
/**
* Discard errors for an answer of multiple operations.
*/
function processPipelineAnswer(results: Array<[Error | null, string]>): string[] {
return results.reduce((accum: string[], errValuePair: [Error | null, string]) => {
if (errValuePair[0] === null) accum.push(errValuePair[1]);
return accum;
}, []);
}
/**
* ISplitsCacheAsync implementation that stores split definitions in Redis.
* Supported by Node.js
*/
export class SplitsCacheInRedis extends AbstractSplitsCacheAsync {
private readonly log: ILogger;
private readonly redis: RedisAdapter;
private readonly keys: KeyBuilderSS;
private redisError?: string;
private readonly flagSetsFilter: string[];
constructor(log: ILogger, keys: KeyBuilderSS, redis: RedisAdapter, splitFiltersValidation?: ISplitFiltersValidation) {
super();
this.log = log;
this.redis = redis;
this.keys = keys;
this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
// There is no need to listen for redis 'error' event, because in that case ioredis calls will be rejected and handled by redis storage adapters.
// But it is done just to avoid getting the ioredis message `Unhandled error event`.
this.redis.on('error', (e) => {
this.redisError = e;
});
this.redis.on('connect', () => {
this.redisError = undefined;
});
}
private _decrementCounts(split: ISplit) {
const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
return this.redis.decr(ttKey).then(count => {
if (count === 0) return this.redis.del(ttKey);
});
}
private _incrementCounts(split: ISplit) {
const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
return this.redis.incr(ttKey);
}
private _updateFlagSets(featureFlagName: string, flagSetsOfRemovedFlag?: string[], flagSetsOfAddedFlag?: string[]) {
const removeFromFlagSets = returnDifference(flagSetsOfRemovedFlag, flagSetsOfAddedFlag);
let addToFlagSets = returnDifference(flagSetsOfAddedFlag, flagSetsOfRemovedFlag);
if (this.flagSetsFilter.length > 0) {
addToFlagSets = addToFlagSets.filter(flagSet => {
return this.flagSetsFilter.some(filterFlagSet => filterFlagSet === flagSet);
});
}
const items = [featureFlagName];
return Promise.all([
...removeFromFlagSets.map(flagSetName => this.redis.srem(this.keys.buildFlagSetKey(flagSetName), items)),
...addToFlagSets.map(flagSetName => this.redis.sadd(this.keys.buildFlagSetKey(flagSetName), items))
]);
}
/**
* Add a given split.
* The returned promise is resolved when the operation success
* or rejected if it fails (e.g., redis operation fails)
*/
addSplit(name: string, split: ISplit): Promise<boolean> {
const splitKey = this.keys.buildSplitKey(name);
return this.redis.get(splitKey).then(splitFromStorage => {
// handling parsing error
let parsedPreviousSplit: ISplit, stringifiedNewSplit;
try {
parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
stringifiedNewSplit = JSON.stringify(split);
} catch (e) {
throw new Error('Error parsing feature flag definition: ' + e);
}
return this.redis.set(splitKey, stringifiedNewSplit).then(() => {
// avoid unnecessary increment/decrement operations
if (parsedPreviousSplit && parsedPreviousSplit.trafficTypeName === split.trafficTypeName) return;
// update traffic type counts
return this._incrementCounts(split).then(() => {
if (parsedPreviousSplit) return this._decrementCounts(parsedPreviousSplit);
});
}).then(() => this._updateFlagSets(name, parsedPreviousSplit && parsedPreviousSplit.sets, split.sets));
}).then(() => true);
}
/**
* Add a list of splits.
* The returned promise is resolved when the operation success
* or rejected if it fails (e.g., redis operation fails)
*/
addSplits(entries: [string, ISplit][]): Promise<boolean[]> {
return Promise.all(entries.map(keyValuePair => this.addSplit(keyValuePair[0], keyValuePair[1])));
}
/**
* Remove a given split.
* The returned promise is resolved when the operation success, with 1 or 0 indicating if the split existed or not.
* or rejected if it fails (e.g., redis operation fails).
*/
removeSplit(name: string) {
return this.getSplit(name).then((split) => {
if (split) {
return this._decrementCounts(split).then(() => this._updateFlagSets(name, split.sets));
}
}).then(() => {
return this.redis.del(this.keys.buildSplitKey(name));
});
}
/**
* Remove a list of splits.
* The returned promise is resolved when the operation success,
* or rejected if it fails (e.g., redis operation fails).
*/
removeSplits(names: string[]): Promise<any> {
return Promise.all(names.map(name => this.removeSplit(name)));
}
/**
* Get split definition or null if it's not defined.
* Returned promise is rejected if redis operation fails.
*/
getSplit(name: string): Promise<ISplit | null> {
if (this.redisError) {
this.log.error(LOG_PREFIX + this.redisError);
return Promise.reject(this.redisError);
}
return this.redis.get(this.keys.buildSplitKey(name))
.then(maybeSplit => maybeSplit && JSON.parse(maybeSplit));
}
/**
* Set till number.
* The returned promise is resolved when the operation success,
* or rejected if it fails.
*/
setChangeNumber(changeNumber: number): Promise<boolean> {
return this.redis.set(this.keys.buildSplitsTillKey(), changeNumber + '').then(
status => status === 'OK'
);
}
/**
* Get till number or -1 if it's not defined.
* The returned promise is resolved with the changeNumber or -1 if it doesn't exist or a redis operation fails.
* The promise will never be rejected.
*/
getChangeNumber(): Promise<number> {
return this.redis.get(this.keys.buildSplitsTillKey()).then((value: string | null) => {
const i = parseInt(value as string, 10);
return isNaNNumber(i) ? -1 : i;
}).catch((e) => {
this.log.error(LOG_PREFIX + 'Could not retrieve changeNumber from storage. Error: ' + e);
return -1;
});
}
/**
* Get list of all split definitions.
* The returned promise is resolved with the list of split definitions,
* or rejected if redis operation fails.
*/
// @TODO we need to benchmark which is the maximun number of commands we could pipeline without kill redis performance.
getAll(): Promise<ISplit[]> {
return this.redis.keys(this.keys.searchPatternForSplitKeys())
.then((listOfKeys) => this.redis.pipeline(listOfKeys.map(k => ['get', k])).exec())
.then(processPipelineAnswer)
.then((splitDefinitions) => splitDefinitions.map((splitDefinition) => {
return JSON.parse(splitDefinition);
}));
}
/**
* Get list of split names.
* The returned promise is resolved with the list of split names,
* or rejected if redis operation fails.
*/
getSplitNames(): Promise<string[]> {
return this.redis.keys(this.keys.searchPatternForSplitKeys()).then(
(listOfKeys) => listOfKeys.map(this.keys.extractKey)
);
}
/**
* Get list of feature flag names related to a given list of flag set names.
* The returned promise is resolved with the list of feature flag names per flag set,
* or rejected if the pipelined redis operation fails (e.g., timeout).
*/
getNamesByFlagSets(flagSets: string[]): Promise<Set<string>[]> {
return this.redis.pipeline(flagSets.map(flagSet => ['smembers', this.keys.buildFlagSetKey(flagSet)])).exec()
.then((results) => results.map(([e, value], index) => {
if (e === null) return value;
this.log.error(LOG_PREFIX + `Could not read result from get members of flag set ${flagSets[index]} due to an error: ${e}`);
}))
.then(namesByFlagSets => namesByFlagSets.map(namesByFlagSet => new Set(namesByFlagSet)));
}
/**
* Check traffic type existence.
* The returned promise is resolved with a boolean indicating whether the TT exist or not.
* In case of redis operation failure, the promise resolves with a true value, assuming that the TT might exist.
* It will never be rejected.
*/
trafficTypeExists(trafficType: string): Promise<boolean> {
// If there is a number there should be > 0, otherwise the TT is considered as not existent.
return this.redis.get(this.keys.buildTrafficTypeKey(trafficType))
.then((ttCount: string | null | number) => {
if (ttCount === null) return false; // if entry doesn't exist, means that TT doesn't exist
ttCount = parseInt(ttCount as string, 10);
if (!isFiniteNumber(ttCount) || ttCount < 0) {
this.log.info(LOG_PREFIX + `Could not validate traffic type existence of ${trafficType} due to data corruption of some sorts.`);
return false;
}
return ttCount > 0;
})
.catch(e => {
this.log.error(LOG_PREFIX + `Could not validate traffic type existence of ${trafficType} due to an error: ${e}.`);
// If there is an error, bypass the validation so the event can get tracked.
return true;
});
}
// @TODO remove or implement. It is not being used.
clear() {
return Promise.resolve();
}
/**
* Fetches multiple splits definitions.
* Returned promise is rejected if redis operation fails.
*/
getSplits(names: string[]): Promise<Record<string, ISplit | null>> {
if (this.redisError) {
this.log.error(LOG_PREFIX + this.redisError);
return Promise.reject(this.redisError);
}
const keys = names.map(name => this.keys.buildSplitKey(name));
return this.redis.mget(...keys)
.then(splitDefinitions => {
const splits: Record<string, ISplit | null> = {};
names.forEach((name, idx) => {
const split = splitDefinitions[idx];
splits[name] = split && JSON.parse(split);
});
return Promise.resolve(splits);
})
.catch(e => {
this.log.error(LOG_PREFIX + `Could not grab feature flags due to an error: ${e}.`);
return Promise.reject(e);
});
}
}