Java Code Examples for org.apache.helix.ConfigAccessor#get()

The following examples show how to use org.apache.helix.ConfigAccessor#get() . 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: 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 2
Source File: HelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
/** Dedicated Thread pool can be provided in configuration or by client.
 *  This method is to check it and update the thread pool if necessary.
 */
private void updateStateTransitionMessageThreadPool(Message message, HelixManager manager) {
  if (!message.getMsgType().equals(MessageType.STATE_TRANSITION.name())) {
    return;
  }

  String resourceName = message.getResourceName();
  String factoryName = message.getStateModelFactoryName();
  String stateModelName = message.getStateModelDef();

  if (factoryName == null) {
    factoryName = HelixConstants.DEFAULT_STATE_MODEL_FACTORY;
  }
  StateModelFactory<? extends StateModel> stateModelFactory =
      manager.getStateMachineEngine().getStateModelFactory(stateModelName, factoryName);

  String perStateTransitionTypeKey =
      getStateTransitionType(getPerResourceStateTransitionPoolName(resourceName),
          message.getFromState(), message.getToState());
  if (perStateTransitionTypeKey != null && stateModelFactory != null
      && !_transitionTypeThreadpoolChecked.contains(perStateTransitionTypeKey)) {
    ExecutorService perStateTransitionTypeExecutor = stateModelFactory
        .getExecutorService(resourceName, message.getFromState(), message.getToState());
    _transitionTypeThreadpoolChecked.add(perStateTransitionTypeKey);

    if (perStateTransitionTypeExecutor != null) {
      _executorMap.put(perStateTransitionTypeKey, perStateTransitionTypeExecutor);
      LOG.info(String
          .format("Added client specified dedicate threadpool for resource %s from %s to %s",
              getPerResourceStateTransitionPoolName(resourceName), message.getFromState(),
              message.getToState()));
      return;
    }
  }

  if (!_resourcesThreadpoolChecked.contains(resourceName)) {
    int threadpoolSize = -1;
    ConfigAccessor configAccessor = manager.getConfigAccessor();
    // Changes to this configuration on thread pool size will only take effect after the participant get restarted.
    if (configAccessor != null) {
      HelixConfigScope scope =
          new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE)
              .forCluster(manager.getClusterName()).forResource(resourceName).build();

      String threadpoolSizeStr = configAccessor.get(scope, MAX_THREADS);
      try {
        if (threadpoolSizeStr != null) {
          threadpoolSize = Integer.parseInt(threadpoolSizeStr);
        }
      } catch (Exception e) {
        LOG.error(
            "Failed to parse ThreadPoolSize from resourceConfig for resource" + resourceName, e);
      }
    }
    final String key = getPerResourceStateTransitionPoolName(resourceName);
    if (threadpoolSize > 0) {
      _executorMap.put(key, Executors.newFixedThreadPool(threadpoolSize, new ThreadFactory() {
        @Override public Thread newThread(Runnable r) {
          return new Thread(r, "GerenricHelixController-message_handle_" + key);
        }
      }));
      LOG.info("Added dedicate threadpool for resource: " + resourceName + " with size: "
          + threadpoolSize);
    } else {
      // if threadpool is not configured
      // check whether client specifies customized threadpool.
      if (stateModelFactory != null) {
        ExecutorService executor = stateModelFactory.getExecutorService(resourceName);
        if (executor != null) {
          _executorMap.put(key, executor);
          LOG.info("Added client specified dedicate threadpool for resource: " + key);
        }
      } else {
        LOG.error(String.format(
            "Fail to get dedicate threadpool defined in stateModelFactory %s: using factoryName: %s for resource %s. No stateModelFactory was found!",
            stateModelName, factoryName, resourceName));
      }
    }
    _resourcesThreadpoolChecked.add(resourceName);
  }
}
 
Example 3
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 4 votes vote down vote up
void registerMessageHandlerFactoryInternal(String type, MessageHandlerFactory factory) {
  _logger.info("registering msg factory for type " + type);
  int threadpoolSize = HelixTaskExecutor.DEFAULT_PARALLEL_TASKS;
  String threadpoolSizeStr = null;
  String key = type + "." + HelixTaskExecutor.MAX_THREADS;

  ConfigAccessor configAccessor = _manager.getConfigAccessor();
  if (configAccessor != null) {
    ConfigScope scope = null;

    // Read the participant config and cluster config for the per-message type thread pool size.
    // participant config will override the cluster config.

    if (_manager.getInstanceType() == InstanceType.PARTICIPANT
        || _manager.getInstanceType() == InstanceType.CONTROLLER_PARTICIPANT) {
      scope =
          new ConfigScopeBuilder().forCluster(_manager.getClusterName())
              .forParticipant(_manager.getInstanceName()).build();
      threadpoolSizeStr = configAccessor.get(scope, key);
    }

    if (threadpoolSizeStr == null) {
      scope = new ConfigScopeBuilder().forCluster(_manager.getClusterName()).build();
      threadpoolSizeStr = configAccessor.get(scope, key);
    }
  }

  if (threadpoolSizeStr != null) {
    try {
      threadpoolSize = Integer.parseInt(threadpoolSizeStr);
      if (threadpoolSize <= 0) {
        threadpoolSize = 1;
      }
    } catch (Exception e) {
      _logger.error("", e);
    }
  }

  _taskExecutor.registerMessageHandlerFactory(type, factory, threadpoolSize);
  // Self-send a no-op message, so that the onMessage() call will be invoked
  // again, and
  // we have a chance to process the message that we received with the new
  // added MessageHandlerFactory
  // before the factory is added.
  sendNopMessageInternal();
}