org.apache.helix.HelixException Java Examples

The following examples show how to use org.apache.helix.HelixException. 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: ScheduledTaskStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Transition(to = "COMPLETED", from = "OFFLINE")
public void onBecomeCompletedFromOffline(Message message, NotificationContext context)
    throws InterruptedException {
  logger.info(_partitionKey + " onBecomeCompletedFromOffline");

  // Construct the inner task message from the mapfields of scheduledTaskQueue resource group
  Map<String, String> messageInfo =
      message.getRecord().getMapField(Message.Attributes.INNER_MESSAGE.toString());
  ZNRecord record = new ZNRecord(_partitionKey);
  record.getSimpleFields().putAll(messageInfo);
  Message taskMessage = new Message(record);
  if (logger.isDebugEnabled()) {
    logger.debug(taskMessage.getRecord().getSimpleFields().toString());
  }
  MessageHandler handler =
      _executor.createMessageHandler(taskMessage, new NotificationContext(null));
  if (handler == null) {
    throw new HelixException("Task message " + taskMessage.getMsgType()
        + " handler not found, task id " + _partitionKey);
  }
  // Invoke the internal handler to complete the task
  handler.handleMessage();
  logger.info(_partitionKey + " onBecomeCompletedFromOffline completed");
}
 
Example #2
Source File: GenericZkHelixApiBuilder.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the given Builder parameters using a generic validation logic.
 */
protected void validate() {
  // Resolve RealmMode based on whether ZK address has been set
  boolean isZkAddressSet = _zkAddress != null && !_zkAddress.isEmpty();
  if (_realmMode == RealmAwareZkClient.RealmMode.SINGLE_REALM && !isZkAddressSet) {
    throw new HelixException("RealmMode cannot be single-realm without a valid ZkAddress set!");
  }
  if (_realmMode == RealmAwareZkClient.RealmMode.MULTI_REALM && isZkAddressSet) {
    throw new HelixException("ZkAddress cannot be set on multi-realm mode!");
  }
  if (_realmMode == null) {
    _realmMode = isZkAddressSet ? RealmAwareZkClient.RealmMode.SINGLE_REALM
        : RealmAwareZkClient.RealmMode.MULTI_REALM;
  }

  initializeConfigsIfNull();
}
 
Example #3
Source File: ClusterSetup.java    From helix with Apache License 2.0 6 votes vote down vote up
public void addCluster(String clusterName, boolean overwritePrevious, CloudConfig cloudConfig)
    throws HelixException {
  _admin.addCluster(clusterName, overwritePrevious);
  for (BuiltInStateModelDefinitions def : BuiltInStateModelDefinitions.values()) {
    addStateModelDef(clusterName, def.getStateModelDefinition().getId(),
        def.getStateModelDefinition(), overwritePrevious);
  }

  if (cloudConfig != null) {
    _admin.addCloudConfig(clusterName, cloudConfig);
    // If cloud is enabled and Cloud Provider is Azure, populated the Topology information in cluster config
    if (cloudConfig.isCloudEnabled()
        && cloudConfig.getCloudProvider().equals(CloudProvider.AZURE.name())) {
      ConfigAccessor configAccessor = new ConfigAccessor(_zkClient);
      ClusterConfig clusterConfig = new ClusterConfig(clusterName);
      clusterConfig.setTopology(AzureConstants.AZURE_TOPOLOGY);
      clusterConfig.setTopologyAwareEnabled(true);
      clusterConfig.setFaultZoneType(AzureConstants.AZURE_FAULT_ZONE_TYPE);
      configAccessor.updateClusterConfig(clusterName, clusterConfig);
    }
  }
}
 
Example #4
Source File: GenericZkHelixApiBuilder.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a RealmAwareZkClient based on the parameters set.
 * To be used in Helix ZK APIs' constructors: ConfigAccessor, ClusterSetup, ZKHelixAdmin
 * @return
 */
