// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements.  See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
= TCP/IP Discovery

:javaFile: {javaCodeDir}/TcpIpDiscovery.java

In an Ignite  cluster, nodes can discover each other by using `DiscoverySpi`.
Ignite provides `TcpDiscoverySpi` as a default implementation of `DiscoverySpi` that uses TCP/IP for node discovery.
Discovery SPI can be configured for Multicast and Static IP based node
discovery.

== Multicast IP Finder

`TcpDiscoveryMulticastIpFinder` uses Multicast to discover other nodes
and is the default IP finder. Here is an example of how to configure
this finder via a Spring XML file or programmatically:


[tabs]
--
tab:XML[]
[source,xml]
----
include::code-snippets/xml/discovery-multicast.xml[tags=ignite-config, indent=0]
----
tab:Java[]
[source,java]
----
include::{javaFile}[tag=multicast,indent=0]
----
tab:C#/.NET[]
[source,csharp]
----
include::code-snippets/dotnet/ClusteringTcpIpDiscovery.cs[tag=multicast,indent=0]
----
tab:C++[unsupported]
--

== Static IP Finder

Static IP Finder, implemented in `TcpDiscoveryVmIpFinder`, allows you to specify a set of IP addresses and ports that will be checked for node discovery.

You are only required to provide at least one IP address of a remote
node, but usually it is advisable to provide 2 or 3 addresses of
nodes that you plan to start in the future. Once a
connection to any of the provided IP addresses is established, Ignite automatically discovers all other nodes.

[TIP]
====
Instead of specifying addresses in the configuration, you can specify them in
the `IGNITE_TCP_DISCOVERY_ADDRESSES` environment variable or in the system property
with the same name. Addresses should be comma separated and may optionally contain
a port range.
====

[TIP]
====
By default, the `TcpDiscoveryVmIpFinder` is used in the 'non-shared' mode.
If you plan to start a server node, then in this mode the list of IP addresses should contain the address of the local node as well. In this case, the node will not wait until other nodes join the cluster; instead, it will become the first cluster node and start to operate normally.
====

You can configure the static IP finder via XML configuration or programmatically:

[tabs]
--
tab:XML[]
[source,xml]
----
include::code-snippets/xml/discovery-static.xml[tags=ignite-config, indent=0]
----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=static,indent=0]
----

tab:C#/.NET[]
[source,csharp]
----
include::code-snippets/dotnet/ClusteringTcpIpDiscovery.cs[tag=static,indent=0]
----

tab:Shell[]
[source,shell]
----
# The configuration should use TcpDiscoveryVmIpFinder without addresses specified:

IGNITE_TCP_DISCOVERY_ADDRESSES=1.2.3.4,1.2.3.5:47500..47509 bin/ignite.sh -v config/default-config.xml
----
--

[WARNING]
====
[discrete]
Provide multiple node addresses only if you are sure that those are reachable. The unreachable addresses increase the
time it takes for the nodes to join the cluster. Let's say you set five IP addresses, and nobody listens for incoming
connections on two addresses out of five. If Ignite starts connecting to the cluster via those two unreachable addresses,
it will impact the node's startup time.
====


== Multicast and Static IP Finder

You can use both Multicast and Static IP based discovery together. In
this case, in addition to any addresses received via multicast,
`TcpDiscoveryMulticastIpFinder` can also work with a pre-configured list
of static IP addresses, just like Static IP-Based Discovery described
above. Here is an example of how to configure Multicast IP finder with
static IP addresses:

[tabs]
--
tab:XML[]
[source,xml]
----
include::code-snippets/xml/discovery-static-and-multicast.xml[tags=ignite-config, indent=0]
----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=multicastAndStatic,indent=0]
----

tab:C#/.NET[]
[source,csharp]
----
include::code-snippets/dotnet/ClusteringTcpIpDiscovery.cs[tag=multicastAndStatic,indent=0]
----

tab:C++[unsupported]

--


== Isolated Clusters on Same Set of Machines

Ignite allows you to start two isolated clusters on the same set of
machines. This can be done if nodes from different clusters use non-intersecting local port ranges for `TcpDiscoverySpi` and `TcpCommunicationSpi`.

Let’s say you need to start two isolated clusters on a single machine
for testing purposes. For the nodes from the first cluster, you
should use the following `TcpDiscoverySpi` and `TcpCommunicationSpi`
configurations:

[tabs]
--
tab:XML[]
[source,xml]
----
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
    <!--
    Explicitly configure TCP discovery SPI to provide list of
    initial nodes from the first cluster.
    -->
    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <!-- Initial local port to listen to. -->
            <property name="localPort" value="48500"/>

            <!-- Changing local port range. This is an optional action. -->
            <property name="localPortRange" value="20"/>

            <!-- Setting up IP finder for this cluster -->
            <property name="ipFinder">
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
                    <property name="addresses">
                        <list>
                            <!--
                            Addresses and port range of nodes from
                            the first cluster.
                            127.0.0.1 can be replaced with actual IP addresses
                            or host names. Port range is optional.
                            -->
                            <value>127.0.0.1:48500..48520</value>
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
    </property>

    <!--
    Explicitly configure TCP communication SPI changing local
    port number for the nodes from the first cluster.
    -->
    <property name="communicationSpi">
        <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi">
            <property name="localPort" value="48100"/>
        </bean>
    </property>
