use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class TestMiniSolrCloudCluster method testCollectionCreateSearchDelete.
@Test
public void testCollectionCreateSearchDelete() throws Exception {
final String collectionName = "testcollection";
MiniSolrCloudCluster miniCluster = createMiniSolrCloudCluster();
final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();
try {
assertNotNull(miniCluster.getZkServer());
List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
assertEquals(NUM_SERVERS, jettys.size());
for (JettySolrRunner jetty : jettys) {
assertTrue(jetty.isRunning());
}
// shut down a server
log.info("#### Stopping a server");
JettySolrRunner stoppedServer = miniCluster.stopJettySolrRunner(0);
assertTrue(stoppedServer.isStopped());
assertEquals(NUM_SERVERS - 1, miniCluster.getJettySolrRunners().size());
// create a server
log.info("#### Starting a server");
JettySolrRunner startedServer = miniCluster.startJettySolrRunner();
assertTrue(startedServer.isRunning());
assertEquals(NUM_SERVERS, miniCluster.getJettySolrRunners().size());
// create collection
log.info("#### Creating a collection");
final String asyncId = (random().nextBoolean() ? null : "asyncId(" + collectionName + ".create)=" + random().nextInt());
createCollection(miniCluster, collectionName, null, asyncId, null, null);
ZkStateReader zkStateReader = miniCluster.getSolrClient().getZkStateReader();
AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
// modify/query collection
log.info("#### updating a querying collection");
cloudSolrClient.setDefaultCollection(collectionName);
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "1");
cloudSolrClient.add(doc);
cloudSolrClient.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse rsp = cloudSolrClient.query(query);
assertEquals(1, rsp.getResults().getNumFound());
// remove a server not hosting any replicas
zkStateReader.forceUpdateCollection(collectionName);
ClusterState clusterState = zkStateReader.getClusterState();
HashMap<String, JettySolrRunner> jettyMap = new HashMap<String, JettySolrRunner>();
for (JettySolrRunner jetty : miniCluster.getJettySolrRunners()) {
String key = jetty.getBaseUrl().toString().substring((jetty.getBaseUrl().getProtocol() + "://").length());
jettyMap.put(key, jetty);
}
Collection<Slice> slices = clusterState.getSlices(collectionName);
// track the servers not host repliacs
for (Slice slice : slices) {
jettyMap.remove(slice.getLeader().getNodeName().replace("_solr", "/solr"));
for (Replica replica : slice.getReplicas()) {
jettyMap.remove(replica.getNodeName().replace("_solr", "/solr"));
}
}
assertTrue("Expected to find a node without a replica", jettyMap.size() > 0);
log.info("#### Stopping a server");
JettySolrRunner jettyToStop = jettyMap.entrySet().iterator().next().getValue();
jettys = miniCluster.getJettySolrRunners();
for (int i = 0; i < jettys.size(); ++i) {
if (jettys.get(i).equals(jettyToStop)) {
miniCluster.stopJettySolrRunner(i);
assertEquals(NUM_SERVERS - 1, miniCluster.getJettySolrRunners().size());
}
}
// re-create a server (to restore original NUM_SERVERS count)
log.info("#### Starting a server");
startedServer = miniCluster.startJettySolrRunner(jettyToStop);
assertTrue(startedServer.isRunning());
assertEquals(NUM_SERVERS, miniCluster.getJettySolrRunners().size());
CollectionAdminRequest.deleteCollection(collectionName).process(miniCluster.getSolrClient());
// create it again
String asyncId2 = (random().nextBoolean() ? null : "asyncId(" + collectionName + ".create)=" + random().nextInt());
createCollection(miniCluster, collectionName, null, asyncId2, null, null);
AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
// check that there's no left-over state
assertEquals(0, cloudSolrClient.query(new SolrQuery("*:*")).getResults().getNumFound());
cloudSolrClient.add(doc);
cloudSolrClient.commit();
assertEquals(1, cloudSolrClient.query(new SolrQuery("*:*")).getResults().getNumFound());
} finally {
miniCluster.shutdown();
}
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class TestCollectionAPI method replicaPropTest.
private void replicaPropTest() throws Exception {
try (CloudSolrClient client = createCloudClient(null)) {
client.connect();
Map<String, Slice> slices = client.getZkStateReader().getClusterState().getCollection(COLLECTION_NAME).getSlicesMap();
List<String> sliceList = new ArrayList<>(slices.keySet());
String c1_s1 = sliceList.get(0);
List<String> replicasList = new ArrayList<>(slices.get(c1_s1).getReplicasMap().keySet());
String c1_s1_r1 = replicasList.get(0);
String c1_s1_r2 = replicasList.get(1);
String c1_s2 = sliceList.get(1);
replicasList = new ArrayList<>(slices.get(c1_s2).getReplicasMap().keySet());
String c1_s2_r1 = replicasList.get(0);
String c1_s2_r2 = replicasList.get(1);
slices = client.getZkStateReader().getClusterState().getCollection(COLLECTION_NAME1).getSlicesMap();
sliceList = new ArrayList<>(slices.keySet());
String c2_s1 = sliceList.get(0);
replicasList = new ArrayList<>(slices.get(c2_s1).getReplicasMap().keySet());
String c2_s1_r1 = replicasList.get(0);
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString());
// Insure we get error returns when omitting required parameters
missingParamsError(client, params);
params.set("collection", COLLECTION_NAME);
missingParamsError(client, params);
params.set("shard", c1_s1);
missingParamsError(client, params);
params.set("replica", c1_s1_r1);
missingParamsError(client, params);
params.set("property", "preferredLeader");
missingParamsError(client, params);
params.set("property.value", "true");
SolrRequest request = new QueryRequest(params);
request.setPath("/admin/collections");
client.request(request);
// The above should have set exactly one preferredleader...
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.preferredleader", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r2, "property", "preferredLeader", "property.value", "true");
// The preferred leader property for shard1 should have switched to the other replica.
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s2, "replica", c1_s2_r1, "property", "preferredLeader", "property.value", "true");
// Now we should have a preferred leader in both shards...
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME1, "shard", c2_s1, "replica", c2_s1_r1, "property", "preferredLeader", "property.value", "true");
// Now we should have three preferred leaders.
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME1, c2_s1_r1, "property.preferredleader", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toString(), "collection", COLLECTION_NAME1, "shard", c2_s1, "replica", c2_s1_r1, "property", "preferredLeader");
// Now we should have two preferred leaders.
// But first we have to wait for the overseer to finish the action
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
// Try adding an arbitrary property to one that has the leader property
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "testprop", "property.value", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r2, "property", "prop", "property.value", "silly");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "testprop", "property.value", "nonsense", SHARD_UNIQUE, "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "nonsense");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "property.testprop", "property.value", "true", SHARD_UNIQUE, "false");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "property.testprop");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.testprop");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
try {
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "preferredLeader", "property.value", "true", SHARD_UNIQUE, "false");
fail("Should have thrown an exception, setting shardUnique=false is not allowed for 'preferredLeader'.");
} catch (SolrException se) {
assertTrue("Should have received a specific error message", se.getMessage().contains("with the shardUnique parameter set to something other than 'true'"));
}
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.testprop");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
Map<String, String> origProps = getProps(client, COLLECTION_NAME, c1_s1_r1, "state", "core", "node_name", "base_url");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "state", "property.value", "state_bad");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "core", "property.value", "core_bad");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "node_name", "property.value", "node_name_bad");
doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "base_url", "property.value", "base_url_bad");
// The above should be on new proeprties.
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.state", "state_bad");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.core", "core_bad");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.node_name", "node_name_bad");
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.base_url", "base_url_bad");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "state");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "core");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "node_name");
doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "base_url");
// They better not have been changed!
for (Map.Entry<String, String> ent : origProps.entrySet()) {
verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, ent.getKey(), ent.getValue());
}
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.state");
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.core");
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.node_name");
verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.base_url");
}
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class TestPullReplica method testCreateDelete.
// 2 times to make sure cleanup is complete and we can create the same collection
@Repeat(iterations = 2)
public void testCreateDelete() throws Exception {
try {
switch(random().nextInt(3)) {
case 0:
// Sometimes use SolrJ
CollectionAdminRequest.createCollection(collectionName, "conf", 2, 1, 0, 3).setMaxShardsPerNode(100).process(cluster.getSolrClient());
break;
case 1:
// Sometimes use v1 API
String url = String.format(Locale.ROOT, "%s/admin/collections?action=CREATE&name=%s&numShards=%s&pullReplicas=%s&maxShardsPerNode=%s", cluster.getRandomJetty(random()).getBaseUrl(), collectionName, // numShards
2, // pullReplicas
3, // maxShardsPerNode
100);
// These options should all mean the same
url = url + pickRandom("", "&nrtReplicas=1", "&replicationFactor=1");
HttpGet createCollectionGet = new HttpGet(url);
cluster.getSolrClient().getHttpClient().execute(createCollectionGet);
break;
case 2:
// Sometimes use V2 API
url = cluster.getRandomJetty(random()).getBaseUrl().toString() + "/____v2/c";
String requestBody = String.format(Locale.ROOT, "{create:{name:%s, numShards:%s, pullReplicas:%s, maxShardsPerNode:%s %s}}", collectionName, // numShards
2, // pullReplicas
3, // maxShardsPerNode
100, // These options should all mean the same
pickRandom("", ", nrtReplicas:1", ", replicationFactor:1"));
HttpPost createCollectionPost = new HttpPost(url);
createCollectionPost.setHeader("Content-type", "application/json");
createCollectionPost.setEntity(new StringEntity(requestBody));
HttpResponse httpResponse = cluster.getSolrClient().getHttpClient().execute(createCollectionPost);
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
break;
}
boolean reloaded = false;
while (true) {
DocCollection docCollection = getCollectionState(collectionName);
assertNotNull(docCollection);
assertEquals("Expecting 4 relpicas per shard", 8, docCollection.getReplicas().size());
assertEquals("Expecting 6 pull replicas, 3 per shard", 6, docCollection.getReplicas(EnumSet.of(Replica.Type.PULL)).size());
assertEquals("Expecting 2 writer replicas, one per shard", 2, docCollection.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
for (Slice s : docCollection.getSlices()) {
// read-only replicas can never become leaders
assertFalse(s.getLeader().getType() == Replica.Type.PULL);
List<String> shardElectionNodes = cluster.getZkClient().getChildren(ZkStateReader.getShardLeadersElectPath(collectionName, s.getName()), null, true);
assertEquals("Unexpected election nodes for Shard: " + s.getName() + ": " + Arrays.toString(shardElectionNodes.toArray()), 1, shardElectionNodes.size());
}
assertUlogPresence(docCollection);
if (reloaded) {
break;
} else {
// reload
CollectionAdminResponse response = CollectionAdminRequest.reloadCollection(collectionName).process(cluster.getSolrClient());
assertEquals(0, response.getStatus());
reloaded = true;
}
}
} finally {
zkClient().printLayoutToStdOut();
}
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class TestPullReplica method testRealTimeGet.
public void testRealTimeGet() throws SolrServerException, IOException, KeeperException, InterruptedException {
// should be redirected to Replica.Type.NRT
int numReplicas = random().nextBoolean() ? 1 : 2;
CollectionAdminRequest.createCollection(collectionName, "conf", 1, numReplicas, 0, numReplicas).setMaxShardsPerNode(100).process(cluster.getSolrClient());
waitForState("Unexpected replica count", collectionName, activeReplicaCount(numReplicas, 0, numReplicas));
DocCollection docCollection = assertNumberOfReplicas(numReplicas, 0, numReplicas, false, true);
HttpClient httpClient = cluster.getSolrClient().getHttpClient();
int id = 0;
Slice slice = docCollection.getSlice("shard1");
List<String> ids = new ArrayList<>(slice.getReplicas().size());
for (Replica rAdd : slice.getReplicas()) {
try (HttpSolrClient client = getHttpSolrClient(rAdd.getCoreUrl(), httpClient)) {
client.add(new SolrInputDocument("id", String.valueOf(id), "foo_s", "bar"));
}
SolrDocument docCloudClient = cluster.getSolrClient().getById(collectionName, String.valueOf(id));
assertEquals("bar", docCloudClient.getFieldValue("foo_s"));
for (Replica rGet : slice.getReplicas()) {
try (HttpSolrClient client = getHttpSolrClient(rGet.getCoreUrl(), httpClient)) {
SolrDocument doc = client.getById(String.valueOf(id));
assertEquals("bar", doc.getFieldValue("foo_s"));
}
}
ids.add(String.valueOf(id));
id++;
}
SolrDocumentList previousAllIdsResult = null;
for (Replica rAdd : slice.getReplicas()) {
try (HttpSolrClient client = getHttpSolrClient(rAdd.getCoreUrl(), httpClient)) {
SolrDocumentList allIdsResult = client.getById(ids);
if (previousAllIdsResult != null) {
assertTrue(compareSolrDocumentList(previousAllIdsResult, allIdsResult));
} else {
// set the first response here
previousAllIdsResult = allIdsResult;
assertEquals("Unexpected number of documents", ids.size(), allIdsResult.getNumFound());
}
}
id++;
}
}
use of org.apache.solr.common.cloud.Slice in project lucene-solr by apache.
the class TestPullReplica method assertUlogPresence.
/**
* Asserts that Update logs don't exist for replicas of type {@link org.apache.solr.common.cloud.Replica.Type#PULL}
*/
private void assertUlogPresence(DocCollection collection) {
for (Slice s : collection.getSlices()) {
for (Replica r : s.getReplicas()) {
if (r.getType() == Replica.Type.NRT) {
continue;
}
SolrCore core = null;
try {
core = cluster.getReplicaJetty(r).getCoreContainer().getCore(r.getCoreName());
assertNotNull(core);
assertFalse("Update log should not exist for replicas of type Passive but file is present: " + core.getUlogDir(), new java.io.File(core.getUlogDir()).exists());
} finally {
core.close();
}
}
}
}
Aggregations