org.apache.helix.model.HelixConfigScope.ConfigScopeProperty Java Examples

The following examples show how to use org.apache.helix.model.HelixConfigScope.ConfigScopeProperty. 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: ConfigScopeBuilder.java    From helix with Apache License 2.0 6 votes vote down vote up
public ConfigScope build(String scopePairs) {
  String[] scopes = scopePairs.split("[\\s,]+");
  for (String scope : scopes) {
    try {
      int idx = scope.indexOf('=');
      if (idx == -1) {
        LOG.error("Invalid scope string: " + scope);
        continue;
      }

      String scopeStr = scope.substring(0, idx);
      String value = scope.substring(idx + 1);
      ConfigScopeProperty scopeProperty = ConfigScopeProperty.valueOf(scopeStr);
      _scopeMap.put(scopeProperty, value);
    } catch (Exception e) {
      LOG.error("Invalid scope string: " + scope);
      continue;
    }
  }

  return build();
}
 
Example #2
Source File: HelixSetupUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
private static void setupHelixClusterIfNeeded(String helixClusterName, String zkPath) {
  HelixAdmin admin = new ZKHelixAdmin(zkPath);
  if (admin.getClusters().contains(helixClusterName)) {
    LOGGER.info("Helix cluster: {} already exists", helixClusterName);
  } else {
    LOGGER.info("Creating a new Helix cluster: {}", helixClusterName);
    admin.addCluster(helixClusterName, false);
    // Enable Auto-Join for the cluster
    HelixConfigScope configScope =
        new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(helixClusterName).build();
    Map<String, String> configMap = new HashMap<>();
    configMap.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, Boolean.toString(true));
    configMap.put(ENABLE_CASE_INSENSITIVE_KEY, Boolean.toString(false));
    configMap.put(DEFAULT_HYPERLOGLOG_LOG2M_KEY, Integer.toString(DEFAULT_HYPERLOGLOG_LOG2M));
    configMap.put(CommonConstants.Broker.CONFIG_OF_ENABLE_QUERY_LIMIT_OVERRIDE, Boolean.toString(false));
    admin.setConfig(configScope, configMap);
    LOGGER.info("New Helix cluster: {} created", helixClusterName);
  }
}
 
Example #3
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Get CustomizedStateConfig of the given cluster.
 * @param clusterName
 * @return The instance of {@link CustomizedStateConfig}
 */
public CustomizedStateConfig getCustomizedStateConfig(String clusterName) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException(String.format("Failed to get config. cluster: %s is not setup.", clusterName));
  }
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CUSTOMIZED_STATE).forCluster(clusterName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No customized state aggregation config found at {}.", scope.getZkPath());
    return null;
  }

  return new CustomizedStateConfig.Builder(record).build();
}
 
Example #4
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Get ClusterConfig of the given cluster.
 *
 * @param clusterName
 *
 * @return
 */
public ClusterConfig getClusterConfig(String clusterName) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to get config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No config found at {}.", scope.getZkPath());
    return null;
  }

  return new ClusterConfig(record);
}
 
Example #5
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Get CloudConfig of the given cluster.
 * @param clusterName
 * @return The instance of {@link CloudConfig}
 */
public CloudConfig getCloudConfig(String clusterName) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException(
        String.format("Failed to get config. cluster: %s is not setup.", clusterName));
  }
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLOUD).forCluster(clusterName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No cloud config found at {}.", scope.getZkPath());
    return null;
  }

  return new CloudConfig(record);
}
 
Example #6
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateRESTConfig(String clusterName, RESTConfig restConfig, boolean overwrite) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("Fail to update REST config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.REST).forCluster(clusterName).build();
  String zkPath = scope.getZkPath();

  // Create "/{clusterId}/CONFIGS/REST" if it does not exist
  String parentPath = HelixUtil.getZkParentPath(zkPath);
  if (!_zkClient.exists(parentPath)) {
    ZKUtil.createOrMerge(_zkClient, parentPath, new ZNRecord(parentPath), true, true);
  }

  if (overwrite) {
    ZKUtil.createOrReplace(_zkClient, zkPath, restConfig.getRecord(), true);
  } else {
    ZKUtil.createOrUpdate(_zkClient, zkPath, restConfig.getRecord(), true, true);
  }
}
 
