Get connection to a specific h2o node. The caller of this method is usually non-H2O node who wants to read H2O
frames or write to H2O frames from non-H2O environment, such as Spark executor.
This node usually does not have H2O running.
160349
public static ByteChannel getConnection(String h2oNodeHostname, int h2oNodeApiPort, short nodeTimeStamp) throws IOException{
        SocketChannelFactory socketFactory = SocketChannelFactory.instance(H2OSecurityManager.instance());
        return H2ONode.openChan(TCPReceiverThread.TCP_EXTERNAL, socketFactory, h2oNodeHostname, h2oNodeApiPort +1, nodeTimeStamp);
    }
1068898
def _get_remote_node_url(self, remote_node):
        
        logging.getLogger("requests").setLevel(logging.WARNING)
        gridextras_port = 3000
        return 'http://{}:{}'.format(remote_node, gridextras_port)
412439
@Override public void dinvoke( H2ONode sender ) {
    _h2o = sender;
    Key k = _key;
    _key = null;          // Not part of the return result
    assert k.home();      // Gets are always from home (less we do replication)
    // Shipping a result?  Track replicas so we can invalidate.  There's a
    // narrow race on a moving K/V mapping tracking this Value just as it gets
    // deleted - in which case, simply retry for another Value.
    do  _val = H2O.get(k);      // The return result
    while( _val != null && !_val.setReplica(sender) );
    tryComplete();
  }
49190
def get_connection(self):
        
        if self.sniffer_timeout:
            if time.time() >= self.last_sniff + self.sniffer_timeout:
                self.sniff_hosts()
        return self.connection_pool.get_connection()
648224
def connect(node_id, other_node_id):
    
    exp = experiment(session)

    # get the parameters
    direction = request_parameter(parameter="direction", default="to")
    if type(direction == Response):
        return direction

    # check the nodes exist
    node = models.Node.query.get(node_id)
    if node is None:
        return error_response(error_type="/node/connect, node does not exist")

    other_node = models.Node.query.get(other_node_id)
    if other_node is None:
        return error_response(
            error_type="/node/connect, other node does not exist",
            participant=node.participant)

    # execute the request
    try:
        vectors = node.connect(whom=other_node, direction=direction)
        for v in vectors:
            assign_properties(v)

        # ping the experiment
        exp.vector_post_request(
            node=node,
            vectors=vectors)

        session.commit()
    except:
        return error_response(error_type="/vector POST server error",
                              status=403,
                              participant=node.participant)

    return success_response(field="vectors",
                            data=[v.__json__() for v in vectors],
                            request_type="vector post")
668370
@Override
    public Object getMember(String name) {
        switch (name) {
            case "version":
                return F_version;
            case "sparkContext":
                return F_sparkContext;
            case "sqlContext":
                return F_sqlContext;
//            case "listenerManager":
//                return F_listenerManager;
//            case "experimental":
//                return F_experimental;
            case "udf":
                return F_udf;
            case "streams":
                return F_streams;
            case "newSession":
                return F_newSession;
//            case "emptyDataset":
//                return F_emptyDataset;
            case "createDataFrame":
                return F_createDataFrame;
            case "createDataFrameFromJson":
                return F_createDataFrameFromJson;
            case "baseRelationToDataFrame":
                return F_baseRelationToDataFrame;
            case "createDataset":
                return F_createDataset;
            case "createDatasetFromJson":
                return F_createDatasetFromJson;
            case "range":
                return F_range;
            case "table":
                return F_table;
            case "sql":
                return F_sql;
            case "read":
                return F_read;
            case "readStream":
                return F_readStream;
            case "stop":
                return F_stop;
        }
        return super.getMember(name);
    }
