public Response updateCluster()

in helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java [235:370]


  public Response updateCluster(@PathParam("clusterId") String clusterId,
      @QueryParam("command") String commandStr, @QueryParam("superCluster") String superCluster,
      @QueryParam("duration") Long duration, String content) {
    Command command;
    try {
      command = getCommand(commandStr);
    } catch (HelixException ex) {
      return badRequest(ex.getMessage());
    }

    ClusterSetup clusterSetup = getClusterSetup();
    HelixAdmin helixAdmin = getHelixAdmin();

    switch (command) {
      case activate:
        if (superCluster == null) {
          return badRequest("Super Cluster name is missing!");
        }
        try {
          clusterSetup.activateCluster(clusterId, superCluster, true);
        } catch (Exception ex) {
          LOG.error("Failed to add cluster {} to super cluster {}.", clusterId, superCluster);
          return serverError(ex);
        }
        break;

      case deactivate:
        if (superCluster == null) {
          return badRequest("Super Cluster name is missing!");
        }
        try {
          clusterSetup.activateCluster(clusterId, superCluster, false);
        } catch (Exception ex) {
          LOG.error("Failed to deactivate cluster {} from super cluster {}.", clusterId, superCluster);
          return serverError(ex);
        }
        break;

      case addVirtualTopologyGroup:
        try {
          addVirtualTopologyGroup(clusterId, content);
        } catch (JsonProcessingException ex) {
          LOG.error("Failed to parse json string: {}", content, ex);
          return badRequest("Invalid payload json body: " + content);
        } catch (IllegalArgumentException ex) {
          LOG.error("Illegal input {} for command {}.", content, command, ex);
          return badRequest(String.format("Illegal input %s for command %s", content, command));
        } catch (Exception ex) {
          LOG.error("Failed to add virtual topology group to cluster {}", clusterId, ex);
          return serverError(ex);
        }
        break;

      case expand:
        try {
          clusterSetup.expandCluster(clusterId);
        } catch (Exception ex) {
          LOG.error("Failed to expand cluster {}.", clusterId);
          return serverError(ex);
        }
        break;

      case enable:
        try {
          helixAdmin.enableCluster(clusterId, true);
        } catch (Exception ex) {
          LOG.error("Failed to enable cluster {}.", clusterId);
          return serverError(ex);
        }
        break;

      case disable:
        try {
          helixAdmin.enableCluster(clusterId, false);
        } catch (Exception ex) {
          LOG.error("Failed to disable cluster {}.", clusterId);
          return serverError(ex);
        }
        break;

      case enableMaintenanceMode:
      case disableMaintenanceMode:
        // Try to parse the content string. If parseable, use it as a KV mapping. Otherwise, treat it
        // as a REASON String
        Map<String, String> customFieldsMap = null;
        try {
          // Try to parse content
          customFieldsMap =
              OBJECT_MAPPER.readValue(content, new TypeReference<HashMap<String, String>>() {
              });
          // content is given as a KV mapping. Nullify content unless (case-insensitive) reason key present in map
          content = null;
          for (Map.Entry<String, String> entry : customFieldsMap.entrySet()) {
            if ("reason".equalsIgnoreCase(entry.getKey())) {
              content = entry.getValue();
            }
          }
        } catch (Exception e) {
          // NOP
        }
        helixAdmin
            .manuallyEnableMaintenanceMode(clusterId, command == Command.enableMaintenanceMode,
                content, customFieldsMap);
        break;
      case enableWagedRebalanceForAllResources:
        // Enable WAGED rebalance for all resources in the cluster
        List<String> resources = helixAdmin.getResourcesInCluster(clusterId);
        try {
          helixAdmin.enableWagedRebalance(clusterId, resources);
        } catch (HelixException e) {
          return badRequest(e.getMessage());
        }
        break;
      case purgeOfflineParticipants:
        if (duration == null || duration < 0) {
          helixAdmin
              .purgeOfflineInstances(clusterId, ClusterConfig.OFFLINE_DURATION_FOR_PURGE_NOT_SET);
        } else {
          helixAdmin.purgeOfflineInstances(clusterId, duration);
        }
        break;
      case onDemandRebalance:
        try {
          helixAdmin.onDemandRebalance(clusterId);
        } catch (Exception ex) {
          LOG.error(
              "Cannot start on-demand rebalance for cluster: {}, Exception: {}", clusterId, ex);
          return serverError(ex);
        }
        break;
      default:
        return badRequest("Unsupported command {}." + command);
    }

    return OK();
  }