Search in sources :

Example 1 with StateStore

use of org.apache.pulsar.functions.api.StateStore in project pulsar by apache.

the class InstanceStateManagerTest method testClose.

@Test
public void testClose() {
    final String fqsn1 = "t/ns/store-1";
    StateStore store1 = mock(StateStore.class);
    when(store1.fqsn()).thenReturn(fqsn1);
    final String fqsn2 = "t/ns/store-2";
    StateStore store2 = mock(StateStore.class);
    when(store2.fqsn()).thenReturn(fqsn2);
    this.stateManager.registerStore(store1);
    this.stateManager.registerStore(store2);
    this.stateManager.close();
    verify(store1, times(1)).close();
    verify(store2, times(1)).close();
}
Also used : StateStore(org.apache.pulsar.functions.api.StateStore) Test(org.testng.annotations.Test)

Example 2 with StateStore

use of org.apache.pulsar.functions.api.StateStore in project pulsar by apache.

the class InstanceStateManager method close.

@Override
public void close() {
    RuntimeException firstException = null;
    for (Map.Entry<String, StateStore> entry : stores.entrySet()) {
        final StateStore store = entry.getValue();
        if (log.isDebugEnabled()) {
            log.debug("Closing store {}", store.fqsn());
        }
        try {
            store.close();
        } catch (RuntimeException e) {
            if (firstException == null) {
                firstException = e;
            }
            log.error("Failed to close state store {}: ", store.fqsn(), e);
        }
    }
    stores.clear();
    if (null != firstException) {
        throw firstException;
    }
}
Also used : StateStore(org.apache.pulsar.functions.api.StateStore) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with StateStore

use of org.apache.pulsar.functions.api.StateStore in project pulsar by apache.

the class InstanceStateManagerTest method testRegisterStoreTwice.

@Test
public void testRegisterStoreTwice() {
    final String fqsn = "t/ns/store";
    StateStore store = mock(StateStore.class);
    when(store.fqsn()).thenReturn(fqsn);
    this.stateManager.registerStore(store);
    try {
        this.stateManager.registerStore(store);
        fail("Should fail to register a store twice");
    } catch (IllegalArgumentException iae) {
    // expected
    }
}
Also used : StateStore(org.apache.pulsar.functions.api.StateStore) Test(org.testng.annotations.Test)

Example 4 with StateStore

use of org.apache.pulsar.functions.api.StateStore 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 5 with StateStore

use of org.apache.pulsar.functions.api.StateStore in project pulsar by yahoo.

the class InstanceStateManagerTest method testCloseException.

@Test
public void testCloseException() {
    final String fqsn1 = "t/ns/store-1";
    StateStore store1 = mock(StateStore.class);
    when(store1.fqsn()).thenReturn(fqsn1);
    RuntimeException exception1 = new RuntimeException("exception 1");
    doThrow(exception1).when(store1).close();
    final String fqsn2 = "t/ns/store-2";
    StateStore store2 = mock(StateStore.class);
    when(store2.fqsn()).thenReturn(fqsn2);
    RuntimeException exception2 = new RuntimeException("exception 2");
    doThrow(exception2).when(store2).close();
    this.stateManager.registerStore(store2);
    this.stateManager.registerStore(store1);
    try {
        this.stateManager.close();
        fail("Should fail to close the state manager");
    } catch (RuntimeException re) {
        assertSame(re, exception2);
    }
    assertTrue(this.stateManager.isEmpty());
}
Also used : StateStore(org.apache.pulsar.functions.api.StateStore) Test(org.testng.annotations.Test)

Aggregations

StateStore (org.apache.pulsar.functions.api.StateStore)21 Test (org.testng.annotations.Test)15 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 StateStoreContext (org.apache.pulsar.functions.api.StateStoreContext)3 InstanceStateManager (org.apache.pulsar.functions.instance.state.InstanceStateManager)3 StateStoreContextImpl (org.apache.pulsar.functions.instance.state.StateStoreContextImpl)3