org.apache.helix.participant.statemachine.Transition Java Examples

The following examples show how to use org.apache.helix.participant.statemachine.Transition. 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: SegmentOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Transition(from = "OFFLINE", to = "DROPPED")
public void onBecomeDroppedFromOffline(Message message, NotificationContext context) {
  _logger.info("SegmentOnlineOfflineStateModel.onBecomeDroppedFromOffline() : " + message);
  String tableNameWithType = message.getResourceName();
  String segmentName = message.getPartitionName();

  // This method might modify the file on disk. Use segment lock to prevent race condition
  Lock segmentLock = SegmentLocks.getSegmentLock(tableNameWithType, segmentName);
  try {
    segmentLock.lock();

    final File segmentDir = new File(_fetcherAndLoader.getSegmentLocalDirectory(tableNameWithType, segmentName));
    if (segmentDir.exists()) {
      FileUtils.deleteQuietly(segmentDir);
      _logger.info("Deleted segment directory {}", segmentDir);
    }
  } catch (final Exception e) {
    _logger.error("Cannot delete the segment : " + segmentName + " from local directory!\n" + e.getMessage(), e);
    Utils.rethrowException(e);
  } finally {
    segmentLock.unlock();
  }
}
 
Example #2
Source File: TaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #3
Source File: TaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "DROPPED", from = "RUNNING")
public void onBecomeDroppedFromRunning(Message msg, NotificationContext context) {
  String taskPartition = msg.getPartitionName();
  if (_taskRunner == null) {
    if (timeout_task != null) {
      timeout_task.cancel(true);
    }
    LOG.error(
        "Participant {}'s thread for task partition {} not found while attempting to cancel the task; Manual cleanup may be required.",
        _manager.getInstanceName(), taskPartition);
    return;
  }

  _taskRunner.cancel();
  TaskResult r = _taskRunner.waitTillDone();
  LOG.info(String.format("Task partition %s returned result %s.", msg.getPartitionName(), r));
  _taskRunner = null;
  timeout_task.cancel(false);
}
 
Example #4
Source File: TaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "TASK_ERROR", from = "RUNNING")
public String onBecomeTaskErrorFromRunning(Message msg, NotificationContext context) {
  String taskPartition = msg.getPartitionName();
  if (_taskRunner == null) {
    throw new IllegalStateException(String.format(
        "Invalid state transition. There is no running task for partition %s.", taskPartition));
  }

  TaskResult r = _taskRunner.waitTillDone();
  if (r.getStatus() != TaskResult.Status.ERROR && r.getStatus() != TaskResult.Status.FAILED) {
    throw new IllegalStateException(String.format(
        "Partition %s received a state transition to %s but the result status code is %s.",
        msg.getPartitionName(), msg.getToState(), r.getStatus()));
  }

  timeout_task.cancel(false);

  return r.getInfo();
}
 
Example #5
Source File: SegmentOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Transition(from = "OFFLINE", to = "ONLINE")
public void onBecomeOnlineFromOffline(Message message, NotificationContext context) {
  _logger.info("SegmentOnlineOfflineStateModel.onBecomeOnlineFromOffline() : " + message);
  String tableNameWithType = message.getResourceName();
  String segmentName = message.getPartitionName();
  try {
    TableType tableType = TableNameBuilder.getTableTypeFromTableName(message.getResourceName());
    Preconditions.checkNotNull(tableType);
    if (tableType == TableType.OFFLINE) {
      _fetcherAndLoader.addOrReplaceOfflineSegment(tableNameWithType, segmentName);
    } else {
      _instanceDataManager.addRealtimeSegment(tableNameWithType, segmentName);
    }
  } catch (Exception e) {
    _logger.error("Caught exception in state transition from OFFLINE -> ONLINE for resource: {}, partition: {}",
        tableNameWithType, segmentName, e);
    Utils.rethrowException(e);
  }
}
 
