Skip to content

Commit

Permalink
general code cleanup to remove most IntelliJ warnings (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
twincitiesguy authored Jul 16, 2021
1 parent ed02e86 commit 2789a51
Show file tree
Hide file tree
Showing 22 changed files with 101 additions and 124 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/emc/rest/smart/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@
* </ul>
*/
public class Host implements HostStats {

private static final Logger log = LoggerFactory.getLogger(Host.class);

public static final int DEFAULT_ERROR_WAIT_MS = 1500;
public static final int LOG_DELAY = 60000; // 1 minute
public static final int MAX_COOL_DOWN_EXP = 4;

private String name;
private final String name;
private boolean healthy = true;
protected int errorWaitTime = DEFAULT_ERROR_WAIT_MS;

Expand Down Expand Up @@ -81,7 +80,7 @@ public synchronized void connectionClosed() {
if (openConnections < 0) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastLogTime > LOG_DELAY) {
log.warn("openConnections for host {} is {} !", this.toString(), Integer.toString(openConnections));
log.warn("openConnections for host {} is {} !", this, openConnections);
lastLogTime = currentTime;
}
}
Expand Down Expand Up @@ -174,7 +173,7 @@ public int hashCode() {
@Override
public String toString() {
return String.format("%s{totalConnections=%d, totalErrors=%d, openConnections=%d, lastConnectionTime=%s}",
name, totalConnections, totalErrors, openConnections, new Date(lastConnectionTime).toString());
name, totalConnections, totalErrors, openConnections, new Date(lastConnectionTime));
}

public int getErrorWaitTime() {
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/emc/rest/smart/HostStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@
import java.util.Date;

public interface HostStats {

long getTotalConnections();


long getTotalErrors();


int getOpenConnections();


Date getLastConnectionTime();

}
14 changes: 6 additions & 8 deletions src/main/java/com/emc/rest/smart/LoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.util.*;

public class LoadBalancer {
private final Deque<Host> hosts = new ArrayDeque<Host>();
private final Deque<Host> hosts = new ArrayDeque<>();
private List<HostVetoRule> vetoRules;

public LoadBalancer(List<Host> initialHosts) {
Expand Down Expand Up @@ -92,14 +92,14 @@ protected boolean shouldVeto(Host host, Map<String, Object> requestProperties) {
* Returns a list of all known hosts. This list is a clone; modification will not affect the load balancer
*/
public synchronized List<Host> getAllHosts() {
return new ArrayList<Host>(hosts);
return new ArrayList<>(hosts);
}

/**
* Returns stats for all active hosts in this load balancer
*/
public synchronized HostStats[] getHostStats() {
return hosts.toArray(new HostStats[hosts.size()]);
return hosts.toArray(new HostStats[0]);
}

/**
Expand Down Expand Up @@ -138,9 +138,9 @@ public long getOpenConnections() {
/**
* Ensure this method is called sparingly as it will block getTopHost() calls, pausing all new connections!
*/
protected void updateHosts(List<Host> updatedHosts) throws Exception {
protected void updateHosts(List<Host> updatedHosts) {
// don't modify the parameter
List<Host> hostList = new ArrayList<Host>(updatedHosts);
List<Host> hostList = new ArrayList<>(updatedHosts);

// remove hosts from stored list that are not present in updated list
// remove hosts in updated list that are already present in stored list
Expand All @@ -166,9 +166,7 @@ protected void updateHosts(List<Host> updatedHosts) throws Exception {
}

// what's left in the updated list are new hosts, so add them
for (Host newHost : hostList) {
hosts.add(newHost);
}
hosts.addAll(hostList);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
@Produces("application/octet-stream")
@Consumes("application/octet-stream")
public class OctetStreamXmlProvider implements MessageBodyReader<Object> {
private MessageBodyReader<Object> delegate;
private final MessageBodyReader<Object> delegate;

public OctetStreamXmlProvider(@Context Injectable<SAXParserFactory> spf, @Context Providers ps) {
this.delegate = new XMLRootElementProvider.General(spf, ps);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/emc/rest/smart/PollingDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PollingDaemon extends Thread {

private static final Logger log = LoggerFactory.getLogger(PollingDaemon.class);

private SmartConfig smartConfig;
private final SmartConfig smartConfig;
private boolean running = true;

public PollingDaemon(SmartConfig smartConfig) {
Expand Down Expand Up @@ -85,9 +85,9 @@ public void run() {

long callTime = System.currentTimeMillis() - start;
try {
long sleepTime = smartConfig.getPollInterval() * 1000 - callTime;
long sleepTime = smartConfig.getPollInterval() * 1000L - callTime;
if (sleepTime < 0) sleepTime = 0;
log.debug("polling daemon finished; will poll again in {}ms..", Long.toString(sleepTime));
log.debug("polling daemon finished; will poll again in {}ms..", sleepTime);
if (sleepTime > 0) Thread.sleep(sleepTime);
} catch (InterruptedException e) {
log.warn("interrupted while sleeping", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/emc/rest/smart/SizeOverrideWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.lang.reflect.Type;

public class SizeOverrideWriter<T> implements MessageBodyWriter<T> {
private static final ThreadLocal<Long> entitySize = new ThreadLocal<Long>();
private static final ThreadLocal<Long> entitySize = new ThreadLocal<>();

public static Long getEntitySize() {
return entitySize.get();
Expand All @@ -51,7 +51,7 @@ public static void setEntitySize(Long size) {
entitySize.set(size);
}

private MessageBodyWriter<T> delegate;
private final MessageBodyWriter<T> delegate;

public SizeOverrideWriter(MessageBodyWriter<T> delegate) {
this.delegate = delegate;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/emc/rest/smart/SmartClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static ApacheHttpClient4Handler createApacheClientHandler(SmartConfig smartConfi
ClientConfig clientConfig = new DefaultClientConfig();

// set up multi-threaded connection pool
org.apache.http.impl.conn.PoolingClientConnectionManager connectionManager = new org.apache.http.impl.conn.PoolingClientConnectionManager();
org.apache.http.impl.conn.PoolingHttpClientConnectionManager connectionManager = new org.apache.http.impl.conn.PoolingHttpClientConnectionManager();
// 999 maximum active connections (max allowed)
connectionManager.setDefaultMaxPerRoute(999);
connectionManager.setMaxTotal(999);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/emc/rest/smart/SmartConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ public class SmartConfig {
private String proxyUser;
private String proxyPass;

private LoadBalancer loadBalancer;
private final LoadBalancer loadBalancer;
private HostListProvider hostListProvider;
private int pollInterval = DEFAULT_POLL_INTERVAL;
private boolean hostUpdateEnabled = true;
private boolean healthCheckEnabled = true;

private Map<String, Object> properties = new HashMap<String, Object>();
private final Map<String, Object> properties = new HashMap<>();

/**
* @see #SmartConfig(LoadBalancer)
*/
public SmartConfig(String... initialHostNames) {
List<Host> hostList = new ArrayList<Host>();
List<Host> hostList = new ArrayList<>();
for (String hostName : initialHostNames) {
hostList.add(new Host(hostName));
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/emc/rest/smart/SmartFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class SmartFilter extends ClientFilter {
public static final String BYPASS_LOAD_BALANCER = "com.emc.rest.smart.bypassLoadBalancer";

private SmartConfig smartConfig;
private final SmartConfig smartConfig;

public SmartFilter(SmartConfig smartConfig) {
this.smartConfig = smartConfig;
Expand Down Expand Up @@ -75,12 +75,8 @@ public ClientResponse handle(ClientRequest request) throws ClientHandlerExceptio
ClientResponse response = getNext().handle(request);

// capture request stats
if (response.getStatus() >= 500 && response.getStatus() != 501) {
// except for 501 (not implemented), all 50x responses are considered server errors
host.callComplete(true);
} else {
host.callComplete(false);
}
// except for 501 (not implemented), all 50x responses are considered server errors
host.callComplete(response.getStatus() >= 500 && response.getStatus() != 501);

// wrap the input stream so we can capture the actual connection close
response.setEntityInputStream(new WrappedInputStream(response.getEntityInputStream(), host));
Expand All @@ -99,8 +95,8 @@ public ClientResponse handle(ClientRequest request) throws ClientHandlerExceptio
/**
* captures closure in host statistics
*/
protected class WrappedInputStream extends FilterInputStream {
private Host host;
protected static class WrappedInputStream extends FilterInputStream {
private final Host host;
private boolean closed = false;

public WrappedInputStream(InputStream in, Host host) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/emc/rest/smart/ecs/EcsHostListProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

Expand All @@ -50,10 +51,10 @@ public class EcsHostListProvider implements HostListProvider {
public static final int DEFAULT_PORT = 9021;

protected final SimpleDateFormat rfc822DateFormat;
private Client client;
private LoadBalancer loadBalancer;
private String user;
private String secret;
private final Client client;
private final LoadBalancer loadBalancer;
private final String user;
private final String secret;
private String protocol = DEFAULT_PROTOCOL;
private int port = DEFAULT_PORT;
private List<Vdc> vdcs;
Expand All @@ -70,7 +71,7 @@ public EcsHostListProvider(Client client, LoadBalancer loadBalancer, String user
public List<Host> getHostList() {
if (vdcs == null || vdcs.isEmpty()) return getDataNodes(loadBalancer.getTopHost(null));

List<Host> hostList = new ArrayList<Host>();
List<Host> hostList = new ArrayList<>();

for (Vdc vdc : vdcs) {
if (vdc.getHosts().isEmpty()) log.warn("VDC " + vdc.getName() + " has no hosts!");
Expand Down Expand Up @@ -111,8 +112,7 @@ public void runHealthCheck(Host host) {
PingItem pingItem = response.getPingItemMap().get(PingItem.MAINTENANCE_MODE);
if (pingItem != null) status = pingItem.getStatus();
}
if (status == PingItem.Status.ON) ((VdcHost) host).setMaintenanceMode(true);
else ((VdcHost) host).setMaintenanceMode(false);
((VdcHost) host).setMaintenanceMode(status == PingItem.Status.ON);
}
}

Expand Down Expand Up @@ -153,7 +153,7 @@ protected List<Host> getDataNodes(Host host) {
log.debug("retrieving VDC node list from {}", host.getName());
List<String> dataNodes = request.get(ListDataNode.class).getDataNodes();

List<Host> hosts = new ArrayList<Host>();
List<Host> hosts = new ArrayList<>();
for (String node : dataNodes) {
hosts.add(new Host(node));
}
Expand All @@ -171,8 +171,8 @@ protected URI getRequestUri(Host host, String path) {

protected String getSignature(String canonicalString, String secret) throws Exception {
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA1"));
String signature = new String(Base64.encodeBase64(mac.doFinal(canonicalString.getBytes("UTF-8"))));
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
String signature = new String(Base64.encodeBase64(mac.doFinal(canonicalString.getBytes(StandardCharsets.UTF_8))));
log.debug("canonicalString:\n" + canonicalString);
log.debug("signature:\n" + signature);
return signature;
Expand All @@ -182,7 +182,7 @@ protected void updateVdcNodes(Vdc vdc, List<Host> nodeList) {
if (nodeList == null || nodeList.isEmpty()) throw new RuntimeException("node list is empty");

// make sure the hosts are associated with the VDC first
List<VdcHost> vdcNodeList = new ArrayList<VdcHost>();
List<VdcHost> vdcNodeList = new ArrayList<>();
for (Host host : nodeList) {
vdcNodeList.add(new VdcHost(vdc, host.getName()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/emc/rest/smart/ecs/ListDataNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

@XmlRootElement(name = "ListDataNode")
public class ListDataNode {
private List<String> dataNodes = new ArrayList<String>();
private List<String> dataNodes = new ArrayList<>();
private String versionInfo;

@XmlElements(@XmlElement(name = "DataNodes"))
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/emc/rest/smart/ecs/PingResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

@XmlRootElement(name = "PingList")
public class PingResponse {
private List<PingItem> pingItems = new ArrayList<PingItem>();
private List<PingItem> pingItems = new ArrayList<>();

@XmlElement(name = "PingItem")
public List<PingItem> getPingItems() {
Expand All @@ -51,7 +51,7 @@ public void setPingItems(List<PingItem> pingItems) {
public Map<String, PingItem> getPingItemMap() {
Map<String, PingItem> pingItemMap = null;
if (pingItems != null) {
pingItemMap = new HashMap<String, PingItem>();
pingItemMap = new HashMap<>();
for (PingItem item : pingItems) {
pingItemMap.put(item.getName(), item);
}
Expand All @@ -60,6 +60,6 @@ public Map<String, PingItem> getPingItemMap() {
}

public void setPingItemMap(Map<String, PingItem> pingItemMap) {
this.pingItems = new ArrayList<PingItem>(pingItemMap.values());
this.pingItems = new ArrayList<>(pingItemMap.values());
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/emc/rest/smart/ecs/Vdc.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

public class Vdc implements Iterable<VdcHost> {
private String name;
private List<VdcHost> hosts;
private final List<VdcHost> hosts;

public Vdc(String... hostNames) {
this.name = hostNames[0];
hosts = new ArrayList<VdcHost>();
hosts = new ArrayList<>();
for (String hostName : hostNames) {
hosts.add(new VdcHost(this, hostName));
}
Expand Down Expand Up @@ -66,7 +66,7 @@ public boolean isHealthy() {
}

protected List<VdcHost> createVdcHosts(List<? extends Host> hosts) {
List<VdcHost> vdcHosts = new ArrayList<VdcHost>();
List<VdcHost> vdcHosts = new ArrayList<>();
for (Host host : hosts) {
vdcHosts.add(new VdcHost(this, host.getName()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/emc/rest/smart/ecs/VdcHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.emc.rest.smart.Host;

public class VdcHost extends Host {
private Vdc vdc;
private final Vdc vdc;
private boolean maintenanceMode;

public VdcHost(Vdc vdc, String name) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/emc/rest/util/SizedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
* reached its end when the specified size has been read.
*/
public class SizedInputStream extends InputStream {
private InputStream source;
private long size;
private final InputStream source;
private final long size;
private long read = 0;

public SizedInputStream(InputStream source, long size) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/emc/rest/util/StreamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class StreamUtil {
/**
* Closes streams no matter what.
*/
public static String readAsString(InputStream inputStream) throws IOException {
public static String readAsString(InputStream inputStream) {
if (inputStream == null) return null;
try {
return new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A").next();
Expand All @@ -65,7 +65,7 @@ public static long copy(InputStream is, OutputStream os, long maxBytes) throws I

try {
while (count < maxBytes) {
maxRead = (int) Math.min((long) buffer.length, maxBytes - count);
maxRead = (int) Math.min(buffer.length, maxBytes - count);
if (-1 == (read = is.read(buffer, 0, maxRead))) break;
os.write(buffer, 0, read);
count += read;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/emc/rest/smart/HostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testHost() throws Exception {
Assert.assertFalse(host.isHealthy());

// cool down should be compounded for consecutive errors (multiplied by powers of 2)
Thread.sleep(2 * errorWaitTime - 500); // wait until just before cool down is over
Thread.sleep(2L * errorWaitTime - 500); // wait until just before cool down is over
Assert.assertFalse(host.isHealthy());

Thread.sleep(600); // wait until cool down period is over
Expand All @@ -104,7 +104,7 @@ public void testHost() throws Exception {
Assert.assertFalse(host.isHealthy());

// cool down should be compounded for consecutive errors (multiplied by powers of 2)
Thread.sleep(2 * 2 * errorWaitTime - 500); // wait until just before cool down is over
Thread.sleep(2L * 2 * errorWaitTime - 500); // wait until just before cool down is over
Assert.assertFalse(host.isHealthy());

Thread.sleep(600); // wait until cool down period is over
Expand Down
Loading

0 comments on commit 2789a51

Please sign in to comment.