Example usage for org.apache.thrift.transport TTransportException NOT_OPEN

List of usage examples for org.apache.thrift.transport TTransportException NOT_OPEN

Introduction

In this page you can find the example usage for org.apache.thrift.transport TTransportException NOT_OPEN.

Prototype

int NOT_OPEN

To view the source code for org.apache.thrift.transport TTransportException NOT_OPEN.

Click Source Link

Usage

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

License:Apache License

@Override
public int read(byte[] buf, int off, int len) throws TTransportException {
    throw new TTransportException(TTransportException.NOT_OPEN, "unsupported inputStream");
}

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

License:Apache License

@Override
public void write(byte[] buf, int off, int len) throws TTransportException {
    if (out == null) {
        throw new TTransportException(TTransportException.NOT_OPEN, "cannot write to null outputStream");
    }/*w w w  .  j  a  v a 2 s. c  o m*/

    out.write(buf, off, len);
}

From source file:com.facebook.hive.metastore.client.RetryingHiveMetastore.java

License:Apache License

@VisibleForTesting
@SuppressWarnings("PMD.PreserveStackTrace")
HiveMetastore connect() throws TException {
    if (closed.get()) {
        throw new TTransportException(TTransportException.NOT_OPEN, "Client is already closed");
    }/*from w ww  . ja  va  2 s.  co  m*/

    HiveMetastore client = clientHolder.get();

    while (client == null) {
        try {
            final HostAndPort hostAndPort = getNextHostAndPort();
            final NiftyClientConnector<? extends NiftyClientChannel> clientConnector = config.isFramed()
                    ? new FramedClientConnector(hostAndPort)
                    : new UnframedClientConnector(hostAndPort);

            client = thriftClient.open(clientConnector).get();
            if (!clientHolder.compareAndSet(null, client)) {
                client.close();
                client = clientHolder.get();
            }
        } catch (final ExecutionException e) {
            final Throwable t = e.getCause();
            Throwables.propagateIfInstanceOf(t, TTransportException.class);
            throw Throwables.propagate(t);
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new TTransportException(TTransportException.NOT_OPEN, "Interrupted while connecting");
        }
    }

    return client;
}

From source file:com.facebook.hive.metastore.client.RetryingHiveMetastore.java

License:Apache License

@SuppressWarnings("PMD.PreserveStackTrace")
private <T> T withRetries(final String apiName, final CallableWithMetastore<T> callable) throws TException {
    checkNotNull(apiName, "apiName is null");
    checkNotNull(callable, "callable is null");

    final long startTime = System.nanoTime();

    int attempt = 0;

    try {//from   w ww.  j av  a2 s.c  o  m
        for (;;) {
            attempt++;
            try {
                final HiveMetastore client = connect();
                log.debug("Executing %s (connected to %s, attempt %s)", apiName, currentHostAndPort.get(),
                        attempt);
                return callable.call(client);
            } catch (final Throwable t) {
                TTransportException te = null;

                if (t instanceof TTransportException) {
                    te = (TTransportException) t;
                } else if (t.getCause() instanceof TTransportException) {
                    te = (TTransportException) t.getCause();
                    log.debug("Found a TTransportException (%s) wrapped in a %s", te.getMessage(),
                            t.getClass().getSimpleName());
                }

                if (te != null) {
                    final Duration now = Duration.nanosSince(startTime);
                    if (attempt > config.getMaxRetries() || now.compareTo(config.getRetryTimeout()) >= 0) {
                        log.warn(
                                "Failed executing %s (last host %s, attempt %s, elapsed time %s), Exception: %s (%s)",
                                apiName, currentHostAndPort.get(), attempt, now.toString(TimeUnit.MILLISECONDS),
                                te.getClass().getSimpleName(), te.getMessage());

                        Throwables.propagateIfInstanceOf(t, TException.class);
                        throw Throwables.propagate(t);
                    }
                    log.debug(
                            "Retry executing %s (last host: %s, attempt %s, elapsed time %s), Exception: %s (%s)",
                            apiName, currentHostAndPort.get(), attempt, now.toString(TimeUnit.MILLISECONDS),
                            te.getClass().getSimpleName(), te.getMessage());

                    internalClose();

                    TimeUnit.MILLISECONDS.sleep(config.getRetrySleep().toMillis());
                } else {
                    log.warn("Failed executing %s, Exception: %s (%s)", apiName, t.getClass().getSimpleName(),
                            t.getMessage());
                    Throwables.propagateIfInstanceOf(t, TException.class);
                    throw Throwables.propagate(t);
                }
            }
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new TTransportException(TTransportException.NOT_OPEN, "Interrupted while connecting");
    }
}

From source file:com.netflix.suro.input.thrift.CustomServerSocket.java

License:Apache License

protected TNonblockingSocket acceptImpl() throws TTransportException {
    if (serverSocket_ == null) {
        throw new TTransportException(TTransportException.NOT_OPEN, "No underlying server socket.");
    }/*from  w w  w. j a  va 2s.  com*/
    try {
        SocketChannel socketChannel = serverSocketChannel.accept();
        if (socketChannel == null) {
            return null;
        }

        TNonblockingSocket tsocket = new TNonblockingSocket(socketChannel);
        tsocket.setTimeout(0); // disabling client timeout
        tsocket.getSocketChannel().socket().setKeepAlive(true);
        tsocket.getSocketChannel().socket().setSendBufferSize(config.getSocketSendBufferBytes());
        tsocket.getSocketChannel().socket().setReceiveBufferSize(config.getSocketRecvBufferBytes());
        return tsocket;
    } catch (IOException iox) {
        throw new TTransportException(iox);
    }
}

From source file:com.si.jupiter.smart.client.channel.AbstractClientChannel.java

License:Apache License

@Override
public void sendAsynchronousRequest(final ByteBuf message, final boolean oneway, final Listener listener)
        throws TException {
    final int sequenceId = extractSequenceId(message);

    // Ensure channel listeners are always called on the channel's I/O thread
    executeInIoThread(new Runnable() {
        @Override/*from  w ww.j a  va 2  s .  c  o m*/
        public void run() {
            try {
                final Request request = makeRequest(sequenceId, listener);

                if (!nettyChannel.isActive()) {
                    fireChannelErrorCallback(listener,
                            new TTransportException(TTransportException.NOT_OPEN, "Channel closed"));
                    return;
                }

                if (hasError()) {
                    fireChannelErrorCallback(listener, new TTransportException(TTransportException.UNKNOWN,
                            "Channel is in a bad state due to failing a previous request"));
                    return;
                }

                ChannelFuture sendFuture = writeRequest(message);
                //queueSendTimeout(request);

                sendFuture.addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        messageSent(future, request, oneway);
                    }
                });
            } catch (Throwable t) {
                // onError calls all registered listeners in the requestMap, but this request
                // may not be registered yet. So we try to remove it (to make sure we don't call
                // the callback twice) and then manually make the callback for this request
                // listener.
                requestMap.remove(sequenceId);
                fireChannelErrorCallback(listener, t);

                onError(t);
            }
        }
    });
}

