use of org.apache.helix.participant.statemachine.Transition in project helix by apache.
the class TaskStateModel method onBecomeOnlineFromOffline.
@Transition(to = "ONLINE", from = "OFFLINE")
public void onBecomeOnlineFromOffline(Message message, NotificationContext context) throws Exception {
LOG.debug(_workerId + " becomes ONLINE from OFFLINE for " + _partition);
ConfigAccessor clusterConfig = context.getManager().getConfigAccessor();
HelixManager manager = context.getManager();
HelixConfigScope clusterScope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(manager.getClusterName()).build();
String json = clusterConfig.get(clusterScope, message.getResourceName());
Dag.Node node = Dag.Node.fromJson(json);
Set<String> parentIds = node.getParentIds();
String resourceName = message.getResourceName();
int numPartitions = node.getNumPartitions();
Task task = _taskFactory.createTask(resourceName, parentIds, manager, _taskResultStore);
manager.addExternalViewChangeListener(task);
LOG.debug("Starting task for " + _partition + "...");
int partitionNum = Integer.parseInt(_partition.split("_")[1]);
task.execute(resourceName, numPartitions, partitionNum);
LOG.debug("Task for " + _partition + " done");
}
use of org.apache.helix.participant.statemachine.Transition in project helix by apache.
the class FileStoreStateModel method onBecomeMasterFromSlave.
/**
* When the node becomes master, it will start accepting writes and increments
* the epoch and starts logging the changes in a file
* @param message
* @param context
* @throws Exception
*/
@Transition(from = "SLAVE", to = "MASTER")
public void onBecomeMasterFromSlave(final Message message, NotificationContext context) throws Exception {
replicator.stop();
System.out.println(_serverId + " transitioning from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
ZkHelixPropertyStore<ZNRecord> helixPropertyStore = context.getManager().getHelixPropertyStore();
String checkpointDirPath = instanceConfig.getRecord().getSimpleField("check_point_dir");
CheckpointFile checkpointFile = new CheckpointFile(checkpointDirPath);
final ChangeRecord lastRecordProcessed = checkpointFile.findLastRecordProcessed();
DataUpdater<ZNRecord> updater = new HighWaterMarkUpdater(message, lastRecordProcessed);
helixPropertyStore.update("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), updater, AccessOption.PERSISTENT);
Stat stat = new Stat();
;
ZNRecord znRecord = helixPropertyStore.get("/TRANSACTION_ID_METADATA" + "/" + message.getResourceName(), stat, AccessOption.PERSISTENT);
int startGen = Integer.parseInt(znRecord.getSimpleField("currentGen"));
int startSeq = Integer.parseInt(znRecord.getSimpleField("currentGenStartSeq"));
String fileStoreDir = instanceConfig.getRecord().getSimpleField("file_store_dir");
String changeLogDir = instanceConfig.getRecord().getSimpleField("change_log_dir");
generator = new ChangeLogGenerator(changeLogDir, startGen, startSeq);
// To indicate that we need callbacks for changes that happen starting now
long now = System.currentTimeMillis();
service = new FileSystemWatchService(fileStoreDir, now, generator);
service.start();
System.out.println(_serverId + " transitioned from " + message.getFromState() + " to " + message.getToState() + " for " + _partition);
}
use of org.apache.helix.participant.statemachine.Transition in project helix by apache.
the class GenericLeaderStandbyModel method onBecomeLeaderFromStandby.
@Transition(to = "LEADER", from = "STANDBY")
public void onBecomeLeaderFromStandby(Message message, NotificationContext context) throws Exception {
LOG.info("Become LEADER from STANDBY");
HelixManager manager = context.getManager();
if (manager == null) {
throw new IllegalArgumentException("Require HelixManager in notification conext");
}
for (ChangeType notificationType : _notificationTypes) {
if (notificationType == ChangeType.LIVE_INSTANCE) {
manager.addLiveInstanceChangeListener(_particHolder);
} else if (notificationType == ChangeType.CONFIG) {
manager.addConfigChangeListener(_particHolder);
} else if (notificationType == ChangeType.EXTERNAL_VIEW) {
manager.addExternalViewChangeListener(_particHolder);
} else {
LOG.error("Unsupport notificationType:" + notificationType.toString());
}
}
}
use of org.apache.helix.participant.statemachine.Transition in project helix by apache.
the class GenericLeaderStandbyModel method onBecomeStandbyFromLeader.
@Transition(to = "STANDBY", from = "LEADER")
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
LOG.info("Become STANDBY from LEADER");
HelixManager manager = context.getManager();
if (manager == null) {
throw new IllegalArgumentException("Require HelixManager in notification conext");
}
Builder keyBuilder = new Builder(manager.getClusterName());
for (ChangeType notificationType : _notificationTypes) {
if (notificationType == ChangeType.LIVE_INSTANCE) {
manager.removeListener(keyBuilder.liveInstances(), _particHolder);
} else if (notificationType == ChangeType.CONFIG) {
manager.removeListener(keyBuilder.instanceConfigs(), _particHolder);
} else if (notificationType == ChangeType.EXTERNAL_VIEW) {
manager.removeListener(keyBuilder.externalViews(), _particHolder);
} else {
LOG.error("Unsupport notificationType:" + notificationType.toString());
}
}
}
use of org.apache.helix.participant.statemachine.Transition in project helix by apache.
the class AgentStateModel method genericStateTransitionHandler.
@Transition(to = "*", from = "*")
public void genericStateTransitionHandler(Message message, NotificationContext context) throws Exception {
// first try get command from message
String cmd = message.getRecord().getSimpleField(CommandAttribute.COMMAND.getName());
String workingDir = message.getRecord().getSimpleField(CommandAttribute.WORKING_DIR.getName());
String timeout = message.getRecord().getSimpleField(CommandAttribute.TIMEOUT.getName());
String pidFile = message.getRecord().getSimpleField(CommandAttribute.PID_FILE.getName());
HelixManager manager = context.getManager();
String clusterName = manager.getClusterName();
String fromState = message.getFromState();
String toState = message.getToState();
// construct keys for command-config
String cmdKey = buildKey(fromState, toState, CommandAttribute.COMMAND);
String workingDirKey = buildKey(fromState, toState, CommandAttribute.WORKING_DIR);
String timeoutKey = buildKey(fromState, toState, CommandAttribute.TIMEOUT);
String pidFileKey = buildKey(fromState, toState, CommandAttribute.PID_FILE);
List<String> cmdConfigKeys = Arrays.asList(cmdKey, workingDirKey, timeoutKey, pidFileKey);
// read command from resource-scope configures
if (cmd == null) {
HelixConfigScope resourceScope = new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName).forResource(message.getResourceName()).build();
Map<String, String> cmdKeyValueMap = manager.getConfigAccessor().get(resourceScope, cmdConfigKeys);
if (cmdKeyValueMap != null) {
cmd = cmdKeyValueMap.get(cmdKey);
workingDir = cmdKeyValueMap.get(workingDirKey);
timeout = cmdKeyValueMap.get(timeoutKey);
pidFile = cmdKeyValueMap.get(pidFileKey);
}
}
// if resource-scope doesn't contain command, fall back to cluster-scope configures
if (cmd == null) {
HelixConfigScope clusterScope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
Map<String, String> cmdKeyValueMap = manager.getConfigAccessor().get(clusterScope, cmdConfigKeys);
if (cmdKeyValueMap != null) {
cmd = cmdKeyValueMap.get(cmdKey);
workingDir = cmdKeyValueMap.get(workingDirKey);
timeout = cmdKeyValueMap.get(timeoutKey);
pidFile = cmdKeyValueMap.get(pidFileKey);
}
}
if (cmd == null) {
throw new Exception("Unable to find command for transition from:" + message.getFromState() + " to:" + message.getToState());
}
_logger.info("Executing command: " + cmd + ", using workingDir: " + workingDir + ", timeout: " + timeout + ", on " + manager.getInstanceName());
// skip nop command
if (cmd.equals(CommandAttribute.NOP.getName())) {
return;
}
// split the cmd to actual cmd and args[]
String[] cmdSplits = cmd.trim().split("\\s+");
String cmdValue = cmdSplits[0];
String[] args = Arrays.copyOfRange(cmdSplits, 1, cmdSplits.length);
// get the command-execution timeout
// 0 means wait for ever
long timeoutValue = 0;
if (timeout != null) {
try {
timeoutValue = Long.parseLong(timeout);
} catch (NumberFormatException e) {
// OK to use 0
}
}
ExternalCommand externalCmd = ExternalCommand.executeWithTimeout(new File(workingDir), cmdValue, timeoutValue, args);
int exitValue = externalCmd.exitValue();
if (_logger.isDebugEnabled()) {
_logger.debug("command: " + cmd + ", exitValue: " + exitValue + " output:\n" + externalCmd.getStringOutput());
}
// monitor pid if pidFile exists
if (pidFile == null) {
// no pid to monitor
return;
}
String pidFileValue = instantiateByMessage(pidFile, message);
String pid = SystemUtil.getPidFromFile(new File(pidFileValue));
if (pid != null) {
new ProcessMonitorThread(pid).start();
}
}
Aggregations