protected RealmAwareZkClient createZkClient(RealmAwareZkClient.RealmMode realmMode,
    RealmAwareZkClient.RealmAwareZkConnectionConfig connectionConfig,
    RealmAwareZkClient.RealmAwareZkClientConfig clientConfig, String zkAddress) {
  switch (realmMode) {
    case MULTI_REALM:
      try {
        return new FederatedZkClient(connectionConfig,
            clientConfig.setZkSerializer(new ZNRecordSerializer()));
      } catch (IOException | InvalidRoutingDataException | IllegalStateException e) {
        throw new HelixException("Failed to create FederatedZkClient!", e);
      }
    case SINGLE_REALM:
      // Create a HelixZkClient: Use a SharedZkClient because ClusterSetup does not need to do
      // ephemeral operations
      return SharedZkClientFactory.getInstance()
          .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress),
              clientConfig.createHelixZkClientConfig().setZkSerializer(new ZNRecordSerializer()));
    default:
      throw new HelixException("Invalid RealmMode given: " + realmMode);
  }
}
 
Example #5
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void addInstance(String clusterName, InstanceConfig instanceConfig) {
  logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName);
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("cluster " + clusterName + " is not setup yet");
  }
  String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName);
  String nodeId = instanceConfig.getId();
  String instanceConfigPath = instanceConfigsPath + "/" + nodeId;

  if (_zkClient.exists(instanceConfigPath)) {
    throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName);
  }

  ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord());

  _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true);
  _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true);
  _zkClient.createPersistent(PropertyPathBuilder.instanceCustomizedState(clusterName, nodeId), true);
  _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true);
  _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true);
  _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true);
}
 
Example #6
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void enableInstance(String clusterName, List<String> instances, boolean enabled) {
  // TODO: Reenable this after storage node bug fixed.
  if (true) {
    throw new HelixException("Current batch enable/disable instances are temporarily disabled!");
  }
  logger.info("Batch {} instances {} in cluster {}.", enabled ? "enable" : "disable",
      HelixUtil.serializeByComma(instances), clusterName);
  BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<>(_zkClient);
  if (enabled) {
    for (String instance : instances) {
      enableSingleInstance(clusterName, instance, enabled, baseAccessor);
    }
  }
  enableBatchInstances(clusterName, instances, enabled, baseAccessor);
}
 
Example #7
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void addResource(String clusterName, String resourceName, IdealState idealstate) {
  logger.info("Add resource {} in cluster {}.", resourceName, clusterName);
  String stateModelRef = idealstate.getStateModelDefRef();
  String stateModelDefPath = PropertyPathBuilder.stateModelDef(clusterName, stateModelRef);
  if (!_zkClient.exists(stateModelDefPath)) {
    throw new HelixException(
        "State model " + stateModelRef + " not found in the cluster STATEMODELDEFS path");
  }

  String idealStatePath = PropertyPathBuilder.idealState(clusterName);
  String resourceIdealStatePath = idealStatePath + "/" + resourceName;
  if (_zkClient.exists(resourceIdealStatePath)) {
    throw new HelixException("Skip the operation. Resource ideal state directory already exists:"
        + resourceIdealStatePath);
  }

  ZKUtil.createChildren(_zkClient, idealStatePath, idealstate.getRecord());
}
 
Example #8
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Partially updates the fields appearing in the given IdealState (input).
 * @param clusterName
 * @param resourceName
 * @param idealState
 */
@Override
public void updateIdealState(String clusterName, String resourceName, IdealState idealState) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException(
        "updateIdealState failed. Cluster: " + clusterName + " is NOT setup properly.");
  }
  String zkPath = PropertyPathBuilder.idealState(clusterName, resourceName);
  if (!_zkClient.exists(zkPath)) {
    throw new HelixException(String.format(
        "updateIdealState failed. The IdealState for the given resource does not already exist. Resource name: %s",
        resourceName));
  }
  // Update by way of merge
  ZKUtil.createOrUpdate(_zkClient, zkPath, idealState.getRecord(), true, true);
}
 
