Java Code Examples for org.apache.helix.HelixDataAccessor#removeProperty()

The following examples show how to use org.apache.helix.HelixDataAccessor#removeProperty() . 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: ZKHelixAdmin.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * @param clusterName
 * @param enabled
 * @param reason      set additional string description on why the cluster is disabled when
 *                    <code>enabled</code> is false.
 */
@Override
public void enableCluster(String clusterName, boolean enabled, String reason) {
  logger.info("{} cluster {} for reason {}.", enabled ? "Enable" : "Disable", clusterName,
      reason == null ? "NULL" : reason);
  HelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();

  if (enabled) {
    accessor.removeProperty(keyBuilder.pause());
  } else {
    PauseSignal pauseSignal = new PauseSignal("pause");
    if (reason != null) {
      pauseSignal.setReason(reason);
    }
    if (!accessor.createPause(pauseSignal)) {
      throw new HelixException("Failed to create pause signal");
    }
  }
}
 
Example 2
Source File: ClusterAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@DELETE
@Path("{clusterId}/statemodeldefs/{statemodel}")
public Response removeClusterStateModelDefinition(@PathParam("clusterId") String clusterId,
    @PathParam("statemodel") String statemodel) {
  //Shall we validate the statemodel string not having special character such as ../ etc?
  if (!StringUtils.isAlphanumeric(statemodel)) {
    return badRequest("Invalid statemodel name!");
  }

  HelixDataAccessor dataAccessor = getDataAccssor(clusterId);
  PropertyKey key = dataAccessor.keyBuilder().stateModelDef(statemodel);
  boolean retcode = true;
  try {
    retcode = dataAccessor.removeProperty(key);
  } catch (Exception e) {
    LOG.error("Failed to remove StateModelDefinition key: {}. Exception: {}.", key, e);
    retcode = false;
  }
  if (!retcode) {
    return badRequest("Failed to remove!");
  }
  return OK();
}
 
Example 3
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void dropResource(String clusterName, String resourceName) {
  logger.info("Drop resource {} from cluster {}", resourceName, clusterName);
  HelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();

  accessor.removeProperty(keyBuilder.idealStates(resourceName));
  accessor.removeProperty(keyBuilder.resourceConfig(resourceName));
}
 
Example 4
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void removeCloudConfig(String clusterName) {
  logger.info("Remove Cloud Config for cluster {}.", clusterName);
  HelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  accessor.removeProperty(keyBuilder.cloudConfig());
}
 
Example 5
Source File: HelixUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the given message from ZK using the given accessor. This function will
 * not throw exception
 * @param accessor HelixDataAccessor
 * @param msg message to remove
 * @param instanceName name of the instance on which the message sits
 * @return true if success else false
 */
public static boolean removeMessageFromZK(HelixDataAccessor accessor, Message msg,
    String instanceName) {
  try {
    return accessor.removeProperty(msg.getKey(accessor.keyBuilder(), instanceName));
  } catch (Exception e) {
    LOG.error("Caught exception while removing message {}.", msg, e);
  }
  return false;
}
 
Example 6
Source File: TaskUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Cleans up IdealState and external view associated with a job/workflow resource.
 */
private static boolean cleanupIdealStateExtView(final HelixDataAccessor accessor,
    String workflowJobResource) {
  boolean success = true;
  PropertyKey isKey = accessor.keyBuilder().idealStates(workflowJobResource);
  if (accessor.getPropertyStat(isKey) != null) {
    if (!accessor.removeProperty(isKey)) {
      LOG.warn(String.format(
          "Error occurred while trying to remove IdealState for %s. Failed to remove node %s.",
          workflowJobResource, isKey));
      success = false;
    }
  }

  // Delete external view
  PropertyKey evKey = accessor.keyBuilder().externalView(workflowJobResource);
  if (accessor.getPropertyStat(evKey) != null) {
    if (!accessor.removeProperty(evKey)) {
      LOG.warn(String.format(
          "Error occurred while trying to remove ExternalView of resource %s. Failed to remove node %s.",
          workflowJobResource, evKey));
      success = false;
    }
  }

  return success;
}
 