Example #7
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateClusterConfig(String clusterName, ClusterConfig clusterConfig,
    boolean overwrite) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to update config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
  String zkPath = scope.getZkPath();

  if (overwrite) {
    ZKUtil.createOrReplace(_zkClient, zkPath, clusterConfig.getRecord(), true);
  } else {
    ZKUtil.createOrUpdate(_zkClient, zkPath, clusterConfig.getRecord(), true, true);
  }
}
 
Example #8
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Get instance config for given resource in given cluster.
 *
 * @param clusterName
 * @param instanceName
 *
 * @return
 */
public InstanceConfig getInstanceConfig(String clusterName, String instanceName) {
  if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) {
    throw new HelixException(
        "fail to get config. instance: " + instanceName + " is NOT setup in cluster: "
            + clusterName);
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName)
          .forParticipant(instanceName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No config found at {}.", scope.getZkPath());
    return null;
  }

  return new InstanceConfig(record);
}
 
Example #9
Source File: IntegrationTest.java    From helix with Apache License 2.0 6 votes vote down vote up
private static void addConfiguration(ClusterSetup setup, String baseDir, String clusterName,
    String instanceName) throws IOException {
  Map<String, String> properties = new HashMap<String, String>();
  HelixConfigScopeBuilder builder = new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT);
  HelixConfigScope instanceScope =
      builder.forCluster(clusterName).forParticipant(instanceName).build();
  properties.put("change_log_dir", baseDir + instanceName + "/translog");
  properties.put("file_store_dir", baseDir + instanceName + "/filestore");
  properties.put("check_point_dir", baseDir + instanceName + "/checkpoint");
  setup.getClusterManagementTool().setConfig(instanceScope, properties);
  FileUtils.deleteDirectory(new File(properties.get("change_log_dir")));
  FileUtils.deleteDirectory(new File(properties.get("file_store_dir")));
  FileUtils.deleteDirectory(new File(properties.get("check_point_dir")));
  new File(properties.get("change_log_dir")).mkdirs();
  new File(properties.get("file_store_dir")).mkdirs();
  new File(properties.get("check_point_dir")).mkdirs();
}
 
Example #10
Source File: ClusterSetup.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * get configs
 * @param type config-scope-type, e.g. CLUSTER, RESOURCE, etc.
 * @param scopeArgsCsv csv-formatted scope-args, e.g myCluster,testDB
 * @param keysCsv csv-formatted keys. e.g. k1,k2
 * @return json-formated key-value pairs, e.g. {k1=v1,k2=v2}
 */
public String getConfig(ConfigScopeProperty type, String scopeArgsCsv, String keysCsv) {
  // ConfigScope scope = new ConfigScopeBuilder().build(scopesStr);

  String[] scopeArgs = scopeArgsCsv.split("[\\s,]");
  HelixConfigScope scope = new HelixConfigScopeBuilder(type, scopeArgs).build();

  String[] keys = keysCsv.split("[\\s,]");
  // parse keys
  // String[] keys = keysStr.split("[\\s,]");
  // Set<String> keysSet = new HashSet<String>(Arrays.asList(keys));

  Map<String, String> keyValueMap = _admin.getConfig(scope, Arrays.asList(keys));
  ZNRecord record = new ZNRecord(type.toString());
  // record.setMapField(scopesStr, propertiesMap);
  record.getSimpleFields().putAll(keyValueMap);
  ZNRecordSerializer serializer = new ZNRecordSerializer();
  return new String(serializer.serialize(record));
}
 
Example #11
Source File: TestConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetRestConfig() {
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;

  ZKHelixAdmin admin = new ZKHelixAdmin(ZK_ADDR);
  admin.addCluster(clusterName, true);
  ConfigAccessor configAccessor = new ConfigAccessor(ZK_ADDR);
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.REST).forCluster(clusterName).build();
  Assert.assertNull(configAccessor.getRESTConfig(clusterName));

  RESTConfig restConfig = new RESTConfig(clusterName);
  restConfig.set(RESTConfig.SimpleFields.CUSTOMIZED_HEALTH_URL, "TEST_URL");
  configAccessor.setRESTConfig(clusterName, restConfig);
  Assert.assertEquals(restConfig, configAccessor.getRESTConfig(clusterName));
}
 