24677
@Override public byte[] load(Value v) {
    long start_io_ms = System.currentTimeMillis();
    byte[] b = MemoryManager.malloc1(v._max);
    Key k = v._key;
    long skip = 0;
    // Skip offset based on chunk number
    if(k._kb[0] == Key.CHK)
      skip = FileVec.chunkOffset(k); // The offset
    // Too complicate matters, S3 likes to reset connections when H2O hits it
    // too hard.  We "fix" this by just trying again, assuming we're getting
    // hit with a bogus resource limit (H2O doing a parse looks like a DDOS to
    // Amazon S3).
    S3ObjectInputStream s = null;

    while( true ) {             // Loop, in case we get premature EOF's
      try {
        long start_ns = System.nanoTime(); // Blocking i/o call timing - without counting repeats
        s = getObjectForKey(k, skip, v._max).getObjectContent();
        ByteStreams.readFully(s, b); // delegate work to Google (it reads the byte buffer in a cycle as we did)
        assert v.isPersisted();
//        TimeLine.record_IOclose(start_ns, start_io_ms, 1/* read */, v._max, Value.S3);
        return b;
        // Explicitly ignore the following exceptions but
        // fail on the rest IOExceptions
      } catch( EOFException e ) {
        ignoreAndWait(e, false);
      } catch( SocketTimeoutException e ) {
        ignoreAndWait(e, false);
      } catch( IOException e ) {
        ignoreAndWait(e, true);
      } finally {
        try {
          if( s != null ) s.close();
        } catch( IOException e ) {}
      }
    }
  }
696430
def find_etcd_node(node)
      grid_subnet = IPAddr.new(node.grid['subnet'])
      tries = node.grid['initial_size']
      begin
        etcd_host = "http://#{grid_subnet[tries]}:2379/v2/members"

        info "connecting to existing etcd at #{etcd_host}"
        connection = Excon.new(etcd_host)
        members = JSON.parse(connection.get.body)

        return connection
      rescue Excon::Errors::Error => exc
        tries -= 1
        if tries > 0
          info 'retrying next etcd host'
          retry
        else
          info 'no online etcd host found, we\'re probably bootstrapping first node'
        end
      end
      nil
    end
