Search in sources :

Example 1 with State

use of io.siddhi.core.util.snapshot.state.State in project siddhi by wso2.

the class SnapshotService method incrementalSnapshot.

public IncrementalSnapshot incrementalSnapshot() {
    try {
        SnapshotRequest.requestForFullSnapshot(false);
        Map<String, Map<String, byte[]>> incrementalSnapshotMap = new HashMap<>();
        Map<String, Map<String, byte[]>> incrementalBaseSnapshotMap = new HashMap<>();
        Map<String, Map<String, byte[]>> periodicSnapshotMap = new HashMap<>();
        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()) {
                                    State state = groupByKeyState.getValue();
                                    Map<String, Object> itemStates = state.snapshot();
                                    if (itemStates != null) {
                                        Map<String, Object> itemSnapshotsIncremental = new HashMap<>();
                                        Map<String, Object> itemSnapshotsIncrementalBase = new HashMap<>();
                                        Map<String, Object> itemSnapshotsPeriodic = new HashMap<>();
                                        for (Map.Entry<String, Object> itemState : itemStates.entrySet()) {
                                            if (itemState.getValue() instanceof Snapshot) {
                                                if (((Snapshot) itemState.getValue()).isIncrementalSnapshot()) {
                                                    itemSnapshotsIncremental.put(itemState.getKey(), itemState.getValue());
                                                } else {
                                                    itemSnapshotsIncrementalBase.put(itemState.getKey(), itemState.getValue());
                                                }
                                            } else {
                                                itemSnapshotsPeriodic.put(itemState.getKey(), itemState.getValue());
                                            }
                                        }
                                        if (!itemSnapshotsIncremental.isEmpty()) {
                                            addToSnapshotIncrements(incrementalSnapshotMap, partitionIdState, queryState, elementState, partitionKeyState, groupByKeyState, itemSnapshotsIncremental);
                                        }
                                        if (!itemSnapshotsIncrementalBase.isEmpty()) {
                                            addToSnapshotIncrements(incrementalBaseSnapshotMap, partitionIdState, queryState, elementState, partitionKeyState, groupByKeyState, itemSnapshotsIncrementalBase);
                                        }
                                        if (!itemSnapshotsPeriodic.isEmpty()) {
                                            addToSnapshotIncrements(periodicSnapshotMap, partitionIdState, queryState, elementState, partitionKeyState, groupByKeyState, itemSnapshotsPeriodic);
                                        }
                                    }
                                }
                            }
                        } finally {
                            elementState.getValue().returnAllStates(partitionKeyStates);
                        }
                    }
                }
            }
        } finally {
            threadBarrier.unlock();
        }
        if (log.isDebugEnabled()) {
            log.debug("Snapshot taken for Siddhi app '" + siddhiAppContext.getName() + "'");
        }
        IncrementalSnapshot snapshot = new IncrementalSnapshot();
        if (!incrementalSnapshotMap.isEmpty()) {
            snapshot.setIncrementalState(incrementalSnapshotMap);
        }
        if (!incrementalBaseSnapshotMap.isEmpty()) {
            snapshot.setIncrementalStateBase(incrementalBaseSnapshotMap);
        }
        if (!periodicSnapshotMap.isEmpty()) {
            snapshot.setPeriodicState(periodicSnapshotMap);
        }
        return snapshot;
    } finally {
        SnapshotRequest.requestForFullSnapshot(false);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) State(io.siddhi.core.util.snapshot.state.State) StateHolder(io.siddhi.core.util.snapshot.state.StateHolder) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 2 with State

use of io.siddhi.core.util.snapshot.state.State in project siddhi by wso2.

the class SnapshotService method restore.

