use of org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException in project ballerina by ballerina-lang.
the class SnapshotService method restore.
public void restore(byte[] snapshot) throws CannotRestoreSiddhiAppStateException {
Map<String, Map<String, Object>> snapshots = (Map<String, Map<String, Object>>) ByteSerializer.byteToObject(snapshot, siddhiAppContext);
List<Snapshotable> snapshotableList;
try {
threadBarrier.lock();
List<Snapshotable> partitionSnapshotables = snapshotableMap.get("partition");
try {
if (partitionSnapshotables != null) {
for (Snapshotable snapshotable : partitionSnapshotables) {
snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
}
}
} catch (Throwable t) {
throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
}
for (Map.Entry<String, List<Snapshotable>> entry : snapshotableMap.entrySet()) {
if (entry.getKey().equals("partition")) {
continue;
}
snapshotableList = entry.getValue();
try {
for (Snapshotable snapshotable : snapshotableList) {
snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
}
} catch (Throwable t) {
throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
}
}
} finally {
threadBarrier.unlock();
}
}
use of org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException in project ballerina by ballerina-lang.
the class PartitionTestCase1 method testPartitionQuery38.
@Test
public void testPartitionQuery38() throws InterruptedException {
log.info("Partition test");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('PartitionTest') " + "define stream streamA (symbol string, price int); " + "define stream streamB (symbol string, price int); " + "partition with (symbol of streamA, symbol of streamB) " + "begin " + "@info(name = 'query1') " + "from streamA " + "select symbol, price " + "insert into StockQuote ; " + "end ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
StreamCallback streamCallback = new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
// AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
count.addAndGet(events.length);
eventArrived = true;
}
};
siddhiAppRuntime.addCallback("StockQuote", streamCallback);
SiddhiAppRuntime siddhiAppRuntime2 = siddhiManager.createSiddhiAppRuntime(siddhiApp);
StreamCallback streamCallback2 = new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
count.addAndGet(events.length);
eventArrived = true;
}
};
siddhiAppRuntime2.addCallback("StockQuote", streamCallback2);
InputHandler inputHandlerA = siddhiAppRuntime.getInputHandler("streamA");
InputHandler inputHandlerB = siddhiAppRuntime2.getInputHandler("streamA");
siddhiAppRuntime.start();
inputHandlerA.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
inputHandlerA.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
inputHandlerA.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
byte[] snapshot = siddhiAppRuntime.snapshot();
siddhiAppRuntime.shutdown();
Thread.sleep(1000);
try {
siddhiAppRuntime2.restore(snapshot);
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
siddhiAppRuntime2.start();
inputHandlerB.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
inputHandlerB.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
inputHandlerB.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
AssertJUnit.assertEquals(6, count.get());
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException in project ballerina by ballerina-lang.
the class PersistenceTestCase 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[price>10]#window.length(10) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream ";
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.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
Thread.sleep(100);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(200), lastValue);
// persisting
Thread.sleep(500);
siddhiAppRuntime.persist();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// restarting siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// shutdown siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count <= 6);
AssertJUnit.assertEquals(new Long(400), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
use of org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException in project ballerina by ballerina-lang.
the class PersistenceTestCase method persistenceTest10.
@Test(dependsOnMethods = "persistenceTest9")
public void persistenceTest10() throws InterruptedException {
log.info("persistence test 10 - sort 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#window.sort(2,volume) " + "select volume " + "insert all events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
eventArrived = true;
atomicCount.incrementAndGet();
for (Event inEvent : inEvents) {
count++;
}
if (removeEvents != null) {
for (Event removeEvent : removeEvents) {
lastValueRemoved = (Integer) removeEvent.getData(0);
}
}
}
};
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 55.6f, 100 });
inputHandler.send(new Object[] { "IBM", 75.6f, 300 });
inputHandler.send(new Object[] { "WSO2", 57.6f, 200 });
Thread.sleep(1000);
AssertJUnit.assertEquals(3, count);
AssertJUnit.assertTrue(eventArrived);
// persisting
siddhiAppRuntime.persist();
inputHandler.send(new Object[] { "WSO2", 55.6f, 20 });
inputHandler.send(new Object[] { "WSO2", 57.6f, 40 });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
inputHandler.send(new Object[] { "WSO2", 55.6f, 20 });
SiddhiTestHelper.waitForEvents(100, 6, atomicCount, 10000);
AssertJUnit.assertEquals(true, eventArrived);
AssertJUnit.assertEquals(200, lastValueRemoved);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException in project ballerina by ballerina-lang.
the class PersistenceTestCase method persistenceTest8.
@Test(dependsOnMethods = "persistenceTest7")
public void persistenceTest8() throws InterruptedException {
log.info("persistence test 8 - 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[price>10]#window.length(10) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream ";
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.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
Thread.sleep(100);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(200), lastValue);
// persisting
Thread.sleep(500);
byte[] snapshot = siddhiAppRuntime.snapshot();
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// restarting siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
// loading
try {
siddhiAppRuntime.restore(snapshot);
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
// shutdown siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count == 6);
AssertJUnit.assertEquals(new Long(400), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
Aggregations