use of com.hazelcast.config.NetworkConfig in project neo4j by neo4j.
the class HazelcastCoreTopologyService method createHazelcastInstance.
private HazelcastInstance createHazelcastInstance() {
System.setProperty(WAIT_SECONDS_BEFORE_JOIN.getName(), "1");
JoinConfig joinConfig = new JoinConfig();
joinConfig.getMulticastConfig().setEnabled(false);
joinConfig.getAwsConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
List<AdvertisedSocketAddress> initialMembers = config.get(CausalClusteringSettings.initial_discovery_members);
for (AdvertisedSocketAddress address : initialMembers) {
tcpIpConfig.addMember(address.toString());
}
Setting<ListenSocketAddress> discovery_listen_address = CausalClusteringSettings.discovery_listen_address;
ListenSocketAddress hazelcastAddress = config.get(discovery_listen_address);
InterfacesConfig interfaces = new InterfacesConfig();
interfaces.addInterface(hazelcastAddress.getHostname());
NetworkConfig networkConfig = new NetworkConfig();
networkConfig.setInterfaces(interfaces);
networkConfig.setPort(hazelcastAddress.getPort());
networkConfig.setJoin(joinConfig);
networkConfig.setPortAutoIncrement(false);
com.hazelcast.config.Config c = new com.hazelcast.config.Config();
c.setProperty(OPERATION_CALL_TIMEOUT_MILLIS.getName(), String.valueOf(10_000));
c.setProperty(MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "10");
c.setProperty(MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "10");
c.setProperty(INITIAL_MIN_CLUSTER_SIZE.getName(), String.valueOf(minimumClusterSizeThatCanTolerateOneFaultForExpectedClusterSize()));
c.setProperty(LOGGING_TYPE.getName(), "none");
c.setNetworkConfig(networkConfig);
MemberAttributeConfig memberAttributeConfig = HazelcastClusterTopology.buildMemberAttributesForCore(myself, config);
c.setMemberAttributeConfig(memberAttributeConfig);
logConnectionInfo(initialMembers);
JobScheduler.JobHandle logJob = scheduler.schedule("HazelcastHealth", HAZELCAST_IS_HEALTHY_TIMEOUT_MS, () -> log.warn("The server has not been able to connect in a timely fashion to the " + "cluster. Please consult the logs for more details. Rebooting the server may " + "solve the problem."));
try {
hazelcastInstance = Hazelcast.newHazelcastInstance(c);
logJob.cancel(true);
} catch (HazelcastException e) {
String errorMessage = String.format("Hazelcast was unable to start with setting: %s = %s", discovery_listen_address.name(), config.get(discovery_listen_address));
userLog.error(errorMessage);
log.error(errorMessage, e);
throw new RuntimeException(e);
}
List<String> groups = config.get(CausalClusteringSettings.server_groups);
refreshGroups(hazelcastInstance, myself.getUuid().toString(), groups);
return hazelcastInstance;
}
use of com.hazelcast.config.NetworkConfig in project hazelcast by hazelcast.
the class DefaultAddressPicker method getPublicAddressByPortSearch.
private AddressDefinition getPublicAddressByPortSearch() throws IOException {
NetworkConfig networkConfig = config.getNetworkConfig();
boolean bindAny = hazelcastProperties.getBoolean(GroupProperty.SOCKET_SERVER_BIND_ANY);
Throwable error = null;
ServerSocket serverSocket = null;
InetSocketAddress inetSocketAddress;
boolean reuseAddress = networkConfig.isReuseAddress();
logger.finest("inet reuseAddress:" + reuseAddress);
int port = networkConfig.getPort();
// port = 0 means system will pick up an ephemeral port.
int portTrialCount = port > 0 && networkConfig.isPortAutoIncrement() ? networkConfig.getPortCount() : 1;
AddressDefinition bindAddressDef = pickAddressDef();
if (port == 0) {
logger.info("No explicit port is given, system will pick up an ephemeral port.");
}
for (int i = 0; i < portTrialCount; i++) {
/*
* Instead of reusing the ServerSocket/ServerSocketChannel, we are going to close and replace them on
* every attempt to find a free port. The reason to do this is because in some cases, when concurrent
* threads/processes try to acquire the same port, the ServerSocket gets corrupted and isn't able to
* find any free port at all (no matter if there are more than enough free ports available). We have
* seen this happening on Linux and Windows environments.
*/
serverSocketChannel = ServerSocketChannel.open();
serverSocket = serverSocketChannel.socket();
serverSocket.setReuseAddress(reuseAddress);
serverSocket.setSoTimeout(SOCKET_TIMEOUT_MILLIS);
try {
if (bindAny) {
inetSocketAddress = new InetSocketAddress(port + i);
} else {
inetSocketAddress = new InetSocketAddress(bindAddressDef.inetAddress, port + i);
}
logger.fine("Trying to bind inet socket address: " + inetSocketAddress);
serverSocket.bind(inetSocketAddress, SOCKET_BACKLOG_LENGTH);
logger.fine("Bind successful to inet socket address: " + serverSocket.getLocalSocketAddress());
break;
} catch (Exception e) {
serverSocket.close();
serverSocketChannel.close();
error = e;
}
}
if (serverSocket == null || !serverSocket.isBound()) {
String message;
if (networkConfig.isPortAutoIncrement()) {
message = "ServerSocket bind has failed. Hazelcast cannot start. config-port: " + networkConfig.getPort() + ", latest-port: " + (port + portTrialCount);
} else {
message = "Port [" + port + "] is already in use and auto-increment is disabled." + " Hazelcast cannot start.";
}
throw new HazelcastException(message, error);
}
// get the actual port that's bound by server socket
port = serverSocket.getLocalPort();
serverSocketChannel.configureBlocking(false);
bindAddress = createAddress(bindAddressDef, port);
logger.info("Picked " + bindAddress + ", using socket " + serverSocket + ", bind any local is " + bindAny);
return getPublicAddress(port);
}
use of com.hazelcast.config.NetworkConfig in project hazelcast by hazelcast.
the class DefaultAddressPicker method pickInterfaceAddressDef.
private AddressDefinition pickInterfaceAddressDef() throws UnknownHostException, SocketException {
Collection<InterfaceDefinition> interfaces = getInterfaces();
if (interfaces.contains(new InterfaceDefinition("127.0.0.1")) || interfaces.contains(new InterfaceDefinition("localhost"))) {
return pickLoopbackAddress();
}
if (preferIPv4Stack()) {
logger.info("Prefer IPv4 stack is true.");
}
if (interfaces.size() > 0) {
AddressDefinition addressDef = pickMatchingAddress(interfaces);
if (addressDef != null) {
return addressDef;
}
}
NetworkConfig networkConfig = config.getNetworkConfig();
if (networkConfig.getInterfaces().isEnabled()) {
String msg = "Hazelcast CANNOT start on this node. No matching network interface found.\n" + "Interface matching must be either disabled or updated in the hazelcast.xml config file.";
logger.severe(msg);
throw new RuntimeException(msg);
}
if (networkConfig.getJoin().getTcpIpConfig().isEnabled()) {
logger.warning("Could not find a matching address to start with! Picking one of non-loopback addresses.");
}
return pickMatchingAddress(null);
}
use of com.hazelcast.config.NetworkConfig in project hazelcast by hazelcast.
the class MulticastJoiner method calculateTryCount.
private int calculateTryCount() {
final NetworkConfig networkConfig = config.getNetworkConfig();
long timeoutMillis = TimeUnit.SECONDS.toMillis(networkConfig.getJoin().getMulticastConfig().getMulticastTimeoutSeconds());
int avgPublishInterval = (PUBLISH_INTERVAL_MAX + PUBLISH_INTERVAL_MIN) / 2;
int tryCount = (int) timeoutMillis / avgPublishInterval;
String host = node.getThisAddress().getHost();
int lastDigits;
try {
lastDigits = Integer.parseInt(host.substring(host.lastIndexOf('.') + 1));
} catch (NumberFormatException e) {
lastDigits = RandomPicker.getInt(TRY_COUNT_MAX_LAST_DIGITS);
}
int portDiff = node.getThisAddress().getPort() - networkConfig.getPort();
tryCount += (lastDigits + portDiff) % TRY_COUNT_MODULO;
return tryCount;
}
use of com.hazelcast.config.NetworkConfig in project hazelcast by hazelcast.
the class NodeIOService method getOutboundPorts.
@Override
public Collection<Integer> getOutboundPorts() {
final NetworkConfig networkConfig = node.getConfig().getNetworkConfig();
final Collection<String> portDefinitions = getPortDefinitions(networkConfig);
final Set<Integer> ports = getPorts(networkConfig);
if (portDefinitions.isEmpty() && ports.isEmpty()) {
// means any port
return Collections.emptySet();
}
if (portDefinitions.contains("*") || portDefinitions.contains("0")) {
// means any port
return Collections.emptySet();
}
transformPortDefinitionsToPorts(portDefinitions, ports);
if (ports.contains(0)) {
// means any port
return Collections.emptySet();
}
return ports;
}
Aggregations