public void initPartition() {
        PartitionState state = stateHolder.getState();
        try {
            Long time = state.partitionKeys.get(SiddhiAppContext.getPartitionFlowId());
            if (time == null) {
                synchronized (state) {
                    time = state.partitionKeys.get(SiddhiAppContext.getPartitionFlowId());
                    if (time == null) {
                        for (QueryRuntime queryRuntime : queryRuntimeList) {
                            ((QueryRuntimeImpl) queryRuntime).initPartition();
                        }
                    }
                    state.partitionKeys.put(SiddhiAppContext.getPartitionFlowId(),
                            siddhiAppContext.getTimestampGenerator().currentTime());
                }
            } else {
                state.partitionKeys.put(SiddhiAppContext.getPartitionFlowId(),
                        siddhiAppContext.getTimestampGenerator().currentTime());
            }
        } finally {
            stateHolder.returnState(state);
        }
        if (purgingEnabled) {
            siddhiAppContext.getScheduledExecutorService().scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
                    PartitionState state = stateHolder.getState();
                    try {
                        synchronized (state) {
                            HashMap<String, Long> partitions = new HashMap<>(state.partitionKeys);
                            for (Map.Entry<String, Long> partition : partitions.entrySet()) {
                                if (partition.getValue() + purgeIdlePeriod < currentTime) {
                                    state.partitionKeys.remove(partition.getKey());
                                    SiddhiAppContext.startPartitionFlow(partition.getKey());
                                    try {
                                        for (QueryRuntime queryRuntime : queryRuntimeList) {
                                            Map<String, StateHolder> elementHolderMap =
                                                    siddhiAppContext.getSnapshotService().getStateHolderMap(
                                                            partitionName, queryRuntime.getQueryId());
                                            for (StateHolder stateHolder : elementHolderMap.values()) {
                                                stateHolder.cleanGroupByStates();
                                            }
                                        }
                                    } finally {
                                        SiddhiAppContext.stopPartitionFlow();
                                    }
                                }
                            }
                        }
                    } finally {
                        stateHolder.returnState(state);
                    }
                }
            }, purgeExecutionInterval, purgeExecutionInterval, TimeUnit.MILLISECONDS);
        }
    }
Runtime class to handle partitioning. It will hold all information regarding current partitions and wil create
partition dynamically during runtime.
modules/siddhi-core/src/main/java/io/siddhi/core/partition/PartitionRuntimeImpl.java