Example #12
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 #13
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateInstanceConfig(String clusterName, String instanceName,
    InstanceConfig instanceConfig, boolean overwrite) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to setup config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.PARTICIPANT).forCluster(clusterName)
          .forParticipant(instanceName).build();
  String zkPath = scope.getZkPath();

  if (!_zkClient.exists(zkPath)) {
    throw new HelixException(
        "updateInstanceConfig failed. Given InstanceConfig does not already exist. instance: "
            + instanceName);
  }

  if (overwrite) {
    ZKUtil.createOrReplace(_zkClient, zkPath, instanceConfig.getRecord(), true);
  } else {
    ZKUtil.createOrUpdate(_zkClient, zkPath, instanceConfig.getRecord(), true, true);
  }
}
 
Example #14
Source File: ConfigAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
private void updateResourceConfig(String clusterName, String resourceName,
    ResourceConfig resourceConfig, boolean overwrite) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to setup config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName)
          .forResource(resourceName).build();
  String zkPath = scope.getZkPath();

  if (overwrite) {
    ZKUtil.createOrReplace(_zkClient, zkPath, resourceConfig.getRecord(), true);
  } else {
    ZKUtil.createOrUpdate(_zkClient, zkPath, resourceConfig.getRecord(), true, true);
  }
}
 
Example #15
Source File: ServiceDiscovery.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * @return returns true if
 */
public boolean start() throws Exception {
  // auto create cluster and allow nodes to automatically join the cluster
  admin =
      HelixManagerFactory.getZKHelixManager(cluster, "service-discovery",
          InstanceType.ADMINISTRATOR, zkAddress);
  admin.connect();
  admin.getClusterManagmentTool().addCluster(cluster, false);
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(cluster).build();

  Map<String, String> properties = new HashMap<String, String>();
  properties.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, String.valueOf(true));
  admin.getClusterManagmentTool().setConfig(scope, properties);
  switch (mode) {
  case POLL:
    startBackgroundTask();
    break;
  case WATCH:
    setupWatcher();
    break;
  case NONE:// dont monitor changes, supports only registration

  }
  refreshCache();
  return true;
}
 
Example #16
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Delete cloud config fields (not the whole config)
 * @param clusterName
 * @param cloudConfig
 */
public void deleteCloudConfigFields(String clusterName, CloudConfig cloudConfig) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to delete cloud config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLOUD).forCluster(clusterName).build();
  remove(scope, cloudConfig.getRecord());
}
 
Example #17
Source File: PropertyKey.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate with a type, scope, associated class, and parameters
 * @param type
 * @param configScope
 * @param typeClazz
 * @param params parameters associated with the key, the first of which is the cluster name
 */
public PropertyKey(PropertyType type, ConfigScopeProperty configScope,
    Class<? extends HelixProperty> typeClazz, String... params) {
  _type = type;
  if (params == null || params.length == 0 || Arrays.asList(params).contains(null)) {
    throw new IllegalArgumentException("params cannot be null");
  }

  _params = params;
  _typeClazz = typeClazz;

  _configScope = configScope;
}
 
Example #18
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Get resource config for given resource in given cluster.
 *
 * @param clusterName
 * @param resourceName
 *
 * @return
 */
public ResourceConfig getResourceConfig(String clusterName, String resourceName) {
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName)
          .forResource(resourceName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No config found at {}.", scope.getZkPath());
    return null;
  }

  return new ResourceConfig(record);
}
 
Example #19
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
public void deleteRESTConfig(String clusterName) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("Fail to delete REST config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.REST).forCluster(clusterName).build();
  String zkPath = scope.getZkPath();

  // Check if "/{clusterId}/CONFIGS/REST" exists
  String parentPath = HelixUtil.getZkParentPath(zkPath);
  if (!_zkClient.exists(parentPath)) {
    throw new HelixException("Fail to delete REST config. cluster: " + clusterName + " does not have a rest config.");    }

  ZKUtil.dropChildren(_zkClient, parentPath, new ZNRecord(clusterName));
}
 
