public synchronized void connect() throws ConnectException { if (isConnected()) return; int retries = 0; InetSocketAddress address = key.addr; if (address.isUnresolved()) { address = resolveAddress(address); } /* do not call await() inside handler */ ChannelFuture f = doConnect(address).awaitUninterruptibly(); if (!f.isSuccess()) { if (maxRetryNum > 0) { doReconnect(address, f, ++retries); } else { throw makeConnectException(address, f); } } }
private void doReconnect(final InetSocketAddress address, ChannelFuture future, int retries) throws ConnectException { for (; ; ) { if (maxRetryNum > retries) { retries++; if(getChannel().eventLoop().isShuttingDown()) { LOG.warn("RPC is shutting down"); return; } LOG.warn(getErrorMessage(ExceptionUtils.getMessage(future.cause())) + "\nTry to reconnect : " + getKey().addr); try { Thread.sleep(RpcConstants.DEFAULT_PAUSE); } catch (InterruptedException e) { } this.channelFuture = doConnect(address).awaitUninterruptibly(); if (this.channelFuture.isDone() && this.channelFuture.isSuccess()) { break; } } else { LOG.error("Max retry count has been exceeded. attempts=" + retries + " caused by: " + future.cause()); throw makeConnectException(address, future); } } }
/** * * @param key RpcConnectionKey * @param <T> Rpc Protocol Class * @return Rpc Client Class * @throws NoSuchMethodException * @throws ClassNotFoundException * @throws ConnectException */ public synchronized <T extends NettyClientBase> T newClient(RpcConnectionKey key, Properties connectionParameters) throws NoSuchMethodException, ClassNotFoundException, ConnectException { T client = makeClient(key, connectionParameters); client.connect(); assert client.isConnected(); return client; }
@Override BlockingInterface getStub() throws ServiceException { return getCatalogConnection().getStub(); }
if (!client.isConnected()) { client.subscribeEvent(target.getKey(), new ChannelEventListener() { @Override public void channelRegistered(ChannelHandlerContext ctx) { client.connect(); assert client.isConnected(); return (T) client;
/** * Connect a {@link NettyClientBase} to the remote {@link NettyServerBase}, and returns rpc client by protocol. * This client will be shared per protocol and address. Client is removed in shared map when a client is closed * @param addr * @param protocolClass * @param asyncMode * @return * @throws NoSuchMethodException * @throws ClassNotFoundException * @throws ConnectTimeoutException */ public NettyClientBase getClient(InetSocketAddress addr, Class<?> protocolClass, boolean asyncMode) throws NoSuchMethodException, ClassNotFoundException, ConnectTimeoutException { RpcConnectionKey key = new RpcConnectionKey(addr, protocolClass, asyncMode); NettyClientBase client; synchronized (clients) { client = clients.get(key); if (client == null) { clients.put(key, client = makeClient(key)); } } if (!client.isConnected()) { client.connect(); client.getChannel().closeFuture().addListener(new ConnectionCloseFutureListener(key)); } assert client.isConnected(); return client; }
public SocketAddress getRemoteAddress() { Channel channel = getChannel(); return channel == null ? null : channel.remoteAddress(); }
public boolean isConnected() { if (!closed.get()) { try { return getTajoMasterConnection().isConnected(); } catch (Throwable e) { return false; } } return false; }
@Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { getHandler().registerCallback(rpcRequest.getId(), callback); } else { if (!future.channel().isActive() && retry < maxRetryNum) { /* schedule the current request for the retry */ LOG.warn(future.cause() + " Try to reconnect :" + getKey().addr); final EventLoop loop = future.channel().eventLoop(); loop.schedule(new Runnable() { @Override public void run() { doConnect(getKey().addr).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { invoke(rpcRequest, callback, retry + 1); } }); } }, RpcConstants.DEFAULT_PAUSE, TimeUnit.MILLISECONDS); } else { /* Max retry count has been exceeded or internal failure */ getHandler().registerCallback(rpcRequest.getId(), callback); getHandler().exceptionCaught(getChannel().pipeline().lastContext(), new RecoverableException(rpcRequest.getId(), future.cause())); } } } });
/** * Request to close this clients * After it is closed, it is removed from clients map. */ public static void close() { LOG.info("Closing RPC client manager"); for (NettyClientBase eachClient : clients.values()) { try { eachClient.close(); } catch (Exception e) { LOG.error(e.getMessage(), e); } } }
private void doReconnect(final InetSocketAddress address, ChannelFuture future, AtomicInteger retries) throws ConnectTimeoutException { for (; ; ) { if (numRetries >= retries.getAndIncrement()) { LOG.warn(future.cause().getMessage() + " Try to reconnect"); try { Thread.sleep(PAUSE); } catch (InterruptedException e) { } this.channelFuture = doConnect(address).awaitUninterruptibly(); if (this.channelFuture.isDone() && this.channelFuture.isSuccess()) { break; } } else { throw new ConnectTimeoutException("Max retry count has been exceeded. attempts=" + numRetries + " caused by: " + future.cause()); } } }
@Override BlockingInterface getStub() throws ServiceException { return getCatalogConnection().getStub(); }
if (!client.isConnected()) { client.subscribeEvent(target.getKey(), new ChannelEventListener() { @Override public void channelRegistered(ChannelHandlerContext ctx) { client.connect(); assert client.isConnected(); return (T) client;
/** * * @param key RpcConnectionKey * @param <T> Rpc Protocol Class * @return Rpc Client Class * @throws NoSuchMethodException * @throws ClassNotFoundException * @throws ConnectException */ public synchronized <T extends NettyClientBase> T newClient(RpcConnectionKey key, Properties connectionParameters) throws NoSuchMethodException, ClassNotFoundException, ConnectException { T client = makeClient(key, connectionParameters); client.connect(); assert client.isConnected(); return client; }
public SocketAddress getRemoteAddress() { Channel channel = getChannel(); return channel == null ? null : channel.remoteAddress(); }
public boolean isConnected() { if (!closed.get()) { try { return getTajoMasterConnection().isConnected(); } catch (Throwable e) { return false; } } return false; }
@Override public void operationComplete(final ChannelFuture future) throws Exception { if (future.isSuccess()) { getHandler().registerCallback(rpcRequest.getId(), callback); } else { if (!future.channel().isActive() && retry < maxRetryNum) { /* schedule the current request for the retry */ LOG.warn(future.cause() + " Try to reconnect :" + getKey().addr); final EventLoop loop = future.channel().eventLoop(); loop.schedule(new Runnable() { @Override public void run() { doConnect(getKey().addr).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { invoke(rpcRequest, callback, retry + 1); } }); } }, RpcConstants.DEFAULT_PAUSE, TimeUnit.MILLISECONDS); } else { /* Max retry count has been exceeded or internal failure */ getHandler().registerCallback(rpcRequest.getId(), callback); getHandler().exceptionCaught(getChannel().pipeline().lastContext(), new RecoverableException(rpcRequest.getId(), future.cause())); } } } });