Search in sources :

Example 16 with PersistenceStore

use of io.siddhi.core.util.persistence.PersistenceStore in project siddhi by wso2.

the class LogTestCase method persistenceTest1.

@Test
public void persistenceTest1() throws InterruptedException {
    log.info("persistence test 1 - window query");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String siddhiApp = "" + "@app:name('Test') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream" + "   #log()" + "   #log('test message')" + "   #log(false)" + "   #log(true)" + "   #log('test message',false)" + "   #log('test message',true)" + "   #log('error','test message')" + "   #log('error','test message',false)" + "   #log('warn','test message',true)" + "select * " + "insert into OutStream ";
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            eventArrived = true;
        }
    };
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", queryCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Event[] { new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100 }), new Event(System.currentTimeMillis(), new Object[] { "GOOG", 70.6f, 100 }) });
    Thread.sleep(100);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(true, eventArrived);
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 17 with PersistenceStore

use of io.siddhi.core.util.persistence.PersistenceStore in project siddhi by wso2.

the class SnapshotService method restoreRevision.

public void restoreRevision(String revision) throws CannotRestoreSiddhiAppStateException {
    PersistenceStore persistenceStore = siddhiAppContext.getSiddhiContext().getPersistenceStore();
    IncrementalPersistenceStore incrementalPersistenceStore = siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore();
    String siddhiAppName = siddhiAppContext.getName();
    if (persistenceStore != null) {
        if (log.isDebugEnabled()) {
            log.debug("Restoring revision: " + revision + " ...");
        }
        byte[] snapshot = persistenceStore.load(siddhiAppContext.getName(), revision);
        if (snapshot != null) {
            restore(snapshot);
            if (log.isDebugEnabled()) {
                log.debug("Restored revision: " + revision);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("No data found for revision: " + revision);
            }
            throw new PersistenceStoreException("No data found for revision: " + revision);
        }
    } else if (incrementalPersistenceStore != null) {
        if (log.isDebugEnabled()) {
            log.debug("Restoring revision: " + revision + " ...");
        }
        IncrementalSnapshotInfo restoreSnapshotInfo = PersistenceHelper.convertRevision(revision);
        List<IncrementalSnapshotInfo> incrementalSnapshotInfos = incrementalPersistenceStore.getListOfRevisionsToLoad(restoreSnapshotInfo.getTime(), restoreSnapshotInfo.getSiddhiAppId());
        if (incrementalSnapshotInfos != null) {
            incrementalSnapshotInfos.sort(new Comparator<IncrementalSnapshotInfo>() {

                @Override
                public int compare(IncrementalSnapshotInfo o1, IncrementalSnapshotInfo o2) {
                    int results = o1.getId().compareTo(o2.getId());
                    if (results == 0) {
                        results = Long.compare(o2.getTime(), o1.getTime());
                        if (results == 0) {
                            return o2.getType().compareTo(o1.getType());
                        }
                    }
                    return results;
                }
            });
            String lastId = null;
            boolean baseFound = false;
            boolean perioicFound = false;
            for (Iterator<IncrementalSnapshotInfo> iterator = incrementalSnapshotInfos.iterator(); iterator.hasNext(); ) {
                IncrementalSnapshotInfo snapshotInfo = iterator.next();
                if (snapshotInfo.getId().equals(lastId)) {
                    if (baseFound && (snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.BASE || snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.INCREMENT)) {
                        iterator.remove();
                    } else if (perioicFound && snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.PERIODIC) {
                        iterator.remove();
                    } else if (snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.BASE) {
                        baseFound = true;
                    } else if (snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.PERIODIC) {
                        perioicFound = true;
                    }
                } else {
                    baseFound = snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.BASE;
                    perioicFound = snapshotInfo.getType() == IncrementalSnapshotInfo.SnapshotType.PERIODIC;
                }
                lastId = snapshotInfo.getId();
            }
            Map<String, Map<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>>> incrementalState = new HashMap<>();
            for (IncrementalSnapshotInfo snapshotInfo : incrementalSnapshotInfos) {
                Map<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>> incrementalStateByPartitionGroupByKey = incrementalState.computeIfAbsent(snapshotInfo.getPartitionId(), k -> new TreeMap<>());
                Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>> incrementalStateByTime = incrementalStateByPartitionGroupByKey.computeIfAbsent(snapshotInfo.getPartitionGroupByKey(), k -> new TreeMap<>());
                Map<Long, Map<IncrementalSnapshotInfo, byte[]>> idByTime = incrementalStateByTime.computeIfAbsent(snapshotInfo.getId(), k -> new TreeMap<>());
                Map<IncrementalSnapshotInfo, byte[]> incrementalStateByInfo = idByTime.computeIfAbsent(snapshotInfo.getTime(), k -> new HashMap<>());
                incrementalStateByInfo.put(snapshotInfo, incrementalPersistenceStore.load(snapshotInfo));
            }
            restore(incrementalState);
            if (log.isDebugEnabled()) {
                log.debug("Restored revision: " + revision);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("No data found for revision: " + revision);
            }
            throw new PersistenceStoreException("No data found for revision: " + revision);
        }
    } else {
        throw new NoPersistenceStoreException("No persistence store assigned for siddhi app " + siddhiAppName);
    }
}
Also used : ThreadBarrier(io.siddhi.core.util.ThreadBarrier) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) HashMap(java.util.HashMap) State(io.siddhi.core.util.snapshot.state.State) IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) PersistenceConstants(io.siddhi.core.util.persistence.util.PersistenceConstants) Map(java.util.Map) PersistenceStoreException(io.siddhi.core.exception.PersistenceStoreException) StateHolder(io.siddhi.core.util.snapshot.state.StateHolder) CannotClearSiddhiAppStateException(io.siddhi.core.exception.CannotClearSiddhiAppStateException) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) PersistenceHelper(io.siddhi.core.util.persistence.util.PersistenceHelper) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) List(java.util.List) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) Logger(org.apache.logging.log4j.Logger) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) PersistenceStoreException(io.siddhi.core.exception.PersistenceStoreException) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) TreeMap(java.util.TreeMap) Comparator(java.util.Comparator) IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) Iterator(java.util.Iterator) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) List(java.util.List) IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 18 with PersistenceStore