660138
static synchronized int doHeartbeat( H2ONode h2o ) {
    // Kill somebody if the jar files mismatch.  Do not attempt to deal with
    // mismatched jars.
    if(!H2O.ARGS.client && !h2o._heartbeat._client) {
      // don't check md5 for client nodes
      if (!h2o._heartbeat.check_jar_md5()) {
        System.out.println("Jar check fails; my hash=" + Arrays.toString(JarHash.JARHASH));
        System.out.println("Jar check fails; received hash=" + Arrays.toString(h2o._heartbeat._jar_md5));
        if (H2O.CLOUD.size() > 1) {
          Log.warn("Killing " + h2o + " because of H2O version mismatch (md5 differs).");
          UDPRebooted.T.mismatch.send(h2o);
        } else {
          H2O.die("Attempting to join " + h2o + " with an H2O version mismatch (md5 differs).  (Is H2O already running?)  Exiting.");
        }
        return 0;
      }
    }else{
      if (!h2o._heartbeat.check_jar_md5()) { // we do not want to disturb the user in this case
        // Just report that client with different md5 tried to connect
        ListenerService.getInstance().report("client_wrong_md5", new Object[]{h2o._heartbeat._jar_md5});
      }
    }

    if(h2o._heartbeat._cloud_name_hash != H2O.SELF._heartbeat._cloud_name_hash){
      // ignore requests from this node as they are coming from different cluster
      return 0;
    }


    // Update manual flatfile in case of flatfile is enabled
    if (H2O.isFlatfileEnabled()) {
      if (!H2O.ARGS.client && h2o._heartbeat._client && !H2O.isNodeInFlatfile(h2o)) {
        // A new client was reported to this node so we propagate this information to all nodes in the cluster, to this
        // as well
        UDPClientEvent.ClientEvent.Type.CONNECT.broadcast(h2o);
      } else if (H2O.ARGS.client && !H2O.isNodeInFlatfile(h2o)) {
        // This node is a client and using a flatfile to figure out a topology of the cluster. The flatfile passed to the
        // client is always modified at the start of H2O to contain only a single node. This node is used to propagate
        // information about the client to the cluster. Once the nodes have the information about the client, then propagate
        // themselves via heartbeat to the client
        H2O.addNodeToFlatfile(h2o);
      }
    }

    // Never heard of this dude?  See if we want to kill him off for being cloud-locked
    if( !PROPOSED.contains(h2o) && !h2o._heartbeat._client ) {
      if( _cloudLocked ) {
        Log.warn("Killing "+h2o+" because the cloud is no longer accepting new H2O nodes.");
        UDPRebooted.T.locked.send(h2o);
        return 0;
      }
      if( _commonKnowledge ) {
        _commonKnowledge = false; // No longer sure about things
        H2O.SELF._heartbeat._common_knowledge = false;
        Log.debug("Cloud voting in progress");
      }

      // Add to proposed set, update cloud hash.  Do not add clients
      H2ONode res = PROPOSED.putIfAbsent(h2o._key,h2o);
      assert res==null;
      H2O.SELF._heartbeat._cloud_hash += h2o.hashCode();

    } else if( _commonKnowledge ) {
      return 0;                 // Already know about you, nothing more to do
    }
    int chash = H2O.SELF._heartbeat._cloud_hash;
    assert chash == doHash() : "mismatched hash4, HB="+chash+" full="+doHash();
    assert !_commonKnowledge;

    // Do we have consensus now?
    H2ONode h2os[] = PROPOSED.values().toArray(new H2ONode[PROPOSED.size()]);
    if( H2O.ARGS.client && h2os.length == 0 ) return 0; // Client stalls until it finds *some* cloud
    for( H2ONode h2o2 : h2os )
      if( chash != h2o2._heartbeat._cloud_hash )
        return print("Heartbeat hashes differ, self=0x"+Integer.toHexString(chash)+" "+h2o2+"=0x"+Integer.toHexString(h2o2._heartbeat._cloud_hash)+" ",PROPOSED);
    // Hashes are same, so accept the new larger cloud-size
    H2O.CLOUD.set_next_Cloud(h2os,chash);

    // Demand everybody has rolled forward to same size before consensus
    boolean same_size=true;
    for( H2ONode h2o2 : h2os )
      same_size &= (h2o2._heartbeat._cloud_size == H2O.CLOUD.size());
    if( !same_size ) return 0;

    H2O.SELF._heartbeat._common_knowledge = true;
    for( H2ONode h2o2 : h2os )
      if( !h2o2._heartbeat._common_knowledge )
        return print("Missing common knowledge from all nodes!" ,PROPOSED);
    _commonKnowledge = true;    // Yup!  Have global consensus

    Paxos.class.notifyAll(); // Also, wake up a worker thread stuck in DKV.put
    Paxos.print("Announcing new Cloud Membership: ", H2O.CLOUD._memary);
    Log.info("Cloud of size ", H2O.CLOUD.size(), " formed ", H2O.CLOUD.toString());
    H2Okey leader = H2O.CLOUD.leader()._key;
    H2O.notifyAboutCloudSize(H2O.SELF_ADDRESS, H2O.API_PORT, leader.getAddress(), leader.htm_port(), H2O.CLOUD.size());
    return 0;
  }
1029063
int getHalfEdgeConnector(int clusterFrom, int clusterTo) {
		int first_edge = getClusterHalfEdge(clusterFrom);
		if (first_edge == -1)
			return -1;
		int edge = first_edge;
		int firstEdgeTo = -1;
		int eTo = -1;
		// Doing two loops in parallel - one on the half-edges attached to the
		// clusterFrom, another - attached to clusterTo.
		do {
			if (getHalfEdgeTo(edge) == clusterTo)
				return edge;

			if (firstEdgeTo == -1) {
				firstEdgeTo = getClusterHalfEdge(clusterTo);
				if (firstEdgeTo == -1)
					return -1;
				eTo = firstEdgeTo;
			}

			if (getHalfEdgeTo(eTo) == clusterFrom) {
				edge = getHalfEdgeTwin(eTo);
				assert (getHalfEdgeTo(edge) == clusterTo && getHalfEdgeOrigin(edge) == clusterFrom);
				return edge;
			}

			edge = getHalfEdgeNext(getHalfEdgeTwin(edge));
			eTo = getHalfEdgeNext(getHalfEdgeTwin(eTo));
		} while (edge != first_edge && eTo != firstEdgeTo);

		return -1;
	}
