Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 9602] Add schema type validation #9797

Merged

Conversation

limingnihao
Copy link
Contributor

Fixes #9602

Motivation

The schema should not be able to modify the type after it is first created. Unless he is Always compatible.

Modifications

When uploading a schema, it is verified that the type of the newly uploaded schema is consistent with the schema in the metadata.

When checkCompatible, change the Primitive type from checking only to checking all.

Verifying this change

  • When a producer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

  • When a producer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

  • When a consumer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

  • When a consumer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

  • When compatibility is set to always compatible. No exceptions are thrown.

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • The schema: (yes)

@limingnihao
Copy link
Contributor Author

/pulsarbot run-failure-checks

@codelipenghui
Copy link
Contributor

@congbobo184 @gaoran10 Please help review this PR.

@codelipenghui codelipenghui added component/schemaregistry type/bug The PR fixed a bug or issue reported a bug labels Mar 4, 2021
@codelipenghui codelipenghui added this to the 2.8.0 milestone Mar 4, 2021
@codelipenghui codelipenghui changed the title [Issue 9602] add scheme type validation [Issue 9602] Add schema type validation Mar 4, 2021

import com.google.common.collect.Sets;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.client.api.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to avoid star import.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the above comment.

private final String namespace = "test-namespace";
private final String namespaceName = PUBLIC_TENANT + "/" + namespace;

@BeforeMethod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to change to BeforeClass? This can avoid start and shutdown the mock service for each test.

@@ -140,6 +140,15 @@
SchemaCompatibilityStrategy strategy) {
return trimDeletedSchemaAndGetList(schemaId).thenCompose(schemaAndMetadataList ->
getSchemaVersionBySchemaData(schemaAndMetadataList, schema).thenCompose(schemaVersion -> {
if (schemaAndMetadataList.size() > 0) {
for (SchemaAndMetadata metadata : schemaAndMetadataList) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to check if strategy is not ALWAYS_COMPATIBLE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is really a need to check the strategy type, so I will add that and unit tests.

compatibilityChecks.getOrDefault(newSchema.getType(), SchemaCompatibilityCheck.DEFAULT)
.checkCompatible(existingSchemaData, newSchema, strategy);
}
if (newSchema.getType() != existingSchemaData.getType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to check if strategy is not ALWAYS_COMPATIBLE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to check compatibility here. Because in checkConsumerCompatibility will judge. If it is ALWAYS_COMPATIBLE, it will not be called here.

@codelipenghui
Copy link
Contributor

@sijie Please review this PR again, thanks.

@codelipenghui codelipenghui merged commit b45adde into apache:master Mar 12, 2021
fmiguelez pushed a commit to fmiguelez/pulsar that referenced this pull request Mar 16, 2021
Fixes apache#9602

### Motivation

The schema should not be able to modify the type after it is first created. Unless he is Always compatible.

### Modifications

When uploading a schema, it is verified that the type of the newly uploaded schema is consistent with the schema in the metadata.

When checkCompatible, change the Primitive type from checking only to checking all.

### Verifying this change

* When a producer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a producer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a consumer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a consumer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When compatibility is set to always compatible. No exceptions are thrown.

### Does this pull request potentially affect one of the following parts:

*If `yes` was chosen, please highlight the changes*
  - The schema: (yes)
@codelipenghui codelipenghui added the cherry-picked/branch-2.7 Archived: 2.7 is end of life label Mar 23, 2021
codelipenghui pushed a commit that referenced this pull request Mar 23, 2021
Fixes #9602

The schema should not be able to modify the type after it is first created. Unless he is Always compatible.

When uploading a schema, it is verified that the type of the newly uploaded schema is consistent with the schema in the metadata.

When checkCompatible, change the Primitive type from checking only to checking all.

* When a producer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a producer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a consumer is created, the schema is defined, and when a producer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When a consumer is created, the schema is defined, and when a consumer is created again, the schema type is not always the same, otherwise an exception will be thrown.

* When compatibility is set to always compatible. No exceptions are thrown.

*If `yes` was chosen, please highlight the changes*
  - The schema: (yes)

(cherry picked from commit b45adde)
codelipenghui added a commit that referenced this pull request Apr 26, 2021
Related to #9797

### Motivation

Fix schema type check issue when use always compatible strategy.

1. For non-transitive strategy, only check schema type for the last schema
2. For transitive strategy, check all schema's type
3. Get schema by schema data should consider different schema types
codelipenghui added a commit that referenced this pull request Apr 27, 2021
Related to #9797

Fix schema type check issue when use always compatible strategy.

1. For non-transitive strategy, only check schema type for the last schema
2. For transitive strategy, check all schema's type
3. Get schema by schema data should consider different schema types

(cherry picked from commit 04f8c96)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-picked/branch-2.7 Archived: 2.7 is end of life release/2.7.2 type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could not add AVRO schema using producer client
5 participants