Package com.linkedin.helix.model

Examples of com.linkedin.helix.model.IdealState


      int replicas = 1;
      String resourceName = resources[i];
      ZNRecord record = IdealStateCalculatorForStorageNode
          .calculateIdealState(instances, partitions, replicas,
              resourceName, "MASTER", "SLAVE");
      IdealState idealState = new IdealState(record);
      idealState.setStateModelDefRef("MasterSlave");
     
      HelixDataAccessor accessor = manager.getHelixDataAccessor();
      Builder keyBuilder = accessor.keyBuilder();
      accessor.setProperty(keyBuilder.idealStates(resourceName),
                                            idealState);


      idealStates.add(idealState);
    }
    // ADD A LIVE INSTANCE WITH A CURRENT STATE THAT CONTAINS RESOURCE WHICH NO
    // LONGER EXISTS IN IDEALSTATE
    String instanceName = "localhost_" + 3;
    LiveInstance liveInstance = new LiveInstance(instanceName);
    String sessionId = UUID.randomUUID().toString();
    liveInstance.setSessionId(sessionId);
   
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.liveInstance(instanceName),
                                          liveInstance);

    String oldResource = "testResourceOld";
    CurrentState currentState = new CurrentState(oldResource);
    currentState.setState("testResourceOld_0", "OFFLINE");
    currentState.setState("testResourceOld_1", "SLAVE");
    currentState.setState("testResourceOld_2", "MASTER");
    currentState.setStateModelDefRef("MasterSlave");
    accessor.setProperty(keyBuilder.currentState(instanceName, sessionId, oldResource),
                                          currentState);

    ResourceComputationStage stage = new ResourceComputationStage();
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);

    Map<String, Resource> resourceMap = event
        .getAttribute(AttributeName.RESOURCES.toString());
    // +1 because it will have one for current state
    AssertJUnit.assertEquals(resources.length + 1, resourceMap.size());

    for (int i = 0; i < resources.length; i++)
    {
      String resourceName = resources[i];
      IdealState idealState = idealStates.get(i);
      AssertJUnit.assertTrue(resourceMap.containsKey(resourceName));
      AssertJUnit.assertEquals(resourceMap.get(resourceName)
          .getResourceName(), resourceName);
      AssertJUnit.assertEquals(resourceMap.get(resourceName)
          .getStateModelDefRef(), idealState.getStateModelDefRef());
      AssertJUnit.assertEquals(resourceMap.get(resourceName)
          .getPartitions().size(), idealState.getNumPartitions());
    }
    // Test the data derived from CurrentState
    AssertJUnit.assertTrue(resourceMap.containsKey(oldResource));
    AssertJUnit.assertEquals(resourceMap.get(oldResource)
        .getResourceName(), oldResource);
