Java Code Examples for io.siddhi.core.config.SiddhiAppContext#stopPartitionFlow()

The following examples show how to use io.siddhi.core.config.SiddhiAppContext#stopPartitionFlow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InsertIntoStreamEndPartitionCallback.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public void send(ComplexEventChunk complexEventChunk, int noOfEvents) {
    if (getSiddhiDebugger() != null) {
        getSiddhiDebugger()
                .checkBreakPoint(getQueryName(), SiddhiDebugger.QueryTerminal.OUT, complexEventChunk.getFirst());
    }
    complexEventChunk.reset();
    if (complexEventChunk.getFirst() != null) {
        String flowId = SiddhiAppContext.getPartitionFlowId();
        SiddhiAppContext.stopPartitionFlow();
        try {
            while (complexEventChunk.hasNext()) {
                ComplexEvent complexEvent = complexEventChunk.next();
                if (complexEvent.getType() == ComplexEvent.Type.EXPIRED) {
                    complexEvent.setType(ComplexEvent.Type.CURRENT);
                }
            }
            publisher.send(complexEventChunk.getFirst());
        } finally {
            SiddhiAppContext.startPartitionFlow(flowId);
        }
    }
}
 
Example 2
Source File: InsertIntoWindowEndPartitionCallback.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
 * Add the event into the {@link Window}
 *
 * @param complexEventChunk the event to add
 * @param noOfEvents        number of events
 */
@Override
public void send(ComplexEventChunk complexEventChunk, int noOfEvents) {
    if (getSiddhiDebugger() != null) {
        getSiddhiDebugger()
                .checkBreakPoint(getQueryName(), SiddhiDebugger.QueryTerminal.OUT, complexEventChunk.getFirst());
    }
    // If events are inserted directly from another window, expired events can arrive
    complexEventChunk.reset();
    if (complexEventChunk.getFirst() != null) {
        String flowId = SiddhiAppContext.getCurrentFlowId();
        SiddhiAppContext.stopPartitionFlow();
        try {
            while (complexEventChunk.hasNext()) {
                ComplexEvent complexEvent = complexEventChunk.next();
                if (complexEvent.getType() == ComplexEvent.Type.EXPIRED) {
                    complexEvent.setType(ComplexEvent.Type.CURRENT);
                }
            }
            window.add(complexEventChunk);
        } finally {
            SiddhiAppContext.startPartitionFlow(flowId);
        }
    }
}
 
Example 3
Source File: Scheduler.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
 * Schedule events which are not scheduled in the queue when switching back from event time to system current time
 */
public void switchToLiveMode() {
    Map<String, Map<String, SchedulerState>> allStates = stateHolder.getAllStates();
    try {
        for (Map.Entry<String, Map<String, SchedulerState>> allStatesEntry : allStates.entrySet()) {
            for (Map.Entry<String, SchedulerState> stateEntry : allStatesEntry.getValue().entrySet()) {
                Long toNotifyTime = stateEntry.getValue().toNotifyQueue.peek();
                if (toNotifyTime != null) {
                    SiddhiAppContext.startPartitionFlow(allStatesEntry.getKey());
                    SiddhiAppContext.startGroupByFlow(stateEntry.getKey());
                    try {
                        schedule(toNotifyTime, stateEntry.getValue(), true);
                    } finally {
                        SiddhiAppContext.stopGroupByFlow();
                        SiddhiAppContext.stopPartitionFlow();
                    }
                }
            }
        }
    } finally {
        stateHolder.returnAllStates(allStates);
    }
}
 
Example 4
Source File: PartitionStreamReceiver.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private void send(String key, ComplexEvent event) {
    if (key != null) {
        SiddhiAppContext.startPartitionFlow(key);
        try {
            partitionRuntime.initPartition();
            streamJunctionMap.get(streamId).sendEvent(event);
        } finally {
            SiddhiAppContext.stopPartitionFlow();
        }
    }
}
 
Example 5
Source File: PartitionStreamReceiver.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private void send(ComplexEvent event) {
    for (String key : partitionRuntime.getPartitionKeys()) {
        SiddhiAppContext.startPartitionFlow(key);
        try {
            streamJunctionMap.get(streamId).sendEvent(event);
        } finally {
            SiddhiAppContext.stopPartitionFlow();
        }
    }
}
 
Example 6
Source File: CronWindowProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public void dispatchEvents() {
    Map<String, Map<String, WindowState>> allStates = stateHolder.getAllStates();
    try {
        for (Map.Entry<String, Map<String, WindowState>> allStatesEntry : allStates.entrySet()) {
            for (Map.Entry<String, WindowState> stateEntry : allStatesEntry.getValue().entrySet()) {
                WindowState windowState = stateEntry.getValue();
                ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<StreamEvent>();
                synchronized (windowState) {
                    if (windowState.currentEventQueue.getFirst() != null) {
                        long currentTime = siddhiQueryContext.getSiddhiAppContext().
                                getTimestampGenerator().currentTime();
                        while (windowState.expiredEventQueue.hasNext()) {
                            StreamEvent expiredEvent = windowState.expiredEventQueue.next();
                            expiredEvent.setTimestamp(currentTime);
                        }
                        if (windowState.expiredEventQueue.getFirst() != null) {
                            streamEventChunk.add(windowState.expiredEventQueue.getFirst());
                        }
                        windowState.expiredEventQueue.clear();
                        while (windowState.currentEventQueue.hasNext()) {
                            StreamEvent currentEvent = windowState.currentEventQueue.next();
                            StreamEvent toExpireEvent =
                                    streamEventClonerHolder.getStreamEventCloner().copyStreamEvent(currentEvent);
                            toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                            windowState.expiredEventQueue.add(toExpireEvent);
                        }

                        streamEventChunk.add(windowState.currentEventQueue.getFirst());
                        windowState.currentEventQueue.clear();
                    }
                }
                SiddhiAppContext.startPartitionFlow(allStatesEntry.getKey());
                SiddhiAppContext.startGroupByFlow(stateEntry.getKey());
                try {
                    if (streamEventChunk.getFirst() != null) {
                        nextProcessor.process(streamEventChunk);
                    }
                } finally {
                    SiddhiAppContext.stopGroupByFlow();
                    SiddhiAppContext.stopPartitionFlow();
                }
            }
        }
    } finally {
        stateHolder.returnAllStates(allStates);
    }
}
 
Example 7
Source File: SnapshotService.java    From siddhi with Apache License 2.0 4 votes vote down vote up
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();
    }
}
 
Example 8
Source File: Scheduler.java    From siddhi with Apache License 2.0 4 votes vote down vote up
/**
 * When an object implementing interface <code>Runnable</code> is used
 * to create a thread, starting the thread causes the object's
 * <code>run</code> method to be called in that separately executing
 * thread.
 * <p>
 * The general contract of the method <code>run</code> is that it may
 * take any action whatsoever.
 *
 * @see Thread#run()
 */
@Override
public synchronized void run() {
    if (stop) {
        return;
    }
    SiddhiAppContext.startPartitionFlow(key);
    try {
        if (!siddhiQueryContext.getSiddhiAppContext().isPlayback()) {
            sendTimerEvents(state);

            Long toNotifyTime = state.toNotifyQueue.peek();
            long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();

            if (toNotifyTime != null) {
                state.scheduledFuture = scheduledExecutorService.
                        schedule(this, toNotifyTime - currentTime, TimeUnit.MILLISECONDS);
            } else {
                try {
                    mutex.acquire();
                    state.running = false;
                    if (state.toNotifyQueue.peek() != null) {
                        state.running = true;
                        state.scheduledFuture = scheduledExecutorService.schedule(this,
                                0, TimeUnit.MILLISECONDS);
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    log.error("Error when scheduling System Time Based Scheduler", e);
                } finally {
                    mutex.release();
                }
            }
        } else {
            state.running = false;
        }
    } catch (Throwable t) {
        log.error("Error while executing Scheduled Timer Event Caller, " + t.getMessage(), t);
    } finally {
        SiddhiAppContext.stopPartitionFlow();
    }
}