Example #9
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void addCloudConfig(String clusterName, CloudConfig cloudConfig) {
  logger.info("Add CloudConfig to cluster {}, CloudConfig is {}.", clusterName,
      cloudConfig.toString());

  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("cluster " + clusterName + " is not setup yet");
  }

  CloudConfig.Builder builder = new CloudConfig.Builder(cloudConfig);
  CloudConfig cloudConfigBuilder = builder.build();

  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  accessor.setProperty(keyBuilder.cloudConfig(), cloudConfigBuilder);
}
 
Example #10
Source File: TestStoppingQueueFailToStop.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test
public void testStoppingQueueFailToStop() throws Exception {
  String jobQueueName = TestHelper.getTestMethodName();
  JobConfig.Builder jobBuilder0 =
      new JobConfig.Builder().setWorkflow(jobQueueName).setTargetResource(DATABASE)
          .setTargetPartitionStates(Sets.newHashSet(MasterSlaveSMD.States.MASTER.name()))
          .setCommand(MockTask.TASK_COMMAND)
          .setJobCommandConfigMap(ImmutableMap.of(MockTask.JOB_DELAY, "100000"));

  JobQueue.Builder jobQueue = TaskTestUtil.buildJobQueue(jobQueueName);
  jobQueue.enqueueJob("JOB0", jobBuilder0);
  _driver.start(jobQueue.build());
  _driver.pollForJobState(jobQueueName, TaskUtil.getNamespacedJobName(jobQueueName, "JOB0"),
      TaskState.IN_PROGRESS);
  boolean exceptionHappened = false;
  try {
    _driver.waitToStop(jobQueueName, 5000L);
  } catch (HelixException e) {
    exceptionHappened = true;
  }
  _driver.pollForWorkflowState(jobQueueName, TaskState.STOPPING);
  Assert.assertTrue(exceptionHappened);
  latch.countDown();
}
 
Example #11
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void addTypeToCustomizedStateConfig(String clusterName, String type) {
  logger.info("Add type {} to CustomizedStateConfig of cluster {}", type, clusterName);

  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("cluster " + clusterName + " is not setup yet");
  }
  CustomizedStateConfig.Builder builder =
      new CustomizedStateConfig.Builder();

  builder.addAggregationEnabledType(type);
  CustomizedStateConfig customizedStateConfigFromBuilder = builder.build();

  ZKHelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  accessor.updateProperty(keyBuilder.customizedStateConfig(),
      customizedStateConfigFromBuilder);
}
 
Example #12
Source File: TestInstanceValidationUtil.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = HelixException.class)
public void TestIsInstanceStable_exception_whenExternalViewNull() {
  String resource = "db";
  Mock mock = new Mock();
  ClusterConfig clusterConfig = new ClusterConfig(TEST_CLUSTER);
  clusterConfig.setPersistIntermediateAssignment(true);
  doReturn(clusterConfig).when(mock.dataAccessor)
      .getProperty(argThat(new PropertyKeyArgument(PropertyType.CONFIGS)));
  doReturn(ImmutableList.of(resource)).when(mock.dataAccessor)
      .getChildNames(argThat(new PropertyKeyArgument(PropertyType.IDEALSTATES)));
  IdealState idealState = mock(IdealState.class);
  when(idealState.isEnabled()).thenReturn(true);
  when(idealState.getPartitionSet()).thenReturn(ImmutableSet.of("db0"));
  when(idealState.getInstanceStateMap("db0"))
      .thenReturn(ImmutableMap.of(TEST_INSTANCE, "Master"));
  when(idealState.isValid()).thenReturn(true);
  when(idealState.getStateModelDefRef()).thenReturn("MasterSlave");
  doReturn(idealState).when(mock.dataAccessor)
      .getProperty(argThat(new PropertyKeyArgument(PropertyType.IDEALSTATES)));
  doReturn(null).when(mock.dataAccessor)
      .getProperty(argThat(new PropertyKeyArgument(PropertyType.EXTERNALVIEW)));
  InstanceValidationUtil.isInstanceStable(mock.dataAccessor, TEST_INSTANCE);
}
 
