/** * Schedule a new SendCreateVisitors task iff there are still buckets to * visit, the visiting has not failed fatally and we haven't already * scheduled such a task. */ private void scheduleSendCreateVisitorsIfApplicable(long delay, TimeUnit unit) { final long elapsedMillis = elapsedTimeMillis(); if (!isInfiniteTimeout(sessionTimeoutMillis()) && (elapsedMillis >= sessionTimeoutMillis())) { transitionTo(new StateDescription(State.TIMED_OUT, String.format("Session timeout of %d ms expired", sessionTimeoutMillis()))); if (visitingCompleted()) { markSessionCompleted(); } return; } if (!mayScheduleCreateVisitorsTask()) { return; } final long messageTimeoutMillis = computeBoundedMessageTimeoutMillis(elapsedMillis); taskExecutor.scheduleTask(new SendCreateVisitorsTask(messageTimeoutMillis), delay, unit); scheduledSendCreateVisitors = true; }
--pendingMessageCount; if (reply.hasErrors()) { handleErrorReply(reply); } else if (reply instanceof CreateVisitorReply) { handleCreateVisitorReply((CreateVisitorReply)reply); } else { String msg = "Received reply we do not know how to handle: " + reply.getClass().getName(); log.log(LogLevel.ERROR, msg); transitionTo(new StateDescription(State.FAILED, msg)); "' while processing reply in visitor session"; e.printStackTrace(); transitionTo(new StateDescription(State.FAILED, msg)); } catch (Throwable t) { continueVisiting();
@Override public MessageBusVisitorSession createVisitorSession(VisitorParameters params) throws ParseException, IllegalArgumentException { MessageBusVisitorSession session = MessageBusVisitorSession.createForMessageBus( bus.getMessageBus(), scheduledExecutorService, params); session.start(); return session; }
private void handleMessageProcessingException(Reply reply, Exception e, String what) { final String errorDesc = formatProcessingException(e, what); final String fullMsg = formatIdentifyingVisitorErrorString(errorDesc); log.log(LogLevel.ERROR, fullMsg, e); int errorCode; synchronized (progress.getToken()) { if (!params.skipBucketsOnFatalErrors()) { errorCode = ErrorCode.APP_FATAL_ERROR; transitionTo(new StateDescription(State.FAILED, errorDesc)); } else { errorCode = DocumentProtocol.ERROR_UNPARSEABLE; } } reply.addError(new Error(errorCode, errorDesc)); }
public MessageBusVisitorSession(VisitorParameters visitorParameters, AsyncTaskExecutor taskExecutor, SenderFactory senderFactory, ReceiverFactory receiverFactory, RoutingTable routingTable, Clock clock) throws ParseException { this.params = visitorParameters; // TODO(vekterli): make copy? legacy impl does not copy initializeRoute(routingTable); this.sender = senderFactory.createSender(createReplyHandler(), this.params); this.receiver = receiverFactory.createReceiver(createMessageHandler(), sessionName); this.taskExecutor = taskExecutor; this.progress = createVisitingProgress(params); this.statistics = new VisitorStatistics(); this.state = new StateDescription(State.NOT_STARTED); this.clock = clock; initializeHandlers(); trace = new Trace(visitorParameters.getTraceLevel()); dataDestination = (params.getLocalDataHandler() == null ? params.getRemoteDataHandler() : receiver.getConnectionSpec()); validateSessionParameters(); // If we're already done, no need to do anything at all! if (progress.getIterator().isDone()) { markSessionCompleted(); } }
public void start() { synchronized (progress.getToken()) { this.startTimeNanos = clock.monotonicNanoTime(); if (progress.getIterator().isDone()) { log.log(LogLevel.DEBUG, sessionName + ": progress token indicates " + "session is done before it could even start; no-op"); return; } transitionTo(new StateDescription(State.WORKING)); taskExecutor.submitTask(new SendCreateVisitorsTask(computeBoundedMessageTimeoutMillis(0))); } }
"' while attempting to send visitors"; log.log(LogLevel.WARNING, msg); transitionTo(new StateDescription(State.FAILED, msg)); continueVisiting(); } catch (Throwable t) {
private boolean mayScheduleCreateVisitorsTask() { return ! (scheduledSendCreateVisitors || !progress.getIterator().hasNext() || state.failed() || enoughHitsReceived()); }
public static MessageBusVisitorSession createForMessageBus(final MessageBus mbus, final ScheduledExecutorService scheduledExecutorService, final VisitorParameters params) throws ParseException { final AsyncTaskExecutor executor = new ThreadAsyncTaskExecutor(scheduledExecutorService); final MessageBusSenderFactory senderFactory = new MessageBusSenderFactory(mbus); final MessageBusReceiverFactory receiverFactory = new MessageBusReceiverFactory(mbus); final RoutingTable table = mbus.getRoutingTable(DocumentProtocol.NAME); return new MessageBusVisitorSession(params, executor, senderFactory, receiverFactory, table); }
/** * A session is considered completed if one or more of the following holds true: * - All buckets have been visited (i.e. no active or pending visitors). * - Visiting has failed fatally (or has been aborted) AND there are no * active visitors remaining. 'Active' here means that we're waiting * for a reply. * - We have received sufficient number of documents (set via visitor * parameters) from the buckets already visited AND there are no * active visitors remaining. * @return true if visiting has completed, false otherwise */ private boolean visitingCompleted() { return (pendingMessageCount == 0) && (progress.getIterator().isDone() || state.failed() || enoughHitsReceived()); }