[fix][broker] Clean up orphan ledger on concurrent initial schema creation in BookkeeperSchemaStorage#25514
Open
geniusjoe wants to merge 1 commit intoapache:masterfrom
Open
Conversation
…ation in BookkeeperSchemaStorage
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #18292
Related #18701
Motivation
When multiple requests concurrently create a schema for a brand-new topic (i.e., the schema locator z-node does not yet exist), each request first creates a BookKeeper ledger via
addNewSchemaEntryToStore, then attempts to create the schema locator z-node via CAS (createSchemaLocatorwithexpectedVersion = -1L).Only one request succeeds; the others fail with BadVersionException (because ZK MetadataStore translates NODEEXISTS to BadVersionException when expectedVersion == -1). However, the ledgers created by the failed requests were never cleaned up, resulting in orphan ledgers (dirty data) in BookKeeper.
Note that the existing
updateSchemaLocatormethod already has cleanup logic for this scenario (deleting the orphan ledger when CAS fails withBadVersionException) in #18701, but thecreateNewSchemamethod — which handles the initial schema creation path — was missing this cleanup.Modifications
BookkeeperSchemaStorage.createNewSchema: Added awhenCompletecallback aftercreateSchemaLocator. When the CAS operation fails due toAlreadyExistsExceptionorBadVersionException, the orphan BookKeeper ledger is asynchronously deleted, consistent with the existing cleanup logic inupdateSchemaLocator. The method is also refactored into three clearly commented steps for better readability.PulsarMockLedgerHandle: Added a new constructor that acceptsMap<String, byte[]> customMetadataand passes it toLedgerMetadataBuilder.withCustomMetadata(), so that mock ledgers can retain custom metadata set during creation.PulsarMockBookKeeper: UpdatedasyncCreateLedgerto forward thepropertiesparameter to the newPulsarMockLedgerHandleconstructor, enabling tests to inspect ledger custom metadata.SchemaTest: AddedtestConcurrentCreateSchemaNoOrphanLedgertest that verifies orphan ledgers are cleaned up when 16 producers concurrently create schema on a brand-new topic. The test inspects surviving BK ledgers viaPulsarMockBookKeeper.getLedgerMap()and asserts that only 1 ledger with matchingpulsar/schemaIdcustom metadata exists.Verifying this change
This change added tests and can be verified as follows:
testConcurrentCreateSchemaNoOrphanLedgerinSchemaTestthat concurrently creates 16 producers with the same AVRO schema on a brand-new topic, then verifies:admin.schemas().getAllSchemas()customMetadata["pulsar/schemaId"]matching the topic's schema name (orphan ledgers from failed concurrent creations were deleted)Does this pull request potentially affect one of the following parts: