Search in sources :

Example 1 with PersistenceReference

use of io.siddhi.core.util.snapshot.PersistenceReference in project siddhi by wso2.

the class PersistenceTestCase method persistenceTest3.

@Test(expectedExceptions = NoPersistenceStoreException.class, dependsOnMethods = "persistenceTest2")
public void persistenceTest3() throws Exception {
    log.info("persistence test 3 - no store defined");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "" + "@info(name = 'query1') " + "from e1=Stream1[price>20] <2:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " + "   e1[3].price as price1_3, e2.price as price2 " + "insert into OutputStream ;";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
            for (Event inEvent : inEvents) {
                count++;
                AssertJUnit.assertArrayEquals(new Object[] { 25.6f, 47.6f, null, null, 45.7f }, inEvent.getData());
            }
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 25.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 13.7f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 0, count);
    AssertJUnit.assertEquals("Event arrived", false, eventArrived);
    // persisting
    Thread.sleep(500);
    PersistenceReference persistenceReference = siddhiAppRuntime.persist();
    try {
        persistenceReference.getFuture().get();
    } catch (ExecutionException e) {
        throw e.getCause() instanceof NoPersistenceStoreException ? new NoPersistenceStoreException() : e;
    }
    // restarting siddhi app
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) Event(io.siddhi.core.event.Event) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException) ExecutionException(java.util.concurrent.ExecutionException) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) PersistenceReference(io.siddhi.core.util.snapshot.PersistenceReference) Test(org.testng.annotations.Test)

Example 2 with PersistenceReference

use of io.siddhi.core.util.snapshot.PersistenceReference in project siddhi by wso2.

the class PersistenceHelper method persist.

public static PersistenceReference persist(byte[] serializeObj, SiddhiAppContext siddhiAppContext) {
    long revisionTime = System.currentTimeMillis();
    // start the snapshot persisting task asynchronously
    AsyncSnapshotPersistor asyncSnapshotPersistor = new AsyncSnapshotPersistor(serializeObj, siddhiAppContext.getSiddhiContext().getPersistenceStore(), siddhiAppContext.getName(), revisionTime);
    Future future = siddhiAppContext.getExecutorService().submit(asyncSnapshotPersistor);
    return new PersistenceReference(future, asyncSnapshotPersistor.getRevision());
}
Also used : AsyncSnapshotPersistor(io.siddhi.core.util.snapshot.AsyncSnapshotPersistor) Future(java.util.concurrent.Future) PersistenceReference(io.siddhi.core.util.snapshot.PersistenceReference)

Example 3 with PersistenceReference

use of io.siddhi.core.util.snapshot.PersistenceReference in project siddhi by wso2.

the class PersistenceHelper method persist.

public static PersistenceReference persist(IncrementalSnapshot serializeObj, SiddhiAppContext siddhiAppContext) {
    long revisionTime = System.currentTimeMillis();
    List<Future> incrementalFutures = new ArrayList<>();
    // Periodic state
    Map<String, Map<String, byte[]>> periodicStateBase = serializeObj.getPeriodicState();
    if (periodicStateBase != null) {
        periodicStateBase.forEach((partitionId, value) -> {
            value.forEach((id, value1) -> {
                String[] items = id.split(PersistenceConstants.REVISION_SEPARATOR);
                AsyncIncrementalSnapshotPersistor asyncIncrementSnapshotPersistor = new AsyncIncrementalSnapshotPersistor(value1, siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore(), new IncrementalSnapshotInfo(siddhiAppContext.getName(), partitionId, items[1], items[2], revisionTime, IncrementalSnapshotInfo.SnapshotType.PERIODIC, items[0]));
                Future future = siddhiAppContext.getExecutorService().submit(asyncIncrementSnapshotPersistor);
                incrementalFutures.add(future);
            });
        });
    }
    // Incremental base state
    Map<String, Map<String, byte[]>> incrementalStateBase = serializeObj.getIncrementalStateBase();
    if (incrementalStateBase != null) {
        incrementalStateBase.forEach((partitionId, value) -> {
            value.forEach((id, value1) -> {
                String[] items = id.split(PersistenceConstants.REVISION_SEPARATOR);
                AsyncIncrementalSnapshotPersistor asyncIncrementSnapshotPersistor = new AsyncIncrementalSnapshotPersistor(value1, siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore(), new IncrementalSnapshotInfo(siddhiAppContext.getName(), partitionId, items[1], items[2], revisionTime, IncrementalSnapshotInfo.SnapshotType.BASE, items[0]));
                Future future = siddhiAppContext.getExecutorService().submit(asyncIncrementSnapshotPersistor);
                incrementalFutures.add(future);
            });
        });
    }
    // Next, handle the increment persistence scenarios
    // Incremental state
    Map<String, Map<String, byte[]>> incrementalState = serializeObj.getIncrementalState();
    if (incrementalState != null) {
        incrementalState.forEach((partitionId, value) -> {
            value.forEach((id, value1) -> {
                String[] items = id.split(PersistenceConstants.REVISION_SEPARATOR);
                AsyncIncrementalSnapshotPersistor asyncIncrementSnapshotPersistor = new AsyncIncrementalSnapshotPersistor(value1, siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore(), new IncrementalSnapshotInfo(siddhiAppContext.getName(), partitionId, items[1], items[2], revisionTime, IncrementalSnapshotInfo.SnapshotType.INCREMENT, items[0]));
                Future future = siddhiAppContext.getExecutorService().submit(asyncIncrementSnapshotPersistor);
                incrementalFutures.add(future);
            });
        });
    }
    return new PersistenceReference(incrementalFutures, revisionTime + PersistenceConstants.REVISION_SEPARATOR + siddhiAppContext.getName());
}
Also used : AsyncIncrementalSnapshotPersistor(io.siddhi.core.util.snapshot.AsyncIncrementalSnapshotPersistor) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Map(java.util.Map) PersistenceReference(io.siddhi.core.util.snapshot.PersistenceReference)

Aggregations

PersistenceReference (io.siddhi.core.util.snapshot.PersistenceReference)3 Future (java.util.concurrent.Future)2 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (io.siddhi.core.SiddhiManager)1 Event (io.siddhi.core.event.Event)1 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)1 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)1 InputHandler (io.siddhi.core.stream.input.InputHandler)1 AsyncIncrementalSnapshotPersistor (io.siddhi.core.util.snapshot.AsyncIncrementalSnapshotPersistor)1 AsyncSnapshotPersistor (io.siddhi.core.util.snapshot.AsyncSnapshotPersistor)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.testng.annotations.Test)1