Package com.linkedin.helix

Examples of com.linkedin.helix.ZNRecord


  private static ZNRecord nextIdealState(final ZNRecord cur, final ZNRecord dest, final int steps)
      throws PropertyStoreException
  {
    // get a deep copy
    ZNRecord next = SERIALIZER.deserialize(SERIALIZER.serialize(cur));
    List<String[]> list = findAllUnfinishPairs(cur, dest);

    // randomly pick up pairs that haven't reached destination state and
    // progress
    for (int i = 0; i < steps; i++)
    {
      int randomInt = RANDOM.nextInt(list.size());
      String[] pair = list.get(randomInt);
      String curState = null;
      Map<String, String> curHostMap = next.getMapField(pair[0]);
      if (curHostMap != null)
      {
        curState = curHostMap.get(pair[1]);
      }
      final String destState = dest.getMapField(pair[0]).get(pair[1]);

      // TODO generalize it using state-model
      if (curState == null && destState != null)
      {
        Map<String, String> hostMap = next.getMapField(pair[0]);
        if (hostMap == null)
        {
          hostMap = new HashMap<String, String>();
        }
        hostMap.put(pair[1], "SLAVE");
        next.setMapField(pair[0], hostMap);
      } else if (curState.equalsIgnoreCase("SLAVE") && destState != null
          && destState.equalsIgnoreCase("MASTER"))
      {
        next.getMapField(pair[0]).put(pair[1], "MASTER");
      } else
      {
        LOG.error("fail to calculate the next ideal state");
      }
      curState = next.getMapField(pair[0]).get(pair[1]);
      if (curState != null && curState.equalsIgnoreCase(destState))
      {
        list.remove(randomInt);
      }
    }
View Full Code Here


  }

  StringRepresentation getConfigScopes() throws Exception
  {
    StringRepresentation representation = null;
    ZNRecord record = new ZNRecord("Config");

    List<String> scopeList =
        Arrays.asList(ConfigScopeProperty.CLUSTER.toString(),
                      ConfigScopeProperty.RESOURCE.toString(),
                      ConfigScopeProperty.PARTICIPANT.toString(),
                      ConfigScopeProperty.PARTITION.toString());
    record.setListField("scopes", scopeList);

    representation =
        new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record),
                                 MediaType.APPLICATION_JSON);
View Full Code Here

    String bufStr = new String(buf);

    // test zkClient
    // legal-sized data gets written to zk
    // write a znode of size less than 1m
    final ZNRecord smallRecord = new ZNRecord("normalsize");
    smallRecord.getSimpleFields().clear();
    for (int i = 0; i < 900; i++)
    {
      smallRecord.setSimpleField(i + "", bufStr);
    }

    String path1 = "/" + root + "/test1";
    zkClient.createPersistent(path1, true);
    zkClient.writeData(path1, smallRecord);

    ZNRecord record = zkClient.readData(path1);
    Assert.assertTrue(serializer.serialize(record).length > 900 * 1024);

    // oversized data doesn't create any data on zk
    // prepare a znode of size larger than 1m
    final ZNRecord largeRecord = new ZNRecord("oversize");
    largeRecord.getSimpleFields().clear();
    for (int i = 0; i < 1024; i++)
    {
      largeRecord.setSimpleField(i + "", bufStr);
    }
    String path2 = "/" + root + "/test2";
    zkClient.createPersistent(path2, true);
    try
    {
      zkClient.writeData(path2, largeRecord);
      Assert.fail("Should fail because data size is larger than 1M");
    }
    catch (HelixException e)
    {
      // OK
    }
    record = zkClient.readData(path2);
    Assert.assertNull(record);

    // oversized write doesn't overwrite existing data on zk
    record = zkClient.readData(path1);
    try
    {
      zkClient.writeData(path1, largeRecord);
      Assert.fail("Should fail because data size is larger than 1M");
    }
    catch (HelixException e)
    {
      // OK
    }
    ZNRecord recordNew = zkClient.readData(path1);
    byte[] arr = serializer.serialize(record);
    byte[] arrNew = serializer.serialize(recordNew);
    Assert.assertTrue(Arrays.equals(arr, arrNew));

    // test ZkDataAccessor
View Full Code Here

    ZkClient zkClient =
        (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    HelixAdmin admin = setupTool.getClusterManagementTool();
    ZNRecord record = new ZNRecord(scopeProperty + " Config");

    List<String> configKeys = admin.getConfigKeys(scopeProperty, clusterName, keys);
    record.setListField(scopeProperty.toString(), configKeys);

    representation =
        new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record),
                                 MediaType.APPLICATION_JSON);
