use of io.siddhi.core.exception.NoPersistenceStoreException in project siddhi by wso2.
the class SnapshotService method clearAllRevisions.
/**
* Clear all the revisions of persistence store of Siddhi App
*/
public void clearAllRevisions() throws CannotClearSiddhiAppStateException {
PersistenceStore persistenceStore = siddhiAppContext.getSiddhiContext().getPersistenceStore();
IncrementalPersistenceStore incrementalPersistenceStore = siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore();
String siddhiAppName = siddhiAppContext.getName();
if (persistenceStore != null) {
persistenceStore.clearAllRevisions(siddhiAppName);
} else if (incrementalPersistenceStore != null) {
incrementalPersistenceStore.clearAllRevisions(siddhiAppName);
} else {
throw new NoPersistenceStoreException("No persistence store assigned for siddhi app " + siddhiAppName);
}
}
use of io.siddhi.core.exception.NoPersistenceStoreException in project siddhi by wso2.
the class SnapshotService method restoreLastRevision.
public String restoreLastRevision() throws CannotRestoreSiddhiAppStateException {
PersistenceStore persistenceStore = siddhiAppContext.getSiddhiContext().getPersistenceStore();
IncrementalPersistenceStore incrementalPersistenceStore = siddhiAppContext.getSiddhiContext().getIncrementalPersistenceStore();
String siddhiAppName = siddhiAppContext.getName();
String revision;
if (persistenceStore != null) {
revision = persistenceStore.getLastRevision(siddhiAppName);
} else if (incrementalPersistenceStore != null) {
revision = incrementalPersistenceStore.getLastRevision(siddhiAppName);
} else {
throw new NoPersistenceStoreException("No persistence store assigned for siddhi app " + siddhiAppName);
}
if (revision != null) {
restoreRevision(revision);
}
return revision;
}
use of io.siddhi.core.exception.NoPersistenceStoreException 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();
}
use of io.siddhi.core.exception.NoPersistenceStoreException 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.exception.NoPersistenceStoreException in project siddhi by wso2.
the class SnapshotService method fullSnapshot.
public byte[] fullSnapshot() {
try {
SnapshotRequest.requestForFullSnapshot(true);
Map<String, Map<String, Map<String, Map<String, Map<String, Object>>>>> fullSnapshot = new HashMap<>();
byte[] serializedFullState = null;
if (log.isDebugEnabled()) {
log.debug("Taking snapshot ...");
}
try {
threadBarrier.lock();
waitForSystemStabilization();
for (Map.Entry<String, PartitionIdStateHolder> partitionIdState : partitionIdStates.entrySet()) {
for (Map.Entry<String, ElementStateHolder> queryState : partitionIdState.getValue().queryStateHolderMap.entrySet()) {
for (Map.Entry<String, StateHolder> elementState : queryState.getValue().elementHolderMap.entrySet()) {
Map<String, Map<String, State>> partitionKeyStates = elementState.getValue().getAllStates();
try {
for (Map.Entry<String, Map<String, State>> partitionKeyState : partitionKeyStates.entrySet()) {
for (Map.Entry<String, State> groupByKeyState : partitionKeyState.getValue().entrySet()) {
String partitionAndGroupByKey = partitionKeyState.getKey() + "--" + groupByKeyState.getKey();
State state = groupByKeyState.getValue();
Map<String, Object> itemStates = state.snapshot();
if (itemStates != null) {
Map<String, Object> itemSnapshots = new HashMap<>();
for (Map.Entry<String, Object> itemState : itemStates.entrySet()) {
if (itemState.getValue() instanceof Snapshot) {
if (((Snapshot) itemState.getValue()).isIncrementalSnapshot()) {
throw new NoPersistenceStoreException("No incremental " + "persistence store exist to store incremental " + "snapshot of siddhiApp:'" + siddhiAppContext.getName() + "' subElement:'" + queryState.getKey() + "' elementId:'" + elementState.getKey() + "' partitionKey:'" + partitionKeyState.getKey() + "' groupByKey:'" + groupByKeyState.getKey() + "' and itemKey:'" + itemState.getKey() + "'");
} else {
itemSnapshots.put(itemState.getKey(), itemState.getValue());
}
} else {
itemSnapshots.put(itemState.getKey(), itemState.getValue());
}
}
Map<String, Map<String, Map<String, Map<String, Object>>>> partitionIdSnapshot = fullSnapshot.computeIfAbsent(partitionIdState.getKey(), k -> new HashMap<>());
Map<String, Map<String, Map<String, Object>>> partitionGroupByKeySnapshot = partitionIdSnapshot.computeIfAbsent(partitionAndGroupByKey, k -> new HashMap<>());
Map<String, Map<String, Object>> querySnapshot = partitionGroupByKeySnapshot.computeIfAbsent(queryState.getKey(), k -> new HashMap<>());
Map<String, Object> elementSnapshot = querySnapshot.get(elementState.getKey());
if (elementSnapshot == null) {
querySnapshot.put(elementState.getKey(), itemSnapshots);
} else {
throw new SiddhiAppRuntimeException("Duplicate state exist for " + "siddhiApp:'" + siddhiAppContext.getName() + "' partitionKey:'" + partitionKeyState.getKey() + "' groupByKey:'" + groupByKeyState.getKey() + "' subElement:'" + queryState.getKey() + "' elementId:'" + elementState.getKey() + "'");
}
}
}
}
} finally {
elementState.getValue().returnAllStates(partitionKeyStates);
}
}
}
}
if (log.isDebugEnabled()) {
log.debug("Snapshot serialization started ...");
}
serializedFullState = ByteSerializer.objectToByte(fullSnapshot, siddhiAppContext);
if (log.isDebugEnabled()) {
log.debug("Snapshot serialization finished.");
}
} finally {
threadBarrier.unlock();
}
if (log.isDebugEnabled()) {
log.debug("Snapshot taken for Siddhi app '" + siddhiAppContext.getName() + "'");
}
return serializedFullState;
} finally {
SnapshotRequest.requestForFullSnapshot(false);
}
}
Aggregations