import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
private static final String QP_KEYWORD = "keyword";
private static final String QP_DEST = "dest";
private static final String QP_TTL = "ttl";
+ private static final String OWNED_STR = "Owned";
// BACKUP data (only buckets for where we are the owner, or a backup)
* @throws IOException when error occurred
*/
public static void bucketMessage(
- final PrintStream out, final String keyword, String message) throws IOException {
+ final PrintStream out, final String keyword, String message) {
if (keyword == null) {
out.println("'keyword' is mandatory");
* on any server.
* Each instance of this class corresponds to a 'Bucket' instance.
*/
+ @EqualsAndHashCode
private static class TestBucket implements Comparable<TestBucket> {
// bucket number
int index;
* around a 'TestServer' instance, as it would be if another specific
* server failed.
*/
+ @EqualsAndHashCode
private static class AdjustedTestServer
implements Comparable<AdjustedTestServer> {
TestServer server;
* 'needBuckets' TreeSet: those with the fewest buckets allocated are
* at the head of the list.
*/
- Comparator<TestServer> bucketCount = new Comparator<>() {
- @Override
- public int compare(TestServer s1, TestServer s2) {
- int rval = s1.buckets.size() - s2.buckets.size();
- if (rval == 0) {
- rval = Util.uuidComparator.compare(s1.uuid, s2.uuid);
- }
- return rval;
+ Comparator<TestServer> bucketCount = (s1, s2) -> {
+ int rval = s1.buckets.size() - s2.buckets.size();
+ if (rval == 0) {
+ rval = Util.uuidComparator.compare(s1.uuid, s2.uuid);
}
+ return rval;
};
// sort servers according to the order in which they can
// populate a 'TreeSet' of 'AdjustedTestServer' instances based
// the failure of 'failedServer'
TreeSet<AdjustedTestServer> adjustedTestServers =
- new TreeSet<AdjustedTestServer>();
+ new TreeSet<>();
for (TestServer server : testServers.values()) {
if (server == failedServer
|| Objects.equals(siteSocketAddress,
// dump out 'owned' bucket information
if (ts.buckets.isEmpty()) {
// no buckets owned by this server
- out.printf(format, ts.uuid, "Owned", 0, "");
+ out.printf(format, ts.uuid, OWNED_STR, 0, "");
} else {
// dump out primary buckets information
totalOwner +=
- dumpBucketsSegment(out, format, ts.buckets, ts.uuid.toString(), "Owned");
+ dumpBucketsSegment(out, format, ts.buckets, ts.uuid.toString(), OWNED_STR);
}
// optionally dump out primary buckets information
totalPrimary +=
// optionally dump out unassigned owned buckets information
if (dumpBucketsSegment(out, format, nullServer.buckets,
- uuidField, "Owned") != 0) {
+ uuidField, OWNED_STR) != 0) {
uuidField = "";
}
// optionally dump out unassigned primary backup buckets information
&& oldOwner.isActive()
&& (delay = getTimeout()) > 0) {
// ignore return value -- 'data' will indicate the result
- dataAvailable.await(delay, TimeUnit.MILLISECONDS);
+ if (!dataAvailable.await(delay, TimeUnit.MILLISECONDS)) {
+ logger.error("CountDownLatch await time reached");
+ }
}
if (lclData == null) {
// no data available -- log an error, and abort
} catch (Exception e) {
logger.error("Exception in {}", this, e);
} finally {
- run_cleanup();
+ runCleanup();
}
}
- private void run_cleanup() {
+ private void runCleanup() {
/*
* cleanly leave state -- we want to make sure that messages
* are processed in order, so the queue needs to remain until
// 'pingHosts' error
static final String PINGHOSTS_ERROR = "Server.pingHosts error";
+ // a string for print
+ static final String PRINTOUT_DASHES = "-------";
+
/*==============================*/
/* Comparable<Server> interface */
/*==============================*/
if (responseCallback != null) {
responseCallback.exceptionResponse(e);
}
- MainLoop.queueWork(() -> {
- // this runs in the 'MainLoop' thread
+ // this runs in the 'MainLoop' thread
- // the DNS cache may have been out-of-date when this server
- // was first contacted -- fix the problem, if needed
- checkServer();
- });
+ // the DNS cache may have been out-of-date when this server
+ // was first contacted -- fix the problem, if needed
+ MainLoop.queueWork(this::checkServer);
}
});
}
sendThreadPool =
new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.MILLISECONDS,
- new LinkedTransferQueue<Runnable>());
+ new LinkedTransferQueue<>());
sendThreadPool.allowCoreThreadTimeOut(true);
}
return sendThreadPool;
new String(Base64.getEncoder().encode(bos.toByteArray()),
StandardCharsets.UTF_8),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
-
- // loop through hosts
- for (InetSocketAddress host : hosts) {
- HttpClient httpClient = null;
-
- try {
- httpClient = buildClient(host.toString(), host,
- socketAddressToName(host));
- getTarget(httpClient).path("admin").request().post(entity);
- httpClient.shutdown();
- httpClient = null;
- } catch (KeyManagementException | NoSuchAlgorithmException e) {
- out.println(host + ": Unable to create client connection");
- logger.error(PINGHOSTS_ERROR, e);
- } catch (NoSuchFieldException | IllegalAccessException e) {
- out.println(host + ": Unable to get link to target");
- logger.error(PINGHOSTS_ERROR, e);
- } catch (Exception e) {
- out.println(host + ": " + e);
- logger.error(PINGHOSTS_ERROR, e);
- }
- if (httpClient != null) {
- httpClient.shutdown();
- }
- }
+ pingHostsLoop(entity, out, hosts);
} catch (IOException e) {
out.println("Unable to generate 'ping' data: " + e);
logger.error(PINGHOSTS_ERROR, e);
}
}
+ /**
+ * This method is used for pingHosts method to reduce its Cognitive Complexity.
+ *
+ * @param entity for sending out to all hosts
+ * @param out the 'PrintStream' to use for displaying information
+ * @param hosts a collection of 'InetSocketAddress' instances, which are
+ * the hosts to send the information to
+ */
+ static void pingHostsLoop(final Entity<String> entity,
+ final PrintStream out,
+ final Collection<InetSocketAddress> hosts) {
+ // loop through hosts
+ for (InetSocketAddress host : hosts) {
+ HttpClient httpClient = null;
+
+ try {
+ httpClient = buildClient(host.toString(), host,
+ socketAddressToName(host));
+ getTarget(httpClient).path("admin").request().post(entity);
+ httpClient.shutdown();
+ httpClient = null;
+ } catch (KeyManagementException | NoSuchAlgorithmException e) {
+ out.println(host + ": Unable to create client connection");
+ logger.error(PINGHOSTS_ERROR, e);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ out.println(host + ": Unable to get link to target");
+ logger.error(PINGHOSTS_ERROR, e);
+ } catch (Exception e) {
+ out.println(host + ": " + e);
+ logger.error(PINGHOSTS_ERROR, e);
+ }
+ if (httpClient != null) {
+ httpClient.shutdown();
+ }
+ }
+ }
+
/**
* This method may be invoked from any thread:
* Dump out the current 'servers' table in a human-readable table form.
"Count", "Update Time", "Elapsed", "Allowed");
out.printf(format, "", "----", "----------", "----",
"---------------", "----",
- "-----", "-----------", "-------", "-------");
+ "-----", "-----------", PRINTOUT_DASHES, PRINTOUT_DASHES);
// @formatter:on
} else {
// @formatter:off
out.printf(format, "", "UUID", "IP Address", "Port",
"Count", "Update Time", "Elapsed", "Allowed");
out.printf(format, "", "----", "----------", "----",
- "-----", "-----------", "-------", "-------");
+ "-----", "-----------", PRINTOUT_DASHES, PRINTOUT_DASHES);
// @formatter:on
}