static RequestStatusState getRequestStateAfterCompletion(String requestId, int waitForSeconds, SolrClient client) throws IOException, SolrServerException { RequestStatusState state = null; final TimeOut timeout = new TimeOut(waitForSeconds, TimeUnit.SECONDS, TimeSource.NANO_TIME); while (!timeout.hasTimedOut()) { state = getRequestState(requestId, client); if (state == RequestStatusState.COMPLETED || state == RequestStatusState.FAILED) { return state; } try { Thread.sleep(1000); } catch (InterruptedException e) { } } return state; }
long timeout = 10000; // number of milliseconds before timeout TimeOut t = new TimeOut(new PhotoProcessor(filePath, params), timeout, true); try { boolean sucess = t.execute(); // Will return false if this times out if (!sucess) { // This thread timed out } else { // This thread ran completely and did not timeout } } catch (InterruptedException e) {}
/** * You can call this method to wait while the ChaosMonkey is running, it waits approximately the specified time, and periodically * logs the status of the collection * @param runLength The time in ms to wait * @param collectionName The main collection being used for the ChaosMonkey * @param zkStateReader current state reader */ public static void wait(long runLength, String collectionName, ZkStateReader zkStateReader) throws InterruptedException { TimeOut t = new TimeOut(runLength, TimeUnit.MILLISECONDS, TimeSource.NANO_TIME); while (!t.hasTimedOut()) { Thread.sleep(Math.min(1000, t.timeLeft(TimeUnit.MILLISECONDS))); logCollectionStateSummary(collectionName, zkStateReader); } }
static void waitForNewLeader(CloudSolrClient cloudClient, String shardName, Replica oldLeader, TimeOut timeOut) throws Exception { log.info("Will wait for a node to become leader for {} secs", timeOut.timeLeft(SECONDS)); ZkStateReader zkStateReader = cloudClient.getZkStateReader(); zkStateReader.forceUpdateCollection(DEFAULT_COLLECTION); for (; ; ) { ClusterState clusterState = zkStateReader.getClusterState(); DocCollection coll = clusterState.getCollection("collection1"); Slice slice = coll.getSlice(shardName); if (slice.getLeader() != null && !slice.getLeader().equals(oldLeader) && slice.getLeader().getState() == Replica.State.ACTIVE) { log.info("Old leader {}, new leader {}. New leader got elected in {} ms", oldLeader, slice.getLeader(),timeOut.timeElapsed(MILLISECONDS) ); break; } if (timeOut.hasTimedOut()) { Diagnostics.logThreadDumps("Could not find new leader in specified timeout"); zkStateReader.getZkClient().printLayoutToStdOut(); fail("Could not find new leader even after waiting for " + timeOut.timeElapsed(MILLISECONDS) + "ms"); } Thread.sleep(100); } }
Replica leader = zkStateReader.getLeaderRetry(collectionName, s.getName(), (int)timeout.timeLeft(TimeUnit.MILLISECONDS)); long leaderIndexVersion = -1; while (!timeout.hasTimedOut()) { leaderIndexVersion = getIndexVersion(leader); if (leaderIndexVersion >= 0) { if (timeout.hasTimedOut()) { fail("Unable to get leader indexVersion"); if (timeout.hasTimedOut()) { logReplicaTypesReplicationInfo(collectionName, zkStateReader); fail(String.format(Locale.ROOT, "Timed out waiting for replica %s (%d) to replicate from leader %s (%d)", pullReplica.getName(), replicaIndexVersion, leader.getName(), leaderIndexVersion));
public static boolean waitForServerDown(String hp, long timeoutMs) { final TimeOut timeout = new TimeOut(timeoutMs, TimeUnit.MILLISECONDS, TimeSource.NANO_TIME); while (true) { try { HostPort hpobj = parseHostPortList(hp).get(0); send4LetterWord(hpobj.host, hpobj.port, "stat"); } catch (IOException e) { return true; } if (timeout.hasTimedOut()) { break; } try { Thread.sleep(250); } catch (InterruptedException e) { // ignore } } return false; }
protected void checkForCollection(String collectionName, List<Integer> numShardsNumReplicaList, List<String> nodesAllowedToRunShards) throws Exception { // check for an expectedSlices new collection - we poll the state final TimeOut timeout = new TimeOut(120, TimeUnit.SECONDS, TimeSource.NANO_TIME); boolean success = false; String checkResult = "Didnt get to perform a single check"; while (! timeout.hasTimedOut()) { checkResult = checkCollectionExpectations(collectionName, numShardsNumReplicaList, nodesAllowedToRunShards); if (checkResult == null) { success = true; break; } Thread.sleep(500); } if (!success) { super.printLayout(); fail(checkResult); } }
public static void waitForNon403or404or503(HttpSolrClient collectionClient) throws Exception { SolrException exp = null; final TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME); while (! timeout.hasTimedOut()) { boolean missing = false; try { collectionClient.query(new SolrQuery("*:*")); } catch (SolrException e) { if (!(e.code() == 403 || e.code() == 503 || e.code() == 404)) { throw e; } exp = e; missing = true; } if (!missing) { return; } Thread.sleep(50); } fail("Could not find the new collection - " + exp.code() + " : " + collectionClient.getBaseURL()); }