</bean>
----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=isolated1,indent=0]
----

tab:C#/.NET[]
[source,csharp]
----
include::code-snippets/dotnet/ClusteringTcpIpDiscovery.cs[tag=isolated1,indent=0]
----

tab:C++[unsupported]

--


For the nodes from the second cluster, the configuration might look like
this:

[tabs]
--
tab:XML[]
[source,xml]
----
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
    <!--
    Explicitly configure TCP discovery SPI to provide list of initial
    nodes from the second cluster.
    -->
    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <!-- Initial local port to listen to. -->
            <property name="localPort" value="49500"/>

            <!-- Changing local port range. This is an optional action. -->
            <property name="localPortRange" value="20"/>

            <!-- Setting up IP finder for this cluster -->
            <property name="ipFinder">
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
                    <property name="addresses">
                        <list>
                            <!--
                            Addresses and port range of the nodes from the second cluster.
                            127.0.0.1 can be replaced with actual IP addresses or host names. Port range is optional.
                            -->
                            <value>127.0.0.1:49500..49520</value>
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
    </property>

    <!--
    Explicitly configure TCP communication SPI changing local port number
    for the nodes from the second cluster.
    -->
    <property name="communicationSpi">
        <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi">
            <property name="localPort" value="49100"/>
        </bean>
    </property>
</bean>

----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=isolated2,indent=0]
----

tab:C#/.NET[]
[source,csharp]
----
include::code-snippets/dotnet/ClusteringTcpIpDiscovery.cs[tag=isolated2,indent=0]
----

tab:C++[unsupported]

--

As you can see from the configurations, the difference between them is minor — only port numbers for SPIs and IP finder vary.

[TIP]
====
If you want the nodes from different clusters to be able to look for
each other using the multicast protocol, replace
`TcpDiscoveryVmIpFinder` with `TcpDiscoveryMulticastIpFinder` and set
unique `TcpDiscoveryMulticastIpFinder.multicastGroups` in each
configuration above.
====

[CAUTION]
====
[discrete]
=== Persistence Files Location

If the isolated clusters use Native Persistence, then every
cluster has to store its persistence files under different paths in the
file system. Refer to the link:persistence/native-persistence[Native Persistence documentation] to learn how you can change persistence related directories.
====


== JDBC-Based IP Finder
NOTE: Not supported in .NET/C#/{cpp}.

You can have your database be a common shared storage of initial IP addresses. With this IP finder, nodes will write their IP addresses to a database on startup. This is done via `TcpDiscoveryJdbcIpFinder`.

[tabs]
--
tab:XML[]
[source,xml]
----
<bean class="org.apache.ignite.configuration.IgniteConfiguration">

  <property name="discoverySpi">
    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
      <property name="ipFinder">
        <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.jdbc.TcpDiscoveryJdbcIpFinder">
          <property name="dataSource" ref="ds"/>
        </bean>
      </property>
    </bean>
  </property>
</bean>

<!-- Configured data source instance. -->
<bean id="ds" class="some.Datasource">

</bean>
----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=jdbc,indent=0]
----

tab:C#/.NET[unsupported]

tab:C++[unsupported]

--


== Shared File System IP Finder

NOTE: Not supported in .NET/C#/{cpp}.

A shared file system can be used as a storage for nodes' IP addresses. The nodes will write their IP addresses to the file system on startup. This behavior is supported by `TcpDiscoverySharedFsIpFinder`.

[tabs]
--
tab:XML[]
[source,xml]
----
<bean class="org.apache.ignite.configuration.IgniteConfiguration">
    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <property name="ipFinder">
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.sharedfs.TcpDiscoverySharedFsIpFinder">
                  <property name="path" value="/var/ignite/addresses"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>
----
tab:Java[]
[source,java]
----
include::{javaFile}[tag=sharedFS,indent=0]
----
tab:C#/.NET[unsupported]
tab:C++[unsupported]
--

== ZooKeeper IP Finder

NOTE: Not supported in .NET/C#.

To set up ZooKeeper IP finder use `TcpDiscoveryZookeeperIpFinder` (note that `ignite-zookeeper` module has to be enabled).

[tabs]
--
tab:XML[]
[source,xml]
----
<bean class="org.apache.ignite.configuration.IgniteConfiguration">

    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <property name="ipFinder">
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.zk.TcpDiscoveryZookeeperIpFinder">
                    <property name="zkConnectionString" value="127.0.0.1:2181"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>
----

tab:Java[]
[source,java]
----
include::{javaFile}[tag=zk,indent=0]
----

tab:C#/.NET[unsupported]
tab:C++[unsupported]

--




