public byte[] getValidValue(ByteArray key) {
    String keyString = ByteUtils.getString(key.get(), "UTF-8");
    if (MetadataStore.CLUSTER_KEY.equals(keyString)) {
      return ByteUtils.getBytes(
          new ClusterMapper().writeCluster(ServerTestUtils.getLocalCluster(1)), "UTF-8");
    } else if (MetadataStore.STORES_KEY.equals(keyString)) {
      return ByteUtils.getBytes(
          new StoreDefinitionsMapper().writeStoreList(ServerTestUtils.getStoreDefs(1)), "UTF-8");

    } else if (MetadataStore.SERVER_STATE_KEY.equals(keyString)) {
      int i = (int) (Math.random() * VoldemortState.values().length);
      return ByteUtils.getBytes(VoldemortState.values()[i].toString(), "UTF-8");
    } else if (MetadataStore.REBALANCING_STEAL_INFO.equals(keyString)) {
      int size = (int) (Math.random() * 10);
      List<Integer> partition = new ArrayList<Integer>();
      for (int i = 0; i < size; i++) {
        partition.add((int) Math.random() * 10);
      }

      return ByteUtils.getBytes(
          new RebalancerState(
                  Arrays.asList(
                      new RebalancePartitionsInfo(
                          0,
                          (int) Math.random() * 5,
                          partition,
                          new ArrayList<Integer>(0),
                          new ArrayList<Integer>(0),
                          Arrays.asList("testStoreName"),
                          new HashMap<String, String>(),
                          new HashMap<String, String>(),
                          (int) Math.random() * 3)))
              .toJsonString(),
          "UTF-8");
    } else if (MetadataStore.GRANDFATHERING_INFO.equals(keyString)) {
      int size = (int) (Math.random() * 10);
      List<Integer> partition = new ArrayList<Integer>();
      for (int i = 0; i < size; i++) {
        partition.add((int) Math.random() * 10);
      }
      return ByteUtils.getBytes(
          new GrandfatherState(
                  Arrays.asList(
                      new RebalancePartitionsInfo(
                          0,
                          (int) Math.random() * 5,
                          partition,
                          new ArrayList<Integer>(0),
                          new ArrayList<Integer>(0),
                          Arrays.asList("testStoreName"),
                          new HashMap<String, String>(),
                          new HashMap<String, String>(),
                          (int) Math.random() * 3)))
              .toJsonString(),
          "UTF-8");
    }

    throw new RuntimeException("Unhandled key:" + keyString + " passed");
  }
  /**
   * @param keyBytes: keyName strings serialized as bytes eg. 'cluster.xml'
   * @return List of values (only 1 for Metadata) versioned byte[] eg. UTF bytes for cluster xml
   *     definitions
   * @throws VoldemortException
   */
  public List<Versioned<byte[]>> get(ByteArray keyBytes) throws VoldemortException {
    try {
      String key = ByteUtils.getString(keyBytes.get(), "UTF-8");

      if (METADATA_KEYS.contains(key)) {
        List<Versioned<byte[]>> values = Lists.newArrayList();

        // Get the cached value and convert to string
        Versioned<String> value = convertObjectToString(key, metadataCache.get(key));

        values.add(
            new Versioned<byte[]>(
                ByteUtils.getBytes(value.getValue(), "UTF-8"), value.getVersion()));

        return values;
      } else {
        throw new VoldemortException("Unhandled Key:" + key + " for MetadataStore get()");
      }
    } catch (Exception e) {
      throw new VoldemortException(
          "Failed to read metadata key:"
              + ByteUtils.getString(keyBytes.get(), "UTF-8")
              + " delete config/.temp config/.version directories and restart.",
          e);
    }
  }
  /**
   * helper function to auto update version and put()
   *
   * @param key
   * @param value
   */
  private void incrementVersionAndPut(MetadataStore metadataStore, String keyString, Object value) {
    ByteArray key = new ByteArray(ByteUtils.getBytes(keyString, "UTF-8"));
    VectorClock current = (VectorClock) metadataStore.getVersions(key).get(0);

    metadataStore.put(
        keyString,
        new Versioned<Object>(value, current.incremented(0, System.currentTimeMillis())));
  }
  @JmxOperation(
      description = "Clean all rebalancing server/cluster states from this node.",
      impact = MBeanOperationInfo.ACTION)
  public void cleanAllRebalancingState() {
    for (String key : OPTIONAL_KEYS) {
      if (!key.equals(NODE_ID_KEY))
        innerStore.delete(key, getVersions(new ByteArray(ByteUtils.getBytes(key, "UTF-8"))).get(0));
    }

    init(getNodeId());
  }
  public byte[] getValidValue(ByteArray key) {
    String keyString = ByteUtils.getString(key.get(), "UTF-8");
    if (MetadataStore.CLUSTER_KEY.equals(keyString)
        || MetadataStore.REBALANCING_SOURCE_CLUSTER_XML.equals(keyString)) {
      return ByteUtils.getBytes(
          new ClusterMapper().writeCluster(ServerTestUtils.getLocalCluster(1)), "UTF-8");
    } else if (MetadataStore.STORES_KEY.equals(keyString)) {
      return ByteUtils.getBytes(
          new StoreDefinitionsMapper().writeStoreList(ServerTestUtils.getStoreDefs(1)), "UTF-8");

    } else if (MetadataStore.SERVER_STATE_KEY.equals(keyString)) {
      int i = (int) (Math.random() * VoldemortState.values().length);
      return ByteUtils.getBytes(VoldemortState.values()[i].toString(), "UTF-8");
    } else if (MetadataStore.REBALANCING_STEAL_INFO.equals(keyString)) {
      int size = (int) (Math.random() * 10) + 1;
      List<Integer> partition = new ArrayList<Integer>();
      for (int i = 0; i < size; i++) {
        partition.add((int) Math.random() * 10);
      }

      List<Integer> partitionIds = partition;

      HashMap<String, List<Integer>> storeToReplicaToPartitionList = Maps.newHashMap();
      storeToReplicaToPartitionList.put("test", partitionIds);

      return ByteUtils.getBytes(
          new RebalancerState(
                  Arrays.asList(
                      new RebalanceTaskInfo(
                          0,
                          (int) Math.random() * 5,
                          storeToReplicaToPartitionList,
                          ServerTestUtils.getLocalCluster(1))))
              .toJsonString(),
          "UTF-8");
    }

    throw new RuntimeException("Unhandled key:" + keyString + " passed");
  }
  private Set<ByteArray> makeSlopKeys(
      Multimap<Integer, ByteArray> failedKeys, Slop.Operation operation) {
    Set<ByteArray> slopKeys = Sets.newHashSet();

    for (Map.Entry<Integer, ByteArray> entry : failedKeys.entries()) {
      byte[] opCode = new byte[] {operation.getOpCode()};
      byte[] spacer = new byte[] {(byte) 0};
      byte[] storeName = ByteUtils.getBytes(STORE_NAME, "UTF-8");
      byte[] nodeIdBytes = new byte[ByteUtils.SIZE_OF_INT];
      ByteUtils.writeInt(nodeIdBytes, entry.getKey(), 0);
      ByteArray slopKey =
          new ByteArray(
              ByteUtils.cat(
                  opCode, spacer, storeName, spacer, nodeIdBytes, spacer, entry.getValue().get()));
      slopKeys.add(slopKey);
    }
    return slopKeys;
  }
  public VAdminProto.UpdateMetadataResponse handleUpdateMetadata(
      VAdminProto.UpdateMetadataRequest request) {
    VAdminProto.UpdateMetadataResponse.Builder response =
        VAdminProto.UpdateMetadataResponse.newBuilder();

    try {
      ByteArray key = ProtoUtils.decodeBytes(request.getKey());
      String keyString = ByteUtils.getString(key.get(), "UTF-8");

      if (MetadataStore.METADATA_KEYS.contains(keyString)) {
        Versioned<byte[]> versionedValue = ProtoUtils.decodeVersioned(request.getVersioned());
        metadataStore.put(new ByteArray(ByteUtils.getBytes(keyString, "UTF-8")), versionedValue);
      }
    } catch (VoldemortException e) {
      response.setError(ProtoUtils.encodeError(errorCodeMapper, e));
      logger.error("handleUpdateMetadata failed for request(" + request.toString() + ")", e);
    }

    return response.build();
  }
 public List<Versioned<byte[]>> get(String key) throws VoldemortException {
   return get(new ByteArray(ByteUtils.getBytes(key, "UTF-8")));
 }
 public ByteArray getValidKey() {
   int i = (int) (Math.random() * TEST_KEYS.size());
   String key = TEST_KEYS.get(i);
   return new ByteArray(ByteUtils.getBytes(key, "UTF-8"));
 }