use of org.apache.helix.model.Message in project helix by apache.
the class HelixTaskExecutor method cancelTask.
@Override
public boolean cancelTask(MessageTask task) {
Message message = task.getMessage();
NotificationContext notificationContext = task.getNotificationContext();
String taskId = task.getTaskId();
synchronized (_lock) {
if (_taskMap.containsKey(taskId)) {
MessageTaskInfo taskInfo = _taskMap.get(taskId);
// cancel timeout task
if (taskInfo._timerTask != null) {
taskInfo._timerTask.cancel();
}
// cancel task
Future<HelixTaskResult> future = taskInfo.getFuture();
removeMessageFromTaskAndFutureMap(message);
_statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "Canceling task: " + taskId, notificationContext.getManager());
// return if it is interrupted.
if (future.cancel(true)) {
_statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "Canceled task: " + taskId, notificationContext.getManager());
_taskMap.remove(taskId);
return true;
} else {
_statusUpdateUtil.logInfo(message, HelixTaskExecutor.class, "fail to cancel task: " + taskId, notificationContext.getManager());
}
} else {
_statusUpdateUtil.logWarning(message, HelixTaskExecutor.class, "fail to cancel task: " + taskId + ", future not found", notificationContext.getManager());
}
}
return false;
}
use of org.apache.helix.model.Message in project helix by apache.
the class HelixTaskExecutor method syncSessionToController.
private void syncSessionToController(HelixManager manager) {
if (_lastSessionSyncTime == null || System.currentTimeMillis() - _lastSessionSyncTime > SESSION_SYNC_INTERVAL) {
// > delay since last sync
HelixDataAccessor accessor = manager.getHelixDataAccessor();
PropertyKey key = new Builder(manager.getClusterName()).controllerMessage(SESSION_SYNC);
if (accessor.getProperty(key) == null) {
LOG.info(String.format("Participant %s syncs session with controller", manager.getInstanceName()));
Message msg = new Message(MessageType.PARTICIPANT_SESSION_CHANGE, SESSION_SYNC);
msg.setSrcName(manager.getInstanceName());
msg.setTgtSessionId("*");
msg.setMsgState(MessageState.NEW);
msg.setMsgId(SESSION_SYNC);
Criteria cr = new Criteria();
cr.setRecipientInstanceType(InstanceType.CONTROLLER);
cr.setSessionSpecific(false);
manager.getMessagingService().send(cr, msg);
_lastSessionSyncTime = System.currentTimeMillis();
}
}
}
use of org.apache.helix.model.Message in project helix by apache.
the class HelixTaskExecutor method cancelNotStartedStateTransition.
// Try to cancel this state transition that has not been started yet.
// Three Types of Cancellation: 1. Message arrived with previous state transition
// 2. Message handled but task not started
// 3. Message handled and task already started
// This method tries to handle the first two cases, it returns true if no further cancellation is needed,
// false if not been able to cancel the state transition (i.e, further cancellation is needed).
private boolean cancelNotStartedStateTransition(Message message, Map<String, MessageHandler> stateTransitionHandlers, HelixDataAccessor accessor, String instanceName) {
String targetMessageName = getMessageTarget(message.getResourceName(), message.getPartitionName());
ProcessedMessageState messageState;
Message targetStateTransitionMessage;
// State transition message and cancel message are in same batch
if (stateTransitionHandlers.containsKey(targetMessageName)) {
targetStateTransitionMessage = stateTransitionHandlers.get(targetMessageName).getMessage();
if (isCancelingSameStateTransition(targetStateTransitionMessage, message)) {
stateTransitionHandlers.remove(targetMessageName);
messageState = ProcessedMessageState.COMPLETED;
} else {
messageState = ProcessedMessageState.DISCARDED;
}
} else if (_messageTaskMap.containsKey(targetMessageName)) {
// Cancel the from future without interrupt -> Cancel the task future without
// interruptting the state transition that is already started. If the state transition
// is already started, we should call cancel in the state model.
String taskId = _messageTaskMap.get(targetMessageName);
HelixTask task = (HelixTask) _taskMap.get(taskId).getTask();
Future<HelixTaskResult> future = _taskMap.get(taskId).getFuture();
targetStateTransitionMessage = task.getMessage();
if (isCancelingSameStateTransition(task.getMessage(), message)) {
boolean success = task.cancel();
if (!success) {
// the state transition is already started, need further cancellation.
return false;
}
future.cancel(false);
_messageTaskMap.remove(targetMessageName);
_taskMap.remove(taskId);
messageState = ProcessedMessageState.COMPLETED;
} else {
messageState = ProcessedMessageState.DISCARDED;
}
} else {
return false;
}
// remove the original state-transition message been cancelled.
removeMessageFromZK(accessor, targetStateTransitionMessage, instanceName);
_monitor.reportProcessedMessage(targetStateTransitionMessage, ParticipantMessageMonitor.ProcessedMessageState.DISCARDED);
// remove the state transition cancellation message
reportAndRemoveMessage(message, accessor, instanceName, messageState);
return true;
}
use of org.apache.helix.model.Message in project helix by apache.
the class HelixTaskExecutor method updateMessageState.
private void updateMessageState(List<Message> readMsgs, HelixDataAccessor accessor, String instanceName) {
Builder keyBuilder = accessor.keyBuilder();
List<PropertyKey> readMsgKeys = new ArrayList<>();
for (Message msg : readMsgs) {
readMsgKeys.add(msg.getKey(keyBuilder, instanceName));
_knownMessageIds.add(msg.getId());
}
accessor.setChildren(readMsgKeys, readMsgs);
}
use of org.apache.helix.model.Message in project helix by apache.
the class HelixTaskExecutor method finishTask.
@Override
public void finishTask(MessageTask task) {
Message message = task.getMessage();
String taskId = task.getTaskId();
LOG.info("message finished: " + taskId + ", took " + (new Date().getTime() - message.getExecuteStartTimeStamp()));
synchronized (_lock) {
if (_taskMap.containsKey(taskId)) {
MessageTaskInfo info = _taskMap.remove(taskId);
removeMessageFromTaskAndFutureMap(message);
if (info._timerTask != null) {
// ok to cancel multiple times
info._timerTask.cancel();
}
} else {
LOG.warn("message " + taskId + " not found in task map");
}
}
}
Aggregations