Ejemplo n.º 1
0
 protected void setIpAddress(final TProtocol in) {
   TTransport transport = in.getTransport();
   TSocket tSocket = getUnderlyingSocketFromTransport(transport);
   if (tSocket != null) {
     setIpAddress(tSocket.getSocket());
   } else {
     LOGGER.warn("Unknown Transport, cannot determine ipAddress");
   }
 }
Ejemplo n.º 2
0
 public static TTransport socketTransport(String host, int port) throws RpcTransportException {
   TSocket transport = new TSocket(host, port);
   try {
     transport.open();
   } catch (TTransportException e) {
     throw new RpcTransportException(e);
   }
   return transport;
 }
Ejemplo n.º 3
0
 private void connect() throws TException {
   TSocket socket = new TSocket(host, port);
   if (timeout != null) {
     socket.setTimeout(timeout);
   }
   conn = new TFramedTransport(socket);
   client = new DistributedRPC.Client(new TBinaryProtocol(conn));
   conn.open();
 }
 @Override
 protected TSocket acceptImpl() throws TTransportException {
   TSocket ts = super.acceptImpl();
   try {
     ts.getSocket().setKeepAlive(true);
   } catch (SocketException e) {
     throw new TTransportException(e);
   }
   return ts;
 }
  @Override
  public void destroyObject(Object o) throws Exception {

    if (o instanceof TSocket) {
      TSocket socket = (TSocket) o;
      if (socket.isOpen()) {
        socket.close();
      }
    }
  }
Ejemplo n.º 6
0
 private ResultCode logThrift(int port, List<LogEntry> messages) throws TException {
   TSocket socket = new TSocket("localhost", port);
   socket.open();
   try {
     TBinaryProtocol tp = new TBinaryProtocol(new TFramedTransport(socket));
     return new scribe.Client(tp).Log(messages);
   } finally {
     socket.close();
   }
 }
 @Override
 public boolean validateObject(Object o) {
   try {
     if (o instanceof TSocket) {
       TSocket thriftSocket = (TSocket) o;
       if (thriftSocket.isOpen()) {
         return true;
       } else {
         return false;
       }
     } else {
       return false;
     }
   } catch (Exception e) {
     return false;
   }
 }
Ejemplo n.º 8
0
  @Override
  protected Integer run() throws Exception {
    String host = this.tsnode.getHost();
    int port = this.tsnode.getPort();

    TSocket transport = new TSocket(host, port, 1000);
    TProtocol protocol = new TBinaryProtocol(transport);
    PoolAble.Client client = new PoolAble.Client(protocol);
    transport.open();
    int vNodes = -1;
    try {
      vNodes = client.ping();
    } finally {
      if (transport.isOpen()) {
        transport.close();
      }
    }
    return vNodes;
  }
 /**
  * Return a client based on the given socket that points to the configured keyspace, and is logged
  * in with the configured credentials.
  *
  * @param socket a socket pointing to a particular node, seed or otherwise
  * @param conf a job configuration
  * @return a cassandra client
  * @throws InvalidRequestException
  * @throws TException
  * @throws AuthenticationException
  * @throws AuthorizationException
  */
 public static Cassandra.Client createAuthenticatedClient(TSocket socket, Configuration conf)
     throws InvalidRequestException, TException, AuthenticationException, AuthorizationException {
   TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket));
   Cassandra.Client client = new Cassandra.Client(binaryProtocol);
   socket.open();
   client.set_keyspace(ConfigHelper.getOutputKeyspace(conf));
   if (ConfigHelper.getOutputKeyspaceUserName(conf) != null) {
     Map<String, String> creds = new HashMap<String, String>();
     creds.put(SimpleAuthenticator.USERNAME_KEY, ConfigHelper.getOutputKeyspaceUserName(conf));
     creds.put(SimpleAuthenticator.PASSWORD_KEY, ConfigHelper.getOutputKeyspacePassword(conf));
     AuthenticationRequest authRequest = new AuthenticationRequest(creds);
     client.login(authRequest);
   }
   return client;
 }
    private void closeClient() {
      if (transport != null) {
        try {
          transport.flush();
        } catch (TTransportException e) {
        } finally {
          try {
            transport.close();
          } catch (Exception e) {
          } finally {
            transport = null;
          }
        }
      }

      if (socket != null) {
        try {
          socket.close();
        } catch (Exception e) {
        } finally {
          socket = null;
        }
      }
    }
 public void setTimeout(int timeout) {
   if (this.timeout != timeout) {
     socket.setTimeout(timeout);
     this.timeout = timeout;
   }
 }
    @Override
    public void open() throws ConnectionException {
      if (cassandraClient != null) {
        throw new IllegalStateException("Open called on already open connection");
      }

      long startTime = System.currentTimeMillis();
      try {
        final SSLConnectionContext sslCxt = cpConfig.getSSLConnectionContext();
        if (sslCxt != null) {
          TSSLTransportParameters params =
              new TSSLTransportParameters(
                  sslCxt.getSslProtocol(), sslCxt.getSslCipherSuites().toArray(new String[0]));
          params.setTrustStore(sslCxt.getSslTruststore(), sslCxt.getSslTruststorePassword());
          // thrift's SSL implementation does not allow you set the socket connect timeout, only
          // read timeout
          socket =
              TSSLTransportFactory.getClientSocket(
                  getHost().getIpAddress(),
                  getHost().getPort(),
                  cpConfig.getSocketTimeout(),
                  params);
        } else {
          socket =
              new TSocket(
                  getHost().getIpAddress(), getHost().getPort(), cpConfig.getConnectTimeout());
        }

        socket.getSocket().setTcpNoDelay(true);
        socket.getSocket().setKeepAlive(true);
        socket.getSocket().setSoLinger(false, 0);

        setTimeout(cpConfig.getSocketTimeout());
        transport = new TFramedTransport(socket);
        if (!transport.isOpen()) transport.open();

        cassandraClient =
            new Cassandra.Client(new TBinaryProtocol.Factory().getProtocol(transport));
        monitor.incConnectionCreated(getHost());

        AuthenticationCredentials credentials = cpConfig.getAuthenticationCredentials();
        if (credentials != null) {
          Map<String, String> thriftCredentials = Maps.newHashMapWithExpectedSize(2);
          thriftCredentials.put("username", credentials.getUsername());
          thriftCredentials.put("password", credentials.getPassword());
          cassandraClient.login(new AuthenticationRequest(thriftCredentials));
        }
      } catch (Exception e) {
        pool.addLatencySample(
            TimeUnit.NANOSECONDS.convert(cpConfig.getSocketTimeout(), TimeUnit.MILLISECONDS),
            System.nanoTime());
        closeClient();
        ConnectionException ce =
            ThriftConverter.ToConnectionPoolException(e)
                .setHost(getHost())
                .setLatency(System.currentTimeMillis() - startTime);
        monitor.incConnectionCreateFailed(getHost(), ce);
        throw ce;
      } catch (Throwable t) {
        LOG.error("Error creating connection", t);
        pool.addLatencySample(
            TimeUnit.NANOSECONDS.convert(cpConfig.getSocketTimeout(), TimeUnit.MILLISECONDS),
            System.nanoTime());
        closeClient();
        ConnectionException ce =
            ThriftConverter.ToConnectionPoolException(
                    new RuntimeException("Error openning connection", t))
                .setHost(getHost())
                .setLatency(System.currentTimeMillis() - startTime);
        monitor.incConnectionCreateFailed(getHost(), ce);
        throw ce;
      }
    }
Ejemplo n.º 13
0
 public static TTransport makeObject() throws Exception {
   TSocket transport = new TSocket("192.168.1.193", 9090, 2000);
   transport.open();
   return transport;
 }
Ejemplo n.º 14
0
 @Test
 public void testValidateWithException() {
   ThriftConnection<HelloClient> conn = new ThriftConnection<>(pool, host);
   when(transport.isOpen()).thenReturn(false);
   assertFalse(conn.validate());
 }
Ejemplo n.º 15
0
  private String getClientIp(TProtocol iprot) {
    TSocket socket = (TSocket) iprot.getTransport();
    InetAddress address = socket.getSocket().getInetAddress();

    return address.getHostAddress();
  }
Ejemplo n.º 16
0
 @Override
 public T getThriftConnection() throws Exception {
   TSocket trans = new TSocket(address.getHostName(), address.getPort(), TIMEOUT);
   trans.open();
   return clientCtor.newInstance(new TBinaryProtocol(trans));
 }