Example #13
Source File: PerInstanceAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@POST
@Path("stoppable")
@Consumes(MediaType.APPLICATION_JSON)
public Response isInstanceStoppable(String jsonContent, @PathParam("clusterId") String clusterId,
    @PathParam("instanceName") String instanceName) throws IOException {
  ObjectMapper objectMapper = new ObjectMapper();
  HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
  InstanceService instanceService =
      new InstanceServiceImpl(new HelixDataAccessorWrapper((ZKHelixDataAccessor) dataAccessor), getConfigAccessor());
  StoppableCheck stoppableCheck = null;
  try {
    stoppableCheck =
        instanceService.getInstanceStoppableCheck(clusterId, instanceName, jsonContent);
  } catch (HelixException e) {
    LOG.error(String.format("Current cluster %s has issue with health checks!", clusterId),
        e);
    return serverError(e);
  }
  return OK(objectMapper.writeValueAsString(stoppableCheck));
}
 
Example #14
Source File: Topology.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Add an end node to the tree, create all the paths to the leaf node if not present.
 */
private void addEndNode(Node root, String instanceName, LinkedHashMap<String, String> pathNameMap,
    int instanceWeight, List<String> liveInstances) {
  Node current = root;
  List<Node> pathNodes = new ArrayList<>();
  for (Map.Entry<String, String> entry : pathNameMap.entrySet()) {
    String pathValue = entry.getValue();
    String path = entry.getKey();

    pathNodes.add(current);
    if (!current.hasChild(pathValue)) {
      buildNewNode(pathValue, path, current, instanceName, instanceWeight,
          liveInstances.contains(instanceName), pathNodes);
    } else if (path.equals(_endNodeType)) {
      throw new HelixException(
          "Failed to add topology node because duplicate leaf nodes are not allowed. Duplicate node name: "
              + pathValue);
    }
    current = current.getChild(pathValue);
  }
}
 
Example #15
Source File: Message.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Create a reply based on an incoming message
 * @param srcMessage the incoming message
 * @param instanceName the instance that is the source of the reply
 * @param taskResultMap the result of executing the incoming message
 * @return the reply Message
 */
public static Message createReplyMessage(Message srcMessage, String instanceName,
    Map<String, String> taskResultMap) {
  if (srcMessage.getCorrelationId() == null) {
    throw new HelixException(
        "Message " + srcMessage.getMsgId() + " does not contain correlation id");
  }
  Message replyMessage = new Message(MessageType.TASK_REPLY, UUID.randomUUID().toString());
  replyMessage.setCorrelationId(srcMessage.getCorrelationId());
  replyMessage.setResultMap(taskResultMap);
  replyMessage.setTgtSessionId("*");
  replyMessage.setMsgState(MessageState.NEW);
  replyMessage.setSrcName(instanceName);
  if (srcMessage.getSrcInstanceType() == InstanceType.CONTROLLER) {
    replyMessage.setTgtName(InstanceType.CONTROLLER.name());
  } else {
    replyMessage.setTgtName(srcMessage.getMsgSrc());
  }
  return replyMessage;
}
 
Example #16
Source File: TestCloudConfig.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = HelixException.class)
public void testUnverifiedCloudConfigBuilder() {
  String className = getShortClassName();
  String clusterName = "CLUSTER_" + className;
  CloudConfig.Builder builder = new CloudConfig.Builder();
  builder.setCloudEnabled(true);
  // Verify will fail because cloudID has net been defined.
  CloudConfig cloudConfig = builder.build();
}
 
