-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
QueryOrchestrator.test.js
526 lines (480 loc) Β· 24.3 KB
/
QueryOrchestrator.test.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* globals describe, beforeEach, afterEach, test, expect */
import { QueryOrchestrator } from '../../src/orchestrator/QueryOrchestrator';
class MockDriver {
constructor() {
this.tables = [];
this.tablesReady = [];
this.executedQueries = [];
this.cancelledQueries = [];
}
query(query) {
this.executedQueries.push(query);
let promise = Promise.resolve([query]);
if (query.match('orders_too_big')) {
promise = promise.then((res) => new Promise(resolve => setTimeout(() => resolve(res), 3000)));
}
if (query.match('orders_delay')) {
promise = promise.then((res) => new Promise(resolve => setTimeout(() => resolve(res), 800)));
}
if (query.match(/^SELECT NOW\(\)$/)) {
promise = promise.then(() => new Date().toJSON());
}
if (this.tablesReady.find(t => query.indexOf(t) !== -1)) {
promise = promise.then(res => res.concat({ tableReady: true }));
}
promise.cancel = () => {
this.cancelledQueries.push(query);
};
return promise;
}
async getTablesQuery(schema) {
if (this.tablesQueryDelay) {
await this.delay(this.tablesQueryDelay);
}
return this.tables.map(t => ({ table_name: t.replace(`${schema}.`, '') }));
}
delay(timeout) {
return new Promise(resolve => setTimeout(() => resolve(), timeout));
}
async createSchemaIfNotExists(schema) {
this.schema = schema;
return null;
}
loadPreAggregationIntoTable(preAggregationTableName, loadSql) {
this.tables.push(preAggregationTableName.substring(0, 100));
const promise = this.query(loadSql);
const resPromise = promise.then(() => this.tablesReady.push(preAggregationTableName.substring(0, 100)));
resPromise.cancel = promise.cancel;
return resPromise;
}
async dropTable(tableName) {
this.tables = this.tables.filter(t => t !== tableName);
return this.query(`DROP TABLE ${tableName}`);
}
async downloadTable(table) {
return { rows: await this.query(`SELECT * FROM ${table}`) };
}
async tableColumnTypes() {
return [{ name: 'foo', type: 'int' }];
}
}
class ExternalMockDriver extends MockDriver {
constructor() {
super();
this.indexes = [];
}
async uploadTable(table) {
this.tables.push(table.substring(0, 100));
throw new Error('uploadTable has been called instead of uploadTableWithIndexes');
}
async uploadTableWithIndexes(table, columns, tableData, indexesSql) {
this.tables.push(table.substring(0, 100));
for (let i = 0; i < indexesSql.length; i++) {
const [query, params] = indexesSql[i].sql;
await this.query(query, params);
}
this.indexes = this.indexes.concat(indexesSql);
}
}
describe('QueryOrchestrator', () => {
let mockDriver = null;
let fooMockDriver = null;
let barMockDriver = null;
let externalMockDriver = null;
const queryOrchestrator = new QueryOrchestrator(
'TEST', (dataSource) => {
if (dataSource === 'foo') {
return fooMockDriver;
} else if (dataSource === 'bar') {
return barMockDriver;
} else {
return mockDriver;
}
},
(msg, params) => console.log(new Date().toJSON(), msg, params), {
preAggregationsOptions: {
queueOptions: {
executionTimeout: 1
},
usedTablePersistTime: 1
},
externalDriverFactory: () => externalMockDriver
}
);
beforeEach(() => {
mockDriver = new MockDriver();
fooMockDriver = new MockDriver();
barMockDriver = new MockDriver();
externalMockDriver = new ExternalMockDriver();
});
afterEach(async () => {
await queryOrchestrator.cleanup();
});
test('basic', async () => {
const query = {
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count20191101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_number_and_count20191101',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_number_and_count20191101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1', ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z']],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
}],
renewQuery: true,
requestId: 'basic'
};
const promise = queryOrchestrator.fetchQuery(query);
console.log(await queryOrchestrator.queryStage(query));
const result = await promise;
console.log(result.data[0]);
expect(result.data[0]).toMatch(/orders_number_and_count20191101_kjypcoio_5yftl5il/);
});
test('indexes', async () => {
const query = {
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count20191101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_number_and_count20191101',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_number_and_count20191101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1', ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z']],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
indexesSql: [{
sql: ['CREATE INDEX orders_number_and_count_week20191101 ON stb_pre_aggregations.orders_number_and_count20191101 ("orders__created_at_week")', []],
indexName: 'orders_number_and_count_week20191101'
}],
}],
renewQuery: true,
requestId: 'indexes'
};
const result = await queryOrchestrator.fetchQuery(query);
console.log(result.data[0]);
expect(result.data[0]).toMatch(/orders_number_and_count20191101_l3kvjcmu_khbemovd/);
expect(mockDriver.executedQueries.join(',')).toMatch(/CREATE INDEX orders_number_and_count_week20191101_l3kvjcmu_khbemovd/);
});
test('external indexes', async () => {
const query = {
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count20191101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_number_and_count20191101',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_number_and_count20191101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1', ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z']],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
indexesSql: [{
sql: ['CREATE INDEX orders_number_and_count_week20191101 ON stb_pre_aggregations.orders_number_and_count20191101 ("orders__created_at_week")', []],
indexName: 'orders_number_and_count_week20191101'
}],
external: true
}],
renewQuery: true,
requestId: 'external indexes'
};
const result = await queryOrchestrator.fetchQuery(query);
console.log(result.data[0]);
expect(result.data[0]).toMatch(/orders_number_and_count20191101_l3kvjcmu_khbemovd/);
expect(externalMockDriver.executedQueries.join(',')).toMatch(/CREATE INDEX orders_number_and_count_week20191101_l3kvjcmu_khbemovd/);
});
test('external join', async () => {
const query = {
query: 'SELECT * FROM stb_pre_aggregations.orders, stb_pre_aggregations.customers',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders', []],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
external: true,
dataSource: 'foo'
}, {
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.customers',
loadSql: ['CREATE TABLE stb_pre_aggregations.customers', []],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
external: true,
dataSource: 'bar'
}],
renewQuery: true,
requestId: 'external join',
dataSource: 'foo',
external: true
};
const result = await queryOrchestrator.fetchQuery(query);
console.log(result.data[0]);
expect(fooMockDriver.executedQueries.join(',')).toMatch(/CREATE TABLE stb_pre_aggregations.orders/);
expect(barMockDriver.executedQueries.join(',')).toMatch(/CREATE TABLE stb_pre_aggregations.customers/);
expect(externalMockDriver.tables).toContainEqual(expect.stringMatching(/stb_pre_aggregations.customers/));
expect(externalMockDriver.tables).toContainEqual(expect.stringMatching(/stb_pre_aggregations.orders/));
expect(externalMockDriver.executedQueries.join(',')).toMatch(/SELECT \* FROM stb_pre_aggregations\.orders.*, stb_pre_aggregations\.customers.*/);
});
test('non default data source pre-aggregation', async () => {
const query = {
query: 'SELECT * FROM stb_pre_aggregations.orders, stb_pre_aggregations.customers',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders', []],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
dataSource: 'foo'
}],
renewQuery: true,
requestId: 'non default data source pre-aggregation',
dataSource: 'foo',
};
const result = await queryOrchestrator.fetchQuery(query);
console.log(result.data[0]);
expect(fooMockDriver.executedQueries.join(',')).toMatch(/CREATE TABLE stb_pre_aggregations.orders/);
expect(mockDriver.executedQueries.length).toEqual(0);
});
test('non default data source query', async () => {
const query = {
query: 'SELECT * FROM orders',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
renewQuery: true,
requestId: 'non default data source query',
dataSource: 'foo',
};
const result = await queryOrchestrator.fetchQuery(query);
console.log(result.data[0]);
expect(fooMockDriver.executedQueries.join(',')).toMatch(/orders/);
expect(mockDriver.executedQueries.length).toEqual(0);
});
test('silent truncate', async () => {
const query = {
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_number_and_count_and_very_very_very_very_very_very_long20191101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1', ['2019-11-01T00:00:00Z', '2019-11-30T23:59:59Z']],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]],
}],
renewQuery: true,
requestId: 'silent truncate'
};
let thrown = true;
try {
await queryOrchestrator.fetchQuery(query);
thrown = false;
} catch (e) {
expect(e.message).toMatch(/Pre-aggregation table is not found/);
}
expect(thrown).toBe(true);
});
test('cancel pre-aggregation', async () => {
const query = {
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_number_and_count20181101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_number_and_count20181101',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_number_and_count20181101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders_too_big AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1', ['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z']],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
}],
renewQuery: true,
requestId: 'cancel pre-aggregation'
};
try {
await queryOrchestrator.fetchQuery(query);
} catch (e) {
expect(e.toString()).toMatch(/timeout/);
}
expect(mockDriver.cancelledQueries[0]).toMatch(/orders_too_big/);
});
test('save structure versions', async () => {
mockDriver.tables = [];
await queryOrchestrator.fetchQuery({
query: 'SELECT * FROM stb_pre_aggregations.orders',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: []
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders AS SELECT * FROM public.orders', []],
invalidateKeyQueries: [['SELECT 1', []]]
}],
renewQuery: true,
requestId: 'save structure versions'
});
await queryOrchestrator.fetchQuery({
query: 'SELECT * FROM stb_pre_aggregations.orders',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: []
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders AS SELECT * FROM public.orders1', []],
invalidateKeyQueries: [['SELECT 1', []]]
}],
renewQuery: true,
requestId: 'save structure versions'
});
await new Promise(resolve => setTimeout(() => resolve(), 1000));
for (let i = 0; i < 5; i++) {
await queryOrchestrator.fetchQuery({
query: 'SELECT * FROM stb_pre_aggregations.orders',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: []
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders AS SELECT * FROM public.orders', []],
invalidateKeyQueries: [['SELECT 2', []]]
}],
renewQuery: true,
requestId: 'save structure versions'
});
}
expect(mockDriver.tables).toContainEqual(expect.stringMatching(/orders_f5v4jw3p_4eysppzt/));
expect(mockDriver.tables).toContainEqual(expect.stringMatching(/orders_mjooke4_ezlvkhjl/));
});
test('intermittent empty rollup', async () => {
const firstQuery = queryOrchestrator.fetchQuery({
query: 'SELECT * FROM stb_pre_aggregations.orders_d20181102',
values: [],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: []
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_d20181102',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_d20181102 AS SELECT * FROM public.orders_delay', []],
invalidateKeyQueries: [['SELECT 2', []]]
}],
requestId: 'intermittent empty rollup'
});
queryOrchestrator.fetchQuery({
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_d20181101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_d20181101',
loadSql: [
'CREATE TABLE stb_pre_aggregations.orders_d20181101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders_delay AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1',
['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z']
],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
}],
requestId: 'intermittent empty rollup'
});
await firstQuery;
const res = await queryOrchestrator.fetchQuery({
query: 'SELECT "orders__created_at_week" "orders__created_at_week", sum("orders__count") "orders__count" FROM (SELECT * FROM stb_pre_aggregations.orders_d20181101) as partition_union WHERE ("orders__created_at_week" >= ($1::timestamptz::timestamptz AT TIME ZONE \'UTC\') AND "orders__created_at_week" <= ($2::timestamptz::timestamptz AT TIME ZONE \'UTC\')) GROUP BY 1 ORDER BY 1 ASC LIMIT 10000',
values: ['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z'],
cacheKeyQueries: {
renewalThreshold: 21600,
queries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_d20181101',
loadSql: [
'CREATE TABLE stb_pre_aggregations.orders_d20181101 AS SELECT\n date_trunc(\'week\', ("orders".created_at::timestamptz AT TIME ZONE \'UTC\')) "orders__created_at_week", count("orders".id) "orders__count", sum("orders".number) "orders__number"\n FROM\n public.orders_delay AS "orders"\n WHERE ("orders".created_at >= $1::timestamptz AND "orders".created_at <= $2::timestamptz) GROUP BY 1',
['2018-11-01T00:00:00Z', '2018-11-30T23:59:59Z']
],
invalidateKeyQueries: [['SELECT date_trunc(\'hour\', (NOW()::timestamptz AT TIME ZONE \'UTC\')) as current_hour', []]]
}],
requestId: 'intermittent empty rollup'
});
console.log(res);
expect(res.data).toContainEqual(expect.objectContaining({ tableReady: true }));
});
test('continue serve old tables cache without resetting it', async () => {
mockDriver.tablesQueryDelay = 300;
const requestId = 'continue serve old tables cache without resetting it';
const baseQuery = {
query: 'SELECT * FROM stb_pre_aggregations.orders_d20181103',
values: [],
cacheKeyQueries: {
renewalThreshold: 1,
queries: []
},
preAggregations: [{
preAggregationsSchema: 'stb_pre_aggregations',
tableName: 'stb_pre_aggregations.orders_d20181103',
loadSql: ['CREATE TABLE stb_pre_aggregations.orders_d20181103 AS SELECT * FROM public.orders', []],
invalidateKeyQueries: [['SELECT NOW()', []]],
refreshKeyRenewalThresholds: [0.001]
}]
};
// create from scratch
await queryOrchestrator.fetchQuery({
...baseQuery,
requestId: `${requestId}: create from scratch`
});
// start renew refresh as scheduled refresh does
const refresh = queryOrchestrator.fetchQuery({
...baseQuery,
renewQuery: true,
requestId: `${requestId}: start refresh`
});
await mockDriver.delay(100);
let firstResolve = null;
console.log('Starting race');
// If database has a significant delay for pre-aggregations tables fetch we should continue serve rollup cache
// instead of waiting tables fetch query to complete.
await Promise.all([
queryOrchestrator.fetchQuery({
...baseQuery,
requestId: `${requestId}: race`
}).then(() => {
if (!firstResolve) {
firstResolve = 'query';
}
}),
mockDriver.delay(150).then(() => {
if (!firstResolve) {
firstResolve = 'delay';
}
})
]);
await refresh;
expect(firstResolve).toBe('query');
});
});