Jason918 commented on a change in pull request #13297:
URL: https://github.com/apache/pulsar/pull/13297#discussion_r773555946



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
##########
@@ -4668,4 +4669,38 @@ private void 
internalGetReplicatedSubscriptionStatusForNonPartitionedTopic(Async
             resumeAsyncResponseExceptionally(asyncResponse, e);
         }
     }
+
+    protected CompletableFuture<SchemaCompatibilityStrategy> 
internalGetSchemaCompatibilityStrategy() {
+        validateTopicOperation(topicName, 
TopicOperation.GET_SCHEMA_COMPATIBILITY_STRATEGY);
+
+        return getTopicPoliciesAsyncWithRetry(topicName)
+                .thenApply(op -> 
op.map(TopicPolicies::getSchemaCompatibilityStrategy)

Review comment:
       This logic seems not right.
   If we have topic level policy setting as 
`SchemaCompatibilityStrategy.UNDEFINED`, we should use namespace level setting. 

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/PersistentTopicsBase.java
##########
@@ -4668,4 +4669,38 @@ private void 
internalGetReplicatedSubscriptionStatusForNonPartitionedTopic(Async
             resumeAsyncResponseExceptionally(asyncResponse, e);
         }
     }
+
+    protected CompletableFuture<SchemaCompatibilityStrategy> 
internalGetSchemaCompatibilityStrategy() {
+        validateTopicOperation(topicName, 
TopicOperation.GET_SCHEMA_COMPATIBILITY_STRATEGY);
+
+        return getTopicPoliciesAsyncWithRetry(topicName)
+                .thenApply(op -> 
op.map(TopicPolicies::getSchemaCompatibilityStrategy)

Review comment:
       And broker level setting is missing here.

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/SchemasResourceBase.java
##########
@@ -133,19 +134,51 @@ public void deleteSchema(boolean authoritative, 
AsyncResponse response) {
                 });
     }
 
-    public void postSchema(PostSchemaPayload payload, boolean authoritative, 
AsyncResponse response) {
-        validateDestinationAndAdminOperation(authoritative);
+    private CompletableFuture<SchemaCompatibilityStrategy> 
getSchemaCompatibilityStrategyAsync() {

Review comment:
       Does this `getSchemaCompatibilityStrategyAsync` have difference with 
`internalGetSchemaCompatibilityStrategy`?
   Can share one method?

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/schema/compatibility/SchemaTypeCompatibilityCheckOnTopicLevelTest.java
##########
@@ -0,0 +1,134 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.schema.compatibility;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+import com.google.common.collect.Sets;
+import java.util.Collections;
+import org.apache.pulsar.broker.PulsarServerException;
+import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
+import org.apache.pulsar.client.admin.PulsarAdminException;
+import org.apache.pulsar.client.api.ProducerBuilder;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.schema.SchemaDefinition;
+import org.apache.pulsar.common.naming.TopicDomain;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.common.policies.data.ClusterData;
+import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy;
+import org.apache.pulsar.common.policies.data.TenantInfo;
+import org.apache.pulsar.schema.Schemas;
+import org.awaitility.Awaitility;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class SchemaTypeCompatibilityCheckOnTopicLevelTest extends 
MockedPulsarServiceBaseTest {
+    private static final String CLUSTER_NAME = "test";
+    private static final String PUBLIC_TENANT = "public";
+    private static final String namespace = "test-namespace";
+    private static final String namespaceName = PUBLIC_TENANT + "/" + 
namespace;
+
+    @BeforeClass
+    @Override
+    public void setup() throws Exception {
+        conf.setTopicLevelPoliciesEnabled(true);
+        conf.setSystemTopicEnabled(true);
+
+        super.internalSetup();
+
+        // Setup namespaces
+        admin.clusters().createCluster(CLUSTER_NAME, 
ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress())
+                .build());
+
+        TenantInfo tenantInfo = TenantInfo.builder()
+                .allowedClusters(Collections.singleton(CLUSTER_NAME))
+                .build();
+        admin.tenants().createTenant(PUBLIC_TENANT, tenantInfo);
+        admin.namespaces().createNamespace(namespaceName, 
Sets.newHashSet(CLUSTER_NAME));
+
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    public void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    private void assertSchemaAlwaysInCompatibleStrategyInTopicLevel(String 
topic)
+            throws PulsarServerException, PulsarAdminException, 
PulsarClientException {
+        String topicName = TopicName.get(
+                TopicDomain.persistent.value(),
+                PUBLIC_TENANT,
+                namespace,
+                topic
+        ).toString();
+
+        pulsar.getAdminClient().topics().createNonPartitionedTopic(topicName);
+        
pulsar.getAdminClient().topicPolicies().setSchemaCompatibilityStrategy(topicName,
+                SchemaCompatibilityStrategy.ALWAYS_INCOMPATIBLE);
+
+        Awaitility.await()
+                .untilAsserted(
+                        () -> assertEquals(
+                                
pulsar.getAdminClient().topicPolicies().getSchemaCompatibilityStrategy(topicName),
+                                
SchemaCompatibilityStrategy.ALWAYS_INCOMPATIBLE));
+
+        
pulsarClient.newProducer(Schema.AVRO(SchemaDefinition.<Schemas.PersonOne>builder().
+                        
withAlwaysAllowNull(true).withPojo(Schemas.PersonOne.class).build()))
+                .topic(topicName)
+                .create();
+
+        ProducerBuilder<Schemas.PersonThree> producerBuilder = 
pulsarClient.newProducer(
+                        
Schema.AVRO(SchemaDefinition.<Schemas.PersonThree>builder().withAlwaysAllowNull(true)
+                                .withPojo(Schemas.PersonThree.class).build()))
+                .topic(topicName);
+
+        Throwable t = 
expectThrows(PulsarClientException.IncompatibleSchemaException.class, 
producerBuilder::create);
+        
assertTrue(t.getMessage().contains("org.apache.avro.SchemaValidationException: 
Unable to read schema"));
+    }
+
+    @Test
+    public void testSchemaAlwaysInCompatibleStrategyInTopicLevel()
+            throws PulsarClientException, PulsarServerException, 
PulsarAdminException {
+        
conf.setSchemaCompatibilityStrategy(SchemaCompatibilityStrategy.UNDEFINED);
+        admin.namespaces().setSchemaCompatibilityStrategy(namespaceName, 
SchemaCompatibilityStrategy.UNDEFINED);
+        
assertSchemaAlwaysInCompatibleStrategyInTopicLevel("testSchemaAlwaysInCompatibleStrategyInTopicLevel");
+    }
+
+    @Test
+    public void 
testSchemaAlwaysInCompatibleStrategyOverriderBrokerConfigInTopicLevel()

Review comment:
       Please add test case for different level policy changed online. 
Something like :
   set broker level value and check...
   set ns level value and check...
   set topic level value and check...
   unset topic level value and check...
   unset ns level value and check...
   unset broker level value and 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to