View Full Code Here


   
    // rename partition name TestDB0_0 tp TestDB0_100
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();

    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
   
    List<String> prioList = idealState.getRecord().getListFields().remove("TestDB0_0");
    idealState.getRecord().getListFields().put("TestDB0_100", prioList);
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);

    boolean result = ClusterStateVerifier.verifyByPolling(
        new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
View Full Code Here

    // calculate idealState
    List<String> instanceNames = Arrays.asList("localhost_12918", "localhost_12919", "localhost_12920",
        "localhost_12921", "localhost_12922");
    ZNRecord destIS = IdealStateCalculatorForStorageNode.calculateIdealState(instanceNames,
        10, 3-1, "TestDB0", "MASTER", "SLAVE");
    IdealState idealState = new IdealState(destIS);
    idealState.setIdealStateMode("CUSTOMIZED");
    idealState.setReplicas("3");
    idealState.setStateModelDefRef("MasterSlave");
   
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
    Builder keyBuilder = accessor.keyBuilder();

    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);

    startAndVerify(clusterName);
   
    Map<String, String> stateMap = idealState.getRecord().getMapFields().remove("TestDB0_0");
    idealState.getRecord().getMapFields().put("TestDB0_100", stateMap);
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);

    boolean result = ClusterStateVerifier.verifyByPolling(
        new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
View Full Code Here

   
    ZkBaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<ZNRecord>(_gZkClient);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
    // String idealStatePath = PropertyPathConfig.getPath(PropertyType.IDEALSTATES, clusterName, "TestDB0");
    Builder keyBuilder = accessor.keyBuilder();
    IdealState idealState = accessor.getProperty(keyBuilder.idealStates("TestDB0"));
    idealState.setBucketSize(1);
    accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);

    TestHelper.startController(clusterName,
                               "controller_0",
                               ZK_ADDR,
View Full Code Here

  {
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(zkClient));
    Builder keyBuilder = accessor.keyBuilder();

    IdealState idealState = accessor.getProperty(keyBuilder.idealStates(resource));
    for (String partitionName : idealState.getPartitionSet())
    {
      if (idealState.getIdealStateMode() == IdealStateModeProperty.AUTO)
      {
        AssertJUnit.assertEquals(repl, idealState.getPreferenceList(partitionName).size());
      }
      else if (idealState.getIdealStateMode() == IdealStateModeProperty.CUSTOMIZED)
      {
        AssertJUnit.assertEquals(repl, idealState.getInstanceStateMap(partitionName)
                                                 .size());
      }
    }
  }
View Full Code Here

      instances.add("localhost_" + i);
    }

    for (String resourceName : resources)
    {
      IdealState idealState = new IdealState(resourceName);
      for (int p = 0; p < partitions; p++)
      {
        List<String> value = new ArrayList<String>();
        for (int r = 0; r < replicas; r++)
        {
          int n = nodes[(p + r) % nodes.length];
          value.add("localhost_" + n);
        }
        idealState.getRecord().setListField(resourceName + "_" + p, value);
      }

      idealState.setReplicas(Integer.toString(replicas));
      idealState.setStateModelDefRef("MasterSlave");
      idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
      idealState.setNumPartitions(partitions);
      idealStates.add(idealState);

      // System.out.println(idealState);
      accessor.setProperty(keyBuilder.idealStates(resourceName), idealState);
    }
