diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryConfiguration.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryConfiguration.java index 0cbe512..fe90e66 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryConfiguration.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryConfiguration.java @@ -11,9 +11,6 @@ */ public class DockerSwarmDiscoveryConfiguration { - private DockerSwarmDiscoveryConfiguration() { - } - // comma delimited list of networks to look for services on public static final PropertyDefinition DOCKER_NETWORK_NAMES = new SimplePropertyDefinition("docker-network-names", PropertyTypeConverter.STRING); @@ -48,4 +45,7 @@ private DockerSwarmDiscoveryConfiguration() { public static final PropertyDefinition STRICT_DOCKER_SERVICE_NAME_COMPARISON = new SimplePropertyDefinition("strict-docker-service-name-comparison", true, PropertyTypeConverter.BOOLEAN); + private DockerSwarmDiscoveryConfiguration() { + } + } diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategy.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategy.java index 2c5930a..6da9774 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategy.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategy.java @@ -28,11 +28,10 @@ public class DockerSwarmDiscoveryStrategy extends AbstractDiscoveryStrategy { /** * Constructor * - * @param localDiscoveryNode * @param logger * @param properties */ - public DockerSwarmDiscoveryStrategy(DiscoveryNode localDiscoveryNode, ILogger logger, Map properties) { + public DockerSwarmDiscoveryStrategy(ILogger logger, Map properties) { super(logger, properties); diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategyFactory.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategyFactory.java index c36da90..499cb86 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategyFactory.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/DockerSwarmDiscoveryStrategyFactory.java @@ -36,7 +36,7 @@ public DiscoveryStrategy newDiscoveryStrategy(DiscoveryNode discoveryNode, ILogger logger, Map properties) { - return new DockerSwarmDiscoveryStrategy(discoveryNode, logger, properties); + return new DockerSwarmDiscoveryStrategy(logger, properties); } } diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmAddressPicker.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmAddressPicker.java index e649c08..1404d74 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmAddressPicker.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmAddressPicker.java @@ -1,7 +1,6 @@ package org.bitsofinfo.hazelcast.discovery.docker.swarm; import com.hazelcast.instance.AddressPicker; -import com.hazelcast.logging.ILogger; import com.hazelcast.nio.Address; import java.net.URI; @@ -46,9 +45,7 @@ public class SwarmAddressPicker implements AddressPicker { /** * Constructor */ - - - public SwarmAddressPicker(final ILogger iLogger) { + public SwarmAddressPicker() { final String dockerNetworkNames = System.getProperty(PROP_DOCKER_NETWORK_NAMES); final String dockerServiceLabels = System.getProperty(PROP_DOCKER_SERVICE_LABELS); final String dockerServiceNames = System.getProperty(PROP_DOCKER_SERVICE_NAMES); @@ -64,19 +61,19 @@ public SwarmAddressPicker(final ILogger iLogger) { skipVerifySsl = Boolean.valueOf(System.getProperty(PROP_SKIP_VERIFY_SSL)); } - initialize(iLogger, dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, swarmMgrUri, skipVerifySsl); + initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, swarmMgrUri, skipVerifySsl); } - public SwarmAddressPicker(final ILogger iLogger, final String dockerNetworkNames, final String dockerServiceLabels, + public SwarmAddressPicker(final String dockerNetworkNames, final String dockerServiceLabels, final String dockerServiceNames, final Integer hazelcastPeerPort) { String swarmMgrUri = System.getenv("DOCKER_HOST"); Boolean skipVerifySsl = false; - initialize(iLogger, dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, swarmMgrUri, skipVerifySsl); + initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, swarmMgrUri, skipVerifySsl); } - private void initialize(final ILogger iLogger, final String dockerNetworkNames, final String dockerServiceLabels, + private void initialize(final String dockerNetworkNames, final String dockerServiceLabels, final String dockerServiceNames, final Integer hazelcastPeerPort, final String swarmMgrUri, final Boolean skipVerifySsl) { final int port; diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmDiscoveryUtil.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmDiscoveryUtil.java index 16319bc..b48107b 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmDiscoveryUtil.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmDiscoveryUtil.java @@ -48,56 +48,29 @@ public class SwarmDiscoveryUtil { private static final int SOCKET_TIMEOUT_MILLIS = (int) TimeUnit.SECONDS.toMillis(1); private static final int SOCKET_BACKLOG_LENGTH = 100; - private Set dockerNetworkNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); - private Map dockerServiceLabels = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - private Set dockerServiceNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); - - private String rawDockerNetworkNames = null; - private String rawDockerServiceLabels = null; - private String rawDockerServiceNames = null; - - private Integer hazelcastPeerPort = 5701; - + private final Set dockerNetworkNames; + private final Map dockerServiceLabels; + private final Set dockerServiceNames; + + private final String rawDockerNetworkNames; + private final String rawDockerServiceLabels; + private final String rawDockerServiceNames; + + private final Integer hazelcastPeerPort; + private final boolean bindSocketChannel; + private final URI swarmMgrUri; + private final boolean skipVerifySsl; + private final boolean logAllServiceNamesOnFailedDiscovery; + private final boolean strictDockerServiceNameComparison; private DiscoveredContainer myContainer = null; private Address myAddress = null; - - private boolean bindSocketChannel = true; private ServerSocketChannel serverSocketChannel = null; - - private URI swarmMgrUri = null; - private boolean skipVerifySsl = false; - - private boolean logAllServiceNamesOnFailedDiscovery = false; - private boolean strictDockerServiceNameComparison = false; - // Since SwarmDiscoveryUtil is used by several components // the context lets us distinguish instances in logs private String context = null; private ILogger logger = Logger.getLogger(SwarmDiscoveryUtil.class); - public SwarmDiscoveryUtil(String context, - String rawDockerNetworkNames, - String rawDockerServiceLabels, - String rawDockerServiceNames, - Integer hazelcastPeerPort, - boolean bindSocketChannel, - boolean logAllServiceNamesOnFailedDiscovery, - boolean strictDockerServiceNameComparison) throws Exception { - - this(context, - rawDockerNetworkNames, - rawDockerServiceLabels, - rawDockerServiceNames, - hazelcastPeerPort, - bindSocketChannel, - new URI(System.getenv("DOCKER_HOST")), - false, - logAllServiceNamesOnFailedDiscovery, - strictDockerServiceNameComparison); - - } - public SwarmDiscoveryUtil(String context, String rawDockerNetworkNames, String rawDockerServiceLabels, @@ -114,59 +87,55 @@ public SwarmDiscoveryUtil(String context, this.skipVerifySsl = skipVerifySsl; this.logAllServiceNamesOnFailedDiscovery = logAllServiceNamesOnFailedDiscovery; this.strictDockerServiceNameComparison = strictDockerServiceNameComparison; - - - if (this.swarmMgrUri == null) { - if (System.getenv("DOCKER_HOST") != null && System.getenv("DOCKER_HOST").trim().isEmpty()) { - this.swarmMgrUri = new URI(System.getenv("DOCKER_HOST")); - } - } - this.bindSocketChannel = bindSocketChannel; this.rawDockerNetworkNames = rawDockerNetworkNames; this.rawDockerServiceLabels = rawDockerServiceLabels; this.rawDockerServiceNames = rawDockerServiceNames; this.hazelcastPeerPort = hazelcastPeerPort; - if (rawDockerNetworkNames != null && !rawDockerNetworkNames.trim().isEmpty()) { - for (String rawElement : rawDockerNetworkNames.split(",")) { - if (!rawElement.trim().isEmpty()) { - dockerNetworkNames.add(rawElement.trim()); - } - } - } else { + dockerNetworkNames = parseCommaSeparatedTokens(rawDockerNetworkNames); + dockerServiceNames = parseCommaSeparatedTokens(rawDockerServiceNames); + dockerServiceLabels = parseCommaSeparatedProperties(rawDockerServiceLabels); + + if (dockerNetworkNames.isEmpty()) { String msg = "SwarmDiscoveryUtil[" + this.context + "]() You must specify at least one value for 'docker-network-names'"; throw new Exception(msg); } - if (rawDockerServiceLabels != null && !rawDockerServiceLabels.trim().isEmpty()) { - for (String rawElement : rawDockerServiceLabels.split(",")) { - if (!rawElement.trim().isEmpty() && rawElement.indexOf('=') != -1) { - String[] labelVal = rawElement.split("="); - dockerServiceLabels.put(labelVal[0].trim(), labelVal[1].trim()); - } - } - } - - if (rawDockerServiceNames != null && !rawDockerServiceNames.trim().isEmpty()) { - for (String rawElement : rawDockerServiceNames.split(",")) { - if (!rawElement.trim().isEmpty()) { - dockerServiceNames.add(rawElement.trim()); - } - } - } - - // invalid setup if (dockerServiceLabels.isEmpty() && dockerServiceNames.isEmpty()) { String msg = "SwarmDiscoveryUtil[" + this.context + "]() You must specify at least one value for " + "either 'docker-service-names' or 'docker-service-labels'"; throw new Exception(msg); } - // discover self discoverSelf(); } + private Map parseCommaSeparatedProperties(String tokens) { + Map propertyMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + if (tokens != null && !tokens.trim().isEmpty()) { + for (String token : tokens.split(",")) { + if (!token.trim().isEmpty() && token.indexOf('=') != -1) { + String[] labelVal = token.split("="); + propertyMap.put(labelVal[0].trim(), labelVal[1].trim()); + } + } + } + return propertyMap; + } + + private Set parseCommaSeparatedTokens(String tokens) { + Set result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + if (tokens != null && !tokens.trim().isEmpty()) { + for (String token : tokens.split(",")) { + if (!token.trim().isEmpty()) { + result.add(token.trim()); + } + } + } + return result; + } + private void discoverSelf() throws Exception { @@ -201,7 +170,6 @@ private void discoverSelf() throws Exception { serverSocket.setReuseAddress(true); serverSocket.setSoTimeout(SOCKET_TIMEOUT_MILLIS); - try { InetSocketAddress inetSocketAddress = new InetSocketAddress(this.myAddress.getHost(), this.getHazelcastPeerPort()); logger.info("SwarmDiscoveryUtil[" + this.context + "] Trying to bind inet socket address: " + inetSocketAddress); @@ -229,70 +197,36 @@ public Set getDockerNetworkNames() { } - public void setDockerNetworkNames(Set dockerNetworkNames) { - this.dockerNetworkNames = dockerNetworkNames; - } - - public Map getDockerServiceLabels() { return dockerServiceLabels; } - public void setDockerServiceLabels(Map dockerServiceLabels) { - this.dockerServiceLabels = dockerServiceLabels; - } - - public Set getDockerServiceNames() { return dockerServiceNames; } - public void setDockerServiceNames(Set dockerServiceNames) { - this.dockerServiceNames = dockerServiceNames; - } - - public String getRawDockerNetworkNames() { return rawDockerNetworkNames; } - public void setRawDockerNetworkNames(String rawDockerNetworkNames) { - this.rawDockerNetworkNames = rawDockerNetworkNames; - } - - public String getRawDockerServiceLabels() { return rawDockerServiceLabels; } - public void setRawDockerServiceLabels(String rawDockerServiceLabels) { - this.rawDockerServiceLabels = rawDockerServiceLabels; - } - - public String getRawDockerServiceNames() { return rawDockerServiceNames; } - public void setRawDockerServiceNames(String rawDockerServiceNames) { - this.rawDockerServiceNames = rawDockerServiceNames; - } - - public Integer getHazelcastPeerPort() { return hazelcastPeerPort; } - public void setHazelcastPeerPort(Integer hazelcastPeerPort) { - this.hazelcastPeerPort = hazelcastPeerPort; - } - public Set discoverContainers() throws Exception { try { @@ -543,22 +477,16 @@ private Set discoverContainersViaCriteria(DockerClient dock return discoveredContainers; } - public DiscoveredContainer getMyContainer() { return myContainer; } - public Address getMyAddress() { return myAddress; } - public ServerSocketChannel getServerSocketChannel() { return serverSocketChannel; } } - - - diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmMemberAddressProvider.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmMemberAddressProvider.java index c1122e8..1c6b15e 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmMemberAddressProvider.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SwarmMemberAddressProvider.java @@ -80,8 +80,8 @@ public SwarmMemberAddressProvider() { strictDockerServiceNameComparison = Boolean.valueOf(System.getProperty(PROP_STRICT_DOCKER_SERVICE_NAME_COMPARISON)); } - initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, - swarmMgrUri, skipVerifySsl, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); + initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, + swarmMgrUri, skipVerifySsl, hazelcastPeerPort, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); } @@ -92,8 +92,8 @@ public SwarmMemberAddressProvider(final String dockerNetworkNames, final String String swarmMgrUri = System.getenv("DOCKER_HOST"); Boolean skipVerifySsl = false; - initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, - swarmMgrUri, skipVerifySsl, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); + initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, + swarmMgrUri, skipVerifySsl, hazelcastPeerPort, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); } /** @@ -123,7 +123,6 @@ public SwarmMemberAddressProvider(Properties properties) { dockerServiceNames = System.getProperty(PROP_DOCKER_SERVICE_NAMES); } - Object rawHazelcastPeerPort = properties.get(PROP_HAZELCAST_PEER_PORT); if (rawHazelcastPeerPort == null || rawHazelcastPeerPort.toString().trim().isEmpty()) { rawHazelcastPeerPort = System.getProperty(PROP_HAZELCAST_PEER_PORT); @@ -138,7 +137,6 @@ public SwarmMemberAddressProvider(Properties properties) { hazelcastPeerPort = (Integer) rawHazelcastPeerPort; } - String swarmMgrUri = (String) properties.get(PROP_SWARM_MGR_URI); if (swarmMgrUri == null || swarmMgrUri.trim().isEmpty()) { swarmMgrUri = System.getProperty(PROP_SWARM_MGR_URI); @@ -147,7 +145,6 @@ public SwarmMemberAddressProvider(Properties properties) { swarmMgrUri = System.getenv("DOCKER_HOST"); } - Object rawSkipVerifySsl = properties.get(PROP_SKIP_VERIFY_SSL); if (rawSkipVerifySsl == null || rawSkipVerifySsl.toString().trim().isEmpty()) { rawSkipVerifySsl = System.getProperty(PROP_SKIP_VERIFY_SSL); @@ -157,11 +154,11 @@ public SwarmMemberAddressProvider(Properties properties) { skipVerifySsl = Boolean.valueOf(rawSkipVerifySsl.toString()); } - Object rawLogAllServiceNamesOnFailedDiscovery = properties.get(PROP_LOG_ALL_SERVICE_NAMES_ON_FAILED_DISCOVERY); if (rawLogAllServiceNamesOnFailedDiscovery == null || rawLogAllServiceNamesOnFailedDiscovery.toString().trim().isEmpty()) { rawLogAllServiceNamesOnFailedDiscovery = System.getProperty(PROP_LOG_ALL_SERVICE_NAMES_ON_FAILED_DISCOVERY); } + Boolean logAllServiceNamesOnFailedDiscovery = false; if (rawLogAllServiceNamesOnFailedDiscovery != null) { logAllServiceNamesOnFailedDiscovery = Boolean.valueOf(rawLogAllServiceNamesOnFailedDiscovery.toString()); @@ -171,13 +168,14 @@ public SwarmMemberAddressProvider(Properties properties) { if (rawStrictDockerServiceNameComparison == null || rawStrictDockerServiceNameComparison.toString().trim().isEmpty()) { rawStrictDockerServiceNameComparison = System.getProperty(PROP_STRICT_DOCKER_SERVICE_NAME_COMPARISON); } + Boolean strictDockerServiceNameComparison = false; if (rawStrictDockerServiceNameComparison != null) { strictDockerServiceNameComparison = Boolean.valueOf(rawStrictDockerServiceNameComparison.toString()); } - initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, hazelcastPeerPort, - swarmMgrUri, skipVerifySsl, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); + initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, + swarmMgrUri, skipVerifySsl, hazelcastPeerPort, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); } public SwarmMemberAddressProvider(final String dockerNetworkNames, @@ -194,16 +192,6 @@ public SwarmMemberAddressProvider(final String dockerNetworkNames, swarmMgrUri, skipVerifySsl, hazelcastPeerPort, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); } - private void initialize(final String dockerNetworkNames, final String dockerServiceLabels, - final String dockerServiceNames, final Integer hazelcastPeerPort, - final String swarmMgrUri, final Boolean skipVerifySsl, final Boolean logAllServiceNamesOnFailedDiscovery, - final Boolean strictDockerServiceNameComparison) { - - initialize(dockerNetworkNames, dockerServiceLabels, dockerServiceNames, - swarmMgrUri, skipVerifySsl, hazelcastPeerPort, logAllServiceNamesOnFailedDiscovery, strictDockerServiceNameComparison); - } - - private void initialize(final String dockerNetworkNames, final String dockerServiceLabels, final String dockerServiceNames, diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SystemPrintLogger.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SystemPrintLogger.java index 2c60885..a132696 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SystemPrintLogger.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/SystemPrintLogger.java @@ -3,15 +3,12 @@ import com.hazelcast.logging.ILogger; import com.hazelcast.logging.LogEvent; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.LogRecord; +@SuppressWarnings("squid:S106") public class SystemPrintLogger implements ILogger { - private DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); - @Override public void finest(String message) { System.out.println("FINEST " + message); diff --git a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/test/DockerTestRunner.java b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/test/DockerTestRunner.java index 938bae4..a879e85 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/test/DockerTestRunner.java +++ b/src/main/java/org/bitsofinfo/hazelcast/discovery/docker/swarm/test/DockerTestRunner.java @@ -2,14 +2,12 @@ import com.hazelcast.config.ClasspathXmlConfig; import com.hazelcast.config.Config; -import com.hazelcast.core.HazelcastInstance; import com.hazelcast.instance.AddressPicker; import com.hazelcast.instance.DefaultNodeContext; import com.hazelcast.instance.HazelcastInstanceFactory; import com.hazelcast.instance.Node; import com.hazelcast.instance.NodeContext; import org.bitsofinfo.hazelcast.discovery.docker.swarm.SwarmAddressPicker; -import org.bitsofinfo.hazelcast.discovery.docker.swarm.SystemPrintLogger; /** * Simple class for manually spawning hz instances and watching what happens @@ -29,32 +27,23 @@ public static void main(String[] args) throws Exception { NodeContext nodeContext = new DefaultNodeContext() { @Override public AddressPicker createAddressPicker(Node node) { - return new SwarmAddressPicker(new SystemPrintLogger()); + return new SwarmAddressPicker(); } }; - HazelcastInstance hazelcastInstance = HazelcastInstanceFactory - .newHazelcastInstance(conf, "hazelcast-docker-swarm-discovery-spi-example", nodeContext); - + HazelcastInstanceFactory.newHazelcastInstance(conf, "hazelcast-docker-swarm-discovery-spi-example", nodeContext); } else if (System.getProperty("swarm-bind-method").equalsIgnoreCase("member-address-provider")) { Config conf = new ClasspathXmlConfig("hazelcast-docker-swarm-discovery-spi-example-member-address-provider.xml"); + HazelcastInstanceFactory.newHazelcastInstance(conf, "hazelcast-docker-swarm-discovery-spi-example", new DefaultNodeContext()); - - HazelcastInstance hazelcastInstance = HazelcastInstanceFactory - .newHazelcastInstance(conf, "hazelcast-docker-swarm-discovery-spi-example", new DefaultNodeContext()); } else if (System.getProperty("swarm-bind-method").equalsIgnoreCase("dockerDNSRR")) { - Config conf = - new ClasspathXmlConfig( - "hazelcast-docker-swarm-dnsrr-discovery-spi-example.xml" - ); - HazelcastInstance hazelcastInstance = - HazelcastInstanceFactory.newHazelcastInstance(conf); + Config conf = new ClasspathXmlConfig("hazelcast-docker-swarm-dnsrr-discovery-spi-example.xml"); + HazelcastInstanceFactory.newHazelcastInstance(conf); } - Thread.sleep(400000); System.exit(0); diff --git a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProvider.java b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProvider.java index b1bb113..e4840ba 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProvider.java +++ b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProvider.java @@ -36,49 +36,35 @@ * * @author Cardds */ -public class DockerDNSRRMemberAddressProvider - implements MemberAddressProvider { - public static Properties properties; - public static InetSocketAddress bindAddress = null; +public class DockerDNSRRMemberAddressProvider implements MemberAddressProvider { + protected Properties properties; + protected InetSocketAddress bindAddress = null; ILogger logger = Logger.getLogger(DockerDNSRRMemberAddressProvider.class); - public DockerDNSRRMemberAddressProvider(Properties properties) - throws - NumberFormatException, - SocketException, - UnknownHostException { - DockerDNSRRMemberAddressProvider.properties = properties; + /** + * @param properties + * @throws NumberFormatException if servicePort cannot be parsed + * @throws SocketException + * @throws UnknownHostException + */ + public DockerDNSRRMemberAddressProvider(Properties properties) throws SocketException, UnknownHostException { + this.properties = properties; if (properties != null) { - String serviceName = properties.getProperty( - DockerDNSRRMemberAddressProviderConfig.SERVICENAME - ); - String portString = properties.getProperty( - DockerDNSRRMemberAddressProviderConfig.SERVICEPORT - ); + String serviceName = properties.getProperty(DockerDNSRRMemberAddressProviderConfig.SERVICENAME); + String portString = properties.getProperty(DockerDNSRRMemberAddressProviderConfig.SERVICEPORT); Integer port = 5701; - if ( - portString != null && - !"".equals( - portString.trim() - ) - ) { + if (portString != null && !"".equals(portString.trim())) { try { port = Integer.valueOf(portString); } catch (NumberFormatException nfe) { - logger.severe( - "Unable to parse " + - DockerDNSRRMemberAddressProviderConfig.SERVICEPORT + - " with value " + - portString - ); + logger.severe("Unable to parse " + DockerDNSRRMemberAddressProviderConfig.SERVICEPORT + " with value " + portString); throw nfe; } } - Set potentialInetAddresses = - resolveServiceName(serviceName); + Set potentialInetAddresses = resolveServiceName(serviceName); Enumeration networkInterfaces; Enumeration networkInterfaceAddresses; @@ -87,12 +73,8 @@ public DockerDNSRRMemberAddressProvider(Properties properties) try { networkInterfaces = NetworkInterface.getNetworkInterfaces(); - while ( - networkInterfaces.hasMoreElements() && - bindAddress == null - ) { - networkInterfaceAddresses = - networkInterfaces.nextElement().getInetAddresses(); + while (networkInterfaces.hasMoreElements() && bindAddress == null) { + networkInterfaceAddresses = networkInterfaces.nextElement().getInetAddresses(); while (networkInterfaceAddresses.hasMoreElements()) { address = networkInterfaceAddresses.nextElement(); @@ -100,47 +82,32 @@ public DockerDNSRRMemberAddressProvider(Properties properties) logger.info("Checking address " + address.toString()); } - if ( - potentialInetAddresses.contains(address) - ) { - bindAddress = new InetSocketAddress( - address, - port - ); + if (potentialInetAddresses.contains(address)) { + bindAddress = new InetSocketAddress(address, port); break; } } } } catch (SocketException e) { - logger.severe( - "Unable to bind socket: " + e.toString() - ); + logger.severe("Unable to bind socket: " + e.toString()); throw e; } } } - private Set resolveServiceName(String serviceName) - throws UnknownHostException { + private Set resolveServiceName(String serviceName) throws UnknownHostException { Set addresses = new HashSet<>(); try { InetAddress[] inetAddresses; inetAddresses = InetAddress.getAllByName(serviceName); - addresses.addAll( - Arrays.asList(inetAddresses) - ); + addresses.addAll(Arrays.asList(inetAddresses)); - logger.info( - "Resolved domain name '" + serviceName + - "' to address(es): " + addresses - ); + logger.info("Resolved domain name '" + serviceName + "' to address(es): " + addresses); } catch (UnknownHostException e) { - logger.severe( - "Unable to resolve service name " + serviceName - ); + logger.severe("Unable to resolve service name " + serviceName); throw e; } diff --git a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProviderConfig.java b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProviderConfig.java index 8054a51..ae3b9d3 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProviderConfig.java +++ b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/DockerDNSRRMemberAddressProviderConfig.java @@ -21,14 +21,10 @@ */ public class DockerDNSRRMemberAddressProviderConfig { - private DockerDNSRRMemberAddressProviderConfig() { - } - /** * Property definition to load name of this docker service */ public static final String SERVICENAME = "serviceName"; - /** * Property definition to load exposed port for hazelcast *

@@ -36,4 +32,7 @@ private DockerDNSRRMemberAddressProviderConfig() { * the network configuration as of the time of writing this. */ public static final String SERVICEPORT = "servicePort"; + + private DockerDNSRRMemberAddressProviderConfig() { + } } diff --git a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryConfiguration.java b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryConfiguration.java index 280d3cd..3f846ad 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryConfiguration.java +++ b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryConfiguration.java @@ -28,20 +28,16 @@ */ public class DockerDNSRRDiscoveryConfiguration { - private DockerDNSRRDiscoveryConfiguration() { - } - /** * Property definition to load CSV of services */ public static final PropertyDefinition SERVICESCSV = - new SimplePropertyDefinition( - "peerServicesCsv", - PropertyTypeConverter.STRING - ); - + new SimplePropertyDefinition("peerServicesCsv", PropertyTypeConverter.STRING); /** * Full list of all properties referenced by this configuration */ public static final Collection PROPERTIES = Arrays.asList(SERVICESCSV); + + private DockerDNSRRDiscoveryConfiguration() { + } } diff --git a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategy.java b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategy.java index 7e45d04..542d85e 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategy.java +++ b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategy.java @@ -55,23 +55,16 @@ public Iterable discoverNodes() { new LinkedList<>(); //Pull properties - String servicesCsv = getOrDefault( - DockerDNSRRDiscoveryConfiguration.SERVICESCSV, - "" - ); + String servicesCsv = getOrDefault(DockerDNSRRDiscoveryConfiguration.SERVICESCSV, ""); //If there are no services configured, no point in doing anything. - if ( - servicesCsv == null || - servicesCsv.trim().isEmpty() - ) { + if (servicesCsv == null || servicesCsv.trim().isEmpty()) { return discoveryNodes; } - Set serviceNameResolutions = - new HashSet<>(); + Set serviceNameResolutions; String[] serviceHostnameAndPort; - Integer port = 5701; + Integer port; //Loop for every service defined in the CSV for (String service : servicesCsv.split(",")) { @@ -80,53 +73,30 @@ public Iterable discoverNodes() { serviceHostnameAndPort = service.split(":"); //Validate hostname exists - if ( - serviceHostnameAndPort[0] == null || - serviceHostnameAndPort[0].trim().isEmpty() - ) { - logger.info( - "Unable to resolve service hostname " + - serviceHostnameAndPort[0] + - " Skipping service entry." - ); + if (serviceHostnameAndPort[0] == null || serviceHostnameAndPort[0].trim().isEmpty()) { + logger.info("Unable to resolve service hostname " + serviceHostnameAndPort[0] + " Skipping service entry."); continue; } //Validate port exists; assume default port if it doesn't - if ( - serviceHostnameAndPort.length <= 1 || - serviceHostnameAndPort[1] == null || - serviceHostnameAndPort[1].trim().isEmpty() - ) { + if (serviceHostnameAndPort.length <= 1 || serviceHostnameAndPort[1] == null || serviceHostnameAndPort[1].trim().isEmpty()) { port = 5701; } else { try { - port = Integer.valueOf( - serviceHostnameAndPort[1] - ); + port = Integer.valueOf(serviceHostnameAndPort[1]); } catch (NumberFormatException nfe) { - logger.info( - "Unable to parse port " + - serviceHostnameAndPort[1] + - " Skipping service entry." - ); + logger.info("Unable to parse port " + serviceHostnameAndPort[1] + " Skipping service entry."); continue; } } //Resolve service hostname to a set of IP addresses, if any - serviceNameResolutions = - resolveDomainNames( - serviceHostnameAndPort[0] - ); + serviceNameResolutions = resolveDomainNames(serviceHostnameAndPort[0]); //Add all IP addresses for service hostname with the given port. for (InetAddress resolution : serviceNameResolutions) { discoveryNodes.add( new SimpleDiscoveryNode( - new Address( - resolution, - port - ) + new Address(resolution, port) ) ); } @@ -143,17 +113,11 @@ private Set resolveDomainNames(String domainName) { InetAddress[] inetAddresses; inetAddresses = InetAddress.getAllByName(domainName); - addresses.addAll( - Arrays.asList(inetAddresses) - ); + addresses.addAll(Arrays.asList(inetAddresses)); - logger.info( - "Resolved domain name '" + domainName + "' to address(es): " + addresses - ); + logger.info("Resolved domain name '" + domainName + "' to address(es): " + addresses); } catch (UnknownHostException e) { - logger.severe( - "Unable to resolve domain name " + domainName - ); + logger.severe("Unable to resolve domain name " + domainName); } return addresses; diff --git a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategyFactory.java b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategyFactory.java index 1685aa7..0388c14 100644 --- a/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategyFactory.java +++ b/src/main/java/org/bitsofinfo/hazelcast/spi/docker/swarm/dnsrr/discovery/DockerDNSRRDiscoveryStrategyFactory.java @@ -42,11 +42,7 @@ public DiscoveryStrategy newDiscoveryStrategy( //Implementing DiscoveryStrategyFactory method with a raw type @SuppressWarnings("rawtypes") Map properties ) { - return - new DockerDNSRRDiscoveryStrategy( - logger, - properties - ); + return new DockerDNSRRDiscoveryStrategy(logger, properties); } @Override