GridGain™ 3.6.0e
Enterprise Edition

org.gridgain.grid.spi.discovery.multicast
Class GridMulticastDiscoverySpi

java.lang.Object
  extended by org.gridgain.grid.spi.GridSpiAdapter
      extended by org.gridgain.grid.spi.discovery.multicast.GridMulticastDiscoverySpi
All Implemented Interfaces:
GridDiscoverySpi, GridMulticastDiscoverySpiMBean, GridSpi, GridSpiJsonConfigurable, GridSpiManagementMBean

@GridSpiInfo(author="GridGain Systems",
             url="www.gridgain.com",
             email="support@gridgain.com",
             version="3.6.0e.13012012")
@GridSpiMultipleInstancesSupport(value=true)
public class GridMulticastDiscoverySpi
extends GridSpiAdapter
implements GridDiscoverySpi, GridMulticastDiscoverySpiMBean

Discovery SPI implementation that uses IP-multicast for node discovery. At startup SPI starts sending IP/Multicast heartbeat messages. Once other nodes receive these messages, they use TCP/IP to exchange node attributes and then add the new node to their topology. When a node shuts down, it sends LEAVE heartbeat to other nodes, so every node in the grid can gracefully remove this node from topology.

Note that since IP/Multicast is not a reliable protocol, there is no guarantee that a node will be discovered by other grid members. However, IP/Multicast works very reliably within LANs and in most cases this SPI provides a very light weight and easy to use grid node discovery.

Configuration

Mandatory

This SPI has no mandatory configuration parameters.

Optional

The following configuration parameters are optional:

Java Example

GridMulticastDiscoverySpi is used by default and should be explicitly configured only if some SPI configuration parameters need to be overridden. Examples below insert own multicast group value that differs from default 228.1.2.4.
 GridMulticastDiscoverySpi spi = new GridMulticastDiscoverySpi();

 // Put another multicast group.
 spi.setMulticastGroup("228.10.10.157");

 GridConfigurationAdapter cfg = new GridConfigurationAdapter();

 // Override default discovery SPI.
 cfg.setDiscoverySpi(spi);

 // Starts grid.
 G.start(cfg);
 

Spring Example

GridMulticastDiscoverySpi can be configured from Spring XML configuration file:
 <bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true">
         ...
         <property name="discoverySpi">
             <bean class="org.gridgain.grid.spi.discovery.multicast.GridMulticastDiscoverySpi">
                 <property name="multicastGroup" value="228.10.10.157"/>
             </bean>
         </property>
         ...
 </bean>
 


For information about Spring framework visit www.springframework.org

Wiki & Forum:


Wiki
Forum

See Also:
GridDiscoverySpi
 

Field Summary
static long DFLT_HEARTBEAT_FREQ
          Default heartbeat delay (value is 3,000ms).
static int DFLT_HEARTBEAT_THREAD_PRIORITY
          Default heartbeat thread priority.
static int DFLT_LEAVE_ATTEMPTS
          Default number of attempts to send leaving notification (value is 3).
static int DFLT_MAX_MISSED_HEARTBEATS
          Default number of heartbeat messages that could be missed (value is 3).
static String DFLT_MCAST_GROUP
          Default multicast IP address (value is 228.1.2.4).
static int DFLT_MCAST_PORT
          Default multicast port number (value is 47200).
static int DFLT_PORT_RANGE
          Default local port range (value is 100).
static int DFLT_SOCK_TIMEOUT
          Default socket operations timeout in milliseconds (value is 2,000ms).
static int DFLT_TCP_PORT
          Default local port number for SPI (value is 47300).
static int DFLT_TTL
          Default multicast messages time-to-live value (value is 8).
 
Constructor Summary
GridMulticastDiscoverySpi()
           
 
Method Summary
 void disconnect()
          Tells discovery SPI to disconnect from topology.