View Full Code Here

    // logger.warn("Skip the operation. DB ideal state directory exists:"
    // + dbIdealStatePath);
    // return;
    // }

    IdealState idealState = new IdealState(resource);
    idealState.setNumPartitions(numResources);
    idealState.setStateModelDefRef(stateModelRef);
    idealState.setReplicas(Integer.toString(0));
    idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
    try
    {
      _store.setProperty(resourceIdealStatePath, idealState.getRecord());
    }
    catch (PropertyStoreException e)
    {
      logger.error("Fail to add resource, cluster:" + clusterName + " resourceName:"
          + resource, e);
View Full Code Here

    {
      String storageNodeName = PARTICIPANT_PREFIX + ":" + (START_PORT + i);
      _setupTool.addInstanceToCluster(CLUSTER_NAME, storageNodeName);
    }
    _setupTool.rebalanceStorageCluster(CLUSTER_NAME, TEST_DB, 3);
    IdealState idealState = _setupTool.getClusterManagementTool().getResourceIdealState(CLUSTER_NAME, TEST_DB);
    idealState.getRecord().setSimpleField(IdealStateProperty.REBALANCE_TIMER_PERIOD.toString(), "500");

    String scnTableQuery = "SELECT T1.instance as instance, T1.mapField as partition, T1.gen as gen, T1.seq as seq " +
        "FROM explodeMap(`INSTANCES/*/HEALTHREPORT/scnTable`) AS T1" +
        " JOIN LIVEINSTANCES as T2 using (T1.instance, T2.id)";

View Full Code Here

    // oversized data should not create any new data on zk
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(className, new ZkBaseDataAccessor(zkClient));
    Builder keyBuilder = accessor.keyBuilder();

    IdealState idealState = new IdealState("currentState");
    idealState.setStateModelDefRef("MasterSlave");
    idealState.setIdealStateMode("AUTO");
    idealState.setNumPartitions(10);

    for (int i = 0; i < 1024; i++)
    {
      idealState.getRecord().setSimpleField(i + "", bufStr);
    }
    boolean succeed = accessor.setProperty(keyBuilder.idealStates("TestDB0"), idealState);
    Assert.assertFalse(succeed);
    HelixProperty property =
        accessor.getProperty(keyBuilder.stateTransitionStatus("localhost_12918",
                                                              "session_1",
                                                              "partition_1"));
    Assert.assertNull(property);

    // legal sized data gets written to zk
    idealState.getRecord().getSimpleFields().clear();
    idealState.setStateModelDefRef("MasterSlave");
    idealState.setIdealStateMode("AUTO");
    idealState.setNumPartitions(10);

    for (int i = 0; i < 900; i++)
    {
      idealState.getRecord().setSimpleField(i + "", bufStr);
    }
    succeed = accessor.setProperty(keyBuilder.idealStates("TestDB1"), idealState);
    Assert.assertTrue(succeed);
    record =
        accessor.getProperty(keyBuilder.idealStates("TestDB1")).getRecord();
    Assert.assertTrue(serializer.serialize(record).length > 900 * 1024);

    // oversized data should not update existing data on zk
    idealState.getRecord().getSimpleFields().clear();
    idealState.setStateModelDefRef("MasterSlave");
    idealState.setIdealStateMode("AUTO");
    idealState.setNumPartitions(10);
    for (int i = 900; i < 1024; i++)
    {
      idealState.getRecord().setSimpleField(i + "", bufStr);
    }
    // System.out.println("record: " + idealState.getRecord());
    succeed =
        accessor.updateProperty(keyBuilder.idealStates("TestDB1"), idealState);
    Assert.assertFalse(succeed);
View Full Code Here

    String controllerName = CONTROLLER_PREFIX + "_0";
    StartCMResult startResult =
    _startCMResultMap.get(controllerName);
    HelixDataAccessor accessor = startResult._manager.getHelixDataAccessor();

    IdealState idealState = _setupTool.getClusterManagementTool().getResourceIdealState(CLUSTER_NAME, TEST_DB);
    Map<String, ZNRecord> scnTableMap = new HashMap<String, ZNRecord>();
    for (int i = 0; i < NODE_NR; i++)
    {
      String instance = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
      ZNRecord scnRecord = new ZNRecord("scnTable");
      scnRecord.setSimpleField("instance", instance);
      scnTableMap.put(instance, scnRecord);
    }
    String instanceDead = PARTICIPANT_PREFIX + "_" + (START_PORT + 0);
    for(int j = 0; j < _PARTITIONS; j++)
    {
      int seq = 50;
      String partition = TEST_DB + "_" + j;
      List<String> idealStatePrefList =
          idealState.getPreferenceList(partition);
      String idealStateMaster = idealStatePrefList.get(0);
      // Switch the scn order of the partitions mastered on instanceDead
      if(idealStateMaster.equals(instanceDead))
      {
        for(int x = 0; x < idealStatePrefList.size(); x++)
        {
          String instance = idealStatePrefList.get(x);
          ZNRecord scnRecord = scnTableMap.get(instance);
          if(!scnRecord.getMapFields().containsKey(partition))
          {
            scnRecord.setMapField(partition, new HashMap<String, String>());
          }
          Map<String, String> scnDetails = scnRecord.getMapField(partition);
          scnDetails.put("gen", "4");
          if(x > 0)
          {
            scnDetails.put("seq", "" + (seq - 22 + 11 *(x)));
          }
          else
          {
            scnDetails.put("seq", "100");
          }
        }
      }
    }

    for(String instanceName : scnTableMap.keySet())
    {
      Builder kb = accessor.keyBuilder();
      accessor.setProperty(kb.healthReport(instanceName, "scnTable"), new HealthStat(scnTableMap.get(instanceName)));
    }

    // kill a node, after a while the master should be the last one in the ideal state pref list
   
    _startCMResultMap.get(instanceDead)._manager.disconnect();
    _startCMResultMap.get(instanceDead)._thread.interrupt();
   
    Thread.sleep(1000);
   
    boolean verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
                                                                              CLUSTER_NAME));
    Assert.assertTrue(verifyResult);
    Builder kb = accessor.keyBuilder();
    ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB));
    for(String partitionName : idealState.getPartitionSet())
    {
      List<String> prefList = idealState.getPreferenceList(partitionName);
      if(prefList.get(0).equals(instanceDead))
      {
        String last = prefList.get(prefList.size() - 1);
        Assert.assertTrue(ev.getStateMap(partitionName).get(last).equals("MASTER"));
      }
    }
   
    // Bring up the previous dead node, but as the SCN is the last for all the
    // master partitions on it, the master partitions should be still on the last if the prefList
    StartCMResult result =
        TestHelper.startDummyProcess(ZK_ADDR, CLUSTER_NAME, instanceDead);
    _startCMResultMap.put(instanceDead, result);

    Thread.sleep(1000);
    verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
                                                                              CLUSTER_NAME));
    Assert.assertTrue(verifyResult);
    for(String partitionName : idealState.getPartitionSet())
    {
      List<String> prefList = idealState.getPreferenceList(partitionName);
      if(prefList.get(0).equals(instanceDead))
      {
        String last = prefList.get(prefList.size() - 1);
        Assert.assertTrue(ev.getStateMap(partitionName).get(last).equals("MASTER"));
      }
    }
    // Reset the scn of the partitions
    for(int j = 0; j < _PARTITIONS; j++)
    {
      String partition = TEST_DB + "_" + j;
      List<String> idealStatePrefList =
          idealState.getPreferenceList(partition);
      String idealStateMaster = idealStatePrefList.get(0);
      // Switch back the scn to the same
      if(idealStateMaster.equals(instanceDead))
      {
        for(int x = 0; x < idealStatePrefList.size(); x++)
        {
          String instance = idealStatePrefList.get(x);
          ZNRecord scnRecord = scnTableMap.get(instance);
          if(!scnRecord.getMapFields().containsKey(partition))
          {
            scnRecord.setMapField(partition, new HashMap<String, String>());
          }
          Map<String, String> scnDetails = scnRecord.getMapField(partition);
          scnDetails.put("gen", "4");
          scnDetails.put("seq", "100");
        }
      }
    }
    // set the scn to normal -- same order as the priority list
    for(String instanceName : scnTableMap.keySet())
    {
      kb = accessor.keyBuilder();
      accessor.setProperty(kb.healthReport(instanceName, "scnTable"), new HealthStat(scnTableMap.get(instanceName)));
    }
    Thread.sleep(1000);
    verifyResult =
        ClusterStateVerifier.verifyByZkCallback(new MasterNbInExtViewVerifier(ZK_ADDR,
                                                                              CLUSTER_NAME));
    Assert.assertTrue(verifyResult);
    // should be reverted to normal
    ev = accessor.getProperty(kb.externalView(TEST_DB));
    for(String partitionName : idealState.getPartitionSet())
    {
      List<String> prefList = idealState.getPreferenceList(partitionName);
      if(prefList.get(0).equals(instanceDead))
      {
        String last = prefList.get(prefList.size() - 1);
        Assert.assertTrue(ev.getStateMap(partitionName).get(last).equals("SLAVE"));
        Assert.assertTrue(ev.getStateMap(partitionName).get(prefList.get(1)).equals("SLAVE"));
View Full Code Here

TOP

Related Classes of com.linkedin.helix.model.IdealState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.