Search in sources :

Example 1 with NetworkException

use of org.corfudb.runtime.exceptions.NetworkException in project CorfuDB by CorfuDB.

the class ObjectsView method TXEnd.

/**
     * End a transaction on the current thread.
     *
     * @throws TransactionAbortedException If the transaction could not be executed successfully.
     *
     * @return The address of the transaction, if it commits.
     */
public long TXEnd() throws TransactionAbortedException {
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    if (context == null) {
        log.warn("Attempted to end a transaction, but no transaction active!");
        return AbstractTransactionalContext.UNCOMMITTED_ADDRESS;
    } else {
        // TODO remove this, doesn't belong here!
        long totalTime = System.currentTimeMillis() - context.getStartTime();
        log.trace("TXCommit[{}] time={} ms", context, totalTime);
        // TODO up to here
        try {
            return TransactionalContext.getCurrentContext().commitTransaction();
        } catch (TransactionAbortedException e) {
            TransactionalContext.getCurrentContext().abortTransaction(e);
            throw e;
        } catch (Exception e) {
            log.trace("TXCommit[{}] Exception {}", context, e);
            AbortCause abortCause;
            if (e instanceof NetworkException) {
                abortCause = AbortCause.NETWORK;
            } else {
                abortCause = AbortCause.UNDEFINED;
            }
            long snapshot_timestamp;
            try {
                snapshot_timestamp = context.getSnapshotTimestamp();
            } catch (NetworkException ne) {
                snapshot_timestamp = -1L;
            }
            TxResolutionInfo txInfo = new TxResolutionInfo(context.getTransactionID(), snapshot_timestamp);
            TransactionAbortedException tae = new TransactionAbortedException(txInfo, null, abortCause);
            context.abortTransaction(tae);
            throw tae;
        } finally {
            TransactionalContext.removeContext();
        }
    }
}
Also used : TxResolutionInfo(org.corfudb.protocols.wireprotocol.TxResolutionInfo) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) AbortCause(org.corfudb.runtime.exceptions.AbortCause) NetworkException(org.corfudb.runtime.exceptions.NetworkException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) NetworkException(org.corfudb.runtime.exceptions.NetworkException)

Example 2 with NetworkException

use of org.corfudb.runtime.exceptions.NetworkException in project CorfuDB by CorfuDB.

the class SealServersHelper method asyncSetRemoteEpoch.

/**
     * Asynchronously set remote epoch on all servers of layout.
     *
     * @param layout Layout to be sealed.
     * @return A map of completableFutures for every remoteSetEpoch call.
     */
public static Map<String, CompletableFuture<Boolean>> asyncSetRemoteEpoch(Layout layout) {
    Map<String, CompletableFuture<Boolean>> resultMap = new HashMap<>();
    // Seal layout servers
    layout.getAllServers().forEach(server -> {
        BaseClient baseClient = layout.getRuntime().getRouter(server).getClient(BaseClient.class);
        CompletableFuture<Boolean> cf = new CompletableFuture<>();
        try {
            cf = baseClient.setRemoteEpoch(layout.getEpoch());
        } catch (NetworkException ne) {
            cf.completeExceptionally(ne);
            log.error("Remote seal SET_EPOCH failed for server {} with {}", server, ne);
        }
        resultMap.put(server, cf);
    });
    return resultMap;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) BaseClient(org.corfudb.runtime.clients.BaseClient) NetworkException(org.corfudb.runtime.exceptions.NetworkException)

Example 3 with NetworkException

use of org.corfudb.runtime.exceptions.NetworkException in project CorfuDB by CorfuDB.

the class NettyClientRouter method start.