protected  List<String> getConsistentAttributeNames()
          Returns back a list of attributes that should be consistent for this SPI.
 long getHeartbeatFrequency()
          Gets delay between heartbeat requests.
 int getHeartbeatThreadPriority()
          Gets heartbeat thread priority.
 int getLeaveAttempts()
          Gets number of attempts to notify another nodes that this one is leaving grid.
 String getLocalAddress()
          Gets local address that was set to SPI with GridMulticastDiscoverySpi.setLocalAddress(String) method.
 GridNode getLocalNode()
          Gets local node.
 int getLocalPortRange()
          Gets local port range for either TCP or multicast ports.
 int getMaximumMissedHeartbeats()
          Gets number of heartbeat requests that could be missed before remote node is considered to be failed.
 String getMulticastGroup()
          Gets IP address of multicast group.
 int getMulticastPort()
          Gets port number which multicast messages are sent to.
 GridNode getNode(UUID nodeId)
          Gets node by ID.
 Map<String,Object> getNodeAttributes()
          This method is called before SPI starts (before method GridSpi.spiStart(String) is called).
 int getRemoteNodeCount()
          Gets the number of remote nodes.
 Collection<UUID> getRemoteNodeIds()
          Gets set of remote nodes IDs that have READY state.
 List<GridNode> getRemoteNodes()
          Gets collection of remote nodes in grid or empty collection if no remote nodes found.
 int getSocketTimeout()
          Gets socket timeout.
 int getTcpPort()
          Gets local port number that is used by discovery SPI.
 int getTimeToLive()
          Gets TCP messages time-to-live.
 boolean isCheckMulticastEnabled()
          By default this value is true.
 void onContextDestroyed()
          Callback invoked prior to stopping grid before SPI context is destroyed.
 void onContextInitialized(GridSpiContext spiCtx)
          Callback invoked when SPI context is initialized.
 boolean pingNode(UUID nodeId)
          Pings the remote node to see if it's alive.
 void reconnect()
          Tells discovery SPI to reconnect to topology.
 void setCheckMulticastEnabled(boolean isCheckMulticastEnabled)
          Enables or disabled check whether multicast is enabled on local node.
 void setHeartbeatFrequency(long beatFreq)
          Sets delay between heartbeat requests.
 void setHeartbeatThreadPriority(int beatThreadPri)
          Sets heartbeat thread priority.
 void setLeaveAttempts(int leaveAttempts)
          Sets number of attempts to notify another nodes that this one is leaving grid.
 void setListener(GridDiscoverySpiListener lsnr)
          Sets a listener for discovery events.
 void setLocalAddress(String localAddr)
          Sets local host IP address that discovery SPI uses.
 void setLocalPortRange(int localPortRange)
          Sets local port range for TCP and Multicast ports (value must greater than or equal to 0).
 void setMaxMissedHeartbeats(int maxMissedBeats)
          Sets number of heartbeat requests that could be missed before remote node is considered to be failed.
 void setMetricsProvider(GridDiscoveryMetricsProvider metricsProvider)
          Sets discovery metrics provider.
 void setMulticastGroup(String mcastGroup)
          Sets IP address of multicast group.
 void setMulticastPort(int mcastPort)
          Sets port number which multicast messages are sent to.
 void setNodeAttributes(Map<String,Object> attrs)
          Sets node attributes which will be distributed in grid during join process.
 void setSocketTimeout(int sockTimeout)
          Sets socket operations timeout.
 void setTcpPort(int tcpPort)
          Sets local TCP port number to be used for node attribute exchange upon discovery.
 void setTimeToLive(int ttl)
          Sets Multicast messages time-to-live in router hops.
 void spiStart(String gridName)
          This method is called to start SPI.
 void spiStop()
          This method is called to stop SPI.
 String toString()
          
 
Methods inherited from class org.gridgain.grid.spi.GridSpiAdapter
assertParameter, checkConfigurationConsistency, configInfo, createSpiAttributeName, getAuthor, getGridGainHome, getLocalNodeId, getName, getSpiContext, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion, injectables, registerMBean, setJson, setName, startInfo, startStopwatch, stopInfo, unregisterMBean
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.gridgain.grid.spi.GridSpi
getName
 
Methods inherited from interface org.gridgain.grid.spi.GridSpiJsonConfigurable
setJson
 