1320995
private void connected(final MemcachedNode node) {
    assert node.getChannel().isConnected() : "Not connected.";
    int rt = node.getReconnectCount();
    node.connected();

    for (ConnectionObserver observer : connObservers) {
      observer.connectionEstablished(node.getSocketAddress(), rt);
    }
  }
1004131
def _maybe_connect(self, node_id):
        """"""
        with self._lock:
            conn = self._conns.get(node_id)

            if conn is None:
                broker = self.cluster.broker_metadata(node_id)
                assert broker, 'Broker id %s not in current metadata' % (node_id,)

                log.debug("Initiating connection to node %s at %s:%s",
                          node_id, broker.host, broker.port)
                host, port, afi = get_ip_port_afi(broker.host)
                cb = WeakMethod(self._conn_state_change)
                conn = BrokerConnection(host, broker.port, afi,
                                        state_change_callback=cb,
                                        node_id=node_id,
                                        **self.config)
                self._conns[node_id] = conn

            # Check if existing connection should be recreated because host/port changed
            elif self._should_recycle_connection(conn):
                self._conns.pop(node_id)
                return False

            elif conn.connected():
                return True

            conn.connect()
            return conn.connected()
397043
def return_connection_handler(nodes, exit_node):
    """"""
    for function_body_node in nodes:
        if isinstance(function_body_node, ConnectToExitNode):
            if exit_node not in function_body_node.outgoing:
                function_body_node.connect(exit_node)
512959
@Override
    public String getConnectionInfo() throws Exception {
        return String.format("Cluster Name - %s : Keyspace Name - %s : CF Name - %s ::: ReadCL - %s : WriteCL - %s ", ClusterName, KeyspaceName, TableName, ReadConsistencyLevel, WriteConsistencyLevel);
    }
861525
@Override
	@JsonIgnore
	public JMXConnector getServerConnection() throws IOException {
		JMXServiceURL url = getJmxServiceURL();
		return JMXConnectorFactory.connect(url, this.getEnvironment());
	}
