@cubejs-backend/query-orchestrator 0.24.0 → 0.24.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/src/driver/BaseDriver.d.ts +36 -0
  3. package/dist/src/driver/BaseDriver.d.ts.map +1 -0
  4. package/dist/src/driver/BaseDriver.js +175 -0
  5. package/dist/src/driver/BaseDriver.js.map +1 -0
  6. package/dist/src/driver/index.d.ts +3 -0
  7. package/dist/src/driver/index.d.ts.map +1 -0
  8. package/dist/src/driver/index.js +15 -0
  9. package/dist/src/driver/index.js.map +1 -0
  10. package/dist/src/driver/utils.d.ts +2 -0
  11. package/dist/src/driver/utils.d.ts.map +1 -0
  12. package/dist/src/driver/utils.js +17 -0
  13. package/dist/src/driver/utils.js.map +1 -0
  14. package/dist/src/index.d.ts +3 -0
  15. package/dist/src/index.d.ts.map +1 -0
  16. package/dist/src/index.js +15 -0
  17. package/dist/src/index.js.map +1 -0
  18. package/dist/src/orchestrator/BaseQueueDriver.d.ts +4 -0
  19. package/dist/src/orchestrator/BaseQueueDriver.d.ts.map +1 -0
  20. package/dist/src/orchestrator/BaseQueueDriver.js +16 -0
  21. package/dist/src/orchestrator/BaseQueueDriver.js.map +1 -0
  22. package/dist/src/orchestrator/ContinueWaitError.d.ts +3 -0
  23. package/dist/src/orchestrator/ContinueWaitError.d.ts.map +1 -0
  24. package/dist/src/orchestrator/ContinueWaitError.js +10 -0
  25. package/dist/src/orchestrator/ContinueWaitError.js.map +1 -0
  26. package/dist/src/orchestrator/LocalCacheDriver.d.ts +8 -0
  27. package/dist/src/orchestrator/LocalCacheDriver.d.ts.map +1 -0
  28. package/dist/src/orchestrator/LocalCacheDriver.js +30 -0
  29. package/dist/src/orchestrator/LocalCacheDriver.js.map +1 -0
  30. package/dist/src/orchestrator/LocalQueueDriver.d.ts +57 -0
  31. package/dist/src/orchestrator/LocalQueueDriver.d.ts.map +1 -0
  32. package/dist/src/orchestrator/LocalQueueDriver.js +230 -0
  33. package/dist/src/orchestrator/LocalQueueDriver.js.map +1 -0
  34. package/dist/src/orchestrator/PreAggregations.d.ts +26 -0
  35. package/dist/src/orchestrator/PreAggregations.d.ts.map +1 -0
  36. package/dist/src/orchestrator/PreAggregations.js +565 -0
  37. package/dist/src/orchestrator/PreAggregations.js.map +1 -0
  38. package/dist/src/orchestrator/QueryCache.d.ts +51 -0
  39. package/dist/src/orchestrator/QueryCache.d.ts.map +1 -0
  40. package/dist/src/orchestrator/QueryCache.js +293 -0
  41. package/dist/src/orchestrator/QueryCache.js.map +1 -0
  42. package/dist/src/orchestrator/QueryOrchestrator.d.ts +27 -0
  43. package/dist/src/orchestrator/QueryOrchestrator.d.ts.map +1 -0
  44. package/dist/src/orchestrator/QueryOrchestrator.js +79 -0
  45. package/dist/src/orchestrator/QueryOrchestrator.js.map +1 -0
  46. package/dist/src/orchestrator/QueryQueue.d.ts +36 -0
  47. package/dist/src/orchestrator/QueryQueue.d.ts.map +1 -0
  48. package/dist/src/orchestrator/QueryQueue.js +351 -0
  49. package/dist/src/orchestrator/QueryQueue.js.map +1 -0
  50. package/dist/src/orchestrator/RedisCacheDriver.d.ts +12 -0
  51. package/dist/src/orchestrator/RedisCacheDriver.d.ts.map +1 -0
  52. package/dist/src/orchestrator/RedisCacheDriver.js +50 -0
  53. package/dist/src/orchestrator/RedisCacheDriver.js.map +1 -0
  54. package/dist/src/orchestrator/RedisFactory.d.ts +3 -0
  55. package/dist/src/orchestrator/RedisFactory.d.ts.map +1 -0
  56. package/dist/src/orchestrator/RedisFactory.js +45 -0
  57. package/dist/src/orchestrator/RedisFactory.js.map +1 -0
  58. package/dist/src/orchestrator/RedisPool.d.ts +10 -0
  59. package/dist/src/orchestrator/RedisPool.d.ts.map +1 -0
  60. package/dist/src/orchestrator/RedisPool.js +57 -0
  61. package/dist/src/orchestrator/RedisPool.js.map +1 -0
  62. package/dist/src/orchestrator/RedisQueueDriver.d.ts +47 -0
  63. package/dist/src/orchestrator/RedisQueueDriver.d.ts.map +1 -0
  64. package/dist/src/orchestrator/RedisQueueDriver.js +253 -0
  65. package/dist/src/orchestrator/RedisQueueDriver.js.map +1 -0
  66. package/dist/src/orchestrator/TimeoutError.d.ts +4 -0
  67. package/dist/src/orchestrator/TimeoutError.d.ts.map +1 -0
  68. package/dist/src/orchestrator/TimeoutError.js +7 -0
  69. package/dist/src/orchestrator/TimeoutError.js.map +1 -0
  70. package/dist/src/orchestrator/index.d.ts +14 -0
  71. package/dist/src/orchestrator/index.d.ts.map +1 -0
  72. package/dist/src/orchestrator/index.js +26 -0
  73. package/dist/src/orchestrator/index.js.map +1 -0
  74. package/driver/BaseDriver.js +5 -221
  75. package/driver/README.md +3 -0
  76. package/driver/utils.js +8 -12
  77. package/orchestrator/BaseQueueDriver.js +5 -8
  78. package/orchestrator/ContinueWaitError.js +6 -5
  79. package/orchestrator/LocalCacheDriver.js +5 -29
  80. package/orchestrator/LocalQueueDriver.js +5 -256
  81. package/orchestrator/PreAggregations.js +4 -764
  82. package/orchestrator/QueryCache.js +5 -381
  83. package/orchestrator/QueryOrchestrator.js +5 -100
  84. package/orchestrator/QueryQueue.js +5 -378
  85. package/orchestrator/README.md +3 -0
  86. package/orchestrator/RedisCacheDriver.js +5 -45
  87. package/orchestrator/RedisFactory.js +6 -46
  88. package/orchestrator/RedisPool.js +5 -49
  89. package/orchestrator/RedisQueueDriver.js +5 -283
  90. package/orchestrator/TimeoutError.js +6 -1
  91. package/package.json +29 -11
  92. package/docker-compose.yml +0 -7
  93. package/test/integration/QueryQueueRedis.test.js +0 -5
  94. package/test/unit/PreAggregations.test.js +0 -337
  95. package/test/unit/QueryOrchestrator.test.js +0 -373
  96. package/test/unit/QueryQueue.test.js +0 -247
