Java Code Examples for org.apache.solr.common.cloud.ClusterState#createFromJson()

The following examples show how to use org.apache.solr.common.cloud.ClusterState#createFromJson() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SliceStateTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultSliceState() {
  Map<String, DocCollection> collectionStates = new HashMap<>();
  Set<String> liveNodes = new HashSet<>();
  liveNodes.add("node1");

  Map<String, Slice> slices = new HashMap<>();
  Map<String, Replica> sliceToProps = new HashMap<>();
  Map<String, Object> props = new HashMap<>();
  props.put("node_name", "127.0.0.1:10000_solr");
  props.put("core", "core1");

  Replica replica = new Replica("node1", props, "collection1", "shard1");
  sliceToProps.put("node1", replica);
  Slice slice = new Slice("shard1", sliceToProps, null, "collection1");
  assertSame("Default state not set to active", Slice.State.ACTIVE, slice.getState());
  slices.put("shard1", slice);
  collectionStates.put("collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT));

  ClusterState clusterState = new ClusterState(liveNodes, collectionStates);
  byte[] bytes = Utils.toJSON(clusterState);
  ClusterState loadedClusterState = ClusterState.createFromJson(-1, bytes, liveNodes);

  assertSame("Default state not set to active", Slice.State.ACTIVE, loadedClusterState.getCollection("collection1").getSlice("shard1").getState());
}
 
Example 2
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * This method reads the meta-data information for the backed-up collection.
 *
 * @param backupLoc The base path used to store the backup data.
 * @param backupId The unique name for the backup.
 * @param collectionName The name of the collection whose meta-data is to be returned.
 * @return the meta-data information for the backed-up collection.
 * @throws IOException in case of errors.
 */
public DocCollection readCollectionState(URI backupLoc, String backupId, String collectionName) throws IOException {
  Objects.requireNonNull(collectionName);

  URI zkStateDir = repository.resolve(backupLoc, backupId, ZK_STATE_DIR);
  try (IndexInput is = repository.openInput(zkStateDir, COLLECTION_PROPS_FILE, IOContext.DEFAULT)) {
    byte[] arr = new byte[(int) is.length()]; // probably ok since the json file should be small.
    is.readBytes(arr, 0, (int) is.length());
    ClusterState c_state = ClusterState.createFromJson(-1, arr, Collections.emptySet());
    return c_state.getCollection(collectionName);
  }
}
 
Example 3
Source File: ClusterStateTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testStoreAndRead() throws Exception {
  Map<String,DocCollection> collectionStates = new HashMap<>();
  Set<String> liveNodes = new HashSet<>();
  liveNodes.add("node1");
  liveNodes.add("node2");
  
  Map<String,Slice> slices = new HashMap<>();
  Map<String,Replica> sliceToProps = new HashMap<>();
  Map<String,Object> props = new HashMap<>();
  props.put("node_name", "node1:10000_solr");
  props.put("core", "core1");

  props.put("prop1", "value");
  props.put("prop2", "value2");
  Replica replica = new Replica("node1", props, "collection1", "shard1");
  sliceToProps.put("node1", replica);
  Slice slice = new Slice("shard1", sliceToProps, null, "collection1");
  slices.put("shard1", slice);
  Slice slice2 = new Slice("shard2", sliceToProps, null, "collection1");
  slices.put("shard2", slice2);
  collectionStates.put("collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT));
  collectionStates.put("collection2", new DocCollection("collection2", slices, null, DocRouter.DEFAULT));

  ClusterState clusterState = new ClusterState(liveNodes, collectionStates);
  byte[] bytes = Utils.toJSON(clusterState);
  // System.out.println("#################### " + new String(bytes));
  ClusterState loadedClusterState = ClusterState.createFromJson(-1, bytes, liveNodes);
  
  assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
      .getLiveNodes().size());
  assertEquals("No collections found", 2, loadedClusterState.getCollectionsMap().size());
  assertEquals("Properties not copied properly", replica.getStr("prop1"), loadedClusterState.getCollection("collection1").getSlice("shard1").getReplicasMap().get("node1").getStr("prop1"));
  assertEquals("Properties not copied properly", replica.getStr("prop2"), loadedClusterState.getCollection("collection1").getSlice("shard1").getReplicasMap().get("node1").getStr("prop2"));

  loadedClusterState = ClusterState.createFromJson(-1, new byte[0], liveNodes);
  
  assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
      .getLiveNodes().size());
  assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size());

  loadedClusterState = ClusterState.createFromJson(-1, (byte[])null, liveNodes);
  
  assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
      .getLiveNodes().size());
  assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size());
}