Methods inherited from interface org.gridgain.grid.spi.GridSpiManagementMBean
getAuthor, getGridGainHome, getLocalNodeId, getName, getStartTimestamp, getStartTimestampFormatted, getUpTime, getUpTimeFormatted, getVendorEmail, getVendorUrl, getVersion
 

Field Detail

DFLT_HEARTBEAT_FREQ

public static final long DFLT_HEARTBEAT_FREQ
Default heartbeat delay (value is 3,000ms).

See Also:
Constant Field Values

DFLT_SOCK_TIMEOUT

public static final int DFLT_SOCK_TIMEOUT
Default socket operations timeout in milliseconds (value is 2,000ms).

See Also:
Constant Field Values

DFLT_HEARTBEAT_THREAD_PRIORITY

public static final int DFLT_HEARTBEAT_THREAD_PRIORITY
Default heartbeat thread priority.

See Also:
Constant Field Values

DFLT_MAX_MISSED_HEARTBEATS

public static final int DFLT_MAX_MISSED_HEARTBEATS
Default number of heartbeat messages that could be missed (value is 3).

See Also:
Constant Field Values

DFLT_MCAST_GROUP

public static final String DFLT_MCAST_GROUP
Default multicast IP address (value is 228.1.2.4).

See Also:
Constant Field Values

DFLT_MCAST_PORT

public static final int DFLT_MCAST_PORT
Default multicast port number (value is 47200).

See Also:
Constant Field Values

DFLT_TCP_PORT

public static final int DFLT_TCP_PORT
Default local port number for SPI (value is 47300).

See Also:
Constant Field Values

DFLT_PORT_RANGE

public static final int DFLT_PORT_RANGE
Default local port range (value is 100). See GridMulticastDiscoverySpi.setLocalPortRange(int) for details.

See Also:
Constant Field Values

DFLT_LEAVE_ATTEMPTS

public static final int DFLT_LEAVE_ATTEMPTS
Default number of attempts to send leaving notification (value is 3).

See Also:
Constant Field Values

DFLT_TTL

public static final int DFLT_TTL
Default multicast messages time-to-live value (value is 8).

See Also:
Constant Field Values
Constructor Detail

GridMulticastDiscoverySpi

public GridMulticastDiscoverySpi()
Method Detail

setMulticastGroup

@GridSpiConfiguration(optional=true)
public void setMulticastGroup(String mcastGroup)
Sets IP address of multicast group.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_MCAST_GROUP.

Parameters:
mcastGroup - Multicast IP address.

getMulticastGroup

public String getMulticastGroup()
Gets IP address of multicast group.

Specified by:
getMulticastGroup in interface GridMulticastDiscoverySpiMBean
Returns:
Multicast IP address.

setMulticastPort

@GridSpiConfiguration(optional=true)
public void setMulticastPort(int mcastPort)
Sets port number which multicast messages are sent to.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_MCAST_PORT.

Parameters:
mcastPort - Multicast port number.

getMulticastPort

public int getMulticastPort()
Gets port number which multicast messages are sent to.

Specified by:
getMulticastPort in interface GridMulticastDiscoverySpiMBean
Returns:
Port number.

setTcpPort

@GridSpiConfiguration(optional=true)
public void setTcpPort(int tcpPort)
Sets local TCP port number to be used for node attribute exchange upon discovery.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_TCP_PORT.

Parameters:
tcpPort - Port number.

getTcpPort

public int getTcpPort()
Gets local port number that is used by discovery SPI.

Specified by:
getTcpPort in interface GridMulticastDiscoverySpiMBean
Returns:
Port number.

setHeartbeatFrequency

@GridSpiConfiguration(optional=true)
public void setHeartbeatFrequency(long beatFreq)
Sets delay between heartbeat requests. SPI sends broadcast messages in configurable time interval to other nodes to notify them about its state.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_HEARTBEAT_FREQ.

Parameters:
beatFreq - Time in milliseconds.

getHeartbeatFrequency

public long getHeartbeatFrequency()
Gets delay between heartbeat requests. SPI sends broadcast messages in configurable time interval to another nodes to notify them about node state.