1132057
public static int getFileCrc(
      int dataTransferVersion, String src,
      ClientProtocol namenode, ProtocolProxy<ClientProtocol> namenodeProxy,
      SocketFactory socketFactory, int socketTimeout
      ) throws IOException {
    //get all block locations
    final LocatedBlocks locatedBlocks = callGetBlockLocations(
        namenode, src, 0, Long.MAX_VALUE, isMetaInfoSuppoted(namenodeProxy));
    if (locatedBlocks == null) {
      throw new IOException("Null block locations, mostly because non-existent file " + src);
    }
    int namespaceId = 0;
    if (locatedBlocks instanceof LocatedBlocksWithMetaInfo) {
      LocatedBlocksWithMetaInfo lBlocks = (LocatedBlocksWithMetaInfo)locatedBlocks;
      dataTransferVersion = lBlocks.getDataProtocolVersion();
      namespaceId = lBlocks.getNamespaceID();
    } else if (dataTransferVersion == -1) {
      dataTransferVersion = namenode.getDataTransferProtocolVersion();
    }
    final List<LocatedBlock> locatedblocks  = locatedBlocks.getLocatedBlocks();
    int fileCrc = 0;

    // get block CRC for each block
    for(int i = 0; i < locatedblocks.size(); i++) {
      LocatedBlock lb = locatedblocks.get(i);
      final Block block = lb.getBlock();
      final DatanodeInfo[] datanodes = lb.getLocations();

      // try each datanode location of the block
      final int timeout = (socketTimeout > 0) ? (socketTimeout +
        HdfsConstants.READ_TIMEOUT_EXTENSION * datanodes.length) : 0;

      boolean done = false;
      for(int j = 0; !done && j < datanodes.length; j++) {
        final Socket sock = socketFactory.createSocket();
        DataOutputStream out = null;
        DataInputStream in = null;

        try {
          // connect to a datanode
          NetUtils.connect(sock,
              NetUtils.createSocketAddr(datanodes[j].getName()), timeout);
          sock.setSoTimeout(timeout);

          out = new DataOutputStream(new BufferedOutputStream(
              NetUtils.getOutputStream(sock), DataNode.SMALL_BUFFER_SIZE));
          in = new DataInputStream(NetUtils.getInputStream(sock));

          if (LOG.isDebugEnabled()) {
            LOG.debug("read block CRC from" + datanodes[j].getName() + ": "
                + DataTransferProtocol.OP_BLOCK_CRC + ", block=" + block);
          }
          
          /* Write the header */
          BlockCrcHeader blockCrcHeader = new BlockCrcHeader(
              dataTransferVersion, namespaceId, block.getBlockId(),
              block.getGenerationStamp());
          blockCrcHeader.writeVersionAndOpCode(out);
          blockCrcHeader.write(out);
          out.flush();

          final short reply = in.readShort();
          if (reply != DataTransferProtocol.OP_STATUS_SUCCESS) {
            throw new IOException("Bad response " + reply + " for block "
                + block + " from datanode " + datanodes[j].getName());
          }

          //read block CRC
          final int blockCrc = (int) in.readLong();
          if (i == 0) {
            fileCrc = blockCrc;
          } else {
            // Concatenate to CRC of previous bytes.
            fileCrc = CrcConcat.concatCrc(fileCrc, blockCrc, (int) lb.getBlockSize());
          }

          done = true;

          if (LOG.isDebugEnabled()) {
            LOG.debug("got reply from " + datanodes[j].getName()
                + ": crc=" + blockCrcHeader);
          }
        } catch (IOException ie) {
          LOG.warn("src=" + src + ", datanodes[" + j + "].getName()="
              + datanodes[j].getName(), ie);
        } finally {
          IOUtils.closeStream(in);
          IOUtils.closeStream(out);
          IOUtils.closeSocket(sock);
        }
      }

      if (!done) {
        throw new IOException("Fail to get block CRC for " + block);
      }
    }

    return fileCrc;
  }
1099774
private Connection getConnection(ConnectionRequestContext context) throws SQLException {
        concurrentConnectionRequestCountHistogram.update(concurrentConnectionRequestCount.incrementAndGet());
        long startNanos = System.nanoTime();
        try {
            Connection connection = null;
            if (!connectionAcquiringStrategies.isEmpty()) {
                for (ConnectionAcquiringStrategy strategy : connectionAcquiringStrategies) {
                    try {
                        connection = strategy.getConnection(context);
                        break;
                    } catch (AcquireTimeoutException e) {
                        LOGGER.warn("Couldn't retrieve connection from strategy {} with context {}", strategy, context);
                    }
                }
            } else {
                connection = poolAdapter.getConnection(context);
            }
            if (connection != null) {
                return connectionProxyFactory.newInstance(connection, this);
            } else {
                throw new CantAcquireConnectionException("Couldn't acquire connection for current strategies: " + connectionAcquiringStrategies);
            }
        } finally {
            long endNanos = System.nanoTime();
            long acquireDurationMillis = TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos);
            connectionAcquireTotalTimer.update(acquireDurationMillis, TimeUnit.MILLISECONDS);
            concurrentConnectionRequestCountHistogram.update(concurrentConnectionRequestCount.decrementAndGet());
            if (acquireDurationMillis > connectionAcquireTimeThresholdMillis) {
                eventPublisher.publish(new ConnectionAcquireTimeThresholdExceededEvent(
                        uniqueName, connectionAcquireTimeThresholdMillis, acquireDurationMillis
                ));
                LOGGER.info("Connection acquired in {} millis, while threshold is set to {} in {} FlexyPoolDataSource",
                        acquireDurationMillis, connectionAcquireTimeThresholdMillis, uniqueName);
            }
        }
    }