예제 #1
0
 private void addInstanceTagIfNeeded(String clusterName, String instanceName) {
   InstanceConfig instanceConfig = _helixAdmin.getInstanceConfig(clusterName, instanceName);
   List<String> instanceTags = instanceConfig.getTags();
   if (instanceTags == null || instanceTags.isEmpty()) {
     if (ZKMetadataProvider.getClusterTenantIsolationEnabled(
         _helixManager.getHelixPropertyStore())) {
       _helixAdmin.addInstanceTag(
           clusterName,
           instanceName,
           ControllerTenantNameBuilder.getBrokerTenantNameForTenant(
               ControllerTenantNameBuilder.DEFAULT_TENANT_NAME));
     } else {
       _helixAdmin.addInstanceTag(
           clusterName, instanceName, CommonConstants.Helix.UNTAGGED_BROKER_INSTANCE);
     }
   }
 }
예제 #2
0
  private void prepare(
      String controllerVersion, String participantVersion, String minSupportedParticipantVersion) {
    List<String> instances =
        Arrays.asList("localhost_0", "localhost_1", "localhost_2", "localhost_3", "localhost_4");
    int partitions = 10;
    int replicas = 1;

    // set ideal state
    String resourceName = "testResource";
    ZNRecord record =
        DefaultTwoStateStrategy.calculateIdealState(
            instances, partitions, replicas, resourceName, "MASTER", "SLAVE");
    IdealState idealState = new IdealState(record);
    idealState.setStateModelDefId(StateModelDefinitionId.from("MasterSlave"));

    PropertyKeyBuilder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);

    // set live instances
    record = new ZNRecord("localhost_0");
    if (participantVersion != null) {
      record.setSimpleField(LiveInstanceProperty.HELIX_VERSION.toString(), participantVersion);
    }
    LiveInstance liveInstance = new LiveInstance(record);
    liveInstance.setSessionId("session_0");
    accessor.setProperty(keyBuilder.liveInstance("localhost_0"), liveInstance);
    InstanceConfig config = new InstanceConfig(liveInstance.getInstanceName());
    accessor.setProperty(keyBuilder.instanceConfig(config.getInstanceName()), config);

    if (controllerVersion != null) {
      ((Mocks.MockManager) manager).setVersion(controllerVersion);
    }

    if (minSupportedParticipantVersion != null) {
      manager
          .getProperties()
          .getProperties()
          .put("minimum_supported_version.participant", minSupportedParticipantVersion);
    }
    event.addAttribute("helixmanager", manager);
    runStage(event, new ReadClusterDataStage());
  }
  public static void main(String[] args) throws Exception {
    if (args.length < 3) {
      System.err.println(
          "USAGE: IdealStateExample zkAddress clusterName idealStateMode (AUTO, AUTO_REBALANCE, or CUSTOMIZED) idealStateJsonFile (required for CUSTOMIZED mode)");
      System.exit(1);
    }

    final String zkAddr = args[0];
    final String clusterName = args[1];
    final String idealStateModeStr = args[2].toUpperCase();
    String idealStateJsonFile = null;
    IdealStateModeProperty idealStateMode = IdealStateModeProperty.valueOf(idealStateModeStr);
    if (idealStateMode == IdealStateModeProperty.CUSTOMIZED) {
      if (args.length < 4) {
        System.err.println("Missng idealStateJsonFile for CUSTOMIZED ideal state mode");
        System.exit(1);
      }
      idealStateJsonFile = args[3];
    }

    // add cluster {clusterName}
    ZkClient zkclient =
        new ZkClient(
            zkAddr,
            ZkClient.DEFAULT_SESSION_TIMEOUT,
            ZkClient.DEFAULT_CONNECTION_TIMEOUT,
            new ZNRecordSerializer());
    ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
    admin.addCluster(clusterName, true);

    // add MasterSlave state mode definition
    StateModelConfigGenerator generator = new StateModelConfigGenerator();
    admin.addStateModelDef(
        clusterName,
        "MasterSlave",
        new StateModelDefinition(generator.generateConfigForMasterSlave()));

    // add 3 participants: "localhost:{12918, 12919, 12920}"
    for (int i = 0; i < 3; i++) {
      int port = 12918 + i;
      InstanceConfig config = new InstanceConfig("localhost_" + port);
      config.setHostName("localhost");
      config.setPort(Integer.toString(port));
      config.setInstanceEnabled(true);
      admin.addInstance(clusterName, config);
    }

    // add resource "TestDB" which has 4 partitions and uses MasterSlave state model
    String resourceName = "TestDB";
    if (idealStateMode == IdealStateModeProperty.AUTO
        || idealStateMode == IdealStateModeProperty.AUTO_REBALANCE) {
      admin.addResource(clusterName, resourceName, 4, "MasterSlave", idealStateModeStr);

      // rebalance resource "TestDB" using 3 replicas
      admin.rebalance(clusterName, resourceName, 3);
    } else if (idealStateMode == IdealStateModeProperty.CUSTOMIZED) {
      admin.addIdealState(clusterName, resourceName, idealStateJsonFile);
    }

    // start helix controller
    new Thread(
            new Runnable() {

              @Override
              public void run() {
                try {
                  HelixControllerMain.main(
                      new String[] {"--zkSvr", zkAddr, "--cluster", clusterName});
                } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
              }
            })
        .start();

    // start 3 dummy participants
    for (int i = 0; i < 3; i++) {
      int port = 12918 + i;
      final String instanceName = "localhost_" + port;
      new Thread(
              new Runnable() {

                @Override
                public void run() {
                  DummyParticipant.main(new String[] {zkAddr, clusterName, instanceName});
                }
              })
          .start();
    }
  }
  @Test
  public void testAlertActionDisableNode() throws InterruptedException {
    ConfigScope scope = new ConfigScopeBuilder().forCluster(CLUSTER_NAME).build();
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("healthChange.enabled", "true");
    _setupTool.getClusterManagementTool().setConfig(scope, properties);

    String alertStr1 =
        "EXP(decay(1.0)(localhost_*.TestStat@DB=db1.TestMetric1))CMP(GREATER)CON(20)ACTION(DISABLE_INSTANCE)";
    String alertStr2 =
        "EXP(decay(1.0)(localhost_*.TestStat@DB=db1.TestMetric2))CMP(GREATER)CON(120)ACTION(DISABLE_INSTANCE)";
    String alertStr3 =
        "EXP(decay(1.0)(localhost_*.TestStat@DB=TestDB;Partition=*.TestMetric2))CMP(GREATER)CON(160)ACTION(DISABLE_PARTITION)";

    _setupTool.getClusterManagementTool().addAlert(CLUSTER_NAME, alertStr1);
    _setupTool.getClusterManagementTool().addAlert(CLUSTER_NAME, alertStr2);
    _setupTool.getClusterManagementTool().addAlert(CLUSTER_NAME, alertStr3);

    int[] metrics1 = {10, 15, 22, 12, 16};
    int[] metrics2 = {22, 115, 22, 163, 16};
    int[] metrics3 = {0, 0, 0, 0, 0};
    setHealthData(metrics1, metrics2);

    String controllerName = CONTROLLER_PREFIX + "_0";
    HelixManager manager = _startCMResultMap.get(controllerName)._manager;

    HealthStatsAggregationTask task =
        new HealthStatsAggregationTask(_startCMResultMap.get(controllerName)._manager);
    task.run();
    Thread.sleep(4000);
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    Builder keyBuilder = helixDataAccessor.keyBuilder();

    boolean result =
        ClusterStateVerifier.verifyByZkCallback(
            new BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);

    Builder kb = manager.getHelixDataAccessor().keyBuilder();
    ExternalView externalView =
        manager.getHelixDataAccessor().getProperty(kb.externalView("TestDB"));
    // Test the DISABLE_INSTANCE alerts
    String participant1 = "localhost_" + (START_PORT + 3);
    String participant2 = "localhost_" + (START_PORT + 2);
    ConfigAccessor configAccessor = manager.getConfigAccessor();
    scope =
        new ConfigScopeBuilder()
            .forCluster(manager.getClusterName())
            .forParticipant(participant1)
            .build();
    String isEnabled = configAccessor.get(scope, "HELIX_ENABLED");
    Assert.assertFalse(Boolean.parseBoolean(isEnabled));

    scope =
        new ConfigScopeBuilder()
            .forCluster(manager.getClusterName())
            .forParticipant(participant2)
            .build();
    isEnabled = configAccessor.get(scope, "HELIX_ENABLED");
    Assert.assertFalse(Boolean.parseBoolean(isEnabled));

    for (String partitionName : externalView.getRecord().getMapFields().keySet()) {
      for (String hostName : externalView.getRecord().getMapField(partitionName).keySet()) {
        if (hostName.equals(participant1) || hostName.equals(participant2)) {
          Assert.assertEquals(
              externalView.getRecord().getMapField(partitionName).get(hostName), "OFFLINE");
        }
      }
    }

    // enable the disabled instances
    setHealthData(metrics3, metrics3);
    task.run();
    Thread.sleep(1000);

    manager.getClusterManagmentTool().enableInstance(manager.getClusterName(), participant2, true);
    manager.getClusterManagmentTool().enableInstance(manager.getClusterName(), participant1, true);

    result =
        ClusterStateVerifier.verifyByZkCallback(
            new BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);

    // Test the DISABLE_PARTITION case
    int[] metrics4 = {22, 115, 22, 16, 163};
    setHealthData2(metrics4);
    task.run();

    scope =
        new ConfigScopeBuilder()
            .forCluster(manager.getClusterName())
            .forParticipant(participant1)
            .build();
    isEnabled = configAccessor.get(scope, "HELIX_ENABLED");
    Assert.assertTrue(Boolean.parseBoolean(isEnabled));

    scope =
        new ConfigScopeBuilder()
            .forCluster(manager.getClusterName())
            .forParticipant(participant2)
            .build();
    isEnabled = configAccessor.get(scope, "HELIX_ENABLED");
    Assert.assertTrue(Boolean.parseBoolean(isEnabled));

    result =
        ClusterStateVerifier.verifyByZkCallback(
            new BestPossAndExtViewZkVerifier(ZK_ADDR, CLUSTER_NAME));
    Assert.assertTrue(result);
    String participant3 = "localhost_" + (START_PORT + 4);
    externalView = manager.getHelixDataAccessor().getProperty(kb.externalView("TestDB"));
    Assert.assertTrue(
        externalView
            .getRecord()
            .getMapField("TestDB_3")
            .get(participant3)
            .equalsIgnoreCase("OFFLINE"));

    InstanceConfig nodeConfig =
        helixDataAccessor.getProperty(keyBuilder.instanceConfig(participant3));
    Assert.assertTrue(
        nodeConfig
            .getRecord()
            .getListField(InstanceConfigProperty.HELIX_DISABLED_PARTITION.toString())
            .contains("TestDB_3"));
  }