Search in sources :

Example 1 with SchemaCompatibilityStrategy

use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.

the class NamespacesImpl method getSchemaCompatibilityStrategyAsync.

@Override
public CompletableFuture<SchemaCompatibilityStrategy> getSchemaCompatibilityStrategyAsync(String namespace) {
    NamespaceName ns = NamespaceName.get(namespace);
    WebTarget path = namespacePath(ns, "schemaCompatibilityStrategy");
    final CompletableFuture<SchemaCompatibilityStrategy> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<SchemaCompatibilityStrategy>() {

        @Override
        public void completed(SchemaCompatibilityStrategy schemaCompatibilityStrategy) {
            future.complete(schemaCompatibilityStrategy);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) CompletableFuture(java.util.concurrent.CompletableFuture) WebTarget(javax.ws.rs.client.WebTarget) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride) AutoTopicCreationOverride(org.apache.pulsar.common.policies.data.AutoTopicCreationOverride) AutoSubscriptionCreationOverride(org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)

Example 2 with SchemaCompatibilityStrategy

use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithBadSchemaData.

@Test
public void testIsCompatibleWithBadSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
    try {
        service.isCompatible(schemaId, schemaData, strategy).get();
        fail("Should fail isCompatible check");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof InvalidSchemaDataException);
    }
    verify(underlyingService, times(0)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) Test(org.testng.annotations.Test)

Example 3 with SchemaCompatibilityStrategy

use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testIsCompatibleWithGoodSchemaData.

@Test
public void testIsCompatibleWithGoodSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    when(underlyingService.isCompatible(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[0]).build();
    assertSame(future, service.isCompatible(schemaId, schemaData, strategy));
    verify(underlyingService, times(1)).isCompatible(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) Test(org.testng.annotations.Test)

Example 4 with SchemaCompatibilityStrategy

use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.

the class PulsarFunctionsTest method testMergeFunction.

protected void testMergeFunction() throws Exception {
    log.info("start merge function test ...");
    String ns = "public/ns-merge-" + randomName(8);
    @Cleanup PulsarAdmin pulsarAdmin = getPulsarAdmin();
    pulsarAdmin.namespaces().createNamespace(ns);
    pulsarAdmin.namespaces().setSchemaCompatibilityStrategy(ns, SchemaCompatibilityStrategy.ALWAYS_COMPATIBLE);
    SchemaCompatibilityStrategy strategy = pulsarAdmin.namespaces().getSchemaCompatibilityStrategy(ns);
    log.info("namespace {} SchemaCompatibilityStrategy is {}", ns, strategy);
    @Cleanup PulsarClient pulsarClient = getPulsarClient();
    ObjectNode inputSpecNode = objectMapper.createObjectNode();
    Map<String, AtomicInteger> topicMsgCntMap = new ConcurrentHashMap<>();
    int messagePerTopic = 10;
    prepareDataForMergeFunction(ns, pulsarClient, inputSpecNode, messagePerTopic, topicMsgCntMap);
    final String outputTopic = ns + "/test-merge-output";
    @Cleanup Consumer<GenericRecord> consumer = pulsarClient.newConsumer(Schema.AUTO_CONSUME()).subscriptionName("test-merge-fn").subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).topic(outputTopic).subscribe();
    final String functionName = "test-merge-fn-" + randomName(8);
    submitFunction(Runtime.JAVA, "", outputTopic, functionName, null, MergeTopicFunction.class.getName(), null, null, inputSpecNode.toString(), SchemaType.AUTO_PUBLISH.name().toUpperCase(), SubscriptionInitialPosition.Earliest);
    getFunctionInfoSuccess(functionName);
    getFunctionStatus(functionName, topicMsgCntMap.keySet().size() * messagePerTopic, true);
    Message<GenericRecord> message;
    do {
        message = consumer.receive(30, TimeUnit.SECONDS);
        if (message != null) {
            String baseTopic = message.getProperty("baseTopic");
            GenericRecord genericRecord = message.getValue();
            log.info("receive msg baseTopic: {}, schemaType: {}, nativeClass: {}, nativeObject: {}", baseTopic, genericRecord.getSchemaType(), genericRecord.getNativeObject().getClass(), genericRecord.getNativeObject());
            checkSchemaForAutoSchema(message, baseTopic);
            topicMsgCntMap.get(baseTopic).decrementAndGet();
            consumer.acknowledge(message);
        }
    } while (message != null);
    for (Map.Entry<String, AtomicInteger> entry : topicMsgCntMap.entrySet()) {
        assertEquals(entry.getValue().get(), 0, "topic " + entry.getKey() + " left message cnt is not 0.");
    }
    deleteFunction(functionName);
    getFunctionInfoNotFound(functionName);
    log.info("finish merge function test.");
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) Cleanup(lombok.Cleanup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MergeTopicFunction(org.apache.pulsar.functions.api.examples.MergeTopicFunction) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SchemaCompatibilityStrategy

use of org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy in project pulsar by apache.

the class SchemaRegistryServiceWithSchemaDataValidatorTest method testPutSchemaIfAbsentWithBadSchemaData.

@Test
public void testPutSchemaIfAbsentWithBadSchemaData() {
    String schemaId = "test-schema-id";
    SchemaCompatibilityStrategy strategy = SchemaCompatibilityStrategy.FULL;
    CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
    when(underlyingService.putSchemaIfAbsent(eq(schemaId), any(SchemaData.class), eq(strategy))).thenReturn(future);
    SchemaData schemaData = SchemaData.builder().type(SchemaType.BOOLEAN).data(new byte[10]).build();
    try {
        service.putSchemaIfAbsent(schemaId, schemaData, strategy).get();
        fail("Should fail putSchemaIfAbsent");
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof InvalidSchemaDataException);
    }
    verify(underlyingService, times(0)).putSchemaIfAbsent(eq(schemaId), same(schemaData), eq(strategy));
}
Also used : InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) SchemaCompatibilityStrategy(org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy) InvalidSchemaDataException(org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException) Test(org.testng.annotations.Test)

Aggregations

SchemaCompatibilityStrategy (org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 SchemaData (org.apache.pulsar.common.protocol.schema.SchemaData)4 Test (org.testng.annotations.Test)4 WebTarget (javax.ws.rs.client.WebTarget)2 InvalidSchemaDataException (org.apache.pulsar.broker.service.schema.exceptions.InvalidSchemaDataException)2 SchemaVersion (org.apache.pulsar.common.protocol.schema.SchemaVersion)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Cleanup (lombok.Cleanup)1 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)1 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)1 PulsarClient (org.apache.pulsar.client.api.PulsarClient)1 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)1 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)1 TopicName (org.apache.pulsar.common.naming.TopicName)1 AutoSubscriptionCreationOverride (org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride)1