Example #20
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Get RestConfig of the given cluster.
 * @param clusterName The cluster
 * @return The instance of {@link RESTConfig}
 */
public RESTConfig getRESTConfig(String clusterName) {
  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.REST).forCluster(clusterName).build();
  ZNRecord record = getConfigZnRecord(scope);

  if (record == null) {
    LOG.warn("No rest config found at {}.", scope.getZkPath());
    return null;
  }

  return new RESTConfig(record);
}
 
Example #21
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
private void updateCloudConfig(String clusterName, CloudConfig cloudConfig, boolean overwrite) {
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("Fail to update cloud config. cluster: " + clusterName + " is NOT setup.");
  }

  HelixConfigScope scope =
      new HelixConfigScopeBuilder(ConfigScopeProperty.CLOUD).forCluster(clusterName).build();
  String zkPath = scope.getZkPath();

  if (overwrite) {
    ZKUtil.createOrReplace(_zkClient, zkPath, cloudConfig.getRecord(), true);
  } else {
    ZKUtil.createOrUpdate(_zkClient, zkPath, cloudConfig.getRecord(), true, true);
  }
}
 
Example #22
Source File: ConfigAccessor.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Set multiple configs, creating them if they don't exist
 * @param scope scope specification of the entity set to query
 *          (e.g. cluster, resource, participant, etc.)
 * @param keyValueMap configurations organized by their identifiers
 */
public void set(HelixConfigScope scope, Map<String, String> keyValueMap) {
  if (scope == null || scope.getType() == null || !scope.isFullKey()) {
    LOG.error("fail to set config. invalid config scope. Scope: {}.", scope);
    return;
  }

  String clusterName = scope.getClusterName();
  if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
    throw new HelixException("fail to set config. cluster: " + clusterName + " is NOT setup.");
  }

  if (scope.getType() == ConfigScopeProperty.PARTICIPANT) {
    if (!ZKUtil.isInstanceSetup(_zkClient, scope.getClusterName(), scope.getParticipantName(),
        InstanceType.PARTICIPANT)) {
      throw new HelixException("fail to set config. instance: " + scope.getParticipantName()
          + " is NOT setup in cluster: " + clusterName);
    }
  }

  String mapKey = scope.getMapKey();
  String zkPath = scope.getZkPath();
  String id = zkPath.substring(zkPath.lastIndexOf('/') + 1);
  ZNRecord update = new ZNRecord(id);
  if (mapKey == null) {
    update.getSimpleFields().putAll(keyValueMap);
  } else {
    update.setMapField(mapKey, keyValueMap);
  }

  ZKUtil.createOrMerge(_zkClient, zkPath, update, true, true);
}
 
Example #23
Source File: HelixConfigScopeBuilder.java    From helix with Apache License 2.0 5 votes vote down vote up
public HelixConfigScopeBuilder(ConfigScopeProperty type, String... keys) {
  int argNum = type.getZkPathArgNum() + type.getMapKeyArgNum();
  if (keys == null || (keys.length != argNum && keys.length != argNum - 1)) {
    throw new IllegalArgumentException("invalid keys. type: " + type + ", keys: "
        + Arrays.asList(keys));
  }

  _type = type;
  _clusterName = keys[0];

  switch (type) {
  case CLUSTER:
    break;
  case REST:
    break;
  case PARTICIPANT:
    if (keys.length > 1) {
      _participantName = keys[1];
    }
    break;
  case RESOURCE:
    if (keys.length > 1) {
      _resourceName = keys[1];
    }
    break;
  case PARTITION:
    _resourceName = keys[1];
    if (keys.length > 2) {
      _partitionName = keys[2];
    }
    break;
  default:
    break;
  }
}
 
