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();
}
}
}
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;
}
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);
}
}
}
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;
}
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;
}
Aggregations