Example 7
Source File: TaskUtil.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Remove workflow or job config.
 * @param accessor
 * @param workflowJobResource the workflow or job name
 */
private static boolean removeWorkflowJobConfig(HelixDataAccessor accessor,
    String workflowJobResource) {
  PropertyKey cfgKey = accessor.keyBuilder().resourceConfig(workflowJobResource);
  if (accessor.getPropertyStat(cfgKey) != null) {
    if (!accessor.removeProperty(cfgKey)) {
      LOG.warn(String.format(
          "Error occurred while trying to remove config for %s. Failed to remove node %s.",
          workflowJobResource, cfgKey));
      return false;
    }
  }

  return true;
}
 
Example 8
Source File: ZKHelixAdmin.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for enabling/disabling maintenance mode.
 * @param clusterName
 * @param enabled
 * @param reason
 * @param internalReason
 * @param customFields
 * @param triggeringEntity
 */
private void processMaintenanceMode(String clusterName, final boolean enabled,
    final String reason, final MaintenanceSignal.AutoTriggerReason internalReason,
    final Map<String, String> customFields,
    final MaintenanceSignal.TriggeringEntity triggeringEntity) {
  HelixDataAccessor accessor =
      new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  logger.info("Cluster {} {} {} maintenance mode for reason {}.", clusterName,
      triggeringEntity == MaintenanceSignal.TriggeringEntity.CONTROLLER ? "automatically"
          : "manually", enabled ? "enters" : "exits", reason == null ? "NULL" : reason);
  final long currentTime = System.currentTimeMillis();
  if (!enabled) {
    // Exit maintenance mode
    accessor.removeProperty(keyBuilder.maintenance());
  } else {
    // Enter maintenance mode
    MaintenanceSignal maintenanceSignal = new MaintenanceSignal(MAINTENANCE_ZNODE_ID);
    if (reason != null) {
      maintenanceSignal.setReason(reason);
    }
    maintenanceSignal.setTimestamp(currentTime);
    maintenanceSignal.setTriggeringEntity(triggeringEntity);
    switch (triggeringEntity) {
      case CONTROLLER:
        // autoEnable
        maintenanceSignal.setAutoTriggerReason(internalReason);
        break;
      case USER:
      case UNKNOWN:
        // manuallyEnable
        if (customFields != null && !customFields.isEmpty()) {
          // Enter all custom fields provided by the user
          Map<String, String> simpleFields = maintenanceSignal.getRecord().getSimpleFields();
          for (Map.Entry<String, String> entry : customFields.entrySet()) {
            if (!simpleFields.containsKey(entry.getKey())) {
              simpleFields.put(entry.getKey(), entry.getValue());
            }
          }
        }
        break;
    }
    if (!accessor.createMaintenance(maintenanceSignal)) {
      throw new HelixException("Failed to create maintenance signal!");
    }
  }

  // Record a MaintenanceSignal history
  if (!accessor.getBaseDataAccessor()
      .update(keyBuilder.controllerLeaderHistory().getPath(),
          (DataUpdater<ZNRecord>) oldRecord -> {
            try {
              if (oldRecord == null) {
                oldRecord = new ZNRecord(PropertyType.HISTORY.toString());
              }
              return new ControllerHistory(oldRecord)
                  .updateMaintenanceHistory(enabled, reason, currentTime, internalReason,
                      customFields, triggeringEntity);
            } catch (IOException e) {
              logger.error("Failed to update maintenance history! Exception: {}", e);
              return oldRecord;
            }
          }, AccessOption.PERSISTENT)) {
    logger.error("Failed to write maintenance history to ZK!");
  }
}
 