Example #24
Source File: TestZkHelixAdmin.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropResource() {
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;

  System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));

  HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
  tool.addCluster(clusterName, true);
  Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient), "Cluster should be setup");

  tool.addStateModelDef(clusterName, "MasterSlave",
      new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave()));
  tool.addResource(clusterName, "test-db", 4, "MasterSlave");
  Map<String, String> resourceConfig = new HashMap<>();
  resourceConfig.put("key1", "value1");
  tool.setConfig(new HelixConfigScopeBuilder(ConfigScopeProperty.RESOURCE).forCluster(clusterName)
      .forResource("test-db").build(), resourceConfig);

  PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
  Assert.assertTrue(_gZkClient.exists(keyBuilder.idealStates("test-db").getPath()),
      "test-db ideal-state should exist");
  Assert.assertTrue(_gZkClient.exists(keyBuilder.resourceConfig("test-db").getPath()),
      "test-db resource config should exist");

  tool.dropResource(clusterName, "test-db");
  Assert.assertFalse(_gZkClient.exists(keyBuilder.idealStates("test-db").getPath()),
      "test-db ideal-state should be dropped");
  Assert.assertFalse(_gZkClient.exists(keyBuilder.resourceConfig("test-db").getPath()),
      "test-db resource config should be dropped");

  tool.dropCluster(clusterName);
  System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
 
Example #25
Source File: TestClusterSetup.java    From helix with Apache License 2.0 5 votes vote down vote up
@Test()
public void testSetGetRemoveParticipantConfig() throws Exception {
  String className = TestHelper.getTestClassName();
  String methodName = TestHelper.getTestMethodName();
  String clusterName = className + "_" + methodName;

  System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));

  _clusterSetup.addCluster(clusterName, true);
  _clusterSetup.addInstanceToCluster(clusterName, "localhost_0");

  // test set/get/remove instance configs
  String scopeArgs = clusterName + ",localhost_0";
  String keyValueMap = "key1=value1,key2=value2";
  String keys = "key1,key2";
  ClusterSetup.processCommandLineArgs(new String[] {
      "--zkSvr", ZK_ADDR, "--setConfig", ConfigScopeProperty.PARTICIPANT.toString(), scopeArgs,
      keyValueMap
  });

  // getConfig returns json-formatted key-value pairs
  String valuesStr = _clusterSetup.getConfig(ConfigScopeProperty.PARTICIPANT, scopeArgs, keys);
  ZNRecordSerializer serializer = new ZNRecordSerializer();
  ZNRecord record = (ZNRecord) serializer.deserialize(valuesStr.getBytes());
  Assert.assertEquals(record.getSimpleField("key1"), "value1");
  Assert.assertEquals(record.getSimpleField("key2"), "value2");

  ClusterSetup.processCommandLineArgs(new String[] {
      "--zkSvr", ZK_ADDR, "--removeConfig", ConfigScopeProperty.PARTICIPANT.toString(), scopeArgs,
      keys
  });
  valuesStr = _clusterSetup.getConfig(ConfigScopeProperty.PARTICIPANT, scopeArgs, keys);
  record = (ZNRecord) serializer.deserialize(valuesStr.getBytes());
  Assert.assertNull(record.getSimpleField("key1"));
  Assert.assertNull(record.getSimpleField("key2"));

  deleteCluster(clusterName);
  System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
 
Example #26
Source File: ClusterSetup.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * set configs
 * @param type config-scope type, e.g. CLUSTER, RESOURCE, etc.
 * @param scopeArgsCsv scopeArgsCsv csv-formatted scope-args, e.g myCluster,testDB
 * @param keyValuePairs csv-formatted key-value pairs. e.g. k1=v1,k2=v2
 */
public void setConfig(ConfigScopeProperty type, String scopeArgsCsv, String keyValuePairs) {
  // ConfigScope scope = new ConfigScopeBuilder().build(scopesKeyValuePairs);
  String[] scopeArgs = scopeArgsCsv.split("[\\s,]");
  HelixConfigScope scope = new HelixConfigScopeBuilder(type, scopeArgs).build();

  Map<String, String> keyValueMap = HelixUtil.parseCsvFormatedKeyValuePairs(keyValuePairs);
  _admin.setConfig(scope, keyValueMap);
}
 
