Search in sources :

Example 11 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeExceptionResponse.

@Test
public void testEncodeExceptionResponse() throws Exception {
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    RpcResult rpcResult = new RpcResult();
    String exceptionMessage = "failed";
    rpcResult.setException(new RuntimeException(exceptionMessage));
    Response response = new Response();
    response.setResult(rpcResult);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.cachedRequest.put(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assert.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assert.assertEquals("echoString", message.name);
    Assert.assertEquals(TMessageType.EXCEPTION, message.type);
    Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.read(protocol);
    protocol.readMessageEnd();
    Assert.assertEquals(exceptionMessage, exception.getMessage());
}
Also used : Demo(com.alibaba.dubbo.rpc.gen.thrift.Demo) Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) RpcResult(com.alibaba.dubbo.rpc.RpcResult) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(com.alibaba.dubbo.common.URL) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) TApplicationException(org.apache.thrift.TApplicationException) Response(com.alibaba.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) Test(org.junit.Test)

Example 12 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project Honu by jboulon.

the class HTTPMessageSender method openConnection.

protected void openConnection() throws CommunicationException {
    try {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (Exception ignored) {
            }
            httpClient = null;
        }
        if (protocol != null) {
            protocol = null;
        }
        if (collector != null) {
            collector = null;
        }
        currentCollectorInfo = CollectorRegistry.getInstance().getCollector();
        if (currentCollectorInfo == null) {
            throw new CommunicationException("collector is null");
        }
        httpClient = new THttpClient(currentCollectorInfo.getHost() + ":" + currentCollectorInfo.getPort());
        httpClient.setConnectTimeout(SENDER_TIMEOUT);
        protocol = new TBinaryProtocol(httpClient);
        collector = new HonuCollector.Client(protocol);
        int status = collector.getStatus();
        if (status != ServiceStatus.ALIVE) {
            throw new RuntimeException("Collector is not alive! -- status:" + status);
        }
    } catch (Throwable e) {
        collector = null;
        throw new CommunicationException(e);
    }
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) HonuCollector(org.honu.thrift.HonuCollector) THttpClient(org.apache.thrift.transport.THttpClient)

Example 13 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project brisk by riptano.

the class Session method getClient.

/**
     * Thrift client connection
     * @param setKeyspace - should we set keyspace for client or not
     * @return cassandra client connection
     */
public Cassandra.Client getClient(boolean setKeyspace) {
    // random node selection for fake load balancing
    String currentNode = nodes[Pricer.randomizer.nextInt(nodes.length)];
    TSocket socket = new TSocket(currentNode, port);
    TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
    Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
    try {
        transport.open();
        if (setKeyspace) {
            client.set_keyspace("PortfolioDemo");
        }
    } catch (InvalidRequestException e) {
        throw new RuntimeException(e.getWhy());
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return client;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 14 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project brisk by riptano.

the class ThriftUtils method createNamenodeClient.

/**
   * Creates a Thrift name node client.
   * 
   * @param conf the HDFS instance
   * @return a Thrift name node client.
   */
public static Namenode.Client createNamenodeClient(Configuration conf) throws Exception {
    String s = conf.get(NamenodePlugin.THRIFT_ADDRESS_PROPERTY, NamenodePlugin.DEFAULT_THRIFT_ADDRESS);
    // TODO(todd) use fs.default.name here if set to 0.0.0.0 - but share this with the code in
    // SecondaryNameNode that does the same
    InetSocketAddress addr = NetUtils.createSocketAddr(s);
    // in the thrift config.
    if (addr.getAddress().isAnyLocalAddress()) {
        InetSocketAddress nnAddr = NameNode.getAddress(conf);
        addr = new InetSocketAddress(nnAddr.getAddress(), addr.getPort());
    }
    TTransport t = new TSocket(addr.getHostName(), addr.getPort());
    if (UserGroupInformation.isSecurityEnabled()) {
        t = new HadoopThriftAuthBridge.Client().createClientTransport(conf.get(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY), addr.getHostName(), "KERBEROS", t);
    }
    t.open();
    TProtocol p = new TBinaryProtocol(t);
    return new Namenode.Client(p);
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TProtocol(org.apache.thrift.protocol.TProtocol) InetSocketAddress(java.net.InetSocketAddress) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 15 with TBinaryProtocol

use of org.apache.thrift.protocol.TBinaryProtocol in project jstorm by alibaba.

the class ThriftClient method reconnect.

public synchronized void reconnect() {
    close();
    try {
        TSocket socket = new TSocket(host, port);
        if (timeout != null) {
            socket.setTimeout(timeout);
        } else {
        // @@@ Todo
        // set the socket default Timeout as xxxx
        }
        // locate login configuration
        Configuration login_conf = AuthUtils.GetConfiguration(conf);
        // construct a transport plugin
        ITransportPlugin transportPlugin = AuthUtils.GetTransportPlugin(type, conf, login_conf);
        final TTransport underlyingTransport = socket;
        // TODO get this from type instead of hardcoding to Nimbus.
        // establish client-server transport via plugin
        // do retries if the connect fails
        TBackoffConnect connectionRetry = new TBackoffConnect(Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_TIMES)), Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_INTERVAL)), Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_INTERVAL_CEILING)));
        _transport = connectionRetry.doConnectWithRetry(transportPlugin, underlyingTransport, host, asUser);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    _protocol = null;
    if (_transport != null) {
        _protocol = new TBinaryProtocol(_transport);
    }
}
Also used : Configuration(javax.security.auth.login.Configuration) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TTransport(org.apache.thrift.transport.TTransport) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)52 TSocket (org.apache.thrift.transport.TSocket)34 TProtocol (org.apache.thrift.protocol.TProtocol)26 TTransport (org.apache.thrift.transport.TTransport)25 TFramedTransport (org.apache.thrift.transport.TFramedTransport)21 TException (org.apache.thrift.TException)16 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)11 IOException (java.io.IOException)10 TMessage (org.apache.thrift.protocol.TMessage)8 TTransportException (org.apache.thrift.transport.TTransportException)8 Test (org.junit.Test)7 Request (com.alibaba.dubbo.remoting.exchange.Request)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)5 Hbase (org.apache.hadoop.hbase.thrift.generated.Hbase)5 URL (com.alibaba.dubbo.common.URL)4 Channel (com.alibaba.dubbo.remoting.Channel)4 Response (com.alibaba.dubbo.remoting.exchange.Response)4