forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueryCache.test.ts
509 lines (429 loc) · 16.7 KB
/
QueryCache.test.ts
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
// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/querycache/QueryCache.test.ts
import moment from 'moment';
import { DataFrame, DataQueryRequest, DateTime, dateTime, TimeRange } from '@grafana/data';
import { QueryEditorMode } from '../querybuilder/shared/types';
import { PromQuery } from '../types';
import { QueryCache } from './QueryCache';
import { IncrementalStorageDataFrameScenarios } from './QueryCacheTestData';
// Will not interpolate vars!
const interpolateStringTest = (query: PromQuery) => {
return query.expr;
};
const getPrometheusTargetSignature = (request: DataQueryRequest<PromQuery>, targ: PromQuery) => {
return `${interpolateStringTest(targ)}|${targ.interval ?? request.interval}|${JSON.stringify(
request.rangeRaw ?? ''
)}|${targ.exemplar}`;
};
const mockPromRequest = (request?: Partial<DataQueryRequest<PromQuery>>): DataQueryRequest<PromQuery> => {
// Histogram
const defaultRequest: DataQueryRequest<PromQuery> = {
app: 'undefined',
requestId: '',
timezone: '',
range: {
from: moment('2023-01-30T19:33:01.332Z') as DateTime,
to: moment('2023-01-30T20:33:01.332Z') as DateTime,
raw: { from: 'now-1h', to: 'now' },
},
interval: '15s',
intervalMs: 15000,
targets: [
{
datasource: { type: 'prometheus', uid: 'OPQv8Kc4z' },
editorMode: QueryEditorMode.Code,
exemplar: false,
expr: 'sum by(le) (rate(cortex_request_duration_seconds_bucket{cluster="dev-us-central-0", job="cortex-dev-01/cortex-gw-internal", namespace="cortex-dev-01"}[$__rate_interval]))',
format: 'heatmap',
legendFormat: '{{le}}',
range: true,
refId: 'A',
utcOffsetSec: -21600,
},
],
maxDataPoints: 871,
scopedVars: {
__interval: { text: '15s', value: '15s' },
__interval_ms: { text: '15000', value: 15000 },
},
startTime: 1675110781332,
rangeRaw: { from: 'now-1h', to: 'now' },
};
return {
...defaultRequest,
...request,
};
};
describe('QueryCache: Generic', function () {
it('instantiates', () => {
const storage = new QueryCache({
getTargetSignature: () => '',
overlapString: '10m',
});
expect(storage).toBeInstanceOf(QueryCache);
});
it('will not modify or crash with empty response', () => {
const storage = new QueryCache({
getTargetSignature: () => '',
overlapString: '10m',
});
const firstFrames: DataFrame[] = [];
const secondFrames: DataFrame[] = [];
const cache = new Map<string, string>();
// start time of scenario
const firstFrom = dateTime(new Date(1675262550000));
// End time of scenario
const firstTo = dateTime(new Date(1675262550000)).add(6, 'hours');
const firstRange: TimeRange = {
from: firstFrom,
to: firstTo,
raw: {
from: 'now-6h',
to: 'now',
},
};
// Same query 2 minutes later
const numberOfSamplesLater = 4;
const interval = 30000;
const secondFrom = dateTime(new Date(1675262550000 + interval * numberOfSamplesLater));
const secondTo = dateTime(new Date(1675262550000 + interval * numberOfSamplesLater)).add(6, 'hours');
const secondRange: TimeRange = {
from: secondFrom,
to: secondTo,
raw: {
from: 'now-6h',
to: 'now',
},
};
const targetSignature = `'1=1'|${interval}|${JSON.stringify(secondRange.raw)}`;
const dashboardId = `dashid`;
const panelId = 2;
const targetIdentity = `${dashboardId}|${panelId}|A`;
cache.set(targetIdentity, targetSignature);
const firstStoredFrames = storage.procFrames(
mockPromRequest({
range: firstRange,
dashboardUID: dashboardId,
panelId: panelId,
}),
{
requests: [], // unused
targSigs: cache,
shouldCache: true,
},
firstFrames
);
const cached = storage.cache.get(targetIdentity);
expect(cached?.frames[0].fields[0].values.length).toEqual(firstFrames[0]?.fields[0]?.values?.length);
expect(firstStoredFrames[0]?.fields[0].values.length).toEqual(firstFrames[0]?.fields[0]?.values?.length);
// Should return the request frames unaltered
expect(firstStoredFrames).toEqual(firstFrames);
const secondRequest = mockPromRequest({
range: secondRange,
dashboardUID: dashboardId,
panelId: panelId,
});
const secondStoredFrames = storage.procFrames(
secondRequest,
{
requests: [], // unused
targSigs: cache,
shouldCache: true,
},
secondFrames
);
const storageLengthAfterSubsequentQuery = storage.cache.get(targetIdentity);
expect(secondStoredFrames).toEqual([]);
storageLengthAfterSubsequentQuery?.frames.forEach((dataFrame, index) => {
const secondFramesLength = secondFrames[index].fields[0].values.length;
const firstFramesLength = firstFrames[index].fields[0].values.length;
const cacheLength = dataFrame.fields[0].values.length;
// Cache can contain more, but never less
expect(cacheLength).toBeGreaterThanOrEqual(secondFramesLength + firstFramesLength - (20 + numberOfSamplesLater));
// Fewer results are sent in incremental result
expect(firstFramesLength).toBeGreaterThan(secondFramesLength);
});
});
});
describe('QueryCache: Prometheus', function () {
it('Merges incremental queries in storage', () => {
const scenarios = [
IncrementalStorageDataFrameScenarios.histogram.getSeriesWithGapAtEnd(),
IncrementalStorageDataFrameScenarios.histogram.getSeriesWithGapInMiddle(),
IncrementalStorageDataFrameScenarios.histogram.getSeriesWithGapAtStart(),
];
scenarios.forEach((scenario, index) => {
const storage = new QueryCache<PromQuery>({
getTargetSignature: getPrometheusTargetSignature,
overlapString: '10m',
});
const firstFrames = scenario.first.dataFrames as unknown as DataFrame[];
const secondFrames = scenario.second.dataFrames as unknown as DataFrame[];
const targetSignatures = new Map<string, string>();
// start time of scenario
const firstFrom = dateTime(new Date(1675262550000));
// End time of scenario
const firstTo = dateTime(new Date(1675262550000)).add(6, 'hours');
const firstRange: TimeRange = {
from: firstFrom,
to: firstTo,
raw: {
from: 'now-6h',
to: 'now',
},
};
// Same query 2 minutes later
const numberOfSamplesLater = 4;
const interval = 30000;
const secondFrom = dateTime(new Date(1675262550000 + interval * numberOfSamplesLater));
const secondTo = dateTime(new Date(1675262550000 + interval * numberOfSamplesLater)).add(6, 'hours');
const secondRange: TimeRange = {
from: secondFrom,
to: secondTo,
raw: {
from: 'now-6h',
to: 'now',
},
};
const dashboardId = `dashid--${index}`;
const panelId = 2 + index;
// This can't change
const targetIdentity = `${dashboardId}|${panelId}|A`;
const request = mockPromRequest({
range: firstRange,
dashboardUID: dashboardId,
panelId: panelId,
});
// But the signature can, and we should clean up any non-matching signatures
const targetSignature = getPrometheusTargetSignature(request, request.targets[0]);
targetSignatures.set(targetIdentity, targetSignature);
const firstStoredFrames = storage.procFrames(
request,
{
requests: [], // unused
targSigs: targetSignatures,
shouldCache: true,
},
firstFrames
);
const cached = storage.cache.get(targetIdentity);
// I would expect that the number of values received from the API should be the same as the cached values?
expect(cached?.frames[0].fields[0].values.length).toEqual(firstFrames[0].fields[0].values.length);
// Should return the request frames unaltered
expect(firstStoredFrames).toEqual(firstFrames);
const secondRequest = mockPromRequest({
range: secondRange,
dashboardUID: dashboardId,
panelId: panelId,
});
const secondStoredFrames = storage.procFrames(
secondRequest,
{
requests: [], // unused
targSigs: targetSignatures,
shouldCache: true,
},
secondFrames
);
const storageLengthAfterSubsequentQuery = storage.cache.get(targetIdentity);
storageLengthAfterSubsequentQuery?.frames.forEach((dataFrame, index) => {
const secondFramesLength = secondFrames[index].fields[0].values.length;
const firstFramesLength = firstFrames[index].fields[0].values.length;
const cacheLength = dataFrame.fields[0].values.length;
// Cache can contain more, but never less
expect(cacheLength).toBeGreaterThanOrEqual(
secondFramesLength + firstFramesLength - (20 + numberOfSamplesLater)
);
// Fewer results are sent in incremental result
expect(firstFramesLength).toBeGreaterThan(secondFramesLength);
});
// All of the new values should be the ones that were stored, this is overkill
secondFrames.forEach((frame, frameIdx) => {
frame.fields.forEach((field, fieldIdx) => {
secondFrames[frameIdx].fields[fieldIdx].values.forEach((value) => {
expect(secondStoredFrames[frameIdx].fields[fieldIdx].values).toContain(value);
});
});
});
const secondRequestModified = {
...secondRequest,
range: {
...secondRequest.range,
to: dateTime(secondRequest.range.to.valueOf() + 30000),
},
};
const cacheRequest = storage.requestInfo(secondRequestModified);
expect(cacheRequest.requests[0].targets).toEqual(secondRequestModified.targets);
expect(cacheRequest.requests[0].range.to).toEqual(secondRequestModified.range.to);
expect(cacheRequest.requests[0].range.raw).toEqual(secondRequestModified.range.raw);
expect(cacheRequest.requests[0].range.from.valueOf() - 21000000).toEqual(
secondRequestModified.range.from.valueOf()
);
expect(cacheRequest.shouldCache).toBe(true);
});
});
it('Will evict old dataframes, and use stored data when user shortens query window', () => {
const storage = new QueryCache<PromQuery>({
getTargetSignature: getPrometheusTargetSignature,
overlapString: '10m',
});
// Initial request with all data for time range
const firstFrames = IncrementalStorageDataFrameScenarios.histogram.evictionRequests.first
.dataFrames as unknown as DataFrame[];
// Shortened request 30s later
const secondFrames = IncrementalStorageDataFrameScenarios.histogram.evictionRequests.second
.dataFrames as unknown as DataFrame[];
// Now the user waits a minute and changes the query duration to just the last 5 minutes, luckily the interval hasn't changed, so we can still use the data in storage except for the latest minute
const thirdFrames = IncrementalStorageDataFrameScenarios.histogram.evictionRequests.second
.dataFrames as unknown as DataFrame[];
const cache = new Map<string, string>();
const interval = 15000;
// start time of scenario
const firstFrom = dateTime(new Date(1675107180000));
const firstTo = dateTime(new Date(1675107180000)).add(1, 'hours');
const firstRange: TimeRange = {
from: firstFrom,
to: firstTo,
raw: {
from: 'now-1h',
to: 'now',
},
};
// 30 seconds later
const secondNumberOfSamplesLater = 2;
const secondFrom = dateTime(new Date(1675107180000 + interval * secondNumberOfSamplesLater));
const secondTo = dateTime(new Date(1675107180000 + interval * secondNumberOfSamplesLater)).add(1, 'hours');
const secondRange: TimeRange = {
from: secondFrom,
to: secondTo,
raw: {
from: 'now-1h',
to: 'now',
},
};
// 1 minute + 30 seconds later, but 5 minute viewing window
const thirdNumberOfSamplesLater = 6;
const thirdFrom = dateTime(new Date(1675107180000 + interval * thirdNumberOfSamplesLater));
const thirdTo = dateTime(new Date(1675107180000 + interval * thirdNumberOfSamplesLater)).add(5, 'minutes');
const thirdRange: TimeRange = {
from: thirdFrom,
to: thirdTo,
raw: {
from: 'now-5m',
to: 'now',
},
};
// Signifier definition
const dashboardId = `dashid`;
const panelId = 200;
const targetIdentity = `${dashboardId}|${panelId}|A`;
const request = mockPromRequest({
range: firstRange,
dashboardUID: dashboardId,
panelId: panelId,
});
const requestInfo = {
requests: [], // unused
targSigs: cache,
shouldCache: true,
};
const targetSignature = `1=1|${interval}|${JSON.stringify(request.rangeRaw ?? '')}`;
cache.set(targetIdentity, targetSignature);
const firstQueryResult = storage.procFrames(request, requestInfo, firstFrames);
const firstMergedLength = firstQueryResult[0].fields[0].values.length;
const secondQueryResult = storage.procFrames(
mockPromRequest({
range: secondRange,
dashboardUID: dashboardId,
panelId: panelId,
}),
{
requests: [], // unused
targSigs: cache,
shouldCache: true,
},
secondFrames
);
const secondMergedLength = secondQueryResult[0].fields[0].values.length;
// Since the step is 15s, and the request was 30 seconds later, we should have 2 extra frames, but we should evict the first two, so we should get the same length
expect(firstMergedLength).toEqual(secondMergedLength);
expect(firstQueryResult[0].fields[0].values[2]).toEqual(secondQueryResult[0].fields[0].values[0]);
expect(firstQueryResult[0].fields[0].values[0] + 30000).toEqual(secondQueryResult[0].fields[0].values[0]);
cache.set(targetIdentity, `'1=1'|${interval}|${JSON.stringify(thirdRange.raw)}`);
storage.procFrames(
mockPromRequest({
range: thirdRange,
dashboardUID: dashboardId,
panelId: panelId,
}),
{
requests: [], // unused
targSigs: cache,
shouldCache: true,
},
thirdFrames
);
const cachedAfterThird = storage.cache.get(targetIdentity);
const storageLengthAfterThirdQuery = cachedAfterThird?.frames[0].fields[0].values.length;
expect(storageLengthAfterThirdQuery).toEqual(20);
});
it('Will build signature using target overrides', () => {
const targetInterval = '30s';
const requestInterval = '15s';
const target: PromQuery = {
datasource: { type: 'prometheus', uid: 'OPQv8Kc4z' },
editorMode: QueryEditorMode.Code,
exemplar: false,
expr: 'sum by(le) (rate(cortex_request_duration_seconds_bucket{cluster="dev-us-central-0", job="cortex-dev-01/cortex-gw-internal", namespace="cortex-dev-01"}[$__rate_interval]))',
format: 'heatmap',
interval: targetInterval,
legendFormat: '{{le}}',
range: true,
refId: 'A',
utcOffsetSec: -21600,
};
const request = mockPromRequest({
interval: requestInterval,
targets: [target],
});
const targSig = getPrometheusTargetSignature(request, target);
expect(targSig).toContain(targetInterval);
expect(targSig.includes(requestInterval)).toBeFalsy();
});
it('will not modify request with absolute duration', () => {
const request = mockPromRequest({
range: {
from: moment('2023-01-30T19:33:01.332Z') as DateTime,
to: moment('2023-01-30T20:33:01.332Z') as DateTime,
raw: { from: '2023-01-30T19:33:01.332Z', to: '2023-01-30T20:33:01.332Z' },
},
rangeRaw: { from: '2023-01-30T19:33:01.332Z', to: '2023-01-30T20:33:01.332Z' },
});
const storage = new QueryCache<PromQuery>({
getTargetSignature: getPrometheusTargetSignature,
overlapString: '10m',
});
const cacheRequest = storage.requestInfo(request);
expect(cacheRequest.requests[0]).toBe(request);
expect(cacheRequest.shouldCache).toBe(false);
});
it('mark request as shouldCache', () => {
const request = mockPromRequest();
const storage = new QueryCache<PromQuery>({
getTargetSignature: getPrometheusTargetSignature,
overlapString: '10m',
});
const cacheRequest = storage.requestInfo(request);
expect(cacheRequest.requests[0]).toBe(request);
expect(cacheRequest.shouldCache).toBe(true);
});
it('Should modify request', () => {
const request = mockPromRequest();
const storage = new QueryCache<PromQuery>({
getTargetSignature: getPrometheusTargetSignature,
overlapString: '10m',
});
const cacheRequest = storage.requestInfo(request);
expect(cacheRequest.requests[0]).toBe(request);
expect(cacheRequest.shouldCache).toBe(true);
});
});