From 20ff04f71502ff8d0a7a88ac450db4979e25c21f Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 17 Aug 2011 02:55:17 +0300 Subject: [PATCH] Start Elastic Node without network link, closes #842. --- .../zen/ping/multicast/MulticastZenPing.java | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java index 0f1075c6400..5421922bcfc 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ping/multicast/MulticastZenPing.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.io.stream.VoidStreamable; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.discovery.DiscoveryException; import org.elasticsearch.discovery.zen.DiscoveryNodesProvider; import org.elasticsearch.discovery.zen.ping.ZenPing; import org.elasticsearch.discovery.zen.ping.ZenPingException; @@ -142,9 +141,14 @@ public class MulticastZenPing extends AbstractLifecycleComponent implem this.datagramPacketReceive = new DatagramPacket(new byte[bufferSize], bufferSize); this.datagramPacketSend = new DatagramPacket(new byte[bufferSize], bufferSize, InetAddress.getByName(group), port); } catch (Exception e) { - throw new DiscoveryException("Failed to set datagram packets", e); + logger.warn("disabled, failed to setup multicast (datagram) discovery : {}", e.getMessage()); + if (logger.isDebugEnabled()) { + logger.debug("disabled, failed to setup multicast (datagram) discovery", e); + } + return; } + InetAddress multicastInterface = null; try { MulticastSocket multicastSocket; // if (NetworkUtils.canBindToMcastAddress()) { @@ -161,7 +165,7 @@ public class MulticastZenPing extends AbstractLifecycleComponent implem multicastSocket.setTimeToLive(ttl); // set the send interface - InetAddress multicastInterface = networkService.resolvePublishHostAddress(address); + multicastInterface = networkService.resolvePublishHostAddress(address); multicastSocket.setInterface(multicastInterface); multicastSocket.joinGroup(InetAddress.getByName(group)); @@ -170,19 +174,35 @@ public class MulticastZenPing extends AbstractLifecycleComponent implem multicastSocket.setSoTimeout(60000); this.multicastSocket = multicastSocket; - } catch (Exception e) { - throw new DiscoveryException("Failed to setup multicast socket", e); - } - this.receiver = new Receiver(); - this.receiverThread = daemonThreadFactory(settings, "discovery#multicast#received").newThread(receiver); - this.receiverThread.start(); + this.receiver = new Receiver(); + this.receiverThread = daemonThreadFactory(settings, "discovery#multicast#receiver").newThread(receiver); + this.receiverThread.start(); + } catch (Exception e) { + datagramPacketReceive = null; + datagramPacketSend = null; + if (multicastSocket != null) { + multicastSocket.close(); + multicastSocket = null; + } + logger.warn("disabled, failed to setup multicast discovery on {}: {}", multicastInterface, e.getMessage()); + if (logger.isDebugEnabled()) { + logger.debug("disabled, failed to setup multicast discovery on {}", e, multicastInterface); + } + } } @Override protected void doStop() throws ElasticSearchException { - receiver.stop(); - receiverThread.interrupt(); - multicastSocket.close(); + if (receiver != null) { + receiver.stop(); + } + if (receiverThread != null) { + receiverThread.interrupt(); + } + if (multicastSocket != null) { + multicastSocket.close(); + multicastSocket = null; + } } @Override protected void doClose() throws ElasticSearchException { @@ -229,6 +249,9 @@ public class MulticastZenPing extends AbstractLifecycleComponent implem } private void sendPingRequest(int id, boolean remove) { + if (multicastSocket == null) { + return; + } synchronized (sendMutex) { CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry(); try {