public void restore(byte[] snapshot) throws CannotRestoreSiddhiAppStateException {
    if (snapshot == null) {
        throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " failed due to no snapshot.");
    }
    Map<String, Map<String, Map<String, Map<String, Map<String, Object>>>>> fullSnapshot = (Map<String, Map<String, Map<String, Map<String, Map<String, Object>>>>>) ByteSerializer.byteToObject(snapshot, siddhiAppContext);
    if (fullSnapshot == null) {
        throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " failed due to invalid snapshot.");
    }
    try {
        threadBarrier.lock();
        waitForSystemStabilization();
        try {
            // cleaning old group by states
            cleanGroupByStates();
            // restore data
            for (Map.Entry<String, Map<String, Map<String, Map<String, Map<String, Object>>>>> partitionIdSnapshot : fullSnapshot.entrySet()) {
                PartitionIdStateHolder partitionStateHolder = partitionIdStates.get(partitionIdSnapshot.getKey());
                if (partitionStateHolder == null) {
                    continue;
                }
                for (Map.Entry<String, Map<String, Map<String, Map<String, Object>>>> partitionGroupByKeySnapshot : partitionIdSnapshot.getValue().entrySet()) {
                    for (Map.Entry<String, Map<String, Map<String, Object>>> querySnapshot : partitionGroupByKeySnapshot.getValue().entrySet()) {
                        ElementStateHolder elementStateHolder = partitionStateHolder.queryStateHolderMap.get(querySnapshot.getKey());
                        if (elementStateHolder == null) {
                            continue;
                        }
                        for (Map.Entry<String, Map<String, Object>> elementSnapshot : querySnapshot.getValue().entrySet()) {
                            StateHolder stateHolder = elementStateHolder.elementHolderMap.get(elementSnapshot.getKey());
                            if (stateHolder == null) {
                                continue;
                            }
                            try {
                                String partitionKey = null;
                                String groupByKey = null;
                                if (partitionGroupByKeySnapshot.getKey() != null) {
                                    String[] keys = partitionGroupByKeySnapshot.getKey().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);
                                State state = stateHolder.getState();
                                try {
                                    if (state == null) {
                                        continue;
                                    }
                                    Map<String, Object> snapshotRestores = new HashMap<>();
                                    for (Map.Entry<String, Object> itemSnapshot : elementSnapshot.getValue().entrySet()) {
                                        if (itemSnapshot.getValue() instanceof Snapshot) {
                                            SnapshotStateList snapshotStateList = new SnapshotStateList();
                                            snapshotStateList.putSnapshotState(0L, (Snapshot) itemSnapshot.getValue());
                                            snapshotRestores.put(itemSnapshot.getKey(), snapshotStateList);
                                        } else {
                                            snapshotRestores.put(itemSnapshot.getKey(), itemSnapshot.getValue());
                                        }
                                    }
                                    state.restore(snapshotRestores);
                                } finally {
                                    stateHolder.returnState(state);
                                }
                            } finally {
                                SiddhiAppContext.stopPartitionFlow();
                                SiddhiAppContext.stopGroupByFlow();
                            }
                        }
                    }
                }
            }
        } 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 : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CannotRestoreSiddhiAppStateException(io.siddhi.core.exception.CannotRestoreSiddhiAppStateException) 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) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap)

Example 3 with State

use of io.siddhi.core.util.snapshot.state.State in project siddhi by wso2.

the class IncrementalDataAggregator method getProcessedEventChunk.

private synchronized ComplexEventChunk<StreamEvent> getProcessedEventChunk() {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>();
    Map<String, State> valueStoreMap = this.valueStateHolder.getAllGroupByStates();
    try {
        for (State aState : valueStoreMap.values()) {
            ValueState state = (ValueState) aState;
            StreamEvent streamEvent = streamEventFactory.newInstance();
            long timestamp = state.lastTimestamp;
            streamEvent.setTimestamp(timestamp);
            state.setValue(timestamp, 0);
            streamEvent.setOutputData(state.values);
            streamEventChunk.add(streamEvent);
        }
    } finally {
        this.valueStateHolder.returnGroupByStates(valueStoreMap);
    }
    return streamEventChunk;
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) State(io.siddhi.core.util.snapshot.state.State) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 4 with State

use of io.siddhi.core.util.snapshot.state.State in project siddhi by wso2.

the class OutOfOrderEventsDataAggregator method createEventChunkFromAggregatedData.

private synchronized ComplexEventChunk<StreamEvent> createEventChunkFromAggregatedData() {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>();
    Map<String, State> valueStoreMap = this.valueStateHolder.getAllGroupByStates();
    try {
        for (State aState : valueStoreMap.values()) {
            ValueState state = (ValueState) aState;
            StreamEvent streamEvent = streamEventFactory.newInstance();
            long timestamp = state.lastTimestamp;
            streamEvent.setTimestamp(timestamp);
            state.setValue(timestamp, 0);
            streamEvent.setOutputData(state.values);
            streamEventChunk.add(streamEvent);
        }
    } finally {
        this.valueStateHolder.returnGroupByStates(valueStoreMap);
    }
    return streamEventChunk;
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) State(io.siddhi.core.util.snapshot.state.State) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 5 with State

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

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