use of io.siddhi.core.util.persistence.PersistenceStore in project siddhi by wso2.

the class SessionWindowTestCase method testSessionWindow18.

@Test(description = "Check if events are persist when using session window")
public void testSessionWindow18() throws InterruptedException {
    log.info("SessionWindow Test18: Testing persistence ");
    PersistenceStore persistenceStore = new InMemoryPersistenceStore();
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setPersistenceStore(persistenceStore);
    String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int); ";
    String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(2 sec, user) " + "select * " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            count.addAndGet(events.length);
            for (Event event : events) {
                innerAssertionsPassed = false;
                AssertJUnit.assertTrue(("101".equals(event.getData(1).toString()) || "102".equals(event.getData(1).toString())) || "103".equals(event.getData(1).toString()));
                innerAssertionsPassed = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "user0", 101, 34.4, 5 });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "user0", 102, 24.5, 2 });
    siddhiAppRuntime.persist();
    siddhiAppRuntime.shutdown();
    inputHandler = siddhiAppRuntime.getInputHandler("purchaseEventStream");
    siddhiAppRuntime.start();
    try {
        siddhiAppRuntime.restoreLastRevision();
    } catch (CannotRestoreSiddhiAppStateException e) {
        Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
    }
    inputHandler.send(new Object[] { "user0", 103, 24.5, 2 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 4200);
    AssertJUnit.assertTrue(innerAssertionsPassed);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) InMemoryPersistenceStore(io.siddhi.core.util.persistence.InMemoryPersistenceStore) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)18 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (io.siddhi.core.SiddhiManager)15 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)15 InputHandler (io.siddhi.core.stream.input.InputHandler)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)15 Test (org.testng.annotations.Test)15 Event (io.siddhi.core.event.Event)14 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)11 StreamCallback (io.siddhi.core.stream.output.StreamCallback)4 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)3 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)3 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)1 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)1 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)1 ThreadBarrier (io.siddhi.core.util.ThreadBarrier)1 IncrementalSnapshotInfo (io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo)1 PersistenceConstants (io.siddhi.core.util.persistence.util.PersistenceConstants)1 PersistenceHelper (io.siddhi.core.util.persistence.util.PersistenceHelper)1