Specified by:
getHeartbeatFrequency in interface GridMulticastDiscoverySpiMBean
Returns:
Time period in milliseconds.

setSocketTimeout

@GridSpiConfiguration(optional=true)
public void setSocketTimeout(int sockTimeout)
Sets socket operations timeout.

This timeout is used to limit connection time and read/write to socket time.

If not specified, default is GridMulticastDiscoverySpi.DFLT_SOCK_TIMEOUT.

Parameters:
sockTimeout - Socket connection timeout.

getSocketTimeout

public int getSocketTimeout()
Gets socket timeout.

Specified by:
getSocketTimeout in interface GridMulticastDiscoverySpiMBean
Returns:
Socket timeout.

getHeartbeatThreadPriority

public int getHeartbeatThreadPriority()
Gets heartbeat thread priority.

Specified by:
getHeartbeatThreadPriority in interface GridMulticastDiscoverySpiMBean
Returns:
Heartbeat thread priority.

setHeartbeatThreadPriority

@GridSpiConfiguration(optional=true)
public void setHeartbeatThreadPriority(int beatThreadPri)
Sets heartbeat thread priority.

Parameters:
beatThreadPri - Heartbeat thread priority.

setMaxMissedHeartbeats

@GridSpiConfiguration(optional=true)
public void setMaxMissedHeartbeats(int maxMissedBeats)
Sets number of heartbeat requests that could be missed before remote node is considered to be failed.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_MAX_MISSED_HEARTBEATS.

Parameters:
maxMissedBeats - Number of missed requests.

getMaximumMissedHeartbeats

public int getMaximumMissedHeartbeats()
Gets number of heartbeat requests that could be missed before remote node is considered to be failed.

Specified by:
getMaximumMissedHeartbeats in interface GridMulticastDiscoverySpiMBean
Returns:
Number of requests.

setLeaveAttempts

@GridSpiConfiguration(optional=true)
public void setLeaveAttempts(int leaveAttempts)
Sets number of attempts to notify another nodes that this one is leaving grid. Multiple leave requests are sent to increase the chance of successful delivery to every node, since IP Multicast protocol is unreliable. Note that on most networks loss of IP Multicast packets is generally negligible.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_LEAVE_ATTEMPTS.

Parameters:
leaveAttempts - Number of attempts.

getLeaveAttempts

public int getLeaveAttempts()
Gets number of attempts to notify another nodes that this one is leaving grid. It might be impossible to send leaving request and node will try to do it several times.

Specified by:
getLeaveAttempts in interface GridMulticastDiscoverySpiMBean
Returns:
Number of retries.

setLocalAddress

@GridSpiConfiguration(optional=true)
@GridLocalHostResource
public void setLocalAddress(String localAddr)
Sets local host IP address that discovery SPI uses.

If not provided, by default a first found non-loopback address will be used. If there is no non-loopback address available, then InetAddress.getLocalHost() will be used.

Parameters:
localAddr - IP address.

getLocalAddress

public String getLocalAddress()
Gets local address that was set to SPI with GridMulticastDiscoverySpi.setLocalAddress(String) method.

Returns:
local address.

getTimeToLive

public int getTimeToLive()
Gets TCP messages time-to-live.

Specified by:
getTimeToLive in interface GridMulticastDiscoverySpiMBean
Returns:
TTL.

setTimeToLive

@GridSpiConfiguration(optional=true)
public void setTimeToLive(int ttl)
Sets Multicast messages time-to-live in router hops.

If not provided, default value is GridMulticastDiscoverySpi.DFLT_TTL.

Parameters:
ttl - Messages TTL.

setLocalPortRange

