Skip to content

Commit

Permalink
feat: additionally store probe ids in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Jan 30, 2025
1 parent 9e8effb commit 32e12d2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/lib/probe-validator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import TTLCache from '@isaacs/ttlcache';
import { type RedisCluster, getMeasurementRedisClient } from './redis/measurement-client.js';

export class ProbeValidator {
private readonly testIdToProbeId = new TTLCache<string, string>({ ttl: 60 * 1000 });

constructor (private readonly redis: RedisCluster) {}

addValidIds (measurementId: string, testId: string, probeUuid: string): void {
const key = ProbeValidator.getKey(measurementId, testId);
this.testIdToProbeId.set(key, probeUuid);
}

validateProbe (measurementId: string, testId: string, probeUuid: string): void {
async validateProbe (measurementId: string, testId: string, probeUuid: string): Promise<void> {
const key = ProbeValidator.getKey(measurementId, testId);
const probeId = this.testIdToProbeId.get(key);
let probeId = this.testIdToProbeId.get(key);

if (!probeId) {
probeId = await this.getProbeIdFromRedis(key);
}

if (!probeId) {
throw new Error(`Probe ID not found for key ${key}`);
Expand All @@ -19,9 +26,13 @@ export class ProbeValidator {
}
}

async getProbeIdFromRedis (key: string) {
return this.redis.hGet('gp:test-to-probe', key);
}

static getKey (measurementId: string, testId: string) {
return `${measurementId}-${testId}`;
return `${measurementId}_${testId}`;
}
}

export const probeValidator = new ProbeValidator();
export const probeValidator = new ProbeValidator(getMeasurementRedisClient());
2 changes: 1 addition & 1 deletion src/measurement/handler/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { probeValidator } from '../../lib/probe-validator.js';
const runner = getMeasurementRunner();

export const handleMeasurementProgress = (probe: Probe) => async (data: MeasurementProgressMessage): Promise<void> => {
probeValidator.validateProbe(data.measurementId, data.testId, probe.uuid);
await probeValidator.validateProbe(data.measurementId, data.testId, probe.uuid);
await runner.recordProgress(data);
};
2 changes: 1 addition & 1 deletion src/measurement/handler/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { probeValidator } from '../../lib/probe-validator.js';
const runner = getMeasurementRunner();

export const handleMeasurementResult = (probe: Probe) => async (data: MeasurementResultMessage): Promise<void> => {
probeValidator.validateProbe(data.measurementId, data.testId, probe.uuid);
await probeValidator.validateProbe(data.measurementId, data.testId, probe.uuid);
await runner.recordResult(data);
};
1 change: 1 addition & 0 deletions src/measurement/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class MeasurementStore {
const measurementWithoutDefaults = this.removeDefaults(measurement, request);

await Promise.all([
this.redis.hSet('gp:test-to-probe', Object.fromEntries(Array.from(onlineProbesMap, ([ testId, probe ]) => [ `${id}_${testId}`, probe.uuid ]))),
this.redis.hSet('gp:in-progress', id, startTime.getTime()),
this.redis.set(getMeasurementKey(id, 'probes_awaiting'), onlineProbesMap.size, { EX: probesAwaitingTtl }),
this.redis.json.set(key, '$', measurementWithoutDefaults),
Expand Down

0 comments on commit 32e12d2

Please sign in to comment.