Search in sources :

Example 1 with IncrementalSnapshotInfo

use of io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo in project siddhi by wso2.

the class IncrementalFileSystemPersistenceStore method getLastRevision.

@Override
public String getLastRevision(String siddhiAppName) {
    long restoreTime = -1;
    IncrementalSnapshotInfo lastSnapshotInfo = null;
    File dir = new File(folder + File.separator + siddhiAppName);
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        return null;
    }
    for (File file : files) {
        String fileName = file.getName();
        IncrementalSnapshotInfo snapshotInfo = PersistenceHelper.convertRevision(fileName);
        if (snapshotInfo.getTime() > restoreTime && siddhiAppName.equals(snapshotInfo.getSiddhiAppId()) && snapshotInfo.getId() != null && snapshotInfo.getQueryName() != null) {
            // Note: Here we discard the (items.length == 2) scenario which is handled
            // by the full snapshot handling
            restoreTime = snapshotInfo.getTime();
            lastSnapshotInfo = snapshotInfo;
        }
    }
    if (restoreTime != -1) {
        if (log.isDebugEnabled()) {
            log.debug("Latest revision to load: " + restoreTime + PersistenceConstants.REVISION_SEPARATOR + siddhiAppName);
        }
        return lastSnapshotInfo.getRevision();
    }
    return null;
}
Also used : IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) File(java.io.File)

Example 2 with IncrementalSnapshotInfo

use of io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo 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 3 with IncrementalSnapshotInfo

use of io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo in project siddhi by wso2.

the class SnapshotService method restore.

public void restore(Map<String, Map<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>>> snapshot) throws CannotRestoreSiddhiAppStateException {
    try {
        threadBarrier.lock();
        waitForSystemStabilization();
        try {
            // cleaning old group by states
            cleanGroupByStates();
            // restore data
            for (Map.Entry<String, Map<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>>> partitionIdSnapshot : snapshot.entrySet()) {
                PartitionIdStateHolder partitionStateHolder = partitionIdStates.get(partitionIdSnapshot.getKey());
                if (partitionStateHolder == null) {
                    continue;
                }
                for (Iterator<Map.Entry<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>>> iterator = partitionIdSnapshot.getValue().entrySet().iterator(); iterator.hasNext(); ) {
                    Map.Entry<String, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>> partitionGroupByKeySnapshot = iterator.next();
                    restoreIncrementalSnapshot(partitionStateHolder, partitionGroupByKeySnapshot.getValue());
                    iterator.remove();
                }
            }
        } 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();
    }
}
Also used : CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 4 with IncrementalSnapshotInfo

use of io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo in project siddhi by wso2.

the class SnapshotService method restoreIncrementalSnapshot.

