Search in sources :

Example 1 with MetaPaths

use of com.ms.silverking.cloud.dht.meta.MetaPaths in project SilverKing by Morgan-Stanley.

the class DHTClient method openSession.

/**
 * Open a new session to the specified SilverKing DHT instance using the given SessionOptions.
 * @param sessionOptions options specifying the SilverKing DHT instance and the parameters of this session
 * @return a new session to the given instance
 * @throws ClientException
 */
public DHTSession openSession(SessionOptions sessionOptions) throws ClientException {
    ClientDHTConfiguration dhtConfig;
    String preferredServer;
    dhtConfig = sessionOptions.getDHTConfig();
    preferredServer = sessionOptions.getPreferredServer();
    if (preferredServer != null) {
        if (preferredServer.equals(SessionOptions.EMBEDDED_PASSIVE_NODE)) {
            embedPassiveNode(dhtConfig);
            preferredServer = null;
        } else if (preferredServer.equals(SessionOptions.EMBEDDED_KVS)) {
            String gcBase;
            String gcName;
            dhtConfig = embedKVS();
            // FIXME - make user configurable
            gcBase = "/tmp";
            gcName = "GC_" + dhtConfig.getName();
            try {
                Log.warningf("GridConfigBase: %s", gcBase);
                Log.warningf("GridConfigName: %s", gcName);
                StaticDHTCreator.writeGridConfig(dhtConfig, gcBase, gcName);
            } catch (IOException e) {
                throw new ClientException("Error creating embedded kvs", e);
            }
            preferredServer = null;
        }
    }
    sessionCreationLock.lock();
    try {
        DHTSession session;
        if (preferredServer == null) {
            preferredServer = IPAddrUtil.localIPString();
        }
        // session = dhtNameToSessionMap.get(dhtConfig.getName());
        // FUTURE - this forces a new session
        session = null;
        // think about whether we want to use multiple or cache to common
        if (session == null) {
            DHTSession prev;
            int serverPort;
            try {
                if (dhtConfig.hasPort()) {
                    serverPort = dhtConfig.getPort();
                } else {
                    MetaClient mc;
                    MetaPaths mp;
                    long latestConfigVersion;
                    Log.warning("dhtConfig.getZkLocs(): " + dhtConfig.getZKConfig());
                    mc = new MetaClient(dhtConfig);
                    mp = mc.getMetaPaths();
                    Log.warning("getting latest version: " + mp.getInstanceConfigPath());
                    latestConfigVersion = mc.getZooKeeper().getLatestVersion(mp.getInstanceConfigPath());
                    Log.warning("latestConfigVersion: " + latestConfigVersion);
                    serverPort = new DHTConfigurationZK(mc).readFromZK(latestConfigVersion, null).getPort();
                }
            } catch (Exception e) {
                throw new ClientException(e);
            }
            try {
                session = new DHTSessionImpl(dhtConfig, new IPAndPort(IPAddrUtil.serverNameToAddr(preferredServer), serverPort), absMillisTimeSource, serializationRegistry, sessionOptions.getTimeoutController());
            } catch (IOException ioe) {
                throw new ClientException(ioe);
            }
            Log.info("session returned: ", session);
        /*
				prev = dhtNameToSessionMap.put(dhtConfig.getName(), session);
				if (prev != null) {
				    throw new RuntimeException("panic");
				}
				*/
        }
        return session;
    } finally {
        sessionCreationLock.unlock();
    }
}
Also used : IPAndPort(com.ms.silverking.net.IPAndPort) MetaClient(com.ms.silverking.cloud.dht.meta.MetaClient) IOException(java.io.IOException) IOException(java.io.IOException) MetaPaths(com.ms.silverking.cloud.dht.meta.MetaPaths) DHTConfigurationZK(com.ms.silverking.cloud.dht.meta.DHTConfigurationZK) DHTSessionImpl(com.ms.silverking.cloud.dht.client.impl.DHTSessionImpl)

Aggregations

DHTSessionImpl (com.ms.silverking.cloud.dht.client.impl.DHTSessionImpl)1 DHTConfigurationZK (com.ms.silverking.cloud.dht.meta.DHTConfigurationZK)1 MetaClient (com.ms.silverking.cloud.dht.meta.MetaClient)1 MetaPaths (com.ms.silverking.cloud.dht.meta.MetaPaths)1 IPAndPort (com.ms.silverking.net.IPAndPort)1 IOException (java.io.IOException)1