Example #6
Source File: TaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "TASK_ABORTED", from = "RUNNING")
public String onBecomeTaskAbortedFromRunning(Message msg, NotificationContext context) {
  String taskPartition = msg.getPartitionName();
  if (_taskRunner == null) {
    throw new IllegalStateException(String.format(
        "Invalid state transition. There is no running task for partition %s.", taskPartition));
  }

  _taskRunner.cancel();
  TaskResult r = _taskRunner.waitTillDone();
  if (r.getStatus() != TaskResult.Status.FATAL_FAILED && r.getStatus() != TaskResult.Status.CANCELED) {
    throw new IllegalStateException(String.format(
        "Partition %s received a state transition to %s but the result status code is %s.",
        msg.getPartitionName(), msg.getToState(), r.getStatus()));
  }

  timeout_task.cancel(false);

  return r.getInfo();
}
 
Example #7
Source File: OnlineOfflineStateModelFactory.java    From terrapin with Apache License 2.0 6 votes vote down vote up
@Transition(from = "OFFLINE", to = "ONLINE")
public void onBecomeOnlineFromOffline(Message message,
                                      NotificationContext context) {
  Pair<String, String> hdfsPathAndPartition = getHdfsPathAndPartitionNum(message);
  String hdfsPath = hdfsPathAndPartition.getLeft();
  LOG.info("Opening " + hdfsPath);
  try {
    // TODO(varun): Maybe retry here.
    HColumnDescriptor family = new HColumnDescriptor(Constants.HFILE_COLUMN_FAMILY);
    family.setBlockCacheEnabled(isBlockCacheEnabled);
    Reader r = readerFactory.createHFileReader(hdfsPath, new CacheConfig(conf, family));
    resourcePartitionMap.addReader(
        message.getResourceName(), hdfsPathAndPartition.getRight(), r);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #8
Source File: TestStateTransitionTimeout.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(to = "MASTER", from = "SLAVE")
public void onBecomeMasterFromSlave(Message message, NotificationContext context)
    throws InterruptedException {
  LOG.info("Become MASTER from SLAVE");
  if (_transition != null && _sleep) {
    _transition.doTransition(message, context);
  }
}
 
Example #9
Source File: BrokerResourceOnlineOfflineStateModelFactory.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(
    from = "OFFLINE",
    to = "ONLINE"
)
public void onBecomeOnlineFromOffline(Message message, NotificationContext context) {
    LOGGER.info("Become Online from Offline");
}
 
Example #10
Source File: TestStateTransitionTimeoutWithResource.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(to = "MASTER", from = "SLAVE")
public void onBecomeMasterFromSlave(Message message, NotificationContext context)
    throws InterruptedException {
  LOG.info("Become MASTER from SLAVE");
  if (_transition != null && _sleep) {
    _transition.doTransition(message, context);
  }
}
 
Example #11
Source File: SegmentOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Transition(from = "ONLINE", to = "OFFLINE")
public void onBecomeOfflineFromOnline(Message message, NotificationContext context) {
  _logger.info("SegmentOnlineOfflineStateModel.onBecomeOfflineFromOnline() : " + message);
  String tableNameWithType = message.getResourceName();
  String segmentName = message.getPartitionName();
  try {
    _instanceDataManager.removeSegment(tableNameWithType, segmentName);
  } catch (Exception e) {
    _logger.error("Caught exception in state transition from ONLINE -> OFFLINE for resource: {}, partition: {}",
        tableNameWithType, segmentName, e);
    Utils.rethrowException(e);
  }
}
 
Example #12
Source File: FileStoreStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Stop writing
 * @param message
 * @param context
 * @throws Exception
 */

@Transition(from = "MASTER", to = "SLAVE")
public void onBecomeSlaveFromMaster(Message message, NotificationContext context)
    throws Exception {
  service.stop();
  LOG.info(_serverId + " transitioning from " + message.getFromState() + " to "
      + message.getToState() + " for " + _partition);
  replicator.start();
}
 
Example #13
Source File: TaskStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(to = "INIT", from = "RUNNING")
public void onBecomeInitFromRunning(Message msg, NotificationContext context) {
  String taskPartition = msg.getPartitionName();
  if (_taskRunner == null) {
    throw new IllegalStateException(String
        .format("Invalid state transition. There is no running task for partition %s.", taskPartition));
  }

  _taskRunner.cancel();
  TaskResult r = _taskRunner.waitTillDone();
  LOG.info(String.format("Task partition %s returned result %s.", msg.getPartitionName(), r));
  _taskRunner = null;
}
 
Example #14
Source File: BrokerResourceOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Transition(from = "ONLINE", to = "OFFLINE")
public void onBecomeOfflineFromOnline(Message message, NotificationContext context) {
  String tableNameWithType = message.getPartitionName();
  LOGGER.info("Processing transition from ONLINE to OFFLINE for table: {}", tableNameWithType);
  try {
    _routingManager.removeRouting(tableNameWithType);
    _queryQuotaManager.dropTableQueryQuota(tableNameWithType);
  } catch (Exception e) {
    LOGGER.error("Caught exception while processing transition from ONLINE to OFFLINE for table: {}",
        tableNameWithType, e);
    throw e;
  }
}
 
Example #15
Source File: BootstrapOnlineOfflineStateModel.java    From Pistachio with Apache License 2.0 5 votes vote down vote up
@Transition(to = "MASTER", from = "SLAVE")
public void onBecomeMasterFromSlave(Message message, NotificationContext context) {
    logger.info("becomes MASTER from SLAVE for {}", partitionId);
    BootstrapPartitionHandler originHandler = handler.get();
    if (originHandler != null) {
        originHandler.selfBootstraping();
    }
}
 
Example #16
Source File: FileStoreStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * If the node is slave, start the rsync thread if it is not started
 * @param message
 * @param context
 * @throws Exception
 */

@Transition(from = "OFFLINE", to = "SLAVE")
public void onBecomeSlaveFromOffline(Message message, NotificationContext context)
    throws Exception {
  System.out.println(_serverId + " transitioning from " + message.getFromState() + " to "
      + message.getToState() + " for " + _partition);

  replicator.start();
  System.out.println(_serverId + " transitioned from " + message.getFromState() + " to "
      + message.getToState() + " for " + _partition);
}
 
Example #17
Source File: SegmentOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Transition(from = "CONSUMING", to = "ONLINE")
public void onBecomeOnlineFromConsuming(Message message, NotificationContext context) {
  String realtimeTableName = message.getResourceName();
  String segmentNameStr = message.getPartitionName();
  LLCSegmentName segmentName = new LLCSegmentName(segmentNameStr);

  TableDataManager tableDataManager = _instanceDataManager.getTableDataManager(realtimeTableName);
  Preconditions.checkNotNull(tableDataManager);
  SegmentDataManager acquiredSegment = tableDataManager.acquireSegment(segmentNameStr);
  // For this transition to be correct in helix, we should already have a segment that is consuming
  if (acquiredSegment == null) {
    throw new RuntimeException("Segment " + segmentNameStr + " + not present ");
  }

  try {
    if (!(acquiredSegment instanceof LLRealtimeSegmentDataManager)) {
      // We found a LLC segment that is not consuming right now, must be that we already swapped it with a
      // segment that has been built. Nothing to do for this state transition.
      _logger
          .info("Segment {} not an instance of LLRealtimeSegmentDataManager. Reporting success for the transition",
              acquiredSegment.getSegmentName());
      return;
    }
    LLRealtimeSegmentDataManager segmentDataManager = (LLRealtimeSegmentDataManager) acquiredSegment;
    RealtimeSegmentZKMetadata metadata = ZKMetadataProvider
        .getRealtimeSegmentZKMetadata(_instanceDataManager.getPropertyStore(), segmentName.getTableName(),
            segmentNameStr);
    segmentDataManager.goOnlineFromConsuming(metadata);
  } catch (InterruptedException e) {
    _logger.warn("State transition interrupted", e);
    throw new RuntimeException(e);
  } finally {
    tableDataManager.releaseSegment(acquiredSegment);
  }
}
 
Example #18
Source File: OnlineOfflineStateModelFactory.java    From terrapin with Apache License 2.0 5 votes vote down vote up
@Transition(from = "ONLINE", to = "OFFLINE")
public void onBecomeOfflineFromOnline(Message message,
                                      NotificationContext context) {
  Pair<String, String> hdfsPathAndPartition = getHdfsPathAndPartitionNum(message);
  String hdfsPath = hdfsPathAndPartition.getLeft();
  LOG.info("Closing " + hdfsPath);
  try {
    Reader r = resourcePartitionMap.removeReader(message.getResourceName(),
        hdfsPathAndPartition.getRight());
    r.close();
  } catch (Exception e) {
    LOG.warn("Could not close reader for " + hdfsPath, e);
  }
}
 
Example #19
Source File: MockDelayMSStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(to = "SLAVE", from = "OFFLINE")
public void onBecomeSlaveFromOffline(Message message, NotificationContext context) {
  if (_delay > 0) {
    try {
      Thread.sleep(_delay);
    } catch (InterruptedException e) {
      LOG.error("Failed to sleep for " + _delay);
    }
  }
  LOG.info("Become SLAVE from OFFLINE");
}
 
Example #20
Source File: BrokerResourceOnlineOfflineStateModelFactory.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(
    from = "ONLINE",
    to = "OFFLINE"
)
public void onBecomeOfflineFromOnline(Message message, NotificationContext context) {
  LOGGER.info("Become Offline from Online");

}
 
Example #21
Source File: FileStoreStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: ConsumerStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
@Transition(to = "ONLINE", from = "OFFLINE")
public void onBecomeOnlineFromOffline(Message message, NotificationContext context) {
  LOG.debug(_consumerId + " becomes ONLINE from OFFLINE for " + _partition);

  if (_thread == null) {
    LOG.debug("Starting ConsumerThread for " + _partition + "...");
    _thread = new ConsumerThread(_partition, _mqServer, _consumerId);
    _thread.start();
    LOG.debug("Starting ConsumerThread for " + _partition + " done");

  }
}
 
Example #23
Source File: SegmentCompletionIntegrationTest.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Transition(from = "OFFLINE", to = "DROPPED")
public void onBecomeDroppedFromOffline(Message message, NotificationContext context) {
}
 
Example #24
Source File: TaskStateModel.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "DROPPED", from = "OFFLINE")
public void onBecomeDroppedFromOffline(Message message, NotificationContext context) {
  LOG.debug(_workerId + " becomes DROPPED from OFFLINE for " + _partition);
}
 
Example #25
Source File: SegmentOnlineOfflineStateModelFactory.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Transition(from = "ERROR", to = "OFFLINE")
public void onBecomeOfflineFromError(Message message, NotificationContext context) {
  _logger.info("Resetting the state for segment:{} from ERROR to OFFLINE", message.getPartitionName());
}
 
Example #26
Source File: TaskStateModel.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "RUNNING", from = "STOPPED")
public void onBecomeRunningFromStopped(Message msg, NotificationContext context) {
  startTask(msg, msg.getPartitionName());
}
 
Example #27
Source File: ControllerTest.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Transition(from = "CONSUMING", to = "ONLINE")
public void onBecomeOnlineFromConsuming(Message message, NotificationContext context) {
  LOGGER.debug("onBecomeOnlineFromConsuming(): {}", message);
}
 
Example #28
Source File: BootstrapHandler.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(from = "SLAVE", to = "OFFLINE")
public void slaveToOffline(Message message, NotificationContext context) {
  System.out.println("BootstrapProcess.BootstrapStateModel.slaveToOffline()");
}
 
Example #29
Source File: AbstractHelixLeaderStandbyStateModel.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "DROPPED", from = "OFFLINE")
public abstract void onBecomeDroppedFromOffline(Message message, NotificationContext context);
 
Example #30
Source File: AbstractHelixLeaderStandbyStateModel.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "LEADER", from = "STANDBY")
public abstract void onBecomeLeaderFromStandby(Message message, NotificationContext context)
    throws Exception;