SOLR-5525

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1549552 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2013-12-09 11:22:29 +00:00
parent a0745eccea
commit 60a881a00b
4 changed files with 33 additions and 8 deletions

View File

@ -86,7 +86,7 @@ public class AssignTest extends SolrTestCaseJ4 {
collectionStates.put(cname, docCollection);
Set<String> liveNodes = new HashSet<String>();
ClusterState state = new ClusterState(liveNodes, collectionStates);
ClusterState state = new ClusterState(-1,liveNodes, collectionStates,ClusterStateTest.getMockZkStateReader(collectionStates.keySet()));
String nodeName = Assign.assignNode("collection1", state);
assertEquals("core_node2", nodeName);

View File

@ -17,6 +17,7 @@ package org.apache.solr.cloud;
* the License.
*/
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -26,12 +27,17 @@ import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.DocRouter;
import org.apache.solr.common.cloud.DocRouter;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.ZkStateReader;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.junit.Test;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expectLastCall;
public class ClusterStateTest extends SolrTestCaseJ4 {
@Test
public void testStoreAndRead() throws Exception {
@ -54,11 +60,12 @@ public class ClusterStateTest extends SolrTestCaseJ4 {
slices.put("shard2", slice2);
collectionStates.put("collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT));
collectionStates.put("collection2", new DocCollection("collection2", slices, null, DocRouter.DEFAULT));
ZkStateReader zkStateReaderMock = getMockZkStateReader(collectionStates.keySet());
ClusterState clusterState = new ClusterState(liveNodes, collectionStates);
ClusterState clusterState = new ClusterState(-1,liveNodes, collectionStates,zkStateReaderMock);
byte[] bytes = ZkStateReader.toJSON(clusterState);
// System.out.println("#################### " + new String(bytes));
ClusterState loadedClusterState = ClusterState.load(null, bytes, liveNodes,null);
ClusterState loadedClusterState = ClusterState.load(-1, bytes, liveNodes,zkStateReaderMock);
assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
.getLiveNodes().size());
@ -66,16 +73,30 @@ public class ClusterStateTest extends SolrTestCaseJ4 {
assertEquals("Poperties not copied properly", replica.getStr("prop1"), loadedClusterState.getSlice("collection1", "shard1").getReplicasMap().get("node1").getStr("prop1"));
assertEquals("Poperties not copied properly", replica.getStr("prop2"), loadedClusterState.getSlice("collection1", "shard1").getReplicasMap().get("node1").getStr("prop2"));
loadedClusterState = ClusterState.load(null, new byte[0], liveNodes,null);
loadedClusterState = ClusterState.load(-1, new byte[0], liveNodes, getMockZkStateReader(Collections.<String>emptySet()));
assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
.getLiveNodes().size());
assertEquals("Should not have collections", 0, loadedClusterState.getCollections().size());
loadedClusterState = ClusterState.load(null, (byte[])null, liveNodes,null);
loadedClusterState = ClusterState.load(-1, (byte[])null, liveNodes,getMockZkStateReader(Collections.<String>emptySet()));
assertEquals("Provided liveNodes not used properly", 2, loadedClusterState
.getLiveNodes().size());
assertEquals("Should not have collections", 0, loadedClusterState.getCollections().size());
}
public static ZkStateReader getMockZkStateReader(final Set<String> collections) {
ZkStateReader mock = createMock(ZkStateReader.class);
EasyMock.reset(mock);
mock.getAllCollections();
EasyMock.expectLastCall().andAnswer(new IAnswer<Set<String>>() {
@Override
public Set<String> answer() throws Throwable {
return collections;
}
}).anyTimes();
return mock;
}
}

View File

@ -49,9 +49,10 @@ public class SliceStateTest extends SolrTestCaseJ4 {
slices.put("shard1", slice);
collectionStates.put("collection1", new DocCollection("collection1", slices, null, DocRouter.DEFAULT));
ClusterState clusterState = new ClusterState(liveNodes, collectionStates);
ZkStateReader mockZkStateReader = ClusterStateTest.getMockZkStateReader(collectionStates.keySet());
ClusterState clusterState = new ClusterState(-1,liveNodes, collectionStates, mockZkStateReader);
byte[] bytes = ZkStateReader.toJSON(clusterState);
ClusterState loadedClusterState = ClusterState.load(null, bytes, liveNodes, null);
ClusterState loadedClusterState = ClusterState.load(-1, bytes, liveNodes, mockZkStateReader);
assertEquals("Default state not set to active", "active", loadedClusterState.getSlice("collection1", "shard1").getState());
}

View File

@ -583,6 +583,9 @@ public class ZkStateReader {
public SolrZkClient getZkClient() {
return zkClient;
}
public Set<String> getAllCollections(){
return clusterState.getCollections();
}
public void updateAliases() throws KeeperException, InterruptedException {
byte[] data = zkClient.getData(ALIASES, null, null, true);