SOLR-12607: Minor refactorings

Replaced a few private instances with lambdas and extracted common code for retrying splits into a new method
This commit is contained in:
Shalin Shekhar Mangar 2018-08-16 16:07:05 +05:30
parent 94ecb0616a
commit 57b33c19a4
1 changed files with 110 additions and 139 deletions

View File

@ -49,7 +49,6 @@ import org.apache.solr.cloud.ChaosMonkey;
import org.apache.solr.cloud.StoppableIndexingThread;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.CollectionStateWatcher;
import org.apache.solr.common.cloud.CompositeIdRouter;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.DocRouter;
@ -80,8 +79,8 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String SHARD1_0 = SHARD1 + "_0";
public static final String SHARD1_1 = SHARD1 + "_1";
private static final String SHARD1_0 = SHARD1 + "_0";
private static final String SHARD1_1 = SHARD1 + "_1";
public ShardSplitTest() {
schemaString = "schema15.xml"; // we need a string id
@ -161,9 +160,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
waitForRecoveriesToFinish(collectionName, true);
// let's wait to see parent shard become inactive
CountDownLatch latch = new CountDownLatch(1);
client.getZkStateReader().registerCollectionStateWatcher(collectionName, new CollectionStateWatcher() {
@Override
public boolean onStateChanged(Set<String> liveNodes, DocCollection collectionState) {
client.getZkStateReader().registerCollectionStateWatcher(collectionName, (liveNodes, collectionState) -> {
Slice parent = collectionState.getSlice(SHARD1);
Slice slice10 = collectionState.getSlice(SHARD1_0);
Slice slice11 = collectionState.getSlice(SHARD1_1);
@ -175,7 +172,6 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
return true; // removes the watch
}
return false;
}
});
latch.await(1, TimeUnit.MINUTES);
if (latch.getCount() != 0) {
@ -211,22 +207,19 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
}
if (state == RequestStatusState.COMPLETED) {
CountDownLatch newReplicaLatch = new CountDownLatch(1);
client.getZkStateReader().registerCollectionStateWatcher(collectionName, new CollectionStateWatcher() {
@Override
public boolean onStateChanged(Set<String> liveNodes, DocCollection collectionState) {
client.getZkStateReader().registerCollectionStateWatcher(collectionName, (liveNodes, collectionState) -> {
if (liveNodes.size() != liveNodeCount) {
return false;
}
Slice slice = collectionState.getSlice(SHARD1_0);
if (slice.getReplicas().size() == 2) {
if (!slice.getReplicas().stream().anyMatch(r -> r.getState() == Replica.State.RECOVERING)) {
if (slice.getReplicas().stream().noneMatch(r -> r.getState() == Replica.State.RECOVERING)) {
// we see replicas and none of them are recovering
newReplicaLatch.countDown();
return true;
}
}
return false;
}
});
newReplicaLatch.await(30, TimeUnit.SECONDS);
// check consistency of sub-shard replica explicitly because checkShardConsistency methods doesn't
@ -422,13 +415,9 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
AtomicBoolean stop = new AtomicBoolean();
AtomicBoolean killed = new AtomicBoolean(false);
Runnable monkey = new Runnable() {
@Override
public void run() {
Runnable monkey = () -> {
ZkStateReader zkStateReader = cloudClient.getZkStateReader();
zkStateReader.registerCollectionStateWatcher(AbstractDistribZkTestBase.DEFAULT_COLLECTION, new CollectionStateWatcher() {
@Override
public boolean onStateChanged(Set<String> liveNodes, DocCollection collectionState) {
zkStateReader.registerCollectionStateWatcher(AbstractDistribZkTestBase.DEFAULT_COLLECTION, (liveNodes, collectionState) -> {
if (stop.get()) {
return true; // abort and remove the watch
}
@ -450,13 +439,10 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
}
log.info("Monkey thread found only one replica for {} {}", AbstractDistribZkTestBase.DEFAULT_COLLECTION, SHARD1);
return false;
}
});
}
};
Thread monkeyThread = null;
monkeyThread = new Thread(monkey);
Thread monkeyThread = new Thread(monkey);
monkeyThread.start();
try {
CollectionAdminRequest.SplitShard splitShard = CollectionAdminRequest.splitShard(AbstractDistribZkTestBase.DEFAULT_COLLECTION);
@ -497,9 +483,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
waitForRecoveriesToFinish(AbstractDistribZkTestBase.DEFAULT_COLLECTION, true);
// let's wait for the overseer to switch shard states
CountDownLatch latch = new CountDownLatch(1);
cloudClient.getZkStateReader().registerCollectionStateWatcher(AbstractDistribZkTestBase.DEFAULT_COLLECTION, new CollectionStateWatcher() {
@Override
public boolean onStateChanged(Set<String> liveNodes, DocCollection collectionState) {
cloudClient.getZkStateReader().registerCollectionStateWatcher(AbstractDistribZkTestBase.DEFAULT_COLLECTION, (liveNodes, collectionState) -> {
Slice parent = collectionState.getSlice(SHARD1);
Slice slice10 = collectionState.getSlice(SHARD1_0);
Slice slice11 = collectionState.getSlice(SHARD1_1);
@ -519,7 +503,6 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
return true;
}
return false;
}
});
latch.await(2, TimeUnit.MINUTES);
@ -660,9 +643,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
}
commit();
Thread indexThread = new Thread() {
@Override
public void run() {
Thread indexThread = new Thread(() -> {
Random random = random();
int max = atLeast(random, 401);
int sleep = atLeast(random, 25);
@ -689,8 +670,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
deleted.add(String.valueOf(id));
}
}
}
};
});
indexThread.start();
try {
@ -776,20 +756,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
collectionClient.commit();
for (int i = 0; i < 3; i++) {
try {
splitShard(collectionName, SHARD1, null, null, false);
break;
} catch (HttpSolrClient.RemoteSolrException e) {
if (e.code() != 500) {
throw e;
}
log.error("SPLITSHARD failed. " + (i < 2 ? " Retring split" : ""), e);
if (i == 2) {
fail("SPLITSHARD was not successful even after three tries");
}
}
}
trySplit(collectionName, null, SHARD1, 3);
waitForRecoveriesToFinish(collectionName, false);
@ -858,20 +825,7 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
collectionClient.commit();
for (int i = 0; i < 3; i++) {
try {
splitShard(collectionName, null, null, splitKey, false);
break;
} catch (HttpSolrClient.RemoteSolrException e) {
if (e.code() != 500) {
throw e;
}
log.error("SPLITSHARD failed. " + (i < 2 ? " Retring split" : ""), e);
if (i == 2) {
fail("SPLITSHARD was not successful even after three tries");
}
}
}
trySplit(collectionName, splitKey, null, 3);
waitForRecoveriesToFinish(collectionName, false);
SolrQuery solrQuery = new SolrQuery("*:*");
@ -886,6 +840,23 @@ public class ShardSplitTest extends AbstractFullDistribZkTestBase {
}
}
private void trySplit(String collectionName, String splitKey, String shardId, int maxTries) throws SolrServerException, IOException {
for (int i = 0; i < maxTries; i++) {
try {
splitShard(collectionName, shardId, null, splitKey, false);
break;
} catch (HttpSolrClient.RemoteSolrException e) {
if (e.code() != 500) {
throw e;
}
log.error("SPLITSHARD failed. " + (i < maxTries - 1 ? " Retring split" : ""), e);
if (i == 2) {
fail("SPLITSHARD was not successful even after three tries");
}
}
}
}
protected void checkDocCountsAndShardStates(int[] docCounts, int numReplicas, Set<String> documentIds) throws Exception {
ClusterState clusterState = null;
Slice slice1_0 = null, slice1_1 = null;