Example 9
Source File: TestDeleteWorkflow.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteWorkflowForcefully() throws InterruptedException {
  String jobQueueName = TestHelper.getTestMethodName();
  JobConfig.Builder jobBuilder = JobConfig.Builder.fromMap(WorkflowGenerator.DEFAULT_JOB_CONFIG)
      .setMaxAttemptsPerTask(1).setWorkflow(jobQueueName)
      .setJobCommandConfigMap(ImmutableMap.of(MockTask.JOB_DELAY, "1000000"));

  JobQueue.Builder jobQueue = TaskTestUtil.buildJobQueue(jobQueueName);
  jobQueue.enqueueJob("job1", jobBuilder);
  _driver.start(jobQueue.build());
  _driver.pollForJobState(jobQueueName, TaskUtil.getNamespacedJobName(jobQueueName, "job1"),
      TaskState.IN_PROGRESS);

  // Check that WorkflowConfig, WorkflowContext, and IdealState are indeed created for this job
  // queue
  Assert.assertNotNull(_driver.getWorkflowConfig(jobQueueName));
  Assert.assertNotNull(_driver.getWorkflowContext(jobQueueName));
  Assert.assertNotNull(_driver.getJobConfig(TaskUtil.getNamespacedJobName(jobQueueName, "job1")));
  Assert
      .assertNotNull(_driver.getJobContext(TaskUtil.getNamespacedJobName(jobQueueName, "job1")));
  Assert.assertNotNull(admin.getResourceIdealState(CLUSTER_NAME, jobQueueName));

  // Delete the idealstate of workflow
  HelixDataAccessor accessor = _manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuild = accessor.keyBuilder();
  accessor.removeProperty(keyBuild.idealStates(jobQueueName));
  Assert.assertNull(admin.getResourceIdealState(CLUSTER_NAME, jobQueueName));

  // Attempt the deletion and and it should time out since idealstate does not exist anymore.
  try {
    _driver.deleteAndWaitForCompletion(jobQueueName, DELETE_DELAY);
    Assert.fail(
        "Delete must time out and throw a HelixException with the Controller paused, but did not!");
  } catch (HelixException e) {
    // Pass
  }

  // delete forcefully
  _driver.delete(jobQueueName, true);

  Assert.assertNull(_driver.getWorkflowConfig(jobQueueName));
  Assert.assertNull(_driver.getWorkflowContext(jobQueueName));
  Assert.assertNull(_driver.getJobConfig(TaskUtil.getNamespacedJobName(jobQueueName, "job1")));
  Assert.assertNull(_driver.getJobContext(TaskUtil.getNamespacedJobName(jobQueueName, "job1")));
  Assert.assertNull(admin.getResourceIdealState(CLUSTER_NAME, jobQueueName));
}
 
Example 10
Source File: TestHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoWriteReadStateForRemovedMessage()
    throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  HelixTaskExecutor executor = new HelixTaskExecutor();
  HelixManager manager = new MockClusterManager();
  TestMessageHandlerFactory factory = new TestMessageHandlerFactory();

  for (String type : factory.getMessageTypes()) {
    executor.registerMessageHandlerFactory(type, factory);
  }

  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  String instanceName = "someInstance";

  List<String> messageIds = new ArrayList<>();
  List<Message> messages = new ArrayList<>();
  int nMsgs1 = 5;
  for (int i = 0; i < nMsgs1; i++) {
    Message msg = new Message(factory.getMessageTypes().get(0), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    msg.setTgtName("Localhost_1123");
    msg.setSrcName("127.101.1.23_2234");
    msg.setCorrelationId(UUID.randomUUID().toString());
    accessor.setProperty(keyBuilder.message(instanceName, msg.getId()), msg);
    messageIds.add(msg.getId());
    messages.add(msg);
  }

  Method updateMessageState = HelixTaskExecutor.class
      .getDeclaredMethod("updateMessageState", List.class, HelixDataAccessor.class, String.class);
  updateMessageState.setAccessible(true);

  updateMessageState.invoke(executor, messages, accessor, instanceName);
  Assert.assertEquals(accessor.getChildNames(keyBuilder.messages(instanceName)).size(), nMsgs1);

  accessor.removeProperty(keyBuilder.message(instanceName, messageIds.get(0)));
  System.out.println(accessor.getChildNames(keyBuilder.messages(instanceName)).size());
  updateMessageState.invoke(executor, messages, accessor, instanceName);
  Assert
      .assertEquals(accessor.getChildNames(keyBuilder.messages(instanceName)).size(), nMsgs1 - 1);
}