use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class TestMiniSolrCloudCluster method testStopAllStartAll.
@Test
public void testStopAllStartAll() throws Exception {
final String collectionName = "testStopAllStartAllCollection";
final MiniSolrCloudCluster miniCluster = createMiniSolrCloudCluster();
try {
assertNotNull(miniCluster.getZkServer());
List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
assertEquals(NUM_SERVERS, jettys.size());
for (JettySolrRunner jetty : jettys) {
assertTrue(jetty.isRunning());
}
createCollection(miniCluster, collectionName, null, null, Boolean.TRUE, null);
final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();
cloudSolrClient.setDefaultCollection(collectionName);
final SolrQuery query = new SolrQuery("*:*");
final SolrInputDocument doc = new SolrInputDocument();
try (SolrZkClient zkClient = new SolrZkClient(miniCluster.getZkServer().getZkAddress(), AbstractZkTestCase.TIMEOUT, AbstractZkTestCase.TIMEOUT, null);
ZkStateReader zkStateReader = new ZkStateReader(zkClient)) {
zkStateReader.createClusterStateWatchersAndUpdate();
AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
// modify collection
final int numDocs = 1 + random().nextInt(10);
for (int ii = 1; ii <= numDocs; ++ii) {
doc.setField("id", "" + ii);
cloudSolrClient.add(doc);
if (ii * 2 == numDocs)
cloudSolrClient.commit();
}
cloudSolrClient.commit();
// query collection
{
final QueryResponse rsp = cloudSolrClient.query(query);
assertEquals(numDocs, rsp.getResults().getNumFound());
}
// the test itself
zkStateReader.forceUpdateCollection(collectionName);
final ClusterState clusterState = zkStateReader.getClusterState();
final HashSet<Integer> leaderIndices = new HashSet<Integer>();
final HashSet<Integer> followerIndices = new HashSet<Integer>();
{
final HashMap<String, Boolean> shardLeaderMap = new HashMap<String, Boolean>();
for (final Slice slice : clusterState.getSlices(collectionName)) {
for (final Replica replica : slice.getReplicas()) {
shardLeaderMap.put(replica.getNodeName().replace("_solr", "/solr"), Boolean.FALSE);
}
shardLeaderMap.put(slice.getLeader().getNodeName().replace("_solr", "/solr"), Boolean.TRUE);
}
for (int ii = 0; ii < jettys.size(); ++ii) {
final URL jettyBaseUrl = jettys.get(ii).getBaseUrl();
final String jettyBaseUrlString = jettyBaseUrl.toString().substring((jettyBaseUrl.getProtocol() + "://").length());
final Boolean isLeader = shardLeaderMap.get(jettyBaseUrlString);
if (Boolean.TRUE.equals(isLeader)) {
leaderIndices.add(new Integer(ii));
} else if (Boolean.FALSE.equals(isLeader)) {
followerIndices.add(new Integer(ii));
}
// else neither leader nor follower i.e. node without a replica (for our collection)
}
}
final List<Integer> leaderIndicesList = new ArrayList<Integer>(leaderIndices);
final List<Integer> followerIndicesList = new ArrayList<Integer>(followerIndices);
// first stop the followers (in no particular order)
Collections.shuffle(followerIndicesList, random());
for (Integer ii : followerIndicesList) {
if (!leaderIndices.contains(ii)) {
miniCluster.stopJettySolrRunner(jettys.get(ii.intValue()));
}
}
// then stop the leaders (again in no particular order)
Collections.shuffle(leaderIndicesList, random());
for (Integer ii : leaderIndicesList) {
miniCluster.stopJettySolrRunner(jettys.get(ii.intValue()));
}
// calculate restart order
final List<Integer> restartIndicesList = new ArrayList<Integer>();
Collections.shuffle(leaderIndicesList, random());
restartIndicesList.addAll(leaderIndicesList);
Collections.shuffle(followerIndicesList, random());
restartIndicesList.addAll(followerIndicesList);
if (random().nextBoolean())
Collections.shuffle(restartIndicesList, random());
// and then restart jettys in that order
for (Integer ii : restartIndicesList) {
final JettySolrRunner jetty = jettys.get(ii.intValue());
if (!jetty.isRunning()) {
miniCluster.startJettySolrRunner(jetty);
assertTrue(jetty.isRunning());
}
}
AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
zkStateReader.forceUpdateCollection(collectionName);
// re-query collection
{
final QueryResponse rsp = cloudSolrClient.query(query);
assertEquals(numDocs, rsp.getResults().getNumFound());
}
}
} finally {
miniCluster.shutdown();
}
}
use of org.apache.solr.client.solrj.response.QueryResponse 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.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class OneQuery method getCount.
public long getCount(HttpSolrClient client, String core) {
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("qt", "/select");
params.set("q", "*:*");
long numFound = 0;
client.setBaseURL(baseUrl + core);
try {
QueryResponse response = client.query(params);
numFound = response.getResults().getNumFound();
} catch (Exception e) {
e.printStackTrace();
}
return numFound;
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class DistributedDebugComponentTest method testRandom.
@Test
public void testRandom() throws Exception {
final int NUM_ITERS = atLeast(50);
for (int i = 0; i < NUM_ITERS; i++) {
SolrClient client = random().nextBoolean() ? collection1 : collection2;
SolrQuery q = new SolrQuery();
q.set("distrib", "true");
q.setFields("id", "text");
boolean shard1Results = random().nextBoolean();
boolean shard2Results = random().nextBoolean();
String qs = "_query_with_no_results_";
if (shard1Results) {
qs += " OR batman";
}
if (shard2Results) {
qs += " OR superman";
}
q.setQuery(qs);
Set<String> shards = new HashSet<String>(Arrays.asList(shard1, shard2));
if (random().nextBoolean()) {
shards.remove(shard1);
shard1Results = false;
} else if (random().nextBoolean()) {
shards.remove(shard2);
shard2Results = false;
}
q.set("shards", StringUtils.join(shards, ","));
List<String> debug = new ArrayList<String>(10);
boolean all = false;
final boolean timing = random().nextBoolean();
final boolean query = random().nextBoolean();
final boolean results = random().nextBoolean();
final boolean track = random().nextBoolean();
if (timing) {
debug.add("timing");
}
if (query) {
debug.add("query");
}
if (results) {
debug.add("results");
}
if (track) {
debug.add("track");
}
if (debug.isEmpty()) {
debug.add("true");
all = true;
}
q.set("debug", (String[]) debug.toArray(new String[debug.size()]));
QueryResponse r = client.query(q);
try {
assertDebug(r, all || track, "track");
assertDebug(r, all || query, "rawquerystring");
assertDebug(r, all || query, "querystring");
assertDebug(r, all || query, "parsedquery");
assertDebug(r, all || query, "parsedquery_toString");
assertDebug(r, all || query, "QParser");
assertDebug(r, all || results, "explain");
assertDebug(r, all || timing, "timing");
} catch (AssertionError e) {
throw new AssertionError(q.toString() + ": " + e.getMessage(), e);
}
}
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class DistributedDebugComponentTest method testSimpleSearch.
@Test
@SuppressWarnings("unchecked")
public void testSimpleSearch() throws Exception {
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.set("debug", "track");
query.set("distrib", "true");
query.setFields("id", "text");
query.set("shards", shard1 + "," + shard2);
QueryResponse response = collection1.query(query);
NamedList<Object> track = (NamedList<Object>) response.getDebugMap().get("track");
assertNotNull(track);
assertNotNull(track.get("rid"));
assertNotNull(track.get("EXECUTE_QUERY"));
assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1));
assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2));
assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard1));
assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard2));
assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("GET_FIELDS")).get(shard1), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
assertElementsPresent((NamedList<String>) ((NamedList<Object>) track.get("GET_FIELDS")).get(shard2), "QTime", "ElapsedTime", "RequestPurpose", "NumFound", "Response");
query.add("omitHeader", "true");
response = collection1.query(query);
assertNull("QTime is not included in the response when omitHeader is set to true", ((NamedList<Object>) response.getDebugMap().get("track")).findRecursive("EXECUTE_QUERY", shard1, "QTime"));
assertNull("QTime is not included in the response when omitHeader is set to true", ((NamedList<Object>) response.getDebugMap().get("track")).findRecursive("GET_FIELDS", shard2, "QTime"));
query.setQuery("id:1");
response = collection1.query(query);
track = (NamedList<Object>) response.getDebugMap().get("track");
assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard1));
assertNotNull(((NamedList<Object>) track.get("EXECUTE_QUERY")).get(shard2));
assertNotNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard1));
// This test is invalid, as GET_FIELDS should not be executed in shard 2
assertNull(((NamedList<Object>) track.get("GET_FIELDS")).get(shard2));
}
Aggregations