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
Fix race condition in concurrent schema deletion #11606
Merged
sijie
merged 7 commits into
apache:master
from
wuzhanpeng:bugfix_race_condition_in_concurrent_schema_deletion
Aug 17, 2021
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
325cada
add test for auto-created partitioned system topic
022fa25
Merge remote-tracking branch 'upstream/master'
fd2e80b
fix race-condition in concurrent schema deletion
e1207d3
reduce code duplication
25e8462
Merge remote-tracking branch 'upstream/master' into bugfix_race_condi…
bfb3925
Merge remote-tracking branch 'upstream/master' into bugfix_race_condi…
d9d604c
add test case
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.ExecutionException; | ||
|
@@ -58,6 +59,7 @@ | |
import org.apache.pulsar.metadata.api.MetadataCache; | ||
import org.apache.pulsar.metadata.api.MetadataSerde; | ||
import org.apache.pulsar.metadata.api.MetadataStore; | ||
import org.apache.pulsar.metadata.api.MetadataStoreException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -174,7 +176,12 @@ BookKeeper getBookKeeper() { | |
|
||
@Override | ||
public CompletableFuture<SchemaVersion> delete(String key, boolean forcefully) { | ||
return deleteSchema(key, forcefully).thenApply(LongSchemaVersion::new); | ||
return deleteSchema(key, forcefully).thenApply(version -> { | ||
if (version == null) { | ||
return null; | ||
} | ||
return new LongSchemaVersion(version); | ||
}); | ||
} | ||
|
||
@Override | ||
|
@@ -370,7 +377,21 @@ private CompletableFuture<Long> createNewSchema(String schemaId, byte[] data, by | |
|
||
@NotNull | ||
private CompletableFuture<Long> deleteSchema(String schemaId, boolean forceFully) { | ||
return (forceFully ? CompletableFuture.completedFuture(null) : getSchema(schemaId)) | ||
return (forceFully ? CompletableFuture.completedFuture(null) : getSchema(schemaId).exceptionally(t -> { | ||
if (t.getCause() != null | ||
&& (t.getCause() instanceof SchemaException) | ||
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. same as above, at least we should have one utility method to detect this case |
||
&& !((SchemaException) t.getCause()).isRecoverable()) { | ||
// Meeting NoSuchLedgerExistsException or NoSuchEntryException when reading schemas in | ||
// bookkeeper. This also means that the data has already been deleted by other operations | ||
// in deleting schema. | ||
if (log.isDebugEnabled()) { | ||
log.debug("Schema:{}'s data in bookkeeper may be deleted by other operations", schemaId); | ||
} | ||
return null; | ||
} | ||
// rethrow other cases | ||
throw t instanceof CompletionException ? (CompletionException) t : new CompletionException(t); | ||
})) | ||
.thenCompose(schemaAndVersion -> { | ||
if (!forceFully && isNull(schemaAndVersion)) { | ||
return completedFuture(null); | ||
|
@@ -405,9 +426,20 @@ private CompletableFuture<Long> deleteSchema(String schemaId, boolean forceFully | |
store.delete(path, Optional.empty()) | ||
.thenRun(() -> { | ||
future.complete(version); | ||
}).exceptionally(ex1 -> { | ||
future.completeExceptionally(ex1); | ||
return null; | ||
}).exceptionally(zkException -> { | ||
if (zkException.getCause() | ||
instanceof MetadataStoreException.NotFoundException) { | ||
// The znode has been deleted by others. | ||
// In some cases, the program may enter this logic. | ||
// Since the znode is gone, we don’t need to deal with it. | ||
if (log.isDebugEnabled()) { | ||
log.debug("No node for schema path: {}", path); | ||
} | ||
future.complete(null); | ||
} else { | ||
future.completeExceptionally(zkException); | ||
} | ||
return null; | ||
}); | ||
}); | ||
} | ||
|
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.
This condition looks a little bit hacky.
t.getCause()
, IMHO we should have some utility method that traverses the full exception chain, looking for a SchemaExceptionisRecoverable()
?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.
Thank you for your reply!
For question 1, the condition is inspired by
SchemaRegistryServiceImpl#trimDeletedSchemaAndGetList(java.lang.String)
, that is, those schemas with unrecoverable exceptions would be regarded as deleted schemas. Therefore here I continue to use the design concept.For question 2, I agree that unrecoverable SchemaException is not equivant to "Schema does not exist". However in schema storage deletion scenario, an unrecoverable exception, which includes
NoSuchLedgerException
andNoSuchEntryException
, is enough to say that the schema has already been deleted by others and we can quit the process.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.
Thanks for your clarification
What about creating an utility method and reduce code duplication?