-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(plugin-server): Improve the robustness of overflow handling tests #21315
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { Message } from 'node-rdkafka' | ||
|
||
import { buildStringMatcher } from '../../../src/config/config' | ||
import { KAFKA_EVENTS_PLUGIN_INGESTION, KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW } from '../../../src/config/kafka-topics' | ||
import { | ||
computeKey, | ||
eachBatchParallelIngestion, | ||
IngestionOverflowMode, | ||
} from '../../../src/main/ingestion-queues/batch-processing/each-batch-ingestion' | ||
import { Hub } from '../../../src/types' | ||
import { createHub } from '../../../src/utils/db/hub' | ||
import { ConfiguredLimiter } from '../../../src/utils/token-bucket' | ||
import { captureIngestionWarning } from './../../../src/worker/ingestion/utils' | ||
|
||
|
@@ -47,58 +52,55 @@ const captureEndpointEvent2 = { | |
} | ||
|
||
describe('eachBatchParallelIngestion with overflow reroute', () => { | ||
let hub: Hub | ||
let closeServer: () => Promise<void> | ||
let queue: any | ||
|
||
function createBatchWithMultipleEventsWithKeys(events: any[], timestamp?: any): any { | ||
return events.map((event) => ({ | ||
function createBatchWithMultipleEvents(events: any[], timestamp?: any, withKey: boolean = true): Message[] { | ||
return events.map((event, i) => ({ | ||
partition: 0, | ||
topic: KAFKA_EVENTS_PLUGIN_INGESTION, | ||
value: JSON.stringify(event), | ||
value: Buffer.from(JSON.stringify(event)), | ||
timestamp, | ||
offset: event.offset, | ||
key: event.team_id + ':' + event.distinct_id, | ||
offset: i, | ||
key: withKey ? computeKey(event) : null, | ||
size: 0, // irrelevant, but needed for type checking | ||
})) | ||
} | ||
|
||
beforeEach(() => { | ||
beforeEach(async () => { | ||
;[hub, closeServer] = await createHub() | ||
queue = { | ||
bufferSleep: jest.fn(), | ||
pluginsServer: { | ||
INGESTION_CONCURRENCY: 4, | ||
kafkaProducer: { | ||
produce: jest.fn(), | ||
}, | ||
db: 'database', | ||
}, | ||
pluginsServer: hub, | ||
} | ||
jest.mock('./../../../src/worker/ingestion/event-pipeline/runner') | ||
}) | ||
|
||
afterEach(async () => { | ||
await closeServer() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('reroutes events with no key to OVERFLOW topic', async () => { | ||
const batch = [ | ||
{ | ||
partition: 0, | ||
topic: KAFKA_EVENTS_PLUGIN_INGESTION, | ||
value: JSON.stringify(captureEndpointEvent1), | ||
timestamp: captureEndpointEvent1['timestamp'], | ||
offset: captureEndpointEvent1['offset'], | ||
key: null, | ||
token: 'ok', | ||
}, | ||
] | ||
const now = Date.now() | ||
const [message] = createBatchWithMultipleEvents( | ||
[captureEndpointEvent1], | ||
now, | ||
false // act as if this message was intended to be routed to overflow by capture | ||
) | ||
|
||
const consume = jest.spyOn(ConfiguredLimiter, 'consume').mockImplementation(() => false) | ||
const produce = jest.spyOn(queue.pluginsServer.kafkaProducer, 'produce') | ||
|
||
const tokenBlockList = buildStringMatcher('another_token,more_token', false) | ||
await eachBatchParallelIngestion(tokenBlockList, batch, queue, IngestionOverflowMode.Reroute) | ||
await eachBatchParallelIngestion(tokenBlockList, [message], queue, IngestionOverflowMode.Reroute) | ||
|
||
expect(consume).not.toHaveBeenCalled() | ||
expect(captureIngestionWarning).not.toHaveBeenCalled() | ||
expect(queue.pluginsServer.kafkaProducer.produce).toHaveBeenCalledWith({ | ||
expect(produce).toHaveBeenCalledWith({ | ||
topic: KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW, | ||
value: JSON.stringify(captureEndpointEvent1), | ||
timestamp: captureEndpointEvent1['timestamp'], | ||
offset: captureEndpointEvent1['offset'], | ||
value: message.value, | ||
key: null, | ||
waitForAck: true, | ||
}) | ||
|
@@ -109,23 +111,23 @@ describe('eachBatchParallelIngestion with overflow reroute', () => { | |
|
||
it('reroutes excess events to OVERFLOW topic', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't quite test this by cherry-picking the problematic change (because it also changed these tests), but #20945 (comment) would have caused this test to hang indefinitely now that it actually produces. This would have let us know something weird was going on earlier. |
||
const now = Date.now() | ||
const batch = createBatchWithMultipleEventsWithKeys([captureEndpointEvent1], now) | ||
const event = captureEndpointEvent1 | ||
const [message] = createBatchWithMultipleEvents([event], now) | ||
const consume = jest.spyOn(ConfiguredLimiter, 'consume').mockImplementation(() => false) | ||
const produce = jest.spyOn(queue.pluginsServer.kafkaProducer, 'produce') | ||
|
||
const tokenBlockList = buildStringMatcher('another_token,more_token', false) | ||
await eachBatchParallelIngestion(tokenBlockList, batch, queue, IngestionOverflowMode.Reroute) | ||
await eachBatchParallelIngestion(tokenBlockList, [message], queue, IngestionOverflowMode.Reroute) | ||
|
||
expect(consume).toHaveBeenCalledWith( | ||
captureEndpointEvent1['token'] + ':' + captureEndpointEvent1['distinct_id'], | ||
computeKey(event), // NOTE: can't use ``message.key`` here as it will already have been mutated | ||
1, | ||
now | ||
) | ||
expect(captureIngestionWarning).not.toHaveBeenCalled() | ||
expect(queue.pluginsServer.kafkaProducer.produce).toHaveBeenCalledWith({ | ||
expect(produce).toHaveBeenCalledWith({ | ||
topic: KAFKA_EVENTS_PLUGIN_INGESTION_OVERFLOW, | ||
value: JSON.stringify(captureEndpointEvent1), | ||
timestamp: captureEndpointEvent1['timestamp'], | ||
offset: captureEndpointEvent1['offset'], | ||
value: message.value, | ||
key: null, | ||
waitForAck: true, | ||
}) | ||
|
@@ -136,8 +138,9 @@ describe('eachBatchParallelIngestion with overflow reroute', () => { | |
|
||
it('does not reroute if not over capacity limit', async () => { | ||
const now = Date.now() | ||
const batch = createBatchWithMultipleEventsWithKeys([captureEndpointEvent1, captureEndpointEvent2], now) | ||
const batch = createBatchWithMultipleEvents([captureEndpointEvent1, captureEndpointEvent2], now) | ||
const consume = jest.spyOn(ConfiguredLimiter, 'consume').mockImplementation(() => true) | ||
const produce = jest.spyOn(queue.pluginsServer.kafkaProducer, 'produce') | ||
|
||
const tokenBlockList = buildStringMatcher('another_token,more_token', false) | ||
await eachBatchParallelIngestion(tokenBlockList, batch, queue, IngestionOverflowMode.Reroute) | ||
|
@@ -153,17 +156,18 @@ describe('eachBatchParallelIngestion with overflow reroute', () => { | |
now | ||
) | ||
expect(captureIngestionWarning).not.toHaveBeenCalled() | ||
expect(queue.pluginsServer.kafkaProducer.produce).not.toHaveBeenCalled() | ||
expect(produce).not.toHaveBeenCalled() | ||
// Event is processed | ||
expect(runEventPipeline).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('does drop events from blocked tokens', async () => { | ||
const now = Date.now() | ||
const batch = createBatchWithMultipleEventsWithKeys( | ||
const batch = createBatchWithMultipleEvents( | ||
[captureEndpointEvent1, captureEndpointEvent2, captureEndpointEvent1], | ||
now | ||
) | ||
const produce = jest.spyOn(queue.pluginsServer.kafkaProducer, 'produce') | ||
const consume = jest.spyOn(ConfiguredLimiter, 'consume').mockImplementation(() => true) | ||
|
||
const tokenBlockList = buildStringMatcher('mytoken,another_token', false) | ||
|
@@ -176,7 +180,7 @@ describe('eachBatchParallelIngestion with overflow reroute', () => { | |
now | ||
) | ||
expect(captureIngestionWarning).not.toHaveBeenCalled() | ||
expect(queue.pluginsServer.kafkaProducer.produce).not.toHaveBeenCalled() | ||
expect(produce).not.toHaveBeenCalled() | ||
expect(runEventPipeline).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were undefined so they weren't being considered in this assertion.
timestamp
can be provided toproduce
, if we want to preserve it across rerouting.