Search in sources :

Example 6 with State

use of io.siddhi.core.util.snapshot.state.State 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

State (io.siddhi.core.util.snapshot.state.State)6 Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)4 StateHolder (io.siddhi.core.util.snapshot.state.StateHolder)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)2 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 StreamEvent (io.siddhi.core.event.stream.StreamEvent)2 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)2 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)2 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)2 SnapshotStateList (io.siddhi.core.util.snapshot.state.SnapshotStateList)2 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 CannotClearSiddhiAppStateException (io.siddhi.core.exception.CannotClearSiddhiAppStateException)1 PersistenceStoreException (io.siddhi.core.exception.PersistenceStoreException)1 ThreadBarrier (io.siddhi.core.util.ThreadBarrier)1 IncrementalPersistenceStore (io.siddhi.core.util.persistence.IncrementalPersistenceStore)1 PersistenceStore (io.siddhi.core.util.persistence.PersistenceStore)1