public void start(long c) {
    shutdown = false;
    if (workerGroup == null || workerGroup.isShutdown() || !channel.isOpen()) {
        workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

            final AtomicInteger threadNum = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("worker-" + threadNum.getAndIncrement());
                t.setDaemon(true);
                return t;
            }
        });
        ee = new DefaultEventExecutorGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {

            final AtomicInteger threadNum = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName(this.getClass().getName() + "event-" + threadNum.getAndIncrement());
                t.setDaemon(true);
                return t;
            }
        });
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.option(ChannelOption.SO_REUSEADDR, true);
        b.option(ChannelOption.TCP_NODELAY, true);
        NettyClientRouter router = this;
        b.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                if (tlsEnabled) {
                    ch.pipeline().addLast("ssl", sslContext.newHandler(ch.alloc()));
                }
                ch.pipeline().addLast(new LengthFieldPrepender(4));
                ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                if (saslPlainTextEnabled) {
                    PlainTextSaslNettyClient saslNettyClient = SaslUtils.enableSaslPlainText(saslPlainTextUsernameFile, saslPlainTextPasswordFile);
                    ch.pipeline().addLast("sasl/plain-text", saslNettyClient);
                }
                ch.pipeline().addLast(ee, new NettyCorfuMessageDecoder());
                ch.pipeline().addLast(ee, new NettyCorfuMessageEncoder());
                ch.pipeline().addLast(ee, router);
            }
        });
        try {
            connectChannel(b, c);
        } catch (Exception e) {
            try {
                // shutdown EventLoopGroup
                workerGroup.shutdownGracefully().sync();
            } catch (InterruptedException ie) {
            }
            throw new NetworkException(e.getClass().getSimpleName() + " connecting to endpoint failed", host + ":" + port, e);
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) NettyCorfuMessageEncoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageEncoder) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) NoSuchElementException(java.util.NoSuchElementException) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) NetworkException(org.corfudb.runtime.exceptions.NetworkException) PlainTextSaslNettyClient(org.corfudb.security.sasl.plaintext.PlainTextSaslNettyClient) NettyCorfuMessageDecoder(org.corfudb.protocols.wireprotocol.NettyCorfuMessageDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NetworkException(org.corfudb.runtime.exceptions.NetworkException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 4 with NetworkException

use of org.corfudb.runtime.exceptions.NetworkException in project CorfuDB by CorfuDB.

the class NettyClientRouter method connectChannel.

synchronized void connectChannel(Bootstrap b, long c) {
    boolean isEnabled = MetricsUtils.isMetricsCollectionEnabled();
    try (Timer.Context context = MetricsUtils.getConditionalContext(isEnabled, timerConnect)) {
        ChannelFuture cf = b.connect(host, port);
        cf.syncUninterruptibly();
        if (!cf.awaitUninterruptibly(timeoutConnect)) {
            // close port
            cf.channel().close();
            MetricsUtils.incConditionalCounter(isEnabled, counterConnectFailed, 1);
            throw new NetworkException(c + " Timeout connecting to endpoint", host + ":" + port);
        }
        channel = cf.channel();
    }
    channel.closeFuture().addListener((r) -> {
        connected = false;
        outstandingRequests.forEach((ReqID, reqCF) -> {
            MetricsUtils.incConditionalCounter(isEnabled, counterSendDisconnected, 1);
            reqCF.completeExceptionally(new NetworkException("Disconnected", host + ":" + port));
            outstandingRequests.remove(ReqID);
        });
        if (!shutdown) {
            log.trace("Disconnected, reconnecting...");
            while (!shutdown) {
                try {
                    connectChannel(b, c);
                    return;
                } catch (Exception ex) {
                    MetricsUtils.incConditionalCounter(isEnabled, counterConnectFailed, 1);
                    log.trace("Exception while reconnecting, retry in {} ms", timeoutRetry);
                    Thread.sleep(timeoutRetry);
                }
            }
        }
    });
    connected = true;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Timer(com.codahale.metrics.Timer) NetworkException(org.corfudb.runtime.exceptions.NetworkException) NoSuchElementException(java.util.NoSuchElementException) WrongEpochException(org.corfudb.runtime.exceptions.WrongEpochException) NetworkException(org.corfudb.runtime.exceptions.NetworkException)

Example 5 with NetworkException

use of org.corfudb.runtime.exceptions.NetworkException in project CorfuDB by CorfuDB.

the class CorfuCompileProxy method abortTransaction.

private void abortTransaction(Exception e) {
    long snapshot_timestamp;
    AbortCause abortCause;
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    if (e instanceof NetworkException) {
        // If a 'NetworkException' was received within a transactional context, an attempt to
        // 'getSnapshotTimestamp' will also fail (as it requests it to the Sequencer). A new NetworkException
        // would prevent the earliest to be propagated and encapsulated as a TransactionAbortedException.
        snapshot_timestamp = -1L;
        abortCause = AbortCause.NETWORK;
    } else {
        snapshot_timestamp = context.getSnapshotTimestamp();
        abortCause = AbortCause.UNDEFINED;
    }
    TxResolutionInfo txInfo = new TxResolutionInfo(context.getTransactionID(), snapshot_timestamp);
    TransactionAbortedException tae = new TransactionAbortedException(txInfo, null, abortCause);
    context.abortTransaction(tae);
    TransactionalContext.removeContext();
    throw tae;
}
Also used : TxResolutionInfo(org.corfudb.protocols.wireprotocol.TxResolutionInfo) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) AbortCause(org.corfudb.runtime.exceptions.AbortCause) NetworkException(org.corfudb.runtime.exceptions.NetworkException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException)

Aggregations

NetworkException (org.corfudb.runtime.exceptions.NetworkException)6 TransactionAbortedException (org.corfudb.runtime.exceptions.TransactionAbortedException)3 NoSuchElementException (java.util.NoSuchElementException)2 TxResolutionInfo (org.corfudb.protocols.wireprotocol.TxResolutionInfo)2 AbortCause (org.corfudb.runtime.exceptions.AbortCause)2 WrongEpochException (org.corfudb.runtime.exceptions.WrongEpochException)2 AbstractTransactionalContext (org.corfudb.runtime.object.transactions.AbstractTransactionalContext)2 Timer (com.codahale.metrics.Timer)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1