-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: record events in memory in mrbv2 (#27734)
- Loading branch information
Showing
21 changed files
with
2,027 additions
and
571 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
plugin-server/src/main/ingestion-queues/session-recording-v2/kafka/offset-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { TopicPartitionOffset } from 'node-rdkafka' | ||
|
||
import { SessionBatchRecorderInterface } from '../sessions/session-batch-recorder' | ||
import { MessageWithTeam } from '../teams/types' | ||
|
||
interface PartitionOffset { | ||
partition: number | ||
offset: number | ||
} | ||
|
||
type CommitOffsetsCallback = (offsets: TopicPartitionOffset[]) => Promise<void> | ||
|
||
class OffsetTrackingSessionBatchRecorderWrapper implements SessionBatchRecorderInterface { | ||
constructor( | ||
private readonly recorder: SessionBatchRecorderInterface, | ||
private readonly offsetManager: KafkaOffsetManager | ||
) {} | ||
|
||
public record(message: MessageWithTeam): number { | ||
const bytesWritten = this.recorder.record(message) | ||
this.offsetManager.trackOffset(message.message.metadata) | ||
return bytesWritten | ||
} | ||
|
||
public async flush(): Promise<void> { | ||
await this.recorder.flush() | ||
} | ||
|
||
public discardPartition(partition: number): void { | ||
this.recorder.discardPartition(partition) | ||
this.offsetManager.discardPartition(partition) | ||
} | ||
|
||
public get size(): number { | ||
return this.recorder.size | ||
} | ||
} | ||
|
||
export class KafkaOffsetManager { | ||
private partitionOffsets: Map<number, number> = new Map() | ||
|
||
constructor(private readonly commitOffsets: CommitOffsetsCallback, private readonly topic: string) {} | ||
|
||
public wrapBatch(recorder: SessionBatchRecorderInterface): SessionBatchRecorderInterface { | ||
return new OffsetTrackingSessionBatchRecorderWrapper(recorder, this) | ||
} | ||
|
||
public trackOffset({ partition, offset }: PartitionOffset): void { | ||
// We track the next offset to process | ||
this.partitionOffsets.set(partition, offset + 1) | ||
} | ||
|
||
public discardPartition(partition: number): void { | ||
this.partitionOffsets.delete(partition) | ||
} | ||
|
||
public async commit(): Promise<void> { | ||
const topicPartitionOffsets: TopicPartitionOffset[] = [] | ||
|
||
for (const [partition, offset] of this.partitionOffsets.entries()) { | ||
topicPartitionOffsets.push({ | ||
topic: this.topic, | ||
partition, | ||
offset, | ||
}) | ||
} | ||
|
||
if (topicPartitionOffsets.length > 0) { | ||
await this.commitOffsets(topicPartitionOffsets) | ||
this.partitionOffsets.clear() | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/ingestion-queues/session-recording-v2/sessions/blackhole-session-batch-writer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { PassThrough } from 'stream' | ||
|
||
import { SessionBatchWriter, StreamWithFinish } from './session-batch-recorder' | ||
|
||
export class BlackholeSessionBatchWriter implements SessionBatchWriter { | ||
public async open(): Promise<StreamWithFinish> { | ||
return Promise.resolve({ | ||
stream: new PassThrough(), | ||
finish: async () => Promise.resolve(), | ||
}) | ||
} | ||
} |
Oops, something went wrong.