Package org.apache.helix

Examples of org.apache.helix.ZNRecord


        }
        else
        {
          _baseDataAccessor.remove(path, options);

          ZNRecord metaRecord = new ZNRecord(value.getId());
          metaRecord.setSimpleFields(value.getRecord().getSimpleFields());
          records.add(metaRecord);

          ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(value.getBucketSize());

          Map<String, ZNRecord> map = bucketizer.bucketize(value.getRecord());
View Full Code Here


    {
      currentStates.put(nodeNames[i], new ArrayList<ZNRecord>());
      currentStates2.put(nodeNames[i], new TreeMap<String, ZNRecord>());
      for (int j = 0; j < dbNames.length; j++)
      {
        ZNRecord dbPartitionState = new ZNRecord(dbNames[j]);
        currentStates2.get(nodeNames[i]).put(dbNames[j], dbPartitionState);
      }
    }

    Random rand = new Random(1234);
View Full Code Here

    ExternalViewGenerator provider = new ExternalViewGenerator();

    List<ZNRecord> mockIdealStates = new ArrayList<ZNRecord>();
    for (String dbName : dbNames)
    {
      ZNRecord rec = new ZNRecord(dbName);
      mockIdealStates.add(rec);
    }
    List<ZNRecord> externalView = provider.computeExternalView(currentStates,
        mockIdealStates);
View Full Code Here

      // null is NOT an instance of any class
      logger.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 + ")");
    }

    ZNRecord record = (ZNRecord) data;
   
    // apply retention policy
    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));
        }
      }
    }

    // do serialization
    ObjectMapper mapper = new ObjectMapper();
    SerializationConfig serializationConfig = mapper.getSerializationConfig();
    serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true);
    serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true);
    serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
    StringWriter sw = new StringWriter();
    try
    {
      mapper.writeValue(sw, data);
    } catch (Exception e)
    {
      logger.error("Exception during data serialization. Will not write to zk. Data (first 1k): "
          + sw.toString().substring(0, 1024), e);
      throw new HelixException(e);
    }
   
    if (sw.toString().getBytes().length > ZNRecord.SIZE_LIMIT)
    {
      logger.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

    deserializationConfig.set(DeserializationConfig.Feature.AUTO_DETECT_FIELDS, true);
    deserializationConfig.set(DeserializationConfig.Feature.AUTO_DETECT_SETTERS, true);
    deserializationConfig.set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    try
    {
      ZNRecord zn = mapper.readValue(bais, ZNRecord.class);
      return zn;
    } catch (Exception e)
    {
      logger.error("Exception during deserialization of bytes: " + new String(bytes), e);
      return null;
View Full Code Here

    // System.out.println(new String(serializer.serialize(record)));
  }

  public ZNRecord generateConfigForLeaderStandby()
  {
    ZNRecord record = new ZNRecord("LeaderStandby");
    record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
                          "OFFLINE");
    List<String> statePriorityList = new ArrayList<String>();
    statePriorityList.add("LEADER");
    statePriorityList.add("STANDBY");
    statePriorityList.add("OFFLINE");
    statePriorityList.add("DROPPED");
    record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
                        statePriorityList);
    for (String state : statePriorityList)
    {
      String key = state + ".meta";
      Map<String, String> metadata = new HashMap<String, String>();
      if (state.equals("LEADER"))
      {
        metadata.put("count", "1");
        record.setMapField(key, metadata);
      }
      if (state.equals("STANDBY"))
      {
        metadata.put("count", "R");
        record.setMapField(key, metadata);
      }
      if (state.equals("OFFLINE"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
      if (state.equals("DROPPED"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }

    }

    for (String state : statePriorityList)
    {
      String key = state + ".next";
      if (state.equals("LEADER"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("STANDBY", "STANDBY");
        metadata.put("OFFLINE", "STANDBY");
        metadata.put("DROPPED", "STANDBY");
        record.setMapField(key, metadata);
      }
      if (state.equals("STANDBY"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("LEADER", "LEADER");
        metadata.put("OFFLINE", "OFFLINE");
        metadata.put("DROPPED", "OFFLINE");
        record.setMapField(key, metadata);
      }
      if (state.equals("OFFLINE"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("STANDBY", "STANDBY");
        metadata.put("LEADER", "STANDBY");
        metadata.put("DROPPED", "DROPPED");
        record.setMapField(key, metadata);
      }

    }
    List<String> stateTransitionPriorityList = new ArrayList<String>();
    stateTransitionPriorityList.add("LEADER-STANDBY");
    stateTransitionPriorityList.add("STANDBY-LEADER");
    stateTransitionPriorityList.add("OFFLINE-STANDBY");
    stateTransitionPriorityList.add("STANDBY-OFFLINE");
    stateTransitionPriorityList.add("OFFLINE-DROPPED");

    record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
                        stateTransitionPriorityList);
    return record;
    // ZNRecordSerializer serializer = new ZNRecordSerializer();
    // System.out.println(new String(serializer.serialize(record)));
  }
View Full Code Here

    // System.out.println(new String(serializer.serialize(record)));
  }

  public ZNRecord generateConfigForOnlineOffline()
  {
    ZNRecord record = new ZNRecord("OnlineOffline");
    record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
                          "OFFLINE");
    List<String> statePriorityList = new ArrayList<String>();
    statePriorityList.add("ONLINE");
    statePriorityList.add("OFFLINE");
    statePriorityList.add("DROPPED");
    record.setListField(StateModelDefinitionProperty.STATE_PRIORITY_LIST.toString(),
                        statePriorityList);
    for (String state : statePriorityList)
    {
      String key = state + ".meta";
      Map<String, String> metadata = new HashMap<String, String>();
      if (state.equals("ONLINE"))
      {
        metadata.put("count", "R");
        record.setMapField(key, metadata);
      }
      if (state.equals("OFFLINE"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
      if (state.equals("DROPPED"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
    }

    for (String state : statePriorityList)
    {
      String key = state + ".next";
      if (state.equals("ONLINE"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("OFFLINE", "OFFLINE");
        metadata.put("DROPPED", "OFFLINE");
        record.setMapField(key, metadata);
      }
      if (state.equals("OFFLINE"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("ONLINE", "ONLINE");
        metadata.put("DROPPED", "DROPPED");
        record.setMapField(key, metadata);
      }
    }
    List<String> stateTransitionPriorityList = new ArrayList<String>();
    stateTransitionPriorityList.add("OFFLINE-ONLINE");
    stateTransitionPriorityList.add("ONLINE-OFFLINE");
    stateTransitionPriorityList.add("OFFLINE-DROPPED");

    record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
                        stateTransitionPriorityList);
    return record;
    // ZNRecordSerializer serializer = new ZNRecordSerializer();
    // System.out.println(new String(serializer.serialize(record)));
  }
View Full Code Here

{

  @Test
  public void testEquals()
  {
    ZNRecord record1 = new ZNRecord("id");
    record1.setSimpleField("k1", "v1");
    record1.setMapField("m1", new HashMap<String, String>());
    record1.getMapField("m1").put("k1", "v1");
    record1.setListField("l1", new ArrayList<String>());
    record1.getListField("l1").add("v1");
    ZNRecord record2 = new ZNRecord("id");
    record2.setSimpleField("k1", "v1");
    record2.setMapField("m1", new HashMap<String, String>());
    record2.getMapField("m1").put("k1", "v1");
    record2.setListField("l1", new ArrayList<String>());
    record2.getListField("l1").add("v1");

    AssertJUnit.assertEquals(record1, record2);
    record2.setSimpleField("k2", "v1");
    AssertJUnit.assertNotSame(record1, record2);
  }
View Full Code Here

  }

  @Test
  public void testMerge()
  {
    ZNRecord record = new ZNRecord("record1");

    // set simple field
    record.setSimpleField("simpleKey1", "simpleValue1");

    // set list field
    List<String> list1 = new ArrayList<String>();
    list1.add("list1Value1");
    list1.add("list1Value2");
    record.setListField("listKey1", list1);

    // set map field
    Map<String, String> map1 = new HashMap<String, String>();
    map1.put("map1Key1", "map1Value1");
    record.setMapField("mapKey1", map1);
    // System.out.println(record);

    ZNRecord updateRecord = new ZNRecord("updateRecord");

    // set simple field
    updateRecord.setSimpleField("simpleKey2", "simpleValue2");

    // set list field
    List<String> newList1 = new ArrayList<String>();
    newList1.add("list1Value1");
    newList1.add("list1Value2");
    newList1.add("list1NewValue1");
    newList1.add("list1NewValue2");
    updateRecord.setListField("listKey1", newList1);

    List<String> list2 = new ArrayList<String>();
    list2.add("list2Value1");
    list2.add("list2Value2");
    updateRecord.setListField("listKey2", list2);

    // set map field
    Map<String, String> newMap1 = new HashMap<String, String>();
    newMap1.put("map1NewKey1", "map1NewValue1");
    updateRecord.setMapField("mapKey1", newMap1);

    Map<String, String> map2 = new HashMap<String, String>();
    map2.put("map2Key1", "map2Value1");
    updateRecord.setMapField("mapKey2", map2);
    // System.out.println(updateRecord);

    record.merge(updateRecord);
    // System.out.println(record);

    ZNRecord expectRecord = new ZNRecord("record1");
    expectRecord.setSimpleField("simpleKey1", "simpleValue1");
    expectRecord.setSimpleField("simpleKey2", "simpleValue2");
    List<String> expectList1 = new ArrayList<String>();
    expectList1.add("list1Value1");
    expectList1.add("list1Value2");
    expectList1.add("list1Value1");
    expectList1.add("list1Value2");
    expectList1.add("list1NewValue1");
    expectList1.add("list1NewValue2");
    expectRecord.setListField("listKey1", expectList1);
    List<String> expectList2 = new ArrayList<String>();
    expectList2.add("list2Value1");
    expectList2.add("list2Value2");
    expectRecord.setListField("listKey2", expectList2);
    Map<String, String> expectMap1 = new HashMap<String, String>();
    expectMap1.put("map1Key1", "map1Value1");
    expectMap1.put("map1NewKey1", "map1NewValue1");
    expectRecord.setMapField("mapKey1", expectMap1);
    Map<String, String> expectMap2 = new HashMap<String, String>();
    expectMap2.put("map2Key1", "map2Value1");
    expectRecord.setMapField("mapKey2", expectMap2);
    Assert.assertEquals(record, expectRecord, "Should be equal.");
  }
View Full Code Here

    instanceNames.add("localhost_1231");
    instanceNames.add("localhost_1232");
    instanceNames.add("localhost_1233");
    instanceNames.add("localhost_1234");

    ZNRecord result = IdealStateCalculatorByShuffling.calculateIdealState(
        instanceNames, partitions, replicas, dbName);
    IdealCalculatorByConsistentHashing.printIdealStateStats(result, "MASTER");
    IdealCalculatorByConsistentHashing.printIdealStateStats(result, "SLAVE");
   
    ZNRecord result2 = IdealStateCalculatorByRush.calculateIdealState(instanceNames, 1, partitions, replicas, dbName);

    ZNRecord result3 = IdealCalculatorByConsistentHashing.calculateIdealState(instanceNames, partitions, replicas, dbName, new IdealCalculatorByConsistentHashing.FnvHash());
    IdealCalculatorByConsistentHashing.printIdealStateStats(result3, "MASTER");
    IdealCalculatorByConsistentHashing.printIdealStateStats(result3, "SLAVE");
    IdealCalculatorByConsistentHashing.printIdealStateStats(result3, "");
    IdealCalculatorByConsistentHashing.printNodeOfflineOverhead(result3);

    // System.out.println(result);
    ObjectMapper mapper = new ObjectMapper();

    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StringWriter sw = new StringWriter();
    try
    {
      mapper.writeValue(sw, result);
      // System.out.println(sw.toString());

      ZNRecord zn = mapper.readValue(new StringReader(sw.toString()),
          ZNRecord.class);
      System.out.println(result.toString());
      System.out.println(zn.toString());
      AssertJUnit.assertTrue(zn.toString().equalsIgnoreCase(result.toString()));
      System.out.println();

      sw= new StringWriter();
      mapper.writeValue(sw, result2);

      ZNRecord zn2 = mapper.readValue(new StringReader(sw.toString()),
          ZNRecord.class);
      System.out.println(result2.toString());
      System.out.println(zn2.toString());
      AssertJUnit.assertTrue(zn2.toString().equalsIgnoreCase(result2.toString()));

      sw= new StringWriter();
      mapper.writeValue(sw, result3);
      System.out.println();

      ZNRecord zn3 = mapper.readValue(new StringReader(sw.toString()),
          ZNRecord.class);
      System.out.println(result3.toString());
      System.out.println(zn3.toString());
      AssertJUnit.assertTrue(zn3.toString().equalsIgnoreCase(result3.toString()));
      System.out.println();

    } catch (JsonGenerationException e)
    {
      // TODO Auto-generated catch block
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.