Search in sources :

Example 1 with InstanceStateManager

use of org.apache.pulsar.functions.instance.state.InstanceStateManager in project pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with InstanceStateManager

use of org.apache.pulsar.functions.instance.state.InstanceStateManager in project pulsar by yahoo.

the class JavaInstanceRunnable method setupStateStore.

private void setupStateStore() throws Exception {
    this.stateManager = new InstanceStateManager();
    if (null == stateStorageServiceUrl) {
        stateStoreProvider = StateStoreProvider.NULL;
    } else {
        stateStoreProvider = getStateStoreProvider();
        Map<String, Object> stateStoreProviderConfig = new HashMap<>();
        stateStoreProviderConfig.put(BKStateStoreProviderImpl.STATE_STORAGE_SERVICE_URL, stateStorageServiceUrl);
        stateStoreProvider.init(stateStoreProviderConfig, instanceConfig.getFunctionDetails());
        StateStore store = stateStoreProvider.getStateStore(instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName());
        StateStoreContext context = new StateStoreContextImpl();
        store.init(context);
        stateManager.registerStore(store);
    }
}
Also used : StateStoreContextImpl(org.apache.pulsar.functions.instance.state.StateStoreContextImpl) HashMap(java.util.HashMap) StateStore(org.apache.pulsar.functions.api.StateStore) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) StateStoreContext(org.apache.pulsar.functions.api.StateStoreContext)

Example 3 with InstanceStateManager

use of org.apache.pulsar.functions.instance.state.InstanceStateManager in project pulsar by yahoo.

the class ContextImplTest method testGetConsumerMultiTopic.

@Test
public void testGetConsumerMultiTopic() throws PulsarClientException {
    config.setExposePulsarAdminClientEnabled(false);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    ConsumerImpl<?> consumer1 = Mockito.mock(ConsumerImpl.class);
    when(consumer1.getTopic()).thenReturn(TopicName.get("first").toString());
    ConsumerImpl<?> consumer2 = Mockito.mock(ConsumerImpl.class);
    when(consumer2.getTopic()).thenReturn(TopicName.get("second").toString());
    List<Consumer<?>> consumersList = Lists.newArrayList(consumer1, consumer2);
    MultiTopicsConsumerImpl mtc = Mockito.mock(MultiTopicsConsumerImpl.class);
    when(mtc.getConsumers()).thenReturn(consumersList);
    context.setInputConsumers(Lists.newArrayList(mtc));
    Assert.assertNotNull(context.getConsumer("first", 0));
    Assert.assertNotNull(context.getConsumer("second", 0));
    try {
        // nknown topic
        context.getConsumer("third", 0);
        Assert.fail("Expected exception");
    } catch (PulsarClientException e) {
    // pass
    }
    // consumers updated
    ConsumerImpl<?> consumer3 = Mockito.mock(ConsumerImpl.class);
    when(consumer3.getTopic()).thenReturn(TopicName.get("third").toString());
    consumersList.add(consumer3);
    Assert.assertNotNull(context.getConsumer("third", 0));
    try {
        // unknown partition
        context.getConsumer("third", 1);
        Assert.fail("Expected exception");
    } catch (PulsarClientException e) {
    // pass
    }
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) MultiTopicsConsumerImpl(org.apache.pulsar.client.impl.MultiTopicsConsumerImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Example 4 with InstanceStateManager

use of org.apache.pulsar.functions.instance.state.InstanceStateManager in project incubator-pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod(alwaysRun = true)
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    producer = mock(Producer.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) Producer(org.apache.pulsar.client.api.Producer) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with InstanceStateManager

use of org.apache.pulsar.functions.instance.state.InstanceStateManager in project incubator-pulsar by apache.

the class ContextImplTest method testGetConsumerMultiTopic.

@Test
public void testGetConsumerMultiTopic() throws PulsarClientException {
    config.setExposePulsarAdminClientEnabled(false);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    ConsumerImpl<?> consumer1 = Mockito.mock(ConsumerImpl.class);
    when(consumer1.getTopic()).thenReturn(TopicName.get("first").toString());
    ConsumerImpl<?> consumer2 = Mockito.mock(ConsumerImpl.class);
    when(consumer2.getTopic()).thenReturn(TopicName.get("second").toString());
    List<Consumer<?>> consumersList = Lists.newArrayList(consumer1, consumer2);
    MultiTopicsConsumerImpl mtc = Mockito.mock(MultiTopicsConsumerImpl.class);
    when(mtc.getConsumers()).thenReturn(consumersList);
    context.setInputConsumers(Lists.newArrayList(mtc));
    Assert.assertNotNull(context.getConsumer("first", 0));
    Assert.assertNotNull(context.getConsumer("second", 0));
    try {
        // nknown topic
        context.getConsumer("third", 0);
        Assert.fail("Expected exception");
    } catch (PulsarClientException e) {
    // pass
    }
    // consumers updated
    ConsumerImpl<?> consumer3 = Mockito.mock(ConsumerImpl.class);
    when(consumer3.getTopic()).thenReturn(TopicName.get("third").toString());
    consumersList.add(consumer3);
    Assert.assertNotNull(context.getConsumer("third", 0));
    try {
        // unknown partition
        context.getConsumer("third", 1);
        Assert.fail("Expected exception");
    } catch (PulsarClientException e) {
    // pass
    }
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) MultiTopicsConsumerImpl(org.apache.pulsar.client.impl.MultiTopicsConsumerImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Aggregations

InstanceStateManager (org.apache.pulsar.functions.instance.state.InstanceStateManager)9 EnvironmentBasedSecretsProvider (org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 HashMap (java.util.HashMap)3 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)3 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)3 Consumer (org.apache.pulsar.client.api.Consumer)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)3 MultiTopicsConsumerImpl (org.apache.pulsar.client.impl.MultiTopicsConsumerImpl)3 ProducerBuilderImpl (org.apache.pulsar.client.impl.ProducerBuilderImpl)3 PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)3 TypedMessageBuilderImpl (org.apache.pulsar.client.impl.TypedMessageBuilderImpl)3 ProducerConfigurationData (org.apache.pulsar.client.impl.conf.ProducerConfigurationData)3 StateStore (org.apache.pulsar.functions.api.StateStore)3 StateStoreContext (org.apache.pulsar.functions.api.StateStoreContext)3 StateStoreContextImpl (org.apache.pulsar.functions.instance.state.StateStoreContextImpl)3 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)3 Logger (org.slf4j.Logger)3 BeforeMethod (org.testng.annotations.BeforeMethod)3