private void restoreIncrementalSnapshot(PartitionIdStateHolder partitionIdStateHolder, Map<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>> incrementalStateByTime) {
    if (incrementalStateByTime != null) {
        String id = null;
        State state = null;
        StateHolder stateHolder = null;
        Map<String, Object> deserializedStateMap = null;
        try {
            for (Iterator<Map.Entry<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>>> iterator = incrementalStateByTime.entrySet().iterator(); iterator.hasNext(); ) {
                Map.Entry<String, Map<Long, Map<IncrementalSnapshotInfo, byte[]>>> incrementalStateByTimeEntry = iterator.next();
                iterator.remove();
                for (Iterator<Map.Entry<Long, Map<IncrementalSnapshotInfo, byte[]>>> partitionGroupByKeyIterator = incrementalStateByTimeEntry.getValue().entrySet().iterator(); partitionGroupByKeyIterator.hasNext(); ) {
                    Map.Entry<Long, Map<IncrementalSnapshotInfo, byte[]>> partitionGroupByKeyStateByTimeEntry = partitionGroupByKeyIterator.next();
                    partitionGroupByKeyIterator.remove();
                    for (Iterator<Map.Entry<IncrementalSnapshotInfo, byte[]>> iterator1 = partitionGroupByKeyStateByTimeEntry.getValue().entrySet().iterator(); iterator1.hasNext(); ) {
                        Map.Entry<IncrementalSnapshotInfo, byte[]> incrementalStateByInfoEntry = iterator1.next();
                        iterator1.remove();
                        IncrementalSnapshotInfo incrementalSnapshotInfo = incrementalStateByInfoEntry.getKey();
                        Map<String, Object> singleIncrementSnapshot = (Map<String, Object>) ByteSerializer.byteToObject(incrementalStateByInfoEntry.getValue(), siddhiAppContext);
                        if (singleIncrementSnapshot != null) {
                            if (!incrementalSnapshotInfo.getId().equals(id)) {
                                if (id != null) {
                                    state.restore(deserializedStateMap);
                                    SiddhiAppContext.startPartitionFlow(id);
                                    try {
                                        stateHolder.returnState(state);
                                    } finally {
                                        SiddhiAppContext.stopPartitionFlow();
                                    }
                                    id = null;
                                    state = null;
                                    stateHolder = null;
                                    deserializedStateMap = null;
                                }
                                ElementStateHolder elementStateHolder = partitionIdStateHolder.queryStateHolderMap.get(incrementalSnapshotInfo.getQueryName());
                                if (elementStateHolder == null) {
                                    continue;
                                }
                                stateHolder = elementStateHolder.elementHolderMap.get(incrementalSnapshotInfo.getElementId());
                                if (stateHolder == null) {
                                    continue;
                                }
                                String partitionKey = null;
                                String groupByKey = null;
                                String[] keys = incrementalSnapshotInfo.getPartitionGroupByKey().split("--");
                                if (keys.length == 2) {
                                    if (!keys[0].equals("null")) {
                                        partitionKey = keys[0];
                                    }
                                    if (!keys[1].equals("null")) {
                                        groupByKey = keys[1];
                                    }
                                }
                                SiddhiAppContext.startPartitionFlow(partitionKey);
                                SiddhiAppContext.startGroupByFlow(groupByKey);
                                try {
                                    state = stateHolder.getState();
                                } finally {
                                    SiddhiAppContext.stopGroupByFlow();
                                    SiddhiAppContext.stopPartitionFlow();
                                }
                                if (state != null) {
                                    id = incrementalSnapshotInfo.getId();
                                    deserializedStateMap = new HashMap<>();
                                }
                            }
                            if (state != null) {
                                for (Map.Entry<String, Object> singleIncrementSnapshotEntry : singleIncrementSnapshot.entrySet()) {
                                    if (singleIncrementSnapshotEntry.getValue() instanceof Snapshot) {
                                        Snapshot snapshot = (Snapshot) singleIncrementSnapshotEntry.getValue();
                                        SnapshotStateList snapshotStateList = (SnapshotStateList) deserializedStateMap.computeIfAbsent(singleIncrementSnapshotEntry.getKey(), k -> new SnapshotStateList());
                                        if (snapshot.isIncrementalSnapshot()) {
                                            snapshotStateList.putSnapshotState(partitionGroupByKeyStateByTimeEntry.getKey(), snapshot);
                                        } else {
                                            snapshotStateList.getSnapshotStates().clear();
                                            snapshotStateList.putSnapshotState(partitionGroupByKeyStateByTimeEntry.getKey(), snapshot);
                                        }
                                    } else {
                                        deserializedStateMap.put(singleIncrementSnapshotEntry.getKey(), singleIncrementSnapshotEntry.getValue());
                                    }
                                }
                            }
                        }
                    }
                }
                if (id != null) {
                    state.restore(deserializedStateMap);
                    SiddhiAppContext.startPartitionFlow(id);
                    try {
                        stateHolder.returnState(state);
                    } finally {
                        SiddhiAppContext.stopPartitionFlow();
                    }
                    id = null;
                    state = null;
                    stateHolder = null;
                    deserializedStateMap = null;
                }
            }
        } finally {
            if (id != null && stateHolder != null && state != null) {
                SiddhiAppContext.startPartitionFlow(id);
                try {
                    stateHolder.returnState(state);
                } finally {
                    SiddhiAppContext.stopPartitionFlow();
                }
                id = null;
                state = null;
                stateHolder = null;
            }
        }
    }
}
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) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) State(io.siddhi.core.util.snapshot.state.State) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) StateHolder(io.siddhi.core.util.snapshot.state.StateHolder) IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 5 with IncrementalSnapshotInfo

use of io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo in project siddhi by wso2.

the class IncrementalFileSystemPersistenceStore method getListOfRevisionsToLoad.

@Override
public List<IncrementalSnapshotInfo> getListOfRevisionsToLoad(long restoreTime, String siddhiAppName) {
    File dir = new File(folder + File.separator + siddhiAppName);
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        return null;
    }
    List<IncrementalSnapshotInfo> results = new ArrayList<>();
    for (File file : files) {
        String fileName = file.getName();
        IncrementalSnapshotInfo snapshotInfo = PersistenceHelper.convertRevision(fileName);
        if (snapshotInfo.getTime() <= restoreTime && siddhiAppName.equals(snapshotInfo.getSiddhiAppId()) && snapshotInfo.getId() != null && snapshotInfo.getQueryName() != null) {
            // by the full snapshot handling
            if (log.isDebugEnabled()) {
                log.debug("List of revisions to load : " + fileName);
            }
            results.add(snapshotInfo);
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) IncrementalSnapshotInfo(io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo) File(java.io.File)

Aggregations

IncrementalSnapshotInfo (io.siddhi.core.util.persistence.util.IncrementalSnapshotInfo)6 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)3 File (java.io.File)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)2 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)2 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)2 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)2 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)2 ThreadBarrier (io.siddhi.core.util.ThreadBarrier)2 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)2 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)2 PersistenceConstants (io.siddhi.core.util.persistence.util.PersistenceConstants)2 PersistenceHelper (io.siddhi.core.util.persistence.util.PersistenceHelper)2 Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)2 SnapshotStateList (io.siddhi.core.util.snapshot.state.SnapshotStateList)2 State (io.siddhi.core.util.snapshot.state.State)2