You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/11/02 06:46:50 UTC
[GitHub] [pulsar] aloyszhang opened a new pull request, #18293: fix creating lots of ledgers for the same schema
aloyszhang opened a new pull request, #18293:
URL: https://github.com/apache/pulsar/pull/18293
Fixes #18292
### Motivation
fix creating lots of ledgers for the same schema
### Modifications
fix the problem described in #18292
### Documentation
<!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
- [ ] `doc` <!-- Your PR contains doc changes. Please attach the local preview screenshots (run `sh start.sh` at `pulsar/site2/website`) to your PR description, or else your PR might not get merged. -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
- [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->
### Matching PR in forked repository
PR in forked repository: <!-- ENTER URL HERE -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015157119
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
Review Comment:
You're right.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codecov-commenter commented on pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1301745568
# [Codecov](https://codecov.io/gh/apache/pulsar/pull/18293?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#18293](https://codecov.io/gh/apache/pulsar/pull/18293?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (bd97e2c) into [master](https://codecov.io/gh/apache/pulsar/commit/6c65ca0d8a80bfaaa4d5869e0cea485f5c94369b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (6c65ca0) will **increase** coverage by `3.85%`.
> The diff coverage is `62.99%`.
[](https://codecov.io/gh/apache/pulsar/pull/18293?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
```diff
@@ Coverage Diff @@
## master #18293 +/- ##
============================================
+ Coverage 34.91% 38.76% +3.85%
- Complexity 5707 8317 +2610
============================================
Files 607 687 +80
Lines 53396 67447 +14051
Branches 5712 7223 +1511
============================================
+ Hits 18644 26147 +7503
- Misses 32119 38273 +6154
- Partials 2633 3027 +394
```
| Flag | Coverage Δ | |
|---|---|---|
| unittests | `38.76% <62.99%> (+3.85%)` | :arrow_up: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://codecov.io/gh/apache/pulsar/pull/18293?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...pulsar/broker/admin/impl/PersistentTopicsBase.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9hZG1pbi9pbXBsL1BlcnNpc3RlbnRUb3BpY3NCYXNlLmphdmE=) | `59.22% <ø> (+47.82%)` | :arrow_up: |
| [.../pulsar/broker/service/AbstractBaseDispatcher.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL0Fic3RyYWN0QmFzZURpc3BhdGNoZXIuamF2YQ==) | `48.17% <ø> (+2.31%)` | :arrow_up: |
| [...che/pulsar/broker/service/BacklogQuotaManager.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL0JhY2tsb2dRdW90YU1hbmFnZXIuamF2YQ==) | `12.39% <0.00%> (+2.91%)` | :arrow_up: |
| [...rg/apache/pulsar/broker/service/BrokerService.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL0Jyb2tlclNlcnZpY2UuamF2YQ==) | `56.39% <ø> (+8.38%)` | :arrow_up: |
| [.../service/SystemTopicBasedTopicPoliciesService.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL1N5c3RlbVRvcGljQmFzZWRUb3BpY1BvbGljaWVzU2VydmljZS5qYXZh) | `62.57% <0.00%> (+10.98%)` | :arrow_up: |
| [.../pulsar/broker/stats/BrokerOperabilityMetrics.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zdGF0cy9Ccm9rZXJPcGVyYWJpbGl0eU1ldHJpY3MuamF2YQ==) | `98.21% <ø> (+5.56%)` | :arrow_up: |
| [...g/apache/pulsar/compaction/CompactedTopicImpl.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NvbXBhY3Rpb24vQ29tcGFjdGVkVG9waWNJbXBsLmphdmE=) | `69.28% <0.00%> (+58.57%)` | :arrow_up: |
| [.../java/org/apache/pulsar/client/impl/ClientCnx.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NsaWVudENueC5qYXZh) | `0.00% <0.00%> (ø)` | |
| [...va/org/apache/pulsar/client/impl/ConsumerBase.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWNsaWVudC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9pbXBsL0NvbnN1bWVyQmFzZS5qYXZh) | `0.00% <0.00%> (ø)` | |
| [...broker/service/schema/BookkeeperSchemaStorage.java](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL3NjaGVtYS9Cb29ra2VlcGVyU2NoZW1hU3RvcmFnZS5qYXZh) | `74.76% <33.33%> (+4.51%)` | :arrow_up: |
| ... and [700 more](https://codecov.io/gh/apache/pulsar/pull/18293/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015351285
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistry.java:
##########
@@ -37,6 +37,9 @@ public interface SchemaRegistry extends AutoCloseable {
CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy);
+ CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
Review Comment:
fixed
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryStats.java:
##########
@@ -36,6 +36,7 @@ class SchemaRegistryStats implements AutoCloseable, Runnable {
private final Counter getOpsFailedCounter;
private final Counter putOpsFailedCounter;
+ private final Counter putOpsRetryCounter;
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016441750
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry + 1 : 1;
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry {} times",
+ t.getCause().getMessage(), schemaId, retryTime);
+ }
+ this.stats.recordPutRetry(schemaId);
+ putSchemaIfAbsent(schemaId, schema, strategy, start, retryTime)
+ .thenAccept(persistentFuture::complete)
+ .exceptionally(ex2 -> {
+ persistentFuture.completeExceptionally(ex2);
+ return null;
+ });
+
+ } else {
+ log.error("[{}] Put schema failed", schemaId, t.getCause());
+ this.stats.recordPutFailed(schemaId);
+ persistentFuture.completeExceptionally(t);
+ }
+ return null;
+ });
Review Comment:
I'll try this, thanks for your suggestion
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016075114
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
Review Comment:
I see the current code is that when finds `BadVersion` then tries again until it succeeds, maybe we can rewrite it like this:
```java
long start = startTime > 0 ? startTime : this.clock.millis();
return schemaStorage.put(schemaId, info.toByteArray(), context).handleAsync((sv, ex) -> {
if (ex != null){
return CompletableFuture.completedFuture(sv);
}
if (xx instanceof xxx){
return putSchemaIfAbsent(schemaId, schema, strategy, start, retryTime + 1);
}
return CompletableFuture.failedFuture(ex);
});
```
this makes it easier to read.
BTW, This change does not seem to solve the problem to be solved
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry + 1 : 1;
Review Comment:
seems `retry` always `>=0`, maybe we can write this line like this: `int retryTime = retry+1`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015252521
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -315,21 +299,7 @@ private CompletableFuture<Long> putSchema(String schemaId, byte[] data, byte[] h
createNewSchema(schemaId, data, hash)
.thenAccept(future::complete)
.exceptionally(ex -> {
- if (ex.getCause() instanceof AlreadyExistsException
- || ex.getCause() instanceof BadVersionException) {
- // There was a race condition on the schema creation. Since it has now been created,
- // retry the whole operation so that we have a chance to recover without bubbling error
- // back to producer/consumer
- putSchema(schemaId, data, hash)
- .thenAccept(future::complete)
- .exceptionally(ex2 -> {
- future.completeExceptionally(ex2);
- return null;
- });
- } else {
- // For other errors, just fail the operation
- future.completeExceptionally(ex);
- }
+ future.completeExceptionally(ex);
Review Comment:
ok
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1011335990
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -580,6 +550,7 @@ private CompletableFuture<LedgerHandle> createLedger(String schemaId) {
@NotNull
private CompletableFuture<LedgerHandle> openLedger(Long ledgerId) {
final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
+ final long start = System.nanoTime();
Review Comment:
removed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015254327
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
+ .thenAccept(persistentFuture::complete)
+ .exceptionally(ex2 -> {
+ persistentFuture.completeExceptionally(ex2);
+ return null;
+ });
+
+ } else {
+ log.error("[{}] Put schema failed", schemaId);
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] AnonHxy commented on pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
AnonHxy commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1306736985
LGTM
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] congbobo184 commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
congbobo184 commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015148231
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -289,22 +287,8 @@ private CompletableFuture<Long> putSchema(String schemaId, byte[] data, byte[] h
updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash)
.thenAccept(future::complete)
.exceptionally(ex -> {
- if (ex.getCause() instanceof BadVersionException) {
- // There was a race condition on the schema creation.
- // Since it has now been created,
- // retry the whole operation so that we have a chance to
- // recover without bubbling error
- putSchema(schemaId, data, hash)
- .thenAccept(future::complete)
- .exceptionally(ex2 -> {
- future.completeExceptionally(ex2);
- return null;
- });
- } else {
- // For other errors, just fail the operation
- future.completeExceptionally(ex);
- }
- return null;
+ future.completeExceptionally(ex);
+ return null;
Review Comment:
return readSchemaEntry(locator.getIndexList().get(0).getPosition())
.thenCompose(schemaEntry -> addNewSchemaEntryToStore(schemaId, locator.getIndexList(), data)
.thenCompose(position ->
updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash))
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -315,21 +299,7 @@ private CompletableFuture<Long> putSchema(String schemaId, byte[] data, byte[] h
createNewSchema(schemaId, data, hash)
.thenAccept(future::complete)
.exceptionally(ex -> {
- if (ex.getCause() instanceof AlreadyExistsException
- || ex.getCause() instanceof BadVersionException) {
- // There was a race condition on the schema creation. Since it has now been created,
- // retry the whole operation so that we have a chance to recover without bubbling error
- // back to producer/consumer
- putSchema(schemaId, data, hash)
- .thenAccept(future::complete)
- .exceptionally(ex2 -> {
- future.completeExceptionally(ex2);
- return null;
- });
- } else {
- // For other errors, just fail the operation
- future.completeExceptionally(ex);
- }
+ future.completeExceptionally(ex);
Review Comment:
return createNewSchema(schemaId, data, hash);
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang closed pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang closed pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
URL: https://github.com/apache/pulsar/pull/18293
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016090482
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
Review Comment:
> BTW, This change does not seem to solve the problem to be solved
What does this mean?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016119443
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
Sounds good.
One point to clarify is "creating lot of versions" will create lots of new schema ledger too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] congbobo184 commented on pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
congbobo184 commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1318551241
@aloyszhang hi, I move this PR to release/2.9.5, if you have any questions, please ping me. thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1324966105
@codelipenghui PTAL, thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
codelipenghui commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1028740950
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -126,33 +128,54 @@ public CompletableFuture<StoredSchema> get(String key, SchemaVersion version) {
@Override
public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key) {
+ return getAll(key, null);
+ }
+
+ public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key,
+ LocatorEntry locator) {
Review Comment:
```suggestion
public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key, LocatorEntry locator) {
```
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +189,101 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
+ BookkeeperSchemaStorage bkSchemaStorage = (BookkeeperSchemaStorage) schemaStorage;
Review Comment:
The `schemaRegistryStorageClassName` is configurable. Users can have their implementations.
```
### --- Schema storage --- ###
# The schema storage implementation used by this broker
schemaRegistryStorageClassName=org.apache.pulsar.broker.service.schema.BookkeeperSchemaStorageFactory
```
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -126,33 +128,54 @@ public CompletableFuture<StoredSchema> get(String key, SchemaVersion version) {
@Override
public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key) {
+ return getAll(key, null);
+ }
+
+ public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key,
+ LocatorEntry locator) {
CompletableFuture<List<CompletableFuture<StoredSchema>>> result = new CompletableFuture<>();
- getLocator(key).thenAccept(locator -> {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Get all schemas - locator: {}", key, locator);
- }
- if (!locator.isPresent()) {
- result.complete(Collections.emptyList());
- return;
- }
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Get all schemas - locator: {}", key, locator);
+ }
+ List<CompletableFuture<StoredSchema>> list = new ArrayList<>();
+ if (locator == null) {
Review Comment:
We can just add
```java
CompletableFuture<Optional<LocatorEntry>> locatorFuture =
locator == null ? getLocator(key) : CompletableFuture.completedFuture(Optional.of(locator));
locatorFuture.thenAccept(...)
```
to reduce the duplicated code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015195639
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
Review Comment:
> Call putSchemaIfAbsent here will retry once, but it will not record metrics this.stats.recordPutLatency or this.stats.recordPutFailed.
`this.stats.recordPutLatenc` is recorded incorrectly since `long start = this.clock.millis()` ;
`this.stats.recordPutFailed` has been recorded when exceptions (not `AlreadyExistsException` or `BadVersionException`) happened, so no need to record again.
> Maybe we can add a new method putSchemaWithRetryIfAbsent, this new method only wraps the original method putSchemaIfAbsent
Agree, and add a new metric to indicate `putOpsRetryCounter` retry times
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] gaoran10 commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
gaoran10 commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015212888
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
Review Comment:
Could the exception be null?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
+ .thenAccept(persistentFuture::complete)
+ .exceptionally(ex2 -> {
+ persistentFuture.completeExceptionally(ex2);
+ return null;
+ });
+
+ } else {
+ log.error("[{}] Put schema failed", schemaId);
Review Comment:
Maybe we can get the original error and print the stack, and it seems that the `persistentFuture` should be completed exceptionally here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] congbobo184 commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
congbobo184 commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015303193
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistry.java:
##########
@@ -37,6 +37,9 @@ public interface SchemaRegistry extends AutoCloseable {
CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy);
+ CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
Review Comment:
don't need add a new interface
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -284,56 +282,12 @@ private CompletableFuture<Long> putSchema(String schemaId, byte[] data, byte[] h
return readSchemaEntry(locator.getIndexList().get(0).getPosition())
.thenCompose(schemaEntry -> addNewSchemaEntryToStore(schemaId,
locator.getIndexList(), data).thenCompose(
- position -> {
- CompletableFuture<Long> future = new CompletableFuture<>();
- updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash)
- .thenAccept(future::complete)
- .exceptionally(ex -> {
- if (ex.getCause() instanceof BadVersionException) {
- // There was a race condition on the schema creation.
- // Since it has now been created,
- // retry the whole operation so that we have a chance to
- // recover without bubbling error
- putSchema(schemaId, data, hash)
- .thenAccept(future::complete)
- .exceptionally(ex2 -> {
- future.completeExceptionally(ex2);
- return null;
- });
- } else {
- // For other errors, just fail the operation
- future.completeExceptionally(ex);
- }
- return null;
- });
- return future;
- })
+ position -> updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash))
);
} else {
// No schema was defined yet
CompletableFuture<Long> future = new CompletableFuture<>();
Review Comment:
delete
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryStats.java:
##########
@@ -36,6 +36,7 @@ class SchemaRegistryStats implements AutoCloseable, Runnable {
private final Counter getOpsFailedCounter;
private final Counter putOpsFailedCounter;
+ private final Counter putOpsRetryCounter;
Review Comment:
if add this metrics, please add the test for it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016119443
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
Sounds good.
One point to clarify is "creating lot of versions" will create lots of new schema ledger.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015357971
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @Override
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry : 1;
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1305354206
> This pr only guarantees the consistency of the zk storage schema, but it still creates useless ledger, right?
The schema will create a useless ledger at two points.
- At the point of client connected the first time concurrently, all producers or consumers see no schema info and create a new schema ledger
- At the point of update zookeeper storage failed, the broker will create a new schema ledger repeatedly if the update zookeeper failed.
This PR tries to resolve the second problem.
I still working on the first problem and will send a new PR for it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] congbobo184 commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
congbobo184 commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015179877
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
Review Comment:
+1
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] labuladong commented on pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
labuladong commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1305695714
To my understanding, this PR canceled the retry action in `putSchema`, and add the retry action in `putSchemaIfAbsent`.
But I notice the `putSchema` method will be called by the `deleteSchema` method, so this PR removes the retry functionality of `deleteSchema` method. Will that be OK?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016221765
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
Review Comment:
I know the root cause now, how about this suggestion?
```java
long start = startTime > 0 ? startTime : this.clock.millis();
return schemaStorage.put(schemaId, info.toByteArray(), context).handleAsync((sv, ex) -> {
if (ex != null){
return CompletableFuture.completedFuture(sv);
}
if (xx instanceof xxx){
return putSchemaIfAbsent(schemaId, schema, strategy, start, retryTime + 1);
}
return CompletableFuture.failedFuture(ex);
});
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1029167280
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +189,101 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
+ BookkeeperSchemaStorage bkSchemaStorage = (BookkeeperSchemaStorage) schemaStorage;
Review Comment:
Ok
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -126,33 +128,54 @@ public CompletableFuture<StoredSchema> get(String key, SchemaVersion version) {
@Override
public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key) {
+ return getAll(key, null);
+ }
+
+ public CompletableFuture<List<CompletableFuture<StoredSchema>>> getAll(String key,
+ LocatorEntry locator) {
CompletableFuture<List<CompletableFuture<StoredSchema>>> result = new CompletableFuture<>();
- getLocator(key).thenAccept(locator -> {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Get all schemas - locator: {}", key, locator);
- }
- if (!locator.isPresent()) {
- result.complete(Collections.emptyList());
- return;
- }
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Get all schemas - locator: {}", key, locator);
+ }
+ List<CompletableFuture<StoredSchema>> list = new ArrayList<>();
+ if (locator == null) {
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1301963952
@gaoran10 @codelipenghui PTAL
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] coderzc commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
coderzc commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1011328443
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -580,6 +550,7 @@ private CompletableFuture<LedgerHandle> createLedger(String schemaId) {
@NotNull
private CompletableFuture<LedgerHandle> openLedger(Long ledgerId) {
final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
+ final long start = System.nanoTime();
Review Comment:
Why need this line?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015195639
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
Review Comment:
> Call putSchemaIfAbsent here will retry once, but it will not record metrics this.stats.recordPutLatency or this.stats.recordPutFailed.
`this.stats.recordPutLatenc` is recorded incorrectly since `long start = this.clock.millis()` , I'll fix this;
`this.stats.recordPutFailed` has been recorded when exceptions (not `AlreadyExistsException` or `BadVersionException`) happened, so no need to record again.
> Maybe we can add a new method putSchemaWithRetryIfAbsent, this new method only wraps the original method putSchemaIfAbsent
Agree, and add a new metric to indicate `putOpsRetryCounter` retry times
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015340203
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @Override
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry : 1;
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] coderzc commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
coderzc commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015299351
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java:
##########
@@ -284,56 +282,12 @@ private CompletableFuture<Long> putSchema(String schemaId, byte[] data, byte[] h
return readSchemaEntry(locator.getIndexList().get(0).getPosition())
.thenCompose(schemaEntry -> addNewSchemaEntryToStore(schemaId,
locator.getIndexList(), data).thenCompose(
- position -> {
- CompletableFuture<Long> future = new CompletableFuture<>();
- updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash)
- .thenAccept(future::complete)
- .exceptionally(ex -> {
- if (ex.getCause() instanceof BadVersionException) {
- // There was a race condition on the schema creation.
- // Since it has now been created,
- // retry the whole operation so that we have a chance to
- // recover without bubbling error
- putSchema(schemaId, data, hash)
- .thenAccept(future::complete)
- .exceptionally(ex2 -> {
- future.completeExceptionally(ex2);
- return null;
- });
- } else {
- // For other errors, just fail the operation
- future.completeExceptionally(ex);
- }
- return null;
- });
- return future;
- })
+ position -> updateSchemaLocator(schemaId, optLocatorEntry.get(), position, hash))
);
} else {
// No schema was defined yet
CompletableFuture<Long> future = new CompletableFuture<>();
Review Comment:
Remove useless `future`.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @Override
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry : 1;
Review Comment:
`retryTime` always seems to be `1`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] AnonHxy commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
AnonHxy commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016094348
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
IIUC, the first put schema request will success ,and the second request will fail with `BadVersionException`. Then the second request will retry call `putSchemaIfAbsent`, result with `schema is already exists`. That's OK.
But I have a minor question, the second request will also create some ledgers, which will never be used again, will this PR try to delete the unused ledgers?If not, it seems that we need change the title of this PR, WDYT :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016225168
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +182,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+ long start = startTime > 0 ? startTime : this.clock.millis();
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by race condition
+ int retryTime = retry > 0 ? retry + 1 : 1;
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry {} times",
+ t.getCause().getMessage(), schemaId, retryTime);
+ }
+ this.stats.recordPutRetry(schemaId);
+ putSchemaIfAbsent(schemaId, schema, strategy, start, retryTime)
+ .thenAccept(persistentFuture::complete)
+ .exceptionally(ex2 -> {
+ persistentFuture.completeExceptionally(ex2);
+ return null;
+ });
+
+ } else {
+ log.error("[{}] Put schema failed", schemaId, t.getCause());
+ this.stats.recordPutFailed(schemaId);
+ persistentFuture.completeExceptionally(t);
+ }
+ return null;
+ });
Review Comment:
suggestion use `handleAsync` instead of `thenAccept ... exceptionally `, just like this:
```java
long start = startTime > 0 ? startTime : this.clock.millis();
return schemaStorage.put(schemaId, info.toByteArray(), context).handleAsync((sv, ex) -> {
if (ex != null){
return CompletableFuture.completedFuture(sv);
}
if (xx instanceof xxx){
return putSchemaIfAbsent(schemaId, schema, strategy, start, retryTime + 1);
}
return CompletableFuture.failedFuture(ex);
});
```
this makes it easier to read.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1305767966
> To my understanding, this PR canceled the retry action in putSchema, and add the retry action in putSchemaIfAbsent.
But I notice the putSchema method will be called by the deleteSchema method, so this PR removes the retry functionality of deleteSchema method too. Will that be OK?
As described in issue #18292, the retry function in `putSchema` is the root cause of creating lots of useless schema ledgers when the update zookeeper failed with `AlreadyExistsException` or `BadVersionException`. So, it should be removed.
BTW, the retry function is only useful when there is a race condition updating the zookeeper, and the command of delete schema is invoked from the CLI or PulsarAdmin which is rare to have a high concurrency. So, little chance for delete the schema will fail.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016101706
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
> But I have a minor question, the second request will also create some ledgers, which will never be used again, will this PR try to delete the unused ledgers?
You can see this reply:
https://github.com/apache/pulsar/pull/18293#issuecomment-1305354206
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] AnonHxy commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
AnonHxy commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016114150
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
OK, how about changing the title of this PR to `fix creating lots of versions for the same schema`. Because it seems that this PR doesn't fix creating lots of ledgers, instead it fix creating lot of versions. @aloyszhang
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1311163670
<img width="930" alt="image" src="https://user-images.githubusercontent.com/48062889/201251077-92c527e5-3869-4491-99be-d3e85d424913.png">
@codelipenghui This is another problem. The root cause is the concurrent access to zookeeper.
Before updating the `schemaLocator` on zk, it reads zk again. The purpose is to obtain the content of the existing locator to build the index information of the new locator, and the version will be +1.
If other threads updated the locator just before this point, it directly adds the position information of a new schema entry to the locator without exist check
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1029167280
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -180,57 +189,101 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@Override
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
- SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
+ SchemaCompatibilityStrategy strategy) {
+
+ return putSchemaIfAbsent(schemaId, schema, strategy, 0, 0);
+ }
+ @NotNull
+ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
+ SchemaCompatibilityStrategy strategy, long startTime, int retry) {
+ BookkeeperSchemaStorage bkSchemaStorage = (BookkeeperSchemaStorage) schemaStorage;
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] EMsnap commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
EMsnap commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1011292588
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -219,7 +221,13 @@ public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, Schem
.whenComplete((__, t) -> {
if (t != null) {
log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator ot zk failed caused by race condition
Review Comment:
put locator to
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015080057
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
+ if (t.getCause() instanceof AlreadyExistsException
+ || t.getCause() instanceof BadVersionException) {
+ // retry if put schemaLocator to zk failed caused by
+ // race condition
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Put schema failed because of {}, retry",
+ t.getCause().getMessage(), schemaId);
+ }
+ putSchemaIfAbsent(schemaId, schema, strategy)
Review Comment:
Call `putSchemaIfAbsent` here will retry once, but it will not record metrics `this.stats.recordPutLatency` or `this.stats.recordPutFailed`. Maybe we can add a new method `putSchemaWithRetryIfAbsent`, this new method only wraps the original method `putSchemaIfAbsent`
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
Review Comment:
We do not need these two logic branch, right?
```java
.exceptionally(t ->{
// the variable t will not be null
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015185988
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
Review Comment:
it's better to move line-186 to this block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015252741
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
+ SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo
+ .newBuilder()
+ .setType(Functions.convertFromDomainType(schema.getType()))
+ .setSchema(ByteString.copyFrom(schema.getData()))
+ .setSchemaId(schemaId)
+ .setUser(schema.getUser())
+ .setDeleted(false)
+ .setTimestamp(clock.millis())
+ .addAllProps(toPairs(schema.getProps()))
+ .build();
+
+ long start = this.clock.millis();
+ schemaStorage
+ .put(schemaId, info.toByteArray(), context)
+ .thenAccept(sv -> {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is successfully added", schemaId);
+ }
+ this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
+ persistentFuture.complete(sv);
+ })
+ .exceptionally(t -> {
+ if (t != null) {
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1015259042
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/SchemaRegistryServiceImpl.java:
##########
@@ -181,56 +183,85 @@ public CompletableFuture<List<CompletableFuture<SchemaAndMetadata>>> getAllSchem
@NotNull
public CompletableFuture<SchemaVersion> putSchemaIfAbsent(String schemaId, SchemaData schema,
SchemaCompatibilityStrategy strategy) {
- return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
- getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
- if (schemaVersion != null) {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Schema is already exists", schemaId);
- }
- return CompletableFuture.completedFuture(schemaVersion);
- }
- CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
- if (schemaAndMetadataList.size() != 0) {
- if (isTransitiveStrategy(strategy)) {
- checkCompatibilityFuture =
- checkCompatibilityWithAll(schemaId, schema, strategy, schemaAndMetadataList);
- } else {
- checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId, schema, strategy);
- }
- } else {
- checkCompatibilityFuture.complete(null);
- }
- return checkCompatibilityFuture.thenCompose(v -> {
- byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
- SchemaRegistryFormat.SchemaInfo info = SchemaRegistryFormat.SchemaInfo.newBuilder()
- .setType(Functions.convertFromDomainType(schema.getType()))
- .setSchema(ByteString.copyFrom(schema.getData()))
- .setSchemaId(schemaId)
- .setUser(schema.getUser())
- .setDeleted(false)
- .setTimestamp(clock.millis())
- .addAllProps(toPairs(schema.getProps()))
- .build();
-
- long start = this.clock.millis();
-
- return schemaStorage
- .put(schemaId, info.toByteArray(), context)
- .whenComplete((__, t) -> {
- if (t != null) {
- log.error("[{}] Put schema failed", schemaId);
- this.stats.recordPutFailed(schemaId);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("[{}] Put schema finished", schemaId);
- }
- this.stats.recordPutLatency(schemaId, this.clock.millis() - start);
- }
- });
-
- });
-
- }));
+ CompletableFuture<SchemaVersion> persistentFuture = new CompletableFuture<>();
+ return trimDeletedSchemaAndGetList(schemaId)
+ .thenCompose(schemaAndMetadataList ->
+ getSchemaVersionBySchemaData(schemaAndMetadataList, schema)
+ .thenCompose(schemaVersion -> {
+ if (schemaVersion != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("[{}] Schema is already exists", schemaId);
+ }
+ return CompletableFuture.completedFuture(schemaVersion);
+ }
+ CompletableFuture<Void> checkCompatibilityFuture = new CompletableFuture<>();
+ if (schemaAndMetadataList.size() != 0) {
+ if (isTransitiveStrategy(strategy)) {
+ checkCompatibilityFuture = checkCompatibilityWithAll(schemaId, schema,
+ strategy, schemaAndMetadataList);
+ } else {
+ checkCompatibilityFuture = checkCompatibilityWithLatest(schemaId,
+ schema, strategy);
+ }
+ } else {
+ checkCompatibilityFuture.complete(null);
+ }
+ return checkCompatibilityFuture.thenCompose(v -> {
+ byte[] context = hashFunction.hashBytes(schema.getData()).asBytes();
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] AnonHxy commented on a diff in pull request #18293: [fix][broker][schema]fix creating lots of ledgers for the same schema
Posted by GitBox <gi...@apache.org>.
AnonHxy commented on code in PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#discussion_r1016094348
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/schema/SchemaServiceTest.java:
##########
@@ -154,6 +168,18 @@ public void writeReadBackDeleteSchemaEntry() throws Exception {
assertNull(schemaRegistryService.getSchema(schemaId1).get());
}
+ @Test
+ public void testConcurrentPutSchema() throws Exception {
+ String schemaId = "tenant/ns/topic_test_concurrent_put_schema" + UUID.randomUUID();
+ CompletableFuture.allOf(
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1),
+ putSchema(schemaId, schemaData1))
+ .get();
+ assertEquals(getAllSchemas(schemaId).size(), 1);
Review Comment:
IIUC, the first put schema request will success ,and the second request will fail with `BadVersionException`. Then the second request will retry call `putSchemaIfAbsent`, result with `schema is already exists`. That's OK.
But I have a minor question, the second request will also create some ledgers, which will never be used again, will this PR try to delete the unused ledgers?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] aloyszhang commented on pull request #18293: [fix][schema] Fix creating lots of versions for the same schema
Posted by GitBox <gi...@apache.org>.
aloyszhang commented on PR #18293:
URL: https://github.com/apache/pulsar/pull/18293#issuecomment-1333639194
close this pull request due to https://github.com/apache/pulsar/pull/18701
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org