Package com.linkedin.helix

Examples of com.linkedin.helix.ZNRecord


        else
        {
          CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
          if (record.getDeltaList().size() > 0)
          {
            ZNRecord value = new ZNRecord(record.getId());
            value.merge(record);
            client.create(path, value, mode);
          }
          else
          {
            client.create(path, record, mode);
View Full Code Here


    {
      if (client.exists(path))
      {
        if (mergeOnUpdate)
        {
          ZNRecord curRecord = client.readData(path);
          if (curRecord != null)
          {
            curRecord.merge(record);
            client.asyncSetData(path, curRecord, -1, null);
          }
          else
          {
            client.asyncSetData(path, record, -1, null);
          }
        }
        else
        {
          client.asyncSetData(path, record, -1, null);
        }
      }
      else
      {
        CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
        if (record.getDeltaList().size() > 0)
        {
          ZNRecord newRecord = new ZNRecord(record.getId());
          newRecord.merge(record);
          client.create(path, null, mode);

          client.asyncSetData(path, newRecord, -1, null);
        }
        else
View Full Code Here

    // for (int i = 0; i < 1; i++) //change 1 back to 5
    // {
    // String instance = "localhost_" + (12918 + i);
    // String instance = "localhost_12918";
    ZNRecord record = accessor.getProperty(keyBuilder.alertStatus()).getRecord();
    Map<String, Map<String, String>> recMap = record.getMapFields();
    Set<String> keySet = recMap.keySet();
    Map<String, String> alertStatusMap = recMap.get(_alertStatusStr);
    String val = alertStatusMap.get(AlertValueAndStatus.VALUE_NAME);
    boolean fired = Boolean.parseBoolean(alertStatusMap.get(AlertValueAndStatus.FIRED_NAME));
    Assert.assertEquals(Double.parseDouble(val), Double.parseDouble("75.0"));
View Full Code Here

    return lastCSUpdateLine;
  }

  static ZNRecord getZNRecord(String line)
  {
    ZNRecord record = null;
    String value = getAttributeValue(line, "data:");
    if (value != null)
    {
      record = (ZNRecord) _deserializer.deserialize(value.getBytes());
      // if (record == null)
View Full Code Here

            System.out.println("START cluster. SETTING lastTestStartTimestamp to "
                + new Timestamp(timestampVal) + "\nline:" + inputLine);
            lastTestStartTimestamp = timestampVal;
          }

          ZNRecord record = getZNRecord(inputLine);
          LiveInstance liveInstance = new LiveInstance(record);
          String session = getAttributeValue(inputLine, "session:");
          sessionMap.put(session, inputLine);
          System.out.println(new Timestamp(Long.parseLong(timestamp)) + ": create LIVEINSTANCE "
              + liveInstance.getInstanceName());
        }
        else if (inputLine.indexOf("closeSession") != -1)
        {
          // kill any instance
          String session = getAttributeValue(inputLine, "session:");
          if (sessionMap.containsKey(session))
          {
            if (timestampVal < lastTestStartTimestamp)
            {
              System.out.println("KILL node. SETTING lastTestStartTimestamp to " + timestampVal
                  + " line:" + inputLine);
              lastTestStartTimestamp = timestampVal;
            }
            String line = sessionMap.get(session);
            ZNRecord record = getZNRecord(line);
            LiveInstance liveInstance = new LiveInstance(record);

            System.out.println(new Timestamp(Long.parseLong(timestamp)) + ": close session "
                + liveInstance.getInstanceName());
            dump = true;
          }
        }
        else if (inputLine.indexOf("/" + clusterName + "/CONFIGS/PARTICIPANT") != -1)
        {
          // disable a partition
          String type = getAttributeValue(inputLine, "type:");
          if (type.equals("setData") && inputLine.indexOf("HELIX_DISABLED_PARTITION") != -1)
          {
            if (timestampVal < lastTestStartTimestamp)
            {
              System.out.println("DISABLE partition. SETTING lastTestStartTimestamp to " + timestampVal
                  + " line:" + inputLine);
              lastTestStartTimestamp = timestampVal;
            }
          }
        } else if (inputLine.indexOf("/" + clusterName + "/CONTROLLER/LEADER") != -1)
        {
          // leaderLine = inputLine;
          ZNRecord record = getZNRecord(inputLine);
          LiveInstance liveInstance = new LiveInstance(record);
          String session = getAttributeValue(inputLine, "session:");
          leaderSession = session;
          controllerStartTime = Long.parseLong(getAttributeValue(inputLine, "time:"));
          sessionMap.put(session, inputLine);
          System.out.println(new Timestamp(Long.parseLong(timestamp)) + ": create LEADER "
              + liveInstance.getInstanceName());
        }
        else if (inputLine.indexOf("/" + clusterName + "/") != -1
            && inputLine.indexOf("/CURRENTSTATES/") != -1)
        {
          String type = getAttributeValue(inputLine, "type:");
          if (type.equals("create"))
          {
            stats.curStateCreateCount++;
          }
          else if (type.equals("setData"))
          {
            String path = getAttributeValue(inputLine, "path:");
            csUpdateLines.add(inputLine);
            stats.curStateUpdateCount++;
            // getAttributeValue(line, "data");
            System.out.println("Update currentstate:"
                + new Timestamp(Long.parseLong(timestamp)) + ":" + timestamp + " path:"
                + path);
          }
        }
        else if (inputLine.indexOf("/" + clusterName + "/EXTERNALVIEW/") != -1)
        {
          String session = getAttributeValue(inputLine, "session:");
          if (session.equals(leaderSession))
          {
            String type = getAttributeValue(inputLine, "type:");
            if (type.equals("create"))
            {
              stats.extViewCreateCount++;
            }
            else if (type.equals("setData"))
            {
              stats.extViewUpdateCount++;
            }
          }

          // pos = inputLine.indexOf("EXTERNALVIEW");
          // pos = inputLine.indexOf("data:{", pos);
          // if (pos != -1)
          // {
          // String timestamp = getAttributeValue(inputLine, "time:");
          // ZNRecord record =
          // (ZNRecord) _deserializer.deserialize(inputLine.substring(pos + 5)
          // .getBytes());
          // ExternalView extView = new ExternalView(record);
          // int masterCnt = ClusterStateVerifier.countStateNbInExtView(extView,
          // "MASTER");
          // int slaveCnt = ClusterStateVerifier.countStateNbInExtView(extView, "SLAVE");
          // if (masterCnt == 1200)
          // {
          // System.out.println(timestamp + ": externalView " + extView.getResourceName()
          // + " has " + masterCnt + " MASTER, " + slaveCnt + " SLAVE");
          // }
          // }
        }
        else if (inputLine.indexOf("/" + clusterName + "/") != -1
            && inputLine.indexOf("/MESSAGES/") != -1)
        {
          String type = getAttributeValue(inputLine, "type:");

          if (type.equals("create"))
          {
            ZNRecord record = getZNRecord(inputLine);
            Message msg = new Message(record);
            String sendSession = getAttributeValue(inputLine, "session:");
            if (sendSession.equals(leaderSession)
                && msg.getMsgType().equals("STATE_TRANSITION")
                && msg.getMsgState() == MessageState.NEW)
View Full Code Here

  private void addNodeToCluster(String clusterName, String host, int port)
  {
    // TODO use ClusterSetup
    String nodeId = host + "_" + port;
    ZNRecord nodeConfig = new ZNRecord(nodeId);
    nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_HOST.toString(), host);
    nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_PORT.toString(),
                              Integer.toString(port));
    nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString(),
                              Boolean.toString(true));
    _mgmtTool.addInstance(CLUSTER_NAME, new InstanceConfig(nodeConfig));
  }
View Full Code Here

    IdealState idealState = _mgmtTool.getResourceIdealState(clusterName, resourceName);
    idealState.setReplicas(Integer.toString(replica));
    int partitions = idealState.getNumPartitions();

    ZNRecord newIdealState =
        IdealStateCalculatorForStorageNode.calculateIdealState(nodeNames,
                                                               partitions,
                                                               replica - 1,
                                                               resourceName,
                                                               "MASTER",
                                                               "SLAVE");

    newIdealState.merge(idealState.getRecord());
    _mgmtTool.setResourceIdealState(clusterName,
                                    resourceName,
                                    new IdealState(newIdealState));
  }
View Full Code Here

    HelixDataAccessor accessor = manager.getHelixDataAccessor();

    List<String> instancesInCluster = manager.getClusterManagmentTool().getInstancesInCluster(manager.getClusterName());
    for(String instance : instancesInCluster)
    {
      ZNRecord record = new ZNRecord("scnTable");
      record.setSimpleField("k1", "v1");
      Builder kb = accessor.keyBuilder();
      accessor.setProperty(kb.healthReport(instance, "scnTable"), new HealthStat(record));
    }

    String path = "INSTANCES/*/HEALTHREPORT/scnTable";
 
View Full Code Here

    System.out.println(instances.size());

    int seq = 50;
    for(String instance : instances)
    {
      ZNRecord scnRecord = new ZNRecord("scnTable");
      scnRecord.setSimpleField("instance", instance);
      for(int i = 0; i < _PARTITIONS; i++)
      {
        Map<String, String> scnDetails = new HashMap<String, String>();

        String partition = TEST_DB + "_" + i;
        List<String> idealStatePrefList =
            resourceIdealState.getPreferenceList(partition);
        String idealStateMaster = idealStatePrefList.get(0);

        scnDetails.put("gen", "4");

        if (instance.equals(idealStateMaster))
        {
          scnDetails.put("seq", "" + (seq - 25));
        }
        else
        {
          scnDetails.put("seq", "" + seq++);
        }
        scnRecord.setMapField(partition, scnDetails);
      }

      Builder kb = accessor.keyBuilder();
      accessor.setProperty(kb.healthReport(instance, "scnTable"), new HealthStat(scnRecord));
    }
View Full Code Here

      LOG.error("Input object must be of type ZNRecord but it is " + data + ". Will not write to zk");
      throw new HelixException("Input object is not of type ZNRecord (was " + data + ")");
    }

    // apply retention policy on list field
    ZNRecord record = (ZNRecord) data;
    int max = getListFieldBound(record);
    if (max < Integer.MAX_VALUE)
    {
      Map<String, List<String>> listMap = record.getListFields();
      for (String key : listMap.keySet())
      {
        List<String> list = listMap.get(key);
        if (list.size() > max)
        {
          listMap.put(key, list.subList(0, max));
        }
      }
    }
   
    StringWriter sw = new StringWriter();
    try
    {
      JsonFactory f = new JsonFactory();
      JsonGenerator g = f.createJsonGenerator(sw);

      g.writeStartObject();

      // write id field
      g.writeRaw("\n  ");
      g.writeStringField("id", record.getId());

      // write simepleFields
      g.writeRaw("\n  ");
      g.writeObjectFieldStart("simpleFields");
      for (String key : record.getSimpleFields().keySet())
      {
        g.writeRaw("\n    ");
        g.writeStringField(key, record.getSimpleField(key));
      }
      g.writeRaw("\n  ");
      g.writeEndObject(); // for simpleFields

      // write listFields
      g.writeRaw("\n  ");
      g.writeObjectFieldStart("listFields");
      for (String key : record.getListFields().keySet())
      {
        // g.writeStringField(key, record.getListField(key).toString());

        // g.writeObjectFieldStart(key);
        g.writeRaw("\n    ");
        g.writeArrayFieldStart(key);
        List<String> list = record.getListField(key);
        for (String listValue : list)
        {
          g.writeString(listValue);
        }
        // g.writeEndObject();
        g.writeEndArray();

      }
      g.writeRaw("\n  ");
      g.writeEndObject(); // for listFields

      // write mapFields
      g.writeRaw("\n  ");
      g.writeObjectFieldStart("mapFields");
      for (String key : record.getMapFields().keySet())
      {
        // g.writeStringField(key, record.getMapField(key).toString());
        g.writeRaw("\n    ");
        g.writeObjectFieldStart(key);
        Map<String, String> map = record.getMapField(key);
        for (String mapKey : map.keySet())
        {
          g.writeRaw("\n      ");
          g.writeStringField(mapKey, map.get(mapKey));
        }
        g.writeRaw("\n    ");
        g.writeEndObject();

      }
      g.writeRaw("\n  ");
      g.writeEndObject(); // for mapFields

      g.writeRaw("\n");
      g.writeEndObject(); // for whole znrecord

      // important: will force flushing of output, close underlying output
      // stream
      g.close();
    }
    catch (Exception e)
    {
      LOG.error("Exception during data serialization. Will not write to zk. Data (first 1k): "
          + sw.toString().substring(0, 1024), e);
      throw new HelixException(e);
    }
   
    // check size
    if (sw.toString().getBytes().length > ZNRecord.SIZE_LIMIT)
    {
      LOG.error("Data size larger than 1M, ZNRecord.id: " + record.getId()
          + ". Will not write to zk. Data (first 1k): " + sw.toString().substring(0, 1024));
      throw new HelixException("Data size larger than 1M, ZNRecord.id: " + record.getId());
    }
   
    return sw.toString().getBytes();
  }
View Full Code Here

TOP

Related Classes of com.linkedin.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.