@@ -1,259 +1,8 @@
1
- const R = require('ramda');
2
- const BaseQueueDriver = require('./BaseQueueDriver');
1
+ const { LocalQueueDriver } = require('../dist/src/orchestrator/LocalQueueDriver');
3
2
 
4
- class LocalQueueDriverConnection {
5
- constructor(driver, options) {
6
- this.redisQueuePrefix = options.redisQueuePrefix;
7
- this.continueWaitTimeout = options.continueWaitTimeout;
8
- this.orphanedTimeout = options.orphanedTimeout;
9
- this.heartBeatTimeout = options.heartBeatTimeout;
10
- this.concurrency = options.concurrency;
11
- this.driver = driver;
12
- this.results = driver.results;
13
- this.resultPromises = driver.resultPromises;
14
- this.queryDef = driver.queryDef;
15
- this.toProcess = driver.toProcess;
16
- this.recent = driver.recent;
17
- this.active = driver.active;
18
- this.heartBeat = driver.heartBeat;
19
- this.processingCounter = driver.processingCounter;
20
- this.processingLocks = driver.processingLocks;
21
- }
22
-
23
- getResultPromise(resultListKey) {
24
- if (!this.resultPromises[resultListKey]) {
25
- let resolveMethod = null;
26
- this.resultPromises[resultListKey] = new Promise(resolve => {
27
- resolveMethod = resolve;
28
- });
29
- this.resultPromises[resultListKey].resolve = resolveMethod;
30
- }
31
- return this.resultPromises[resultListKey];
32
- }
33
-
34
- async getResultBlocking(queryKey) {
35
- const resultListKey = this.resultListKey(queryKey);
36
- if (!this.queryDef[this.redisHash(queryKey)] && !this.resultPromises[resultListKey]) {
37
- return null;
38
- }
39
- const timeoutPromise = (timeout) => new Promise((resolve) => setTimeout(() => resolve(null), timeout));
40
-
41
- const res = await Promise.race([
42
- this.getResultPromise(resultListKey),
43
- timeoutPromise(this.continueWaitTimeout * 1000),
44
- ]);
45
-
46
- if (res) {
47
- delete this.resultPromises[resultListKey];
48
- }
49
- return res;
50
- }
51
-
52
- async getResult(queryKey) {
53
- const resultListKey = this.resultListKey(queryKey);
54
- if (this.resultPromises[resultListKey] && this.resultPromises[resultListKey].resolved) {
55
- return this.getResultBlocking(queryKey);
56
- }
57
- return null;
58
- }
59
-
60
- queueArray(queueObj, orderFilterLessThan) {
61
- return R.pipe(
62
- R.values,
63
- R.filter(orderFilterLessThan ? q => q.order < orderFilterLessThan : R.identity),
64
- R.sortBy(q => q.order),
65
- R.map(q => q.key)
66
- )(queueObj);
67
- }
68
-
69
- addToQueue(keyScore, queryKey, time, queryHandler, query, priority, options) {
70
- const queryQueueObj = {
71
- queryHandler,
72
- query,
73
- queryKey,
74
- stageQueryKey: options.stageQueryKey,
75
- priority,
76
- requestId: options.requestId,
77
- addedToQueueTime: new Date().getTime()
78
- };
79
- const key = this.redisHash(queryKey);
80
- if (!this.queryDef[key]) {
81
- this.queryDef[key] = queryQueueObj;
82
- }
83
- let added = 0;
84
- if (!this.toProcess[key]) {
85
- this.toProcess[key] = {
86
- order: keyScore,
87
- key
88
- };
89
- added = 1;
90
- }
91
- this.recent[key] = { order: time, key };
92
-
93
- return [added, null, null, Object.keys(this.toProcess).length]; // TODO nulls
94
- }
95
-
96
- getToProcessQueries() {
97
- return this.queueArray(this.toProcess);
98
- }
99
-
100
- getActiveQueries() {
101
- return this.queueArray(this.active);
102
- }
103
-
104
- async getQueryAndRemove(queryKey) {
105
- const key = this.redisHash(queryKey);
106
- const query = this.queryDef[key];
107
- delete this.active[key];
108
- delete this.heartBeat[key];
109
- delete this.toProcess[key];
110
- delete this.recent[key];
111
- delete this.queryDef[key];
112
- delete this.processingLocks[key];
113
- return [query];
114
- }
115
-
116
- async setResultAndRemoveQuery(queryKey, executionResult, processingId) {
117
- const key = this.redisHash(queryKey);
118
- if (this.processingLocks[key] !== processingId) {
119
- return false;
120
- }
121
- const promise = this.getResultPromise(this.resultListKey(queryKey));
122
- delete this.active[key];
123
- delete this.heartBeat[key];
124
- delete this.toProcess[key];
125
- delete this.recent[key];
126
- delete this.queryDef[key];
127
- delete this.processingLocks[key];
128
- promise.resolved = true;
129
- promise.resolve(executionResult);
130
- return true;
131
- }
132
-
133
- getNextProcessingId() {
134
- this.processingCounter.counter = this.processingCounter.counter ? this.processingCounter.counter + 1 : 1;
135
- return this.processingCounter.counter;
136
- }
137
-
138
- getOrphanedQueries() {
139
- return this.queueArray(this.recent, new Date().getTime() - this.orphanedTimeout * 1000);
140
- }
141
-
142
- getStalledQueries() {
143
- return this.queueArray(this.heartBeat, new Date().getTime() - this.heartBeatTimeout * 1000);
144
- }
145
-
146
- async getQueryStageState(onlyKeys) {
147
- return [this.queueArray(this.active), this.queueArray(this.toProcess), onlyKeys ? {} : R.clone(this.queryDef)];
148
- }
149
-
150
- async getQueryDef(queryKey) {
151
- return this.queryDef[this.redisHash(queryKey)];
152
- }
153
-
154
- updateHeartBeat(queryKey) {
155
- const key = this.redisHash(queryKey);
156
- if (this.heartBeat[key]) {
157
- this.heartBeat[key] = { key, order: new Date().getTime() };
158
- }
159
- }
160
-
161
- retrieveForProcessing(queryKey, processingId) {
162
- const key = this.redisHash(queryKey);
163
- let lockAcquired = false;
164
- if (!this.processingLocks[key]) {
165
- this.processingLocks[key] = processingId;
166
- lockAcquired = true;
167
- } else {
168
- return null;
169
- }
170
- let added = 0;
171
- if (Object.keys(this.active).length < this.concurrency && !this.active[key]) {
172
- this.active[key] = { key, order: processingId };
173
- added = 1;
174
- }
175
- this.heartBeat[key] = { key, order: new Date().getTime() };
176
- return [
177
- added, null, this.queueArray(this.active), Object.keys(this.toProcess).length, this.queryDef[key], lockAcquired
178
- ]; // TODO nulls
179
- }
180
-
181
- freeProcessingLock(queryKey, processingId, activated) {
182
- const key = this.redisHash(queryKey);
183
- if (this.processingLocks[key] === processingId) {
184
- delete this.processingLocks[key];
185
- if (activated) {
186
- delete this.active[key];
187
- }
188
- }
189
- }
190
-
191
- async optimisticQueryUpdate(queryKey, toUpdate, processingId) {
192
- const key = this.redisHash(queryKey);
193
- if (this.processingLocks[key] !== processingId) {
194
- return false;
195
- }
196
- this.queryDef[key] = { ...this.queryDef[key], ...toUpdate };
197
- return true;
198
- }
199
-
200
- release() {
201
- }
202
-
203
- queryRedisKey(queryKey, suffix) {
204
- return `${this.redisQueuePrefix}_${this.redisHash(queryKey)}_${suffix}`;
205
- }
206
-
207
- resultListKey(queryKey) {
208
- return this.queryRedisKey(queryKey, 'RESULT');
209
- }
210
-
211
- redisHash(queryKey) {
212
- return this.driver.redisHash(queryKey);
213
- }
214
- }
215
-
216
- const results = {};
217
- const resultPromises = {};
218
- const queryDef = {};
219
- const toProcess = {};
220
- const recent = {};
221
- const active = {};
222
- const heartBeat = {};
223
- const processingCounters = {};
224
- const processingLocks = {};
225
-
226
- class LocalQueueDriver extends BaseQueueDriver {
227
- constructor(options) {
228
- super();
229
- this.options = options;
230
- results[options.redisQueuePrefix] = results[options.redisQueuePrefix] || {};
231
- resultPromises[options.redisQueuePrefix] = resultPromises[options.redisQueuePrefix] || {};
232
- queryDef[options.redisQueuePrefix] = queryDef[options.redisQueuePrefix] || {};
233
- toProcess[options.redisQueuePrefix] = toProcess[options.redisQueuePrefix] || {};
234
- recent[options.redisQueuePrefix] = recent[options.redisQueuePrefix] || {};
235
- active[options.redisQueuePrefix] = active[options.redisQueuePrefix] || {};
236
- heartBeat[options.redisQueuePrefix] = heartBeat[options.redisQueuePrefix] || {};
237
- processingCounters[options.redisQueuePrefix] = processingCounters[options.redisQueuePrefix] || {};
238
- processingLocks[options.redisQueuePrefix] = processingLocks[options.redisQueuePrefix] || {};
239
- this.results = results[options.redisQueuePrefix];
240
- this.resultPromises = resultPromises[options.redisQueuePrefix];
241
- this.queryDef = queryDef[options.redisQueuePrefix];
242
- this.toProcess = toProcess[options.redisQueuePrefix];
243
- this.recent = recent[options.redisQueuePrefix];
244
- this.active = active[options.redisQueuePrefix];
245
- this.heartBeat = heartBeat[options.redisQueuePrefix];
246
- this.processingCounter = processingCounters[options.redisQueuePrefix];
247
- this.processingLocks = processingLocks[options.redisQueuePrefix];
248
- }
249
-
250
- createConnection() {
251
- return new LocalQueueDriverConnection(this, this.options);
252
- }
253
-
254
- release(client) {
255
- client.release();
256
- }
257
- }
3
+ process.emitWarning(
4
+ 'Using absolute import with @cubejs-backend/query-orchestrator is deprecated',
5
+ 'DeprecationWarning'
6
+ );
258
7
 
259
8
  module.exports = LocalQueueDriver;