Search in sources :

Example 1 with PersistenceStoreException

use of io.siddhi.core.exception.PersistenceStoreException 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)

Aggregations

SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)1 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)1 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)1 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)1 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)1 ThreadBarrier (io.siddhi.core.util.ThreadBarrier)1 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)1 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)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 Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)1 SnapshotStateList (io.siddhi.core.util.snapshot.state.SnapshotStateList)1 State (io.siddhi.core.util.snapshot.state.State)1 StateHolder (io.siddhi.core.util.snapshot.state.StateHolder)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1