Example #17
Source File: ExternalViewCache.java    From helix with Apache License 2.0 5 votes vote down vote up
private PropertyKey externalViewsKey(PropertyKey.Builder keyBuilder) {
  PropertyKey evPropertyKey;
  if (_type.equals(PropertyType.EXTERNALVIEW)) {
    evPropertyKey = keyBuilder.externalViews();
  } else if (_type.equals(PropertyType.TARGETEXTERNALVIEW)) {
    evPropertyKey = keyBuilder.targetExternalViews();
  } else {
    throw new HelixException(
        "Failed to refresh ExternalViewCache, Wrong property type " + _type + "!");
  }

  return evPropertyKey;
}
 
Example #18
Source File: TestCloudConfig.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = HelixException.class)
public void testCloudConfigNonExistentCluster() {
  String className = getShortClassName();
  String clusterName = "CLUSTER_" + className;
  // Read CloudConfig from Zookeeper and get exception since cluster in not setup yet
  ConfigAccessor _configAccessor = new ConfigAccessor(_gZkClient);
  CloudConfig cloudConfigFromZk = _configAccessor.getCloudConfig(clusterName);
}
 
Example #19
Source File: AbstractEvenDistributionRebalanceStrategy.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Force uniform distribution based on the parent strategy class's calculation result.
 *
 * @param allNodes       All instances
 * @param liveNodes      List of live instances
 * @param currentMapping current replica mapping
 * @param clusterData    cluster data
 * @return
 * @throws HelixException
 */
@Override
public ZNRecord computePartitionAssignment(final List<String> allNodes,
    final List<String> liveNodes, final Map<String, Map<String, String>> currentMapping,
    ResourceControllerDataProvider clusterData) {
  // validate the instance configs
  Map<String, InstanceConfig> instanceConfigMap = clusterData.getInstanceConfigMap();
  if (instanceConfigMap == null || !instanceConfigMap.keySet().containsAll(allNodes)) {
    throw new HelixException(String.format("Config for instances %s is not found!",
            allNodes.removeAll(instanceConfigMap.keySet())));
  }
  // only compute assignments for instances with non-zero weight
  return computeBestPartitionAssignment(getNonZeroWeightNodes(allNodes, instanceConfigMap),
      liveNodes, currentMapping, clusterData);
}
 
Example #20
Source File: TaskDriver.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Delete a job from an existing named queue,
 * the queue has to be stopped prior to this call
 * @param queue queue name
 * @param job job name: namespaced job name
 * @param forceDelete CAUTION: if set true, all job's related zk nodes will
 *          be removed from zookeeper even if its JobQueue information can not be found.
 */
public void deleteNamespacedJob(final String queue, final String job, boolean forceDelete) {
  WorkflowConfig jobQueueConfig = TaskUtil.getWorkflowConfig(_accessor, queue);
  boolean isRecurringWorkflow;

  // Force deletion of a job
  if (forceDelete) {
    // remove all job-related ZNodes
    LOG.info("Forcefully removing job: {} from queue: {}", job, queue);
    if (!TaskUtil.removeJob(_accessor, _propertyStore, job)) {
      LOG.info("Failed to delete job: {} from queue: {}", job, queue);
      throw new HelixException("Failed to delete job: " + job + " from queue: " + queue);
    }
    // In case this was a recurrent workflow, remove it from last scheduled queue as well
    if (jobQueueConfig != null) {
      isRecurringWorkflow = jobQueueConfig.getScheduleConfig() != null
          && jobQueueConfig.getScheduleConfig().isRecurring();
      if (isRecurringWorkflow) {
        deleteJobFromLastScheduledQueue(queue, TaskUtil.getDenamespacedJobName(queue, job));
      }
    }
    return;
  }

  // Regular, non-force, deletion of a job
  if (jobQueueConfig == null) {
    throw new IllegalArgumentException(
        String.format("JobQueue %s's config is not found!", queue));
  }
  if (!jobQueueConfig.isJobQueue()) {
    throw new IllegalArgumentException(String.format("%s is not a queue!", queue));
  }
  isRecurringWorkflow = jobQueueConfig.getScheduleConfig() != null
      && jobQueueConfig.getScheduleConfig().isRecurring();
  String denamespacedJob = TaskUtil.getDenamespacedJobName(queue, job);
  if (isRecurringWorkflow) {
    deleteJobFromLastScheduledQueue(queue, denamespacedJob);
  }
  deleteJobFromQueue(queue, denamespacedJob);
}
 
