Package org.apache.helix

Examples of org.apache.helix.ZNRecord


  public static ZNRecord calculateIdealState(
      List<List<String>> instanceClusters,
      List<Integer> instanceClusterWeights, int partitions, int replicas,
      String dbName) throws Exception
  {
    ZNRecord result = new ZNRecord(dbName);

    int numberOfClusters = instanceClusters.size();
    List<List<String>> nodesInClusters = instanceClusters;
    List<Integer> clusterWeights = instanceClusterWeights;

    HashMap<String, Object> rushConfig = buildRushConfig(numberOfClusters,
        nodesInClusters, replicas + 1, clusterWeights);
    RUSHrHash rushHash = new RUSHrHash(rushConfig);

    Random r = new Random(0);
    for (int i = 0; i < partitions; i++)
    {
      int partitionId = i;
      String partitionName = dbName + ".partition-" + partitionId;

      ArrayList<HashMap> partitionAssignmentResult = rushHash
          .findNode(i);
      List<String> nodeNames = new ArrayList<String>();
      for (HashMap<?, ?> p : partitionAssignmentResult)
      {
        for (Object key : p.keySet())
        {
          if (p.get(key) instanceof String)
          {
            nodeNames.add(p.get(key).toString());
          }
        }
      }
      Map<String, String> partitionAssignment = new TreeMap<String, String>();

      for (int j = 0; j < nodeNames.size(); j++)
      {
        partitionAssignment.put(nodeNames.get(j), "SLAVE");
      }
      int master = r.nextInt(nodeNames.size());
      //master = nodeNames.size()/2;
      partitionAssignment.put(nodeNames.get(master), "MASTER");


      result.setMapField(partitionName, partitionAssignment);
    }
    result.setSimpleField(IdealStateProperty.NUM_PARTITIONS.toString(), String.valueOf(partitions));
    return result;
  }
View Full Code Here


      instanceNames.add("local"+i+"host_123" + i);
    }
    instanceCluster1.add(instanceNames);
    List<Integer> weights1 = new ArrayList<Integer>();
    weights1.add(1);
    ZNRecord result = IdealStateCalculatorByRush.calculateIdealState(
        instanceCluster1, weights1, partitions, replicas, dbName);

    printIdealStateStats(result);

    List<String> instanceNames2 = new ArrayList<String>();
    for (int i = 400; i < 405; i++)
    {
      instanceNames2.add("localhost_123" + i);
    }
    instanceCluster1.add(instanceNames2);
    weights1.add(1);
    ZNRecord result2 = IdealStateCalculatorByRush.calculateIdealState(
        instanceCluster1, weights1, partitions, replicas, dbName);

    printDiff(result, result2);
    printIdealStateStats(result2);
  }
View Full Code Here

    List<ZNRecord> partitions = new ArrayList<ZNRecord>();
    for (ZNRecord idealState : idealStateList)
    {
      for (String partitionName : idealState.getMapFields().keySet())
      {
        partitions.add(new ZNRecord(partitionName));
      }
    }
   
    List<ZNRecord> externalViewList = HelixProperty.convertToList(accessor.getChildValues(keyBuilder.externalViews()));
    // ExternalViews are stored in a map from resource name to idealState
    // ZNRecord
    Map<String, ZNRecord> externalViewMap = new HashMap<String, ZNRecord>();
    for (ZNRecord externalView : externalViewList)
    {
      externalViewMap.put(externalView.getId(), externalView);
    }
    // Map from instance name to a map from resource to current state ZNRecord
    Map<String, Map<String, ZNRecord>> currentStatesMap = new HashMap<String, Map<String, ZNRecord>>();
    // Map from instance name to a list of combined flat ZNRecordRow
    Map<String, List<ZNRecordRow>> flatCurrentStateMap = new HashMap<String, List<ZNRecordRow>>();

    for (ZNRecord instance : liveInstances)
    {
      String host = instance.getId();
      String sessionId = instance.getSimpleField(LiveInstanceProperty.SESSION_ID.toString());
      Map<String, ZNRecord> currentStates = new HashMap<String, ZNRecord>();
      List<ZNRecord> instanceCurrentStateList = new ArrayList<ZNRecord>();
      for (ZNRecord idealState : idealStateList)
      {
        String resourceName = idealState.getId();
       
        HelixProperty property = accessor.getProperty(keyBuilder.currentState(host, sessionId, resourceName));
        ZNRecord currentState =null;
        if (property == null)
        {
          _logger.warn("Resource " + resourceName + " has null currentState");
          currentState = new ZNRecord(resourceName);
        }else{
          currentState = property.getRecord();
        }
        currentStates.put(resourceName, currentState);
        instanceCurrentStateList.add(currentState);
View Full Code Here

    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));

    HelixProperty property = accessor.getProperty(propertyKey);
    ZNRecord record = property == null ? null : property.getRecord();
    return ZNRecordToJson(record);
  }
