public Map<String, Object> queryState(String queryName) {
        Map<String, Object> queryState = new HashMap<>();
        try {
            // Lock the threads in Siddhi
            threadBarrier.lock();
            waitForSystemStabilization();
            PartitionIdStateHolder partitionIdStateHolder = partitionIdStates.get("");
            if (partitionIdStateHolder != null) {
                ElementStateHolder elementStateHolder = partitionIdStateHolder.queryStateHolderMap.get(queryName);
                if (elementStateHolder != null) {
                    for (Map.Entry<String, StateHolder> elementState : elementStateHolder.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 id = partitionKeyState.getKey() + "--" + groupByKeyState.getKey() + "_"
                                            + queryName + "_" + elementState.getKey();
                                    queryState.put(id, groupByKeyState.getValue().snapshot());
                                }
                            }
                        } finally {
                            elementState.getValue().returnAllStates(partitionKeyStates);
                        }
                    }
                }
            }
        } finally {
            threadBarrier.unlock();
        }
        if (log.isDebugEnabled()) {
            log.debug("Taking snapshot finished.");
        }
        return queryState;
    }
Service level implementation to take/restore snapshots of processing elements.
Memory   : PartitionId + QueryName + ElementId + PartitionGroupByKey + Item
Snapshot : PartitionId + PartitionGroupByKey + QueryName + ElementId + Item
modules/siddhi-core/src/main/java/io/siddhi/core/util/snapshot/SnapshotService.java