View Full Code Here

    ZkClient zkClient =
        (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    HelixAdmin admin = setupTool.getClusterManagementTool();
    ZNRecord record = new ZNRecord(scopeProperty + " Config");

    List<String> configKeys = admin.getConfigKeys(scopeProperty, clusterName, keys);
    Map<String, String> configs = admin.getConfig(scope, new HashSet<String>(configKeys));
    record.setSimpleFields(configs);

    representation =
        new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record),
                                 MediaType.APPLICATION_JSON);
View Full Code Here

    List<String> instances = new ArrayList<String>();
    for(int i = 0;i<5; i++)
    {
      String instance = "localhost_"+(12918+i);
      instances.add(instance);
      ZNRecord metaData = new ZNRecord(instance);
      metaData.setSimpleField(LiveInstanceProperty.SESSION_ID.toString(),
          UUID.randomUUID().toString());
      metaData.setSimpleField("SCN", "" + (10-i));
      liveInstances.add(metaData);
      liveInstanceMap.put(instance, metaData);
    }
   
    //liveInstances.remove(0);
    ZNRecord externalView = IdealStateCalculatorForStorageNode.calculateIdealState(
        instances, 21, 3, "TestDB", "MASTER", "SLAVE");
   
   
    Criteria criteria = new Criteria();
    criteria.setInstanceName("%");
