Search in sources :

Example 1 with Snapshot

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

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

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

the class SnapshotableStreamEventQueue method getSnapshot.

public Snapshot getSnapshot() {
    if (isFullSnapshot()) {
        forceFullSnapshot = false;
        return new Snapshot(this.getFirst(), false);
    } else {
        Snapshot snapshot = new Snapshot(operationChangeLog, true);
        operationChangeLog = new ArrayList<>();
        return snapshot;
    }
}
Also used : Snapshot(io.siddhi.core.util.snapshot.state.Snapshot)

Example 4 with Snapshot

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

the class IndexEventHolder method restore.

public void restore(SnapshotStateList snapshotStatelist) {
    TreeMap<Long, Snapshot> revisions = snapshotStatelist.getSnapshotStates();
    Iterator<Map.Entry<Long, Snapshot>> itr = revisions.entrySet().iterator();
    this.isOperationLogEnabled = false;
    while (itr.hasNext()) {
        Map.Entry<Long, Snapshot> snapshotEntry = itr.next();
        if (!snapshotEntry.getValue().isIncrementalSnapshot()) {
            this.deleteAll();
            IndexEventHolder snapshotEventHolder = (IndexEventHolder) snapshotEntry.getValue().getState();
            if (primaryKeyData != null) {
                primaryKeyData.clear();
                primaryKeyData.putAll(snapshotEventHolder.primaryKeyData);
            }
            if (indexData != null) {
                indexData.clear();
                indexData.putAll(snapshotEventHolder.indexData);
            }
            forceFullSnapshot = false;
        } else {
            ArrayList<Operation> operations = (ArrayList<Operation>) snapshotEntry.getValue().getState();
            for (Operation op : operations) {
                switch(op.operation) {
                    case ADD:
                        add((StreamEvent) op.parameters);
                        break;
                    case REMOVE:
                        deleteAll((StreamEvent) op.parameters);
                        break;
                    case CLEAR:
                        deleteAll();
                        break;
                    case OVERWRITE:
                        overwrite((StreamEvent) op.parameters);
                        break;
                    case DELETE_BY_OPERATOR:
                        Object[] args = (Object[]) op.parameters;
                        delete((String) args[0], (Compare.Operator) args[1], args[2]);
                        break;
                    default:
                        continue;
                }
            }
        }
    }
    this.isOperationLogEnabled = true;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) Compare(io.siddhi.query.api.expression.condition.Compare) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 5 with Snapshot

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

the class SnapshotableEventQueueTestCase method incrementalPersistenceTest3.

@Test
public void incrementalPersistenceTest3() throws InterruptedException, IOException, ClassNotFoundException {
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addOutputData(new Attribute("symbol", Attribute.Type.STRING));
    metaStreamEvent.addOutputData(new Attribute("price", Attribute.Type.FLOAT));
    metaStreamEvent.addOutputData(new Attribute("volume", Attribute.Type.LONG));
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, new StreamEventFactory(metaStreamEvent));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setOutputData(new Object[] { "IBM", 500.6f, 1 });
    for (int i = 0; i < 10; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    HashMap<Long, String> snapshots = new HashMap<>();
    Snapshot snapshot1 = snapshotableStreamEventQueue.getSnapshot();
    StreamEvent streamEvents = (StreamEvent) snapshot1.getState();
    Assert.assertTrue(streamEvents != null);
    snapshots.put(3L, toString(snapshot1));
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    for (int i = 7; i < 10; i++) {
        snapshotableStreamEventQueue.next();
        snapshotableStreamEventQueue.remove();
    }
    Snapshot snapshot2 = snapshotableStreamEventQueue.getSnapshot();
    ArrayList<Operation> operationLog = (ArrayList<Operation>) snapshot2.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(4L, toString(snapshot2));
    for (int i = 10; i < 15; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    Snapshot snapshot3 = snapshotableStreamEventQueue.getSnapshot();
    operationLog = (ArrayList<Operation>) snapshot3.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(5L, toString(snapshot3));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue2 = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    SnapshotStateList snapshotStateList = new SnapshotStateList();
    for (Map.Entry<Long, String> entry : snapshots.entrySet()) {
        snapshotStateList.putSnapshotState(entry.getKey(), (Snapshot) fromString(entry.getValue()));
    }
    snapshotableStreamEventQueue2.restore(snapshotStateList);
    Assert.assertEquals(snapshotableStreamEventQueue, snapshotableStreamEventQueue2);
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) HashMap(java.util.HashMap) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SnapshotableStreamEventQueue(io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue) Test(org.testng.annotations.Test)

Aggregations

Snapshot (io.siddhi.core.util.snapshot.state.Snapshot)14 Map (java.util.Map)12 HashMap (java.util.HashMap)11 SnapshotStateList (io.siddhi.core.util.snapshot.state.SnapshotStateList)8 Operation (io.siddhi.core.event.stream.Operation)7 ArrayList (java.util.ArrayList)7 TreeMap (java.util.TreeMap)7 StreamEvent (io.siddhi.core.event.stream.StreamEvent)6 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)5 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)5 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)5 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)5 StreamEventClonerHolder (io.siddhi.core.event.stream.holder.StreamEventClonerHolder)5 State (io.siddhi.core.util.snapshot.state.State)5 StateHolder (io.siddhi.core.util.snapshot.state.StateHolder)5 Attribute (io.siddhi.query.api.definition.Attribute)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Test (org.testng.annotations.Test)5 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)3 NoPersistenceStoreException (io.siddhi.core.exception.NoPersistenceStoreException)3