Example #27
Source File: HelixSetupUtils.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
public static void createHelixClusterIfNeeded(String helixClusterName, String zkPath) {
  final HelixAdmin admin = new ZKHelixAdmin(zkPath);

  if (admin.getClusters().contains(helixClusterName)) {
    LOGGER.info("cluster already exist, skipping it.. ********************************************* ");
    return;
  }

  LOGGER.info("Creating a new cluster, as the helix cluster : " + helixClusterName
      + " was not found ********************************************* ");
  admin.addCluster(helixClusterName, false);

  LOGGER.info("Enable mirror maker machines auto join.");
  final HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER)
      .forCluster(helixClusterName).build();

  final Map<String, String> props = new HashMap<String, String>();
  props.put(ZKHelixManager.ALLOW_PARTICIPANT_AUTO_JOIN, String.valueOf(true));
  props.put(MessageType.STATE_TRANSITION + "." + HelixTaskExecutor.MAX_THREADS,
      String.valueOf(100));

  admin.setConfig(scope, props);

  LOGGER.info("Adding state model definition named : OnlineOffline generated using : "
      + OnlineOfflineStateModel.class.toString()
      + " ********************************************** ");

  // add state model definition
  admin.addStateModelDef(helixClusterName, "OnlineOffline", OnlineOfflineStateModel.build());
  LOGGER.info("New Cluster setup completed... ********************************************** ");
}
 
Example #28
Source File: ConfigScopeBuilder.java    From helix with Apache License 2.0 5 votes vote down vote up
public ConfigScope build(ConfigScopeProperty scope, String clusterName, String... scopeKeys) {
  if (scopeKeys == null) {
    scopeKeys = new String[] {};
  }

  String[] args = new String[1 + scopeKeys.length];
  args[0] = clusterName;
  System.arraycopy(scopeKeys, 0, args, 1, scopeKeys.length);
  String scopePairs = template.instantiate(scope, args);

  return build(scopePairs);
}
 
Example #29
Source File: ClusterSetup.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * remove configs
 * @param type config-scope type, e.g. CLUSTER, RESOURCE, etc.
 * @param scopeArgsCsv csv-formatted scope-args, e.g myCluster,testDB
 * @param keysCsv csv-formatted keys. e.g. k1,k2
 */
public void removeConfig(ConfigScopeProperty type, String scopeArgsCsv, String keysCsv) {
  // ConfigScope scope = new ConfigScopeBuilder().build(scopesStr);
  //
  // // parse keys
  // String[] keys = keysStr.split("[\\s,]");
  // Set<String> keysSet = new HashSet<String>(Arrays.asList(keys));

  String[] scopeArgs = scopeArgsCsv.split("[\\s,]");
  HelixConfigScope scope = new HelixConfigScopeBuilder(type, scopeArgs).build();

  String[] keys = keysCsv.split("[\\s,]");

  _admin.removeConfig(scope, Arrays.asList(keys));
}
 
Example #30
Source File: ConfigScope.java    From helix with Apache License 2.0 5 votes vote down vote up
public ConfigScope(ConfigScopeBuilder configScopeBuilder) {
  Map<ConfigScopeProperty, String> scopeMap = configScopeBuilder.getScopeMap();
  List<String> keys = new ArrayList<String>();

  ConfigScopeProperty curScope = null;
  for (ConfigScopeProperty scope : scopePriority) {
    if (scopeMap.containsKey(scope)) {
      if (curScope == null && scope == ConfigScopeProperty.CLUSTER) {
        keys.add(scopeMap.get(scope));
        curScope = ConfigScopeProperty.CLUSTER;
      } else if (curScope == null) {
        throw new IllegalArgumentException("Missing CLUSTER scope. Can't build scope using "
            + configScopeBuilder);
      } else {
        if (!scopeTransition.containsKey(curScope)
            || !scopeTransition.get(curScope).containsKey(scope)) {
          throw new IllegalArgumentException("Can't build scope using " + configScopeBuilder);
        }
        keys.add(scopeMap.get(scope));
        curScope = scopeTransition.get(curScope).get(scope);
      }
    }
  }

  if (curScope == ConfigScopeProperty.CLUSTER) {
    // append one more {clusterName}
    keys.add(scopeMap.get(ConfigScopeProperty.CLUSTER));
  }

  String scopeStr = template.instantiate(curScope, keys.toArray(new String[0]));

  _clusterName = keys.get(0);
  _scopeStr = scopeStr;
  _scope = curScope;
}