Created
March 17, 2018 16:24
-
-
Save liyuntao/72e5d0f9ae2864bd2cb8f9e26696f4fe to your computer and use it in GitHub Desktop.
Apache Ignite discovery for Rancher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.ignite.internal.IgniteInterruptedCheckedException; | |
import org.apache.ignite.internal.util.typedef.internal.U; | |
import org.apache.ignite.spi.IgniteSpiException; | |
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinderAdapter; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
public class TcpDiscoveryRancherIpFinder extends TcpDiscoveryIpFinderAdapter { | |
private Logger log = LoggerFactory.getLogger("TcpDiscoveryRancherIpFinder"); | |
private URL url; | |
private final AtomicBoolean initGuard = new AtomicBoolean(); | |
private final CountDownLatch initLatch = new CountDownLatch(1); | |
public TcpDiscoveryRancherIpFinder() { | |
setShared(true); | |
} | |
private String getServiceNameFromMeta() throws IOException { | |
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setRequestMethod("GET"); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); | |
String result = ""; | |
String line; | |
while ((line = bufferedReader.readLine()) != null) { | |
result += line; | |
} | |
bufferedReader.close(); | |
return result; | |
} | |
@Override | |
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException { | |
init(); | |
Collection<InetSocketAddress> addrs = new ArrayList<>(); | |
try { | |
String serviceNameInRancherStack = getServiceNameFromMeta().trim(); | |
if (log.isDebugEnabled()) | |
log.debug("Getting Apache Ignite endpoints from: " + serviceNameInRancherStack); | |
InetAddress[] addresses = InetAddress.getAllByName(serviceNameInRancherStack); | |
for (InetAddress address : addresses) { | |
if (log.isDebugEnabled()) | |
log.debug("Added an address to the list: " + address.getHostAddress()); | |
addrs.add(InetSocketAddress.createUnresolved(address.getHostAddress(), 0)); | |
} | |
} catch (Exception e) { | |
throw new IgniteSpiException("Failed to retrieve Ignite pods IP addresses.", e); | |
} | |
return addrs; | |
} | |
private void init() throws IgniteSpiException { | |
if (initGuard.compareAndSet(false, true)) { | |
try { | |
url = new URL("http://rancher-metadata/2015-12-19/self/service/name"); | |
} catch (Exception e) { | |
throw new IgniteSpiException("Failed to connect to Ignite's Kubernetes Service.", e); | |
} finally { | |
initLatch.countDown(); | |
} | |
} else { | |
try { | |
U.await(initLatch); | |
} catch (IgniteInterruptedCheckedException e) { | |
throw new IgniteSpiException("Thread has been interrupted.", e); | |
} | |
if (url == null) | |
throw new IgniteSpiException("RancherIp finder has not been initialized properly."); | |
} | |
} | |
@Override | |
public void registerAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException { | |
// No-op | |
} | |
@Override | |
public void unregisterAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException { | |
// No-op | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apache Ignite discovery for Rancher, without external dependence.