-
Notifications
You must be signed in to change notification settings - Fork 104
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
Create consumer using rack id #2352
Merged
Merged
Changes from all commits
Commits
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,4 +1,5 @@ | ||
import { | ||
getAvailabilityZoneId, | ||
logger, | ||
} from '@dydxprotocol-indexer/base'; | ||
import { | ||
|
@@ -13,15 +14,10 @@ const groupIdPrefix: string = config.SERVICE_NAME; | |
const groupIdSuffix: string = config.KAFKA_ENABLE_UNIQUE_CONSUMER_GROUP_IDS ? `_${uuidv4()}` : ''; | ||
const groupId: string = `${groupIdPrefix}${groupIdSuffix}`; | ||
|
||
export const consumer: Consumer = kafka.consumer({ | ||
groupId, | ||
sessionTimeout: config.KAFKA_SESSION_TIMEOUT_MS, | ||
rebalanceTimeout: config.KAFKA_REBALANCE_TIMEOUT_MS, | ||
heartbeatInterval: config.KAFKA_HEARTBEAT_INTERVAL_MS, | ||
maxWaitTimeInMs: config.KAFKA_WAIT_MAX_TIME_MS, | ||
readUncommitted: false, | ||
maxBytes: 4194304, // 4MB | ||
}); | ||
// As a hack, we made this mutable since CommonJS doesn't support top level await. | ||
// Top level await would needed to fetch the az id (used as rack id). | ||
// eslint-disable-next-line import/no-mutable-exports | ||
export let consumer: Consumer | undefined; | ||
|
||
// List of functions to run per message consumed. | ||
let onMessageFunction: (topic: string, message: KafkaMessage) => Promise<void>; | ||
|
@@ -51,38 +47,51 @@ export function updateOnBatchFunction( | |
// Whether the consumer is stopped. | ||
let stopped: boolean = false; | ||
|
||
consumer.on('consumer.disconnect', async () => { | ||
export async function stopConsumer(): Promise<void> { | ||
logger.info({ | ||
at: 'consumers#disconnect', | ||
message: 'Kafka consumer disconnected', | ||
at: 'kafka-consumer#stop', | ||
message: 'Stopping kafka consumer', | ||
groupId, | ||
}); | ||
|
||
if (!stopped) { | ||
await consumer.connect(); | ||
logger.info({ | ||
at: 'kafka-consumer#disconnect', | ||
message: 'Kafka consumer reconnected', | ||
groupId, | ||
}); | ||
} else { | ||
stopped = true; | ||
await consumer!.disconnect(); | ||
} | ||
|
||
export async function initConsumer(): Promise<void> { | ||
consumer = kafka.consumer({ | ||
groupId, | ||
sessionTimeout: config.KAFKA_SESSION_TIMEOUT_MS, | ||
rebalanceTimeout: config.KAFKA_REBALANCE_TIMEOUT_MS, | ||
heartbeatInterval: config.KAFKA_HEARTBEAT_INTERVAL_MS, | ||
maxWaitTimeInMs: config.KAFKA_WAIT_MAX_TIME_MS, | ||
readUncommitted: false, | ||
maxBytes: 4194304, // 4MB | ||
rackId: await getAvailabilityZoneId(), | ||
}); | ||
|
||
consumer!.on('consumer.disconnect', async () => { | ||
logger.info({ | ||
at: 'kafka-consumer#disconnect', | ||
message: 'Not reconnecting since task is shutting down', | ||
at: 'consumers#disconnect', | ||
message: 'Kafka consumer disconnected', | ||
groupId, | ||
}); | ||
} | ||
}); | ||
|
||
export async function stopConsumer(): Promise<void> { | ||
logger.info({ | ||
at: 'kafka-consumer#stop', | ||
message: 'Stopping kafka consumer', | ||
groupId, | ||
if (!stopped) { | ||
await consumer!.connect(); | ||
logger.info({ | ||
at: 'kafka-consumer#disconnect', | ||
dydxwill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message: 'Kafka consumer reconnected', | ||
groupId, | ||
}); | ||
} else { | ||
logger.info({ | ||
at: 'kafka-consumer#disconnect', | ||
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. kafka-consumer#initConsumer? |
||
message: 'Not reconnecting since task is shutting down', | ||
groupId, | ||
}); | ||
} | ||
}); | ||
|
||
stopped = true; | ||
await consumer.disconnect(); | ||
} | ||
|
||
export async function startConsumer(batchProcessing: boolean = false): Promise<void> { | ||
|
@@ -104,7 +113,7 @@ export async function startConsumer(batchProcessing: boolean = false): Promise<v | |
}; | ||
} | ||
|
||
await consumer.run(consumerRunConfig); | ||
await consumer!.run(consumerRunConfig); | ||
|
||
logger.info({ | ||
at: 'consumers#connect', | ||
|
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
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.
kafka-consumer#initConsumer?