Example #21
Source File: HelixRestServer.java    From helix with Apache License 2.0 5 votes vote down vote up
public void start() throws HelixException, InterruptedException {
  try {
    _server.start();
  } catch (Exception ex) {
    LOG.error("Failed to start Helix rest server, " + ex);
    throw new HelixException("Failed to start Helix rest server! " + ex);
  }

  LOG.info("Helix rest server started!");
}
 
Example #22
Source File: ZkBaseDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used for constructors that are not based on the Builder for
 * backward-compatibility.
 * It checks if there is a System Property config set for Multi-ZK mode and determines if a
 * FederatedZkClient should be created.
 * @param clientConfig default RealmAwareZkClientConfig with ZK serializer set
 * @param zkAddress
 * @param zkClientType
 * @return
 */
static RealmAwareZkClient buildRealmAwareZkClientWithDefaultConfigs(
    RealmAwareZkClient.RealmAwareZkClientConfig clientConfig, String zkAddress,
    ZkClientType zkClientType) {
  if (Boolean.getBoolean(SystemPropertyKeys.MULTI_ZK_ENABLED)) {
    // If the multi ZK config is enabled, use multi-realm mode with FederatedZkClient
    try {
      return new FederatedZkClient(
          new RealmAwareZkClient.RealmAwareZkConnectionConfig.Builder().build(), clientConfig);
    } catch (IllegalStateException | IOException | InvalidRoutingDataException e) {
      throw new HelixException("Not able to connect on multi-realm mode.", e);
    }
  }

  RealmAwareZkClient zkClient;
  switch (zkClientType) {
    case DEDICATED:
      zkClient = DedicatedZkClientFactory.getInstance()
          .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress),
              clientConfig.createHelixZkClientConfig());
      break;
    case SHARED:
    default:
      zkClient = SharedZkClientFactory.getInstance()
          .buildZkClient(new HelixZkClient.ZkConnectionConfig(zkAddress),
              clientConfig.createHelixZkClientConfig());

      zkClient
          .waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
      break;
  }
  return zkClient;
}
 
Example #23
Source File: ZkBaseDataAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public List<T> get(List<String> paths, List<Stat> stats, int options, boolean throwException)
    throws HelixException {
  boolean[] needRead = new boolean[paths.size()];
  Arrays.fill(needRead, true);

  return get(paths, stats, needRead, throwException);
}
 
Example #24
Source File: TestZkConnectionLost.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = {
    "testLostZkConnection"
}, enabled = false)
public void testLostZkConnectionNegative() throws Exception {
  System.setProperty(SystemPropertyKeys.ZK_WAIT_CONNECTED_TIMEOUT, "10");
  System.setProperty(SystemPropertyKeys.ZK_SESSION_TIMEOUT, "1000");

  try {
    String queueName = TestHelper.getTestMethodName();

    stopParticipants();
    startParticipants(_zkAddr);

    LOG.info("Starting job-queue: " + queueName);
    JobQueue.Builder queueBuild = TaskTestUtil.buildRecurrentJobQueue(queueName, 0, 6000);
    createAndEnqueueJob(queueBuild, 3);

    _driver.start(queueBuild.build());

    restartZkServer();

    WorkflowContext wCtx = TaskTestUtil.pollForWorkflowContext(_driver, queueName);
    // ensure job 1 is started before stop it
    String scheduledQueue = wCtx.getLastScheduledSingleWorkflow();

    try {
      _driver.pollForWorkflowState(scheduledQueue, 30000, TaskState.COMPLETED);
      Assert.fail("Test failure!");
    } catch (HelixException ex) {
      // test succeeded
    }
  } finally {
    System.clearProperty(SystemPropertyKeys.ZK_WAIT_CONNECTED_TIMEOUT);
    System.clearProperty(SystemPropertyKeys.ZK_SESSION_TIMEOUT);
  }
}
 
