Search in sources :

Example 1 with NetworkConfig

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;
}
Also used : JobScheduler(org.neo4j.kernel.impl.util.JobScheduler) InterfacesConfig(com.hazelcast.config.InterfacesConfig) HazelcastException(com.hazelcast.core.HazelcastException) JoinConfig(com.hazelcast.config.JoinConfig) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) Config(org.neo4j.kernel.configuration.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig) InterfacesConfig(com.hazelcast.config.InterfacesConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) ListenSocketAddress(org.neo4j.helpers.ListenSocketAddress) JoinConfig(com.hazelcast.config.JoinConfig) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig)

Example 2 with NetworkConfig

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);
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) InetSocketAddress(java.net.InetSocketAddress) NetworkConfig(com.hazelcast.config.NetworkConfig) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 3 with NetworkConfig

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);
}
Also used : NetworkConfig(com.hazelcast.config.NetworkConfig)

Example 4 with NetworkConfig

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;
}
Also used : NetworkConfig(com.hazelcast.config.NetworkConfig)

Example 5 with NetworkConfig

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;
}
Also used : NetworkConfig(com.hazelcast.config.NetworkConfig)

Aggregations

NetworkConfig (com.hazelcast.config.NetworkConfig)33 Config (com.hazelcast.config.Config)25 JoinConfig (com.hazelcast.config.JoinConfig)23 Test (org.junit.Test)13 InterfacesConfig (com.hazelcast.config.InterfacesConfig)10 TcpIpConfig (com.hazelcast.config.TcpIpConfig)10 QuickTest (com.hazelcast.test.annotation.QuickTest)10 ListenerConfig (com.hazelcast.config.ListenerConfig)8 PartitionGroupConfig (com.hazelcast.config.PartitionGroupConfig)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)6 MembershipEvent (com.hazelcast.core.MembershipEvent)4 Node (com.hazelcast.instance.Node)4 UnknownHostException (java.net.UnknownHostException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 MulticastConfig (com.hazelcast.config.MulticastConfig)3 MembershipAdapter (com.hazelcast.core.MembershipAdapter)3 FirewallingTcpIpConnectionManager (com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)3 NightlyTest (com.hazelcast.test.annotation.NightlyTest)3 MapConfig (com.hazelcast.config.MapConfig)2