Search in sources :

Example 1 with NoPersistenceStoreException

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);
    }
}
Also used : IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException)

Example 2 with NoPersistenceStoreException

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;
}
Also used : IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) PersistenceStore(io.siddhi.core.util.persistence.PersistenceStore) IncrementalPersistenceStore(io.siddhi.core.util.persistence.IncrementalPersistenceStore) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException)

Example 3 with NoPersistenceStoreException

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();
}
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 4 with NoPersistenceStoreException

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);
    }
}
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 5 with NoPersistenceStoreException

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);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) StateHolder(io.siddhi.core.util.snapshot.state.StateHolder) NoPersistenceStoreException(io.siddhi.core.exception.NoPersistenceStoreException) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) State(io.siddhi.core.util.snapshot.state.State) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Aggregations

NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)5 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)3 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)3 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)2 Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)2 State (io.siddhi.core.util.snapshot.state.State)2 StateHolder (io.siddhi.core.util.snapshot.state.StateHolder)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (io.siddhi.core.SiddhiManager)1 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 Event (io.siddhi.core.event.Event)1 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)1 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)1 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)1 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)1 InputHandler (io.siddhi.core.stream.input.InputHandler)1