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);
}
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);
}
}
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
}
}
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);
}
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
}
}
Aggregations