Example #25
Source File: StateModelsResource.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public Representation post(Representation entity) {
  try {
    String clusterName = (String) getRequest().getAttributes().get("clusterName");
    ZkClient zkClient =
        (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    ;

    JsonParameters jsonParameters = new JsonParameters(entity);
    String command = jsonParameters.getCommand();

    if (command.equalsIgnoreCase(ClusterSetup.addStateModelDef)) {
      ZNRecord newStateModel =
          jsonParameters.getExtraParameter(JsonParameters.NEW_STATE_MODEL_DEF);
      HelixDataAccessor accessor =
          ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);

      accessor.setProperty(accessor.keyBuilder().stateModelDef(newStateModel.getId()),
          new StateModelDefinition(newStateModel));
      getResponse().setEntity(getStateModelsRepresentation());
    } else {
      throw new HelixException("Unsupported command: " + command + ". Should be one of ["
          + ClusterSetup.addStateModelDef + "]");
    }

    getResponse().setStatus(Status.SUCCESS_OK);
  } catch (Exception e) {
    getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
        MediaType.APPLICATION_JSON);
    getResponse().setStatus(Status.SUCCESS_OK);
    LOG.error("Error in posting " + entity, e);
  }
  return null;
}
 
Example #26
Source File: ZKDistributedNonblockingLock.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
  if (isCurrentOwner()) {
    throw new HelixException("Please unlock the lock before closing it.");
  }
  _baseDataAccessor.close();
}
 
Example #27
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void enableBatchMessageMode(String clusterName, String resourceName, boolean enabled) {
  logger.info("{} batch message mode for resource {} in cluster {}.",
      enabled ? "Enable" : "Disable", resourceName, clusterName);
  // TODO: Change IdealState to ResourceConfig when configs are migrated to ResourceConfig
  IdealState idealState = getResourceIdealState(clusterName, resourceName);
  if (idealState == null) {
    throw new HelixException("Cluster " + clusterName + ", resource: " + resourceName
        + ", ideal-state does not exist");
  }

  idealState.setBatchMessageMode(enabled);
  setResourceIdealState(clusterName, resourceName, idealState);
}
 
Example #28
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
  // TODO Auto-generated method stub
  if (message.getMsgSubType() != null && message.getMsgSubType().equals("EXCEPTION")) {
    throw new HelixException("Test Message handler exception, can ignore");
  }
  _handlersCreated++;
  return new TestMessageHandler(message, context);
}
 
Example #29
Source File: TestZKHelixNonblockingLock.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseLockedLock() {
  _lock.tryLock();
  Assert.assertTrue(_lock.isCurrentOwner());
  try {
    _lock.close();
    Assert.fail("Should throw exception here.");
  } catch (HelixException e) {
    Assert.assertEquals(e.getMessage(), "Please unlock the lock before closing it.");
  }
  Assert.assertTrue(_lock.isCurrentOwner());
}
 
Example #30
Source File: TestRoutingTableProviderFromCurrentStates.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test(dependsOnMethods = { "TestInconsistentStateEventProcessing" })
public void testWithSupportSourceDataType() {
  new RoutingTableProvider(_manager, PropertyType.EXTERNALVIEW).shutdown();
  new RoutingTableProvider(_manager, PropertyType.TARGETEXTERNALVIEW).shutdown();
  new RoutingTableProvider(_manager, PropertyType.CURRENTSTATES).shutdown();

  try {
    new RoutingTableProvider(_manager, PropertyType.IDEALSTATES).shutdown();
    Assert.fail();
  } catch (HelixException ex) {
    Assert.assertTrue(ex.getMessage().contains("Unsupported source data type"));
  }
}