From source file:com.tna.cep.service.thrift.flume.TSaneServerSocket.java

License:Apache License

protected TTransport acceptImpl() throws TTransportException {
    if (serverSocket_ == null) {
        throw new TTransportException(TTransportException.NOT_OPEN,
                "No underlying com.tna.cep.service.thrift.flume.server socket.");
    }//  w ww .  j  av a  2 s.co m
    try {
        Socket result = serverSocket_.accept();
        TSocket result2 = new TBufferedSocket(result);
        result2.setTimeout(clientTimeout_);
        return result2;
    } catch (IOException iox) {
        throw new TTransportException(iox);
    }
}

From source file:com.uber.jaeger.reporters.protocols.ThriftUdpTransport.java

License:Open Source License

@Override
public int read(byte[] bytes, int offset, int len) throws TTransportException {
    if (!this.isOpen()) {
        throw new TTransportException(TTransportException.NOT_OPEN);
    }//from   ww w  .  j a v  a2s.c om
    if (this.receiveOffSet == -1) {
        this.receiveBuf = new byte[MAX_PACKET_SIZE];
        DatagramPacket dg = new DatagramPacket(this.receiveBuf, MAX_PACKET_SIZE);
        try {
            this.socket.receive(dg);
        } catch (IOException e) {
            throw new TTransportException(TTransportException.UNKNOWN, "ERROR from underlying socket", e);
        }
        this.receiveOffSet = 0;
        this.receiveLength = dg.getLength();
    }
    int curDataSize = this.receiveLength - this.receiveOffSet;
    if (curDataSize <= len) {
        System.arraycopy(this.receiveBuf, this.receiveOffSet, bytes, offset, curDataSize);
        this.receiveOffSet = -1;
        return curDataSize;
    } else {
        System.arraycopy(this.receiveBuf, this.receiveOffSet, bytes, offset, len);
        this.receiveOffSet += len;
        return len;
    }
}

From source file:com.uber.jaeger.reporters.protocols.ThriftUdpTransport.java

License:Open Source License

@Override
public void write(byte[] bytes, int offset, int len) throws TTransportException {
    if (!this.isOpen()) {
        throw new TTransportException(TTransportException.NOT_OPEN);
    }/* w  w  w. j  a  v  a  2 s. co m*/
    if (this.writeBuffer == null) {
        this.writeBuffer = ByteBuffer.allocate(MAX_PACKET_SIZE);
    }
    if (this.writeBuffer.position() + len > MAX_PACKET_SIZE) {
        throw new TTransportException(TTransportException.UNKNOWN,
                "Message size too large: " + len + " > " + MAX_PACKET_SIZE);
    }
    this.writeBuffer.put(bytes, offset, len);
}

From source file:nu.ted.thrift.TedServerSocket.java

License:Apache License

protected TSocket acceptImpl() throws TTransportException {
    if (serverSocket_ == null) {
        throw new TTransportException(TTransportException.NOT_OPEN, "No underlying server socket.");
    }//from   w  w w. j a  v a 2s.c om
    try {
        Socket result = serverSocket_.accept();
        TSocket result2 = createTSocket(result);
        result2.setTimeout(clientTimeout_);
        return result2;
    } catch (IOException iox) {
        throw new TTransportException(iox);
    }
}