Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M2-009): PERSIST_LUA: convert SessionBuffer.persist to an atomic Lua script (against ioredis, pre-P3)

ralph870265928179
diff --git a/packages/db/src/buffers/session-buffer.test.ts b/packages/db/src/buffers/session-buffer.test.ts
index e6ba88bca..45d4b9faf 100644
--- a/packages/db/src/buffers/session-buffer.test.ts
+++ b/packages/db/src/buffers/session-buffer.test.ts
@@ -1,5 +1,13 @@
import { getRedisCache } from '@openpanel/redis';
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import {
+ afterAll,
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+} from 'vitest';
import { ch } from '../clickhouse/client';
vi.mock('../clickhouse/client', () => ({
@@ -374,4 +382,53 @@ describe('SessionBuffer', () => {
insertSpy.mockRestore();
});
+
+ // persist() is one Lua script, so a failure part-way through must leave the
+ // four session keys and the buffer list exactly as they were. A wrong-type
+ // key is the cheapest way to force that failure from outside.
+ describe('persist atomicity', () => {
+ // These tests poison shared keys on the local Redis on purpose. Clear them
+ // here too — beforeEach only protects this file, not a concurrently
+ // running stack.
+ afterEach(async () => {
+ await redis.del('session-buffer', `session:wallclock:${projectId}`);
+ });
+
+ async function expectNothingWritten() {
+ expect(await redis.get(`session:${projectId}:${deviceId}`)).toBeNull();
+ expect(
+ await redis.get(`session:profile:${projectId}:profile-1`)
+ ).toBeNull();
+ expect(await redis.sismember('session:projects', projectId)).toBe(0);
+ }
+
+ it('writes no key when the wallclock set holds the wrong type', async () => {
+ await redis.set(`session:wallclock:${projectId}`, 'not-a-zset');
+
+ const result = await sessionBuffer.ingest(makePayload());
+
+ expect(result).toBeNull();
+ await expectNothingWritten();
+ expect(await redis.llen('session-buffer')).toBe(0);
+ expect(await redis.get(`session:wallclock:${projectId}`)).toBe(
+ 'not-a-zset'
+ );
+ });
+
+ it('writes no key when the buffer list holds the wrong type', async () => {
+ // The buffer list is written last, so without an up-front type check
+ // the blob, wallclock entry and project registration would already be
+ // committed by the time this fails — Redis does not roll them back.
+ await redis.set('session-buffer', 'not-a-list');
+
+ const result = await sessionBuffer.ingest(makePayload());
+
+ expect(result).toBeNull();
+ await expectNothingWritten();
+ expect(
+ await redis.zscore(`session:wallclock:${projectId}`, deviceId)
+ ).toBeNull();
+ expect(await redis.get('session-buffer')).toBe('not-a-list');
+ });
+ });
});
diff --git a/packages/db/src/buffers/session-buffer.ts b/packages/db/src/buffers/session-buffer.ts
index 8ff03ac63..b93684d06 100644
--- a/packages/db/src/buffers/session-buffer.ts
+++ b/packages/db/src/buffers/session-buffer.ts
@@ -42,6 +42,52 @@ end
return 1
`;
+const PERSIST_KEY_COUNT = 5;
+
+// Atomic session write-back: blob + wallclock entry + project registration +
+// optional profile pointer + the ClickHouse rows, in one round trip. Returns
+// the buffer list length so the caller can decide on a flush without a second
+// call.
+//
+// Redis does not undo a script's completed writes when a later command fails,
+// so every key is type-checked before the first write — a wrong-type key then
+// aborts the script with nothing written, rather than half of it.
+const PERSIST_LUA = `
+local blobKey, wallclockKey, projectsKey, profileKey, bufferKey =
+ KEYS[1], KEYS[2], KEYS[3], KEYS[4], KEYS[5]
+local session, wallclockScore, deviceId, projectId =
+ ARGV[1], ARGV[2], ARGV[3], ARGV[4]
+
+local function assertType(key, expected)
+ if key == '' then
+ return
+ end
+ local actual = redis.call('TYPE', key)['ok']
+ if actual ~= 'none' and actual ~= expected then
+ error({ err = 'WRONGTYPE ' .. key .. ' holds ' .. actual .. ', expected ' .. expected })
+ end
+end
+
+assertType(blobKey, 'string')
+assertType(wallclockKey, 'zset')
+assertType(projectsKey, 'set')
+assertType(profileKey, 'string')
+assertType(bufferKey, 'list')
+
+redis.call('SET', blobKey, session)
+redis.call('ZADD', wallclockKey, wallclockScore, deviceId)
+redis.call('SADD', projectsKey, projectId)
+if profileKey ~= '' then
+ redis.call('SET', profileKey, deviceId)
+end
+
+local bufferLength = redis.call('LLEN', bufferKey)
+for i = 5, #ARGV do
+ bufferLength = redis.call('RPUSH', bufferKey, ARGV[i])
+end
+return bufferLength
+`;
+
export type SessionIngestResult =
| { kind: 'new'; current: IClickhouseSession }
| { kind: 'extend'; current: IClickhouseSession }
@@ -334,7 +380,7 @@ export class SessionBuffer extends BaseBuffer {
/**
* Atomic write-back: session blob + wallclock ZSET + projects SET +
- * profile index + ClickHouse buffer rows + counter.
+ * profile index + ClickHouse buffer rows, in a single Lua script.
*
* No TTLs on the blob or profile index — they are removed exclusively by
* `cleanup()` after `session_end` emission. This guarantees the reaper
@@ -347,23 +393,26 @@ export class SessionBuffer extends BaseBuffer {
) {
const projectId = current.project_id;
const deviceId = current.device_id;
- const wallClockMs = Date.now();
-
- const multi = this.redis.multi();
- multi.set(sessionKey(projectId, deviceId), JSON.stringify(current));
- multi.zadd(wallclockSetKey(projectId), wallClockMs.toString(), deviceId);
- multi.sadd(PROJECTS_SET_KEY, projectId);
-
- if (current.profile_id && current.profile_id !== current.device_id) {
- multi.set(profileIndexKey(projectId, current.profile_id), deviceId);
- }
+ const pointerKey =
+ current.profile_id && current.profile_id !== current.device_id
+ ? profileIndexKey(projectId, current.profile_id)
+ : '';
- for (const row of chRows) {
- multi.rpush(this.redisKey, JSON.stringify(row));
- }
- await multi.exec();
+ const bufferLength = (await this.redis.eval(
+ PERSIST_LUA,
+ PERSIST_KEY_COUNT,
+ sessionKey(projectId, deviceId),
+ wallclockSetKey(projectId),
+ PROJECTS_SET_KEY,
+ pointerKey,
+ this.redisKey,
+ JSON.stringify(current),
+ Date.now().toString(),
+ deviceId,
+ projectId,
+ ...chRows.map((row) => JSON.stringify(row))
+ )) as number;
- const bufferLength = await this.getBufferSize();
if (bufferLength >= this.batchSize) {
await this.tryFlush();
}