View Full Code Here

      IOException
  {
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));

    ZNRecord records = accessor.getProperty(propertyKey).getRecord();
    return ZNRecordToJson(records);
  }
View Full Code Here

      IOException
  {
    ZKHelixDataAccessor accessor =
        new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));

    ZNRecord record = EMPTY_ZNRECORD;
    HelixProperty property = accessor.getProperty(propertyKey);
    if (property != null)
    {
      record = property.getRecord();
    }
View Full Code Here

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

    ZNRecord liveInstance =
        accessor.getProperty(keyBuilder.liveInstance(instanceName)).getRecord();

    return liveInstance.getSimpleField(LiveInstanceProperty.SESSION_ID.toString());
  }
View Full Code Here

    for (int i = 0; i < 3; i++)
    {
      try
      {
        Stat stat = new Stat();
        ZNRecord record = zkClient.<ZNRecord> readDataAndStat(path, stat, true);

        if (isValueExpected(record, type, key, expect, diff))
        {
          if (arg._operation.compareTo("+") == 0)
          {
            if (record == null)
            {
              record = new ZNRecord("default");
            }
            ZnodeModValueType valueType = getValueType(arg._propertyType, arg._key);
            switch (valueType)
            {
            case SINGLE_VALUE:
View Full Code Here

    public void run()
    {
      boolean result = false;
      long now = System.currentTimeMillis();
      final long timeout = now + _command._trigger._timeout;
      ZNRecord diff = new ZNRecord("diff");
      try
      {
        if (now < _startTime)
        {
          Thread.sleep(_startTime - now);
        }

        do
        {
          if (_command._commandType == CommandType.MODIFY)
          {
            ZnodeOpArg arg = _command._znodeOpArg;
            final ZnodeValue expectValue = _command._trigger._expectValue;
            result = compareAndSetZnode(expectValue, arg, _zkClient, diff);
            // logger.error("result:" + result + ", " + _command);

            if (result == true)
            {
              _command._finishTimestamp = System.currentTimeMillis();
              _testResults.put(_command, true);

              break;
            }
            else
            {
              // logger.error("result:" + result + ", diff:" + diff);
            }
          }
          else if (_command._commandType == CommandType.VERIFY)
          {
            ZnodeOpArg arg = _command._znodeOpArg;
            final String znodePath = arg._znodePath;
            ZNRecord record = _zkClient.<ZNRecord> readData(znodePath, true);

            result = executeVerifier(record, _command, diff);
            // logger.error("result:" + result + ", " + _command.toString());
            if (result == true)
            {
View Full Code Here

  {
    ZkClient client = new ZkClient(zkServer);
    client.setZkSerializer(new ZNRecordSerializer());
    String path = HelixUtil.getMessagePath(clusterName, instanceName) + "/" + message.getId();
    client.delete(path);
    ZNRecord record = client.readData(HelixUtil.getLiveInstancePath(clusterName, instanceName));
    message.setTgtSessionId(record.getSimpleField(LiveInstanceProperty.SESSION_ID.toString()).toString());
    message.setTgtName(record.getId());
    //System.out.println(message);
    client.createPersistent(path, message.getRecord());
  }
View Full Code Here

TOP

Related Classes of org.apache.helix.ZNRecord

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.