View Full Code Here

  {
    for (int i = 0; i < NODE_NR; i++)
    {
      String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
      HelixManager manager = _startCMResultMap.get(instanceName)._manager;
      ZNRecord record = new ZNRecord(_stat);
      Map<String, String> valMap = new HashMap<String, String>();
      valMap.put(metricName1, val1[i] + "");
      valMap.put(metricName2, val2[i] + "");
      record.setSimpleField("TimeStamp", new Date().getTime() + "");
      record.setMapField(_statName, valMap);
      HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
      Builder keyBuilder = helixDataAccessor.keyBuilder();
      helixDataAccessor
        .setProperty(keyBuilder.healthReport( manager.getInstanceName(), record.getId()), new HealthStat(record))
    }
    try
    {
      Thread.sleep(1000);
    }
View Full Code Here

   * to last state
   */

  public ZNRecord generateConfigForStorageSchemata()
  {
    ZNRecord record = new ZNRecord("STORAGE_DEFAULT_SM_SCHEMATA");
    record.setSimpleField(StateModelDefinitionProperty.INITIAL_STATE.toString(),
                          "OFFLINE");
    List<String> statePriorityList = new ArrayList<String>();
    statePriorityList.add("MASTER");
    statePriorityList.add("OFFLINE");
    statePriorityList.add("DROPPED");
    statePriorityList.add("ERROR");
    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("MASTER"))
      {
        metadata.put("count", "N");
        record.setMapField(key, metadata);
      }
      else if (state.equals("OFFLINE"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
      else if (state.equals("DROPPED"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
      else if (state.equals("ERROR"))
      {
        metadata.put("count", "-1");
        record.setMapField(key, metadata);
      }
    }
    for (String state : statePriorityList)
    {
      String key = state + ".next";
      if (state.equals("MASTER"))
      {
        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("MASTER", "MASTER");
        metadata.put("DROPPED", "DROPPED");
        record.setMapField(key, metadata);
      }
      if (state.equals("ERROR"))
      {
        Map<String, String> metadata = new HashMap<String, String>();
        metadata.put("OFFLINE", "OFFLINE");
        record.setMapField(key, metadata);
      }
    }
    List<String> stateTransitionPriorityList = new ArrayList<String>();
    stateTransitionPriorityList.add("MASTER-OFFLINE");
    stateTransitionPriorityList.add("OFFLINE-MASTER");
    record.setListField(StateModelDefinitionProperty.STATE_TRANSITION_PRIORITYLIST.toString(),
                        stateTransitionPriorityList);
    return record;
  }
View Full Code Here

    int historySize = 0;
    HelixDataAccessor helixDataAccessor = manager.getHelixDataAccessor();
    Builder keyBuilder = helixDataAccessor.keyBuilder();
    HelixProperty property = helixDataAccessor.getProperty(keyBuilder.alertHistory());
    ZNRecord history = null;
    if(property != null)
    {
      history = property.getRecord();
      historySize = property.getRecord().getMapFields().size();
    }
   
    HealthStatsAggregationTask task = new HealthStatsAggregationTask(_startCMResultMap.get(controllerName)._manager);
    task.run();
    Thread.sleep(100);
   
   
    history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
    //
    Assert.assertEquals(history.getMapFields().size(), 1 + historySize);
    TreeMap<String, Map<String, String>> recordMap = new TreeMap<String, Map<String, String>>();
    recordMap.putAll( history.getMapFields());
    Map<String, String> lastRecord = recordMap.firstEntry().getValue();
    Assert.assertTrue(lastRecord.size() == 4);
    Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
   
    setHealthData(metrics1, metrics2);
    task.run();
    Thread.sleep(100);
    history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
    // no change
    Assert.assertEquals(history.getMapFields().size(), 1 + historySize);
    recordMap = new TreeMap<String, Map<String, String>>();
    recordMap.putAll( history.getMapFields());
    lastRecord = recordMap.firstEntry().getValue();
    Assert.assertTrue(lastRecord.size() == 4);
    Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
   
    int [] metrics3 = {21, 44, 22, 14, 16};
    int [] metrics4 = {122, 115, 222, 41,16};
    setHealthData(metrics3, metrics4);
    task.run();
    Thread.sleep(100);
    history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
    // new delta should be recorded
    Assert.assertEquals(history.getMapFields().size(), 2 + historySize);
    recordMap = new TreeMap<String, Map<String, String>>();
    recordMap.putAll( history.getMapFields());
    lastRecord = recordMap.lastEntry().getValue();
    Assert.assertEquals(lastRecord.size(), 6);
    Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
    Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
   
    int [] metrics5 = {0, 0, 0, 0, 0};
    int [] metrics6 = {0, 0, 0, 0,0};
    setHealthData(metrics5, metrics6);
    task.run();
    Thread.sleep(500);
    history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
    // reset everything
    Assert.assertEquals(history.getMapFields().size(), 3 + historySize);
    recordMap = new TreeMap<String, Map<String, String>>();
    recordMap.putAll( history.getMapFields());
    lastRecord = recordMap.lastEntry().getValue();
    Assert.assertTrue(lastRecord.size() == 6);
    Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
    Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
   
    // Size of the history should be 30
    for(int i = 0;i < 27; i++)
    {
      int x = i % 2;
      int y = (i+1) % 2;
      int[] metricsx = {19 + 3*x, 19 + 3*y, 19 + 4*x, 18+4*y, 17+5*y};
      int[] metricsy = {99 + 3*x, 99 + 3*y, 98 + 4*x, 98+4*y, 97+5*y};
     
      setHealthData(metricsx, metricsy);
      task.run();
      Thread.sleep(100);
      history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
     
      Assert.assertEquals(history.getMapFields().size(), Math.min(3 + i + 1 + historySize, 30));
      recordMap = new TreeMap<String, Map<String, String>>();
      recordMap.putAll( history.getMapFields());
      lastRecord = recordMap.lastEntry().getValue();
      if(i == 0)
      {
        Assert.assertTrue(lastRecord.size() == 6);
        Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
        Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
        Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
        Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
        Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
        Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
      }
      else
      {
        System.out.println(lastRecord.size());
        Assert.assertEquals(lastRecord.size() , 10);
        if(x == 0)
        {
          Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
        }
        else
        {
          Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12922.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12920.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12921.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("OFF"));
          Assert.assertTrue(lastRecord.get("(localhost_12918.TestStat@DB#db1.TestMetric2)GREATER(100)").equals("ON"));
          Assert.assertTrue(lastRecord.get("(localhost_12919.TestStat@DB#db1.TestMetric1)GREATER(20)").equals("OFF"));
        }
      }
    }
    // size limit is 30
    for(int i = 0;i < 10; i++)
    {
      int x = i % 2;
      int y = (i+1) % 2;
      int[] metricsx = {19 + 3*x, 19 + 3*y, 19 + 4*x, 18+4*y, 17+5*y};
      int[] metricsy = {99 + 3*x, 99 + 3*y, 98 + 4*x, 98+4*y, 97+5*y};
     
      setHealthData(metricsx, metricsy);
      task.run();
      Thread.sleep(100);
      history = helixDataAccessor.getProperty(keyBuilder.alertHistory()).getRecord();
     
      Assert.assertEquals(history.getMapFields().size(), 30);
      recordMap = new TreeMap<String, Map<String, String>>();
      recordMap.putAll( history.getMapFields());
      lastRecord = recordMap.lastEntry().getValue();
     
      Assert.assertEquals(lastRecord.size() , 10);
      if(x == 0)
      {
View Full Code Here

   
    Map<String, ZNRecord> scnMap = new HashMap<String, ZNRecord>();
    for(int i = 0;i < NODE_NR; i++)
    {
      String instance = "localhost_"+(12918+i);
      ZNRecord metaData = new ZNRecord(instance);
      metaData.setSimpleField(LiveInstanceProperty.SESSION_ID.toString(),
          UUID.randomUUID().toString());
      metaData.setMapField("SCN", new HashMap<String, String>());
      for(int j = 0;j < _PARTITIONS; j++)
      {
        metaData.getMapField("SCN").put(TEST_DB+"_"+j, ""+i);
      }
      scnMap.put(instance, metaData);
    }
    Map<String, Object> bindVariables = new HashMap<String, Object>();
    bindVariables.put("scnMap", scnMap);
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.