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