@GridSpiConfiguration(optional=true)
public void setLocalPortRange(int localPortRange)
Sets local port range for TCP and Multicast ports (value must greater than or equal to 0). If provided local port (see GridMulticastDiscoverySpi.setMulticastPort(int) or GridMulticastDiscoverySpi.setTcpPort(int) is occupied, implementation will try to increment the port number for as long as it is less than initial value plus this range.

If port range value is 0, then implementation will try bind only to the port provided by GridMulticastDiscoverySpi.setMulticastPort(int) or GridMulticastDiscoverySpi.setTcpPort(int) methods and fail if binding to these ports did not succeed.

Local port range is very useful during development when more than one grid nodes need to run on the same physical machine.

Parameters:
localPortRange - New local port range.
See Also:
GridMulticastDiscoverySpi.DFLT_PORT_RANGE

getLocalPortRange

public int getLocalPortRange()
Gets local port range for either TCP or multicast ports. See GridMulticastDiscoverySpi.setLocalPortRange(int) for details.

Specified by:
getLocalPortRange in interface GridMulticastDiscoverySpiMBean
Returns:
Local port range

isCheckMulticastEnabled

public boolean isCheckMulticastEnabled()
By default this value is true. On startup GridGain will check if local node can receive multicast packets, and if not, will not allow the node to startup.

Specified by:
isCheckMulticastEnabled in interface GridMulticastDiscoverySpiMBean
Returns:
checkMulticastEnabled True if multicast check is enabled, false otherwise.

setCheckMulticastEnabled

public void setCheckMulticastEnabled(boolean isCheckMulticastEnabled)
Enables or disabled check whether multicast is enabled on local node. By default this value is true. On startup GridGain will check if local node can receive multicast packets, and if not, will not allow the node to startup.

This property should be disabled in rare cases when loopback multicast is disabled, but multicast to other remote boxes is enabled.

Parameters:
isCheckMulticastEnabled - True for enabling multicast check, false for disabling it.

setNodeAttributes

public void setNodeAttributes(Map<String,Object> attrs)
Sets node attributes which will be distributed in grid during join process. Note that these attributes cannot be changed and set only once.

Specified by:
setNodeAttributes in interface GridDiscoverySpi
Parameters:
attrs - Map of node attributes.

getRemoteNodes

public List<GridNode> getRemoteNodes()
Gets collection of remote nodes in grid or empty collection if no remote nodes found.

Specified by:
getRemoteNodes in interface GridDiscoverySpi
Returns:
Collection of remote nodes.

getNode

@Nullable
public GridNode getNode(UUID nodeId)
Gets node by ID.

Specified by:
getNode in interface GridDiscoverySpi
Parameters:
nodeId - Node ID.
Returns:
Node with given ID or null if node is not found.

getRemoteNodeIds

public Collection<UUID> getRemoteNodeIds()
Gets set of remote nodes IDs that have READY state.

Specified by:
getRemoteNodeIds in interface GridMulticastDiscoverySpiMBean
Returns:
Set of remote nodes IDs.

getRemoteNodeCount

public int getRemoteNodeCount()
Gets the number of remote nodes.

Specified by:
getRemoteNodeCount in interface GridMulticastDiscoverySpiMBean
Returns:
Number of remote nodes.

getLocalNode

public GridNode getLocalNode()
Gets local node.

Specified by:
getLocalNode in interface GridDiscoverySpi
Returns:
Local node.

setListener

public void setListener(GridDiscoverySpiListener lsnr)
Sets a listener for discovery events. Refer to GridDiscoveryEvent for a set of all possible discovery events.

Note that as of GridGain 3.0.2 this method is called before method GridSpi.spiStart(String) is called. This is done to avoid potential window when SPI is started but the listener is not registered yet.

Specified by:
setListener in interface GridDiscoverySpi
Parameters:
lsnr - Listener to discovery events or null to unset the listener.

setMetricsProvider

public void setMetricsProvider(GridDiscoveryMetricsProvider metricsProvider)
Sets discovery metrics provider. Use metrics provided by GridDiscoveryMetricsProvider.getMetrics() method to exchange dynamic metrics between nodes.

Specified by:
setMetricsProvider in interface GridDiscoverySpi
Parameters:
metricsProvider - Provider of metrics data.

getNodeAttributes

public Map<String,Object> getNodeAttributes()
                                     throws GridSpiException
This method is called before SPI starts (before method GridSpi.spiStart(String) is called). It allows SPI implementation to add attributes to a local node. Kernel collects these attributes from all SPI implementations loaded up and then passes it to discovery SPI so that they can be exchanged with other nodes.

Specified by:
getNodeAttributes in interface GridSpi
Overrides:
getNodeAttributes in class GridSpiAdapter
Throws:
GridSpiException - Throws in case of any error.
Returns:
Map of local node attributes this SPI wants to add.

spiStart

public void spiStart(String gridName)
              throws GridSpiException
This method is called to start SPI. After this method returns successfully kernel assumes that SPI is fully operational.

Specified by:
spiStart in interface GridSpi
Throws:
GridSpiException - Throws in case of any error during SPI start.
Parameters:
gridName - Name of grid instance this SPI is being started for (null for default grid).

spiStop

public void spiStop()
             throws GridSpiException
This method is called to stop SPI. After this method returns kernel assumes that this SPI is finished and all resources acquired by it are released.

Note that this method can be called at any point including during recovery of failed start. It should make no assumptions on what state SPI will be in when this method is called.

Specified by:
spiStop in interface GridSpi
Throws:
GridSpiException - Thrown in case of any error during SPI stop.

onContextInitialized

public void onContextInitialized(GridSpiContext spiCtx)
                          throws GridSpiException
Callback invoked when SPI context is initialized. SPI implementation may store SPI context for future access.

This method is invoked after GridSpi.spiStart(String) method is completed, so SPI should be fully functional at this point. Use this method for post-start initialization, such as subscribing a discovery listener, sending a message to remote node, etc...

Specified by:
onContextInitialized in interface GridSpi
Overrides:
onContextInitialized in class GridSpiAdapter
Throws:
GridSpiException - If context initialization failed (grid will be stopped).
Parameters:
spiCtx - Spi context.

onContextDestroyed

public void onContextDestroyed()
Callback invoked prior to stopping grid before SPI context is destroyed. Once this method is complete, grid will begin shutdown sequence. Use this callback for de-initialization logic that may involve SPI context. Note that invoking SPI context after this callback is complete is considered illegal and may produce unknown results.

If GridSpiAdapter is used for SPI implementation, then it will replace actual context with dummy no-op context which is usually good-enough since grid is about to shut down.

Specified by:
onContextDestroyed in interface GridSpi
Overrides:
onContextDestroyed in class GridSpiAdapter

disconnect

public void disconnect()
                throws GridSpiException
Tells discovery SPI to disconnect from topology. This is very close to calling GridSpi.spiStop() with accounting that it is not a full stop, but disconnect before reconnect (i.e. some internal threads and resources may be preserved on disconnect and reused on reconnect again).

Specified by:
disconnect in interface GridDiscoverySpi
Throws:
GridSpiException - If any error occurs.
See Also:
GridDiscoverySpi.reconnect()

reconnect

public void reconnect()
               throws GridSpiException
Tells discovery SPI to reconnect to topology. This is very close to calling GridSpi.spiStart(String) with accounting that it is not a full start, but reconnect (i.e. some internal threads and resources may be preserved on disconnect and reused on reconnect again).

Prior to calling this method manager will ensure, that it is in the correct network segment.

Specified by:
reconnect in interface GridDiscoverySpi
Throws:
GridSpiException - If any error occurs.
See Also:
GridDiscoverySpi.disconnect()

pingNode

public boolean pingNode(UUID nodeId)
Pings the remote node to see if it's alive.

Specified by:
pingNode in interface GridDiscoverySpi
Parameters:
nodeId - Node Id.
Returns:
true if node alive, false otherwise.

getConsistentAttributeNames

protected List<String> getConsistentAttributeNames()
Returns back a list of attributes that should be consistent for this SPI. Consistency means that remote node has to have the same attribute with the same value.

Overrides:
getConsistentAttributeNames in class GridSpiAdapter
Returns:
List or attribute names.

toString

public String toString()

Overrides:
toString in class Object

GridGain™ 3.6.0e
Enterprise Edition

GridGain - Real Time Big Data
Enterprise Edition, ver. 3.6.0e.13012012
2012 Copyright © GridGain Systems
Follow us:   Follow GridGain on Github Follow GridGain on Facebook Join GridGain User Group Follow GridGain on Twitter Follow GridGain on YouTube