Example usage for org.apache.thrift.protocol TProtocol getTransport

List of usage examples for org.apache.thrift.protocol TProtocol getTransport

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TProtocol getTransport.

Prototype

public TTransport getTransport() 

Source Link

Document

Transport accessor

Usage

From source file:alluxio.master.file.FileSystemMasterClientServiceProcessor.java

License:Apache License

@Override
public boolean process(TProtocol in, TProtocol out) throws TException {
    TTransport transport = in.getTransport();
    if (transport instanceof TSaslServerTransport) {
        transport = ((TSaslServerTransport) transport).getUnderlyingTransport();
    }/*from   w w  w.j  av  a  2s  .c o m*/
    if (transport instanceof TSocket) {
        String ip = ((TSocket) transport).getSocket().getInetAddress().toString();
        sClientIpThreadLocal.set(ip);
    } else {
        LOG.warn("Failed to obtain client IP: underlying transport is not TSocket");
        sClientIpThreadLocal.set(null);
    }
    return super.process(in, out);
}

From source file:alluxio.master.PollingMasterInquireClient.java

License:Apache License

private void pingMetaService(InetSocketAddress address) throws UnauthenticatedException, TTransportException {
    TTransport transport = TransportProvider.Factory.create().getClientTransport(address);
    TProtocol protocol = ThriftUtils.createThriftProtocol(transport, Constants.META_MASTER_SERVICE_NAME);
    protocol.getTransport().open();
    protocol.getTransport().close();/*from w  w w .j  a  v  a  2 s.  c  o m*/
}

From source file:com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor.java

License:Open Source License

public boolean process(TProtocol in, TProtocol out) throws TException {

    short magic = in.readI16();

    if (magic != ThriftCodec.MAGIC) {
        logger.error(new StringBuilder(24).append("Unsupported magic ").append(magic).toString());
        return false;
    }/*from  w ww  .ja v a  2s.  c o m*/

    in.readI32();
    in.readI16();
    byte version = in.readByte();
    String serviceName = in.readString();
    long id = in.readI64();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);

    TIOStreamTransport transport = new TIOStreamTransport(bos);

    TProtocol protocol = protocolFactory.getProtocol(transport);

    TProcessor processor = processorMap.get(serviceName);

    if (processor == null) {
        logger.error(new StringBuilder(32).append("Could not find processor for service ").append(serviceName)
                .toString());
        return false;
    }

    // todo if exception
    boolean result = processor.process(in, protocol);

    ByteArrayOutputStream header = new ByteArrayOutputStream(512);

    TIOStreamTransport headerTransport = new TIOStreamTransport(header);

    TProtocol headerProtocol = protocolFactory.getProtocol(headerTransport);

    headerProtocol.writeI16(magic);
    headerProtocol.writeI32(Integer.MAX_VALUE);
    headerProtocol.writeI16(Short.MAX_VALUE);
    headerProtocol.writeByte(version);
    headerProtocol.writeString(serviceName);
    headerProtocol.writeI64(id);
    headerProtocol.getTransport().flush();

    out.writeI16(magic);
    out.writeI32(bos.size() + header.size());
    out.writeI16((short) (0xffff & header.size()));
    out.writeByte(version);
    out.writeString(serviceName);
    out.writeI64(id);

    out.getTransport().write(bos.toByteArray());
    out.getTransport().flush();

    return result;

}

From source file:com.baidu.oped.apm.plugin.thrift.interceptor.client.TServiceClientSendBaseInterceptor.java

License:Apache License

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }//from w  ww .j a  v a 2 s.c om
    if (target instanceof TServiceClient) {
        TServiceClient client = (TServiceClient) target;
        TProtocol oprot = client.getOutputProtocol();
        TTransport transport = oprot.getTransport();
        final Trace trace = traceContext.currentRawTraceObject();
        if (trace == null) {
            return;
        }
        ThriftRequestProperty parentTraceInfo = new ThriftRequestProperty();
        final boolean shouldSample = trace.canSampled();
        if (!shouldSample) {
            if (isDebug) {
                logger.debug("set Sampling flag=false");
            }
            parentTraceInfo.setShouldSample(shouldSample);
        } else {
            SpanEventRecorder recorder = trace.traceBlockBegin();
            recorder.recordServiceType(ThriftConstants.THRIFT_CLIENT);

            // retrieve connection information
            String remoteAddress = ThriftConstants.UNKNOWN_ADDRESS;
            if (transport instanceof SocketFieldAccessor) {
                Socket socket = ((SocketFieldAccessor) transport)._$APM$_getSocket();
                if (socket != null) {
                    remoteAddress = ThriftUtils.getHostPort(socket.getRemoteSocketAddress());
                }
            } else {
                if (isDebug) {
                    logger.debug("Invalid target object. Need field accessor({}).",
                            SocketFieldAccessor.class.getName());
                }
            }
            recorder.recordDestinationId(remoteAddress);

            String methodName = ThriftConstants.UNKNOWN_METHOD_NAME;
            if (args[0] instanceof String) {
                methodName = (String) args[0];
            }
            String serviceName = ThriftUtils.getClientServiceName(client);

            String thriftUrl = getServiceUrl(remoteAddress, serviceName, methodName);
            recorder.recordAttribute(ThriftConstants.THRIFT_URL, thriftUrl);

            TraceId nextId = trace.getTraceId().getNextTraceId();
            recorder.recordNextSpanId(nextId.getSpanId());

            parentTraceInfo.setTraceId(nextId.getTransactionId());
            parentTraceInfo.setSpanId(nextId.getSpanId());
            parentTraceInfo.setParentSpanId(nextId.getParentSpanId());

            parentTraceInfo.setFlags(nextId.getFlags());
            parentTraceInfo.setParentApplicationName(traceContext.getApplicationName());
            parentTraceInfo.setParentApplicationType(traceContext.getServerTypeCode());
            parentTraceInfo.setAcceptorHost(remoteAddress);
        }
        InterceptorGroupInvocation currentTransaction = this.group.getCurrentInvocation();
        currentTransaction.setAttachment(parentTraceInfo);
    }
}

From source file:com.baidu.oped.apm.plugin.thrift.interceptor.server.TBaseProcessorProcessInterceptor.java

License:Apache License

private void processTraceObject(final Trace trace, Object target, Object[] args, Throwable throwable) {
    // end spanEvent
    try {/*from w w  w  .ja va2 s. co m*/
        SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        // TODO Might need a way to collect and record method arguments
        // trace.recordAttribute(...);
        recorder.recordException(throwable);
        recorder.recordApi(this.descriptor);
    } catch (Throwable t) {
        logger.warn("Error processing trace object. Cause:{}", t.getMessage(), t);
    } finally {
        trace.traceBlockEnd();
    }

    // end root span
    SpanRecorder recorder = trace.getSpanRecorder();
    String methodUri = getMethodUri(target);
    recorder.recordRpcName(methodUri);
    // retrieve connection information
    String localIpPort = ThriftConstants.UNKNOWN_ADDRESS;
    String remoteAddress = ThriftConstants.UNKNOWN_ADDRESS;
    if (args.length == 2 && args[0] instanceof TProtocol) {
        TProtocol inputProtocol = (TProtocol) args[0];
        TTransport inputTransport = inputProtocol.getTransport();
        if (inputTransport instanceof SocketFieldAccessor) {
            Socket socket = ((SocketFieldAccessor) inputTransport)._$APM$_getSocket();
            if (socket != null) {
                localIpPort = ThriftUtils.getHostPort(socket.getLocalSocketAddress());
                remoteAddress = ThriftUtils.getHost(socket.getRemoteSocketAddress());
            }
        } else {
            if (isDebug) {
                logger.debug("Invalid target object. Need field accessor({}).",
                        SocketFieldAccessor.class.getName());
            }
        }
    }
    if (localIpPort != ThriftConstants.UNKNOWN_ADDRESS) {
        recorder.recordEndPoint(localIpPort);
    }
    if (remoteAddress != ThriftConstants.UNKNOWN_ADDRESS) {
        recorder.recordRemoteAddress(remoteAddress);
    }
}

From source file:com.baidu.oped.apm.thrift.io.TReplaceListProtocol.java

License:Apache License

public TReplaceListProtocol(final TProtocol protocol) {
    super(protocol.getTransport());
    this.protocol = protocol;
}

From source file:com.cloudera.llama.server.AuthzTProcessor.java

License:Apache License

@Override
public boolean process(TProtocol inProt, TProtocol outProt) throws TException {
    TSaslServerTransport saslServerTransport = (TSaslServerTransport) inProt.getTransport();
    String principal = saslServerTransport.getSaslServer().getAuthorizationID();
    try {//from  w w  w  . ja v  a2s  .com
        if (isAuthorized(principal)) {
            LOG.debug("Authorization for '{}' as '{}' user, OK", principal, userType);
        } else {
            LOG.warn("Authorization for '{}' as '{}' user, FAILED", principal, userType);
            throw new TException("Unauthorized");
        }
        return tProcessor.process(inProt, outProt);
    } catch (IOException ex) {
        LOG.error("Could not verify authorization, {}", ex.toString(), ex);
        throw new TException(ex);
    }
}

From source file:com.cloudera.llama.server.ClientPrincipalTProcessor.java

License:Apache License

@Override
public boolean process(TProtocol inProt, TProtocol outProt) throws TException {
    TSaslServerTransport saslServerTransport = (TSaslServerTransport) inProt.getTransport();
    String principal = saslServerTransport.getSaslServer().getAuthorizationID();
    try {/*from   ww w . java2  s .  co  m*/
        PRINCIPAL_TL.set(principal);
        return tProcessor.process(inProt, outProt);
    } finally {
        PRINCIPAL_TL.remove();
    }
}

From source file:com.cloudera.llama.server.TestClientPrincipalTProcessor.java

License:Apache License

@Test
public void testSetGetPrincipal() throws Exception {
    invoked = false;/*from   w  w w  . j av a 2 s.  c  om*/
    user = "foo";
    SaslServer saslServer = Mockito.mock(SaslServer.class);
    Mockito.when(saslServer.getAuthorizationID()).thenReturn("foo");
    MyTSaslServerTransport tst = Mockito.mock(MyTSaslServerTransport.class);
    Mockito.when(tst.getSaslServer()).thenReturn(saslServer);
    Mockito.when(tst.getSaslServer()).thenReturn(saslServer);
    TProtocol p = Mockito.mock(TProtocol.class);
    Mockito.when(p.getTransport()).thenReturn(tst);

    TProcessor cpTp = new ClientPrincipalTProcessor(this);

    Assert.assertNull(ClientPrincipalTProcessor.getPrincipal());
    cpTp.process(p, p);
    Assert.assertTrue(invoked);
    Assert.assertNull(ClientPrincipalTProcessor.getPrincipal());
}

From source file:com.dell.doradus.service.db.thrift.DBConn.java

License:Apache License

/**
 * Close the database connection. This object can be reused after the connection has
 * been closed by calling {@link #open()} again.
 *///  w  ww . java 2  s .  com
public void close() {
    // Get the connection's protocol (TBinaryProtocol), and the protocol's transport
    // (TSocket) and close it.
    if (m_client != null) {
        TProtocol protocol = m_client.getInputProtocol();
        if (protocol != null) {
            TTransport transport = protocol.getTransport();
            if (transport != null) {
                transport.close();
            }
        }
    }
    m_client = null;
    m_bFailed = true; // Prevent reusing this connection until reconnected
    m_bDBOpen = false;
}