List of usage examples for io.netty.util.concurrent Future getNow
V getNow();
From source file:org.redisson.command.CommandSyncService.java
License:Apache License
<R> R sync(boolean readOnlyMode, Codec codec, NodeSource source, SyncOperation<R> operation, int attempt) { if (!connectionManager.getShutdownLatch().acquire()) { throw new IllegalStateException("Redisson is shutdown"); }//www . j a v a2s . c o m try { Future<RedisConnection> connectionFuture; if (readOnlyMode) { connectionFuture = connectionManager.connectionReadOp(source, null); } else { connectionFuture = connectionManager.connectionWriteOp(source, null); } connectionFuture.syncUninterruptibly(); RedisConnection connection = connectionFuture.getNow(); try { return operation.execute(codec, connection); } catch (RedisMovedException e) { return sync(readOnlyMode, codec, new NodeSource(e.getSlot(), e.getAddr(), Redirect.MOVED), operation, attempt); } catch (RedisAskException e) { return sync(readOnlyMode, codec, new NodeSource(e.getSlot(), e.getAddr(), Redirect.ASK), operation, attempt); } catch (RedisLoadingException e) { return sync(readOnlyMode, codec, source, operation, attempt); } catch (RedisTimeoutException e) { if (attempt == connectionManager.getConfig().getRetryAttempts()) { throw e; } attempt++; return sync(readOnlyMode, codec, source, operation, attempt); } finally { connectionManager.getShutdownLatch().release(); if (readOnlyMode) { connectionManager.releaseRead(source, connection); } else { connectionManager.releaseWrite(source, connection); } } } catch (RedisException e) { if (attempt == connectionManager.getConfig().getRetryAttempts()) { throw e; } try { Thread.sleep(connectionManager.getConfig().getRetryInterval()); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } attempt++; return sync(readOnlyMode, codec, source, operation, attempt); } }
From source file:org.redisson.CommandExecutorService.java
License:Apache License
private <R, T> void retryReadRandomAsync(final RedisCommand<T> command, final Promise<R> mainPromise, final List<Integer> slots, final Object... params) { final Promise<R> attemptPromise = connectionManager.newPromise(); attemptPromise.addListener(new FutureListener<R>() { @Override/* w ww. j a v a2 s. co m*/ public void operationComplete(Future<R> future) throws Exception { if (future.isSuccess()) { if (future.getNow() == null) { if (slots.isEmpty()) { mainPromise.setSuccess(null); } else { retryReadRandomAsync(command, mainPromise, slots, params); } } else { mainPromise.setSuccess(future.getNow()); } } else { mainPromise.setFailure(future.cause()); } } }); Integer slot = slots.remove(0); async(true, slot, null, connectionManager.getCodec(), command, params, attemptPromise, 0); }
From source file:org.redisson.CommandExecutorService.java
License:Apache License
public <V> V get(Future<V> future) { future.awaitUninterruptibly();/*from w ww.java2 s .c om*/ if (future.isSuccess()) { return future.getNow(); } throw future.cause() instanceof RedisException ? (RedisException) future.cause() : new RedisException("Unexpected exception while processing command", future.cause()); }
From source file:org.redisson.connection.ClientConnectionsEntry.java
License:Apache License
private <T extends RedisConnection> void addFireEventListener(T conn, RPromise<T> connectionFuture) { connectionManager.getConnectListener().onConnect(connectionFuture, conn, nodeType, connectionManager.getConfig()); if (connectionFuture.isSuccess()) { connectionManager.getConnectionEventsHub() .fireConnect(connectionFuture.getNow().getRedisClient().getAddr()); return;/*from w w w .j a v a 2s. com*/ } connectionFuture.addListener(new FutureListener<T>() { @Override public void operationComplete(Future<T> future) throws Exception { if (future.isSuccess()) { connectionManager.getConnectionEventsHub() .fireConnect(future.getNow().getRedisClient().getAddr()); } } }); }
From source file:org.redisson.connection.ConnectionEntry.java
License:Apache License
public Future<RedisConnection> connect(final MasterSlaveServersConfig config) { final Promise<RedisConnection> connectionFuture = client.getBootstrap().group().next().newPromise(); Future<RedisConnection> future = client.connectAsync(); future.addListener(new FutureListener<RedisConnection>() { @Override/*from w ww. ja v a 2s.c o m*/ public void operationComplete(Future<RedisConnection> future) throws Exception { if (!future.isSuccess()) { connectionFuture.tryFailure(future.cause()); return; } RedisConnection conn = future.getNow(); log.debug("new connection created: {}", conn); FutureConnectionListener<RedisConnection> listener = new FutureConnectionListener<RedisConnection>( connectionFuture, conn); connectListener.onConnect(config, nodeType, listener); listener.executeCommands(); addReconnectListener(config, conn); } }); return connectionFuture; }
From source file:org.redisson.connection.ConnectionEntry.java
License:Apache License
public Future<RedisPubSubConnection> connectPubSub(final MasterSlaveServersConfig config) { final Promise<RedisPubSubConnection> connectionFuture = client.getBootstrap().group().next().newPromise(); Future<RedisPubSubConnection> future = client.connectPubSubAsync(); future.addListener(new FutureListener<RedisPubSubConnection>() { @Override// w ww .j a va 2s . c o m public void operationComplete(Future<RedisPubSubConnection> future) throws Exception { if (!future.isSuccess()) { connectionFuture.tryFailure(future.cause()); return; } RedisPubSubConnection conn = future.getNow(); log.debug("new pubsub connection created: {}", conn); FutureConnectionListener<RedisPubSubConnection> listener = new FutureConnectionListener<RedisPubSubConnection>( connectionFuture, conn); connectListener.onConnect(config, nodeType, listener); listener.executeCommands(); addReconnectListener(config, conn); } }); return connectionFuture; }
From source file:org.redisson.connection.DNSMonitor.java
License:Apache License
public DNSMonitor(ConnectionManager connectionManager, RedisClient masterHost, Collection<RedisURI> slaveHosts, long dnsMonitoringInterval, AddressResolverGroup<InetSocketAddress> resolverGroup) { this.resolver = resolverGroup.getResolver(connectionManager.getGroup().next()); masterHost.resolveAddr().syncUninterruptibly(); masters.put(masterHost.getConfig().getAddress(), masterHost.getAddr()); for (RedisURI host : slaveHosts) { Future<InetSocketAddress> resolveFuture = resolver .resolve(InetSocketAddress.createUnresolved(host.getHost(), host.getPort())); resolveFuture.syncUninterruptibly(); slaves.put(host, resolveFuture.getNow()); }//www . j a va2 s . c om this.connectionManager = connectionManager; this.dnsMonitoringInterval = dnsMonitoringInterval; }
From source file:org.redisson.connection.DNSMonitor.java
License:Apache License
private void monitorMasters(AtomicInteger counter) { for (Entry<RedisURI, InetSocketAddress> entry : masters.entrySet()) { Future<InetSocketAddress> resolveFuture = resolver.resolve( InetSocketAddress.createUnresolved(entry.getKey().getHost(), entry.getKey().getPort())); resolveFuture.addListener(new FutureListener<InetSocketAddress>() { @Override/*from ww w .j a va 2 s . c o m*/ public void operationComplete(Future<InetSocketAddress> future) throws Exception { if (counter.decrementAndGet() == 0) { monitorDnsChange(); } if (!future.isSuccess()) { log.error("Unable to resolve " + entry.getKey().getHost(), future.cause()); return; } InetSocketAddress currentMasterAddr = entry.getValue(); InetSocketAddress newMasterAddr = future.getNow(); if (!newMasterAddr.getAddress().equals(currentMasterAddr.getAddress())) { log.info("Detected DNS change. Master {} has changed ip from {} to {}", entry.getKey(), currentMasterAddr.getAddress().getHostAddress(), newMasterAddr.getAddress().getHostAddress()); MasterSlaveEntry masterSlaveEntry = connectionManager.getEntry(currentMasterAddr); if (masterSlaveEntry == null) { if (connectionManager instanceof SingleConnectionManager) { log.error( "Unable to find master entry for {}. Multiple IP bindings for single hostname supported only in Redisson PRO!", currentMasterAddr); } else { log.error("Unable to find master entry for {}", currentMasterAddr); } return; } masterSlaveEntry.changeMaster(newMasterAddr, entry.getKey()); masters.put(entry.getKey(), newMasterAddr); } } }); } }
From source file:org.redisson.connection.DNSMonitor.java
License:Apache License
private void monitorSlaves(AtomicInteger counter) { for (Entry<RedisURI, InetSocketAddress> entry : slaves.entrySet()) { Future<InetSocketAddress> resolveFuture = resolver.resolve( InetSocketAddress.createUnresolved(entry.getKey().getHost(), entry.getKey().getPort())); resolveFuture.addListener(new FutureListener<InetSocketAddress>() { @Override//from ww w .j a va2s . c o m public void operationComplete(Future<InetSocketAddress> future) throws Exception { if (counter.decrementAndGet() == 0) { monitorDnsChange(); } if (!future.isSuccess()) { log.error("Unable to resolve " + entry.getKey().getHost(), future.cause()); return; } InetSocketAddress currentSlaveAddr = entry.getValue(); InetSocketAddress newSlaveAddr = future.getNow(); if (!newSlaveAddr.getAddress().equals(currentSlaveAddr.getAddress())) { log.info("Detected DNS change. Slave {} has changed ip from {} to {}", entry.getKey().getHost(), currentSlaveAddr.getAddress().getHostAddress(), newSlaveAddr.getAddress().getHostAddress()); for (MasterSlaveEntry masterSlaveEntry : connectionManager.getEntrySet()) { if (!masterSlaveEntry.hasSlave(currentSlaveAddr)) { continue; } if (masterSlaveEntry.hasSlave(newSlaveAddr)) { masterSlaveEntry.slaveUp(newSlaveAddr, FreezeReason.MANAGER); masterSlaveEntry.slaveDown(currentSlaveAddr, FreezeReason.MANAGER); } else { RFuture<Void> addFuture = masterSlaveEntry.addSlave(newSlaveAddr, entry.getKey()); addFuture.onComplete((res, e) -> { if (e != null) { log.error("Can't add slave: " + newSlaveAddr, e); return; } masterSlaveEntry.slaveDown(currentSlaveAddr, FreezeReason.MANAGER); }); } break; } slaves.put(entry.getKey(), newSlaveAddr); } } }); } }
From source file:org.redisson.connection.MasterSlaveConnectionManager.java
License:Apache License
private <V, T> void writeAllAsync(final int slot, final AsyncOperation<V, T> asyncOperation, final AtomicInteger counter, final Promise<T> mainPromise, final int attempt) { final Promise<T> promise = getGroup().next().newPromise(); final AtomicReference<RedisException> ex = new AtomicReference<RedisException>(); TimerTask timerTask = new TimerTask() { @Override// w ww . j a va 2 s .co m public void run(Timeout timeout) throws Exception { if (promise.isDone()) { return; } if (attempt == config.getRetryAttempts()) { promise.setFailure(ex.get()); return; } promise.cancel(true); int count = attempt + 1; writeAllAsync(slot, asyncOperation, counter, mainPromise, count); } }; try { RedisConnection<Object, V> connection = connectionWriteOp(slot); RedisAsyncConnection<Object, V> async = connection.getAsync(); asyncOperation.execute(promise, async); ex.set(new RedisTimeoutException()); Timeout timeout = timer.newTimeout(timerTask, config.getTimeout(), TimeUnit.MILLISECONDS); promise.addListener(createReleaseWriteListener(slot, connection, timeout)); } catch (RedisConnectionException e) { ex.set(e); timer.newTimeout(timerTask, config.getRetryInterval(), TimeUnit.MILLISECONDS); } promise.addListener(new FutureListener<T>() { @Override public void operationComplete(Future<T> future) throws Exception { if (future.isCancelled()) { return; } if (future.cause() instanceof RedisMovedException) { RedisMovedException ex = (RedisMovedException) future.cause(); writeAllAsync(ex.getSlot(), asyncOperation, counter, mainPromise, attempt); return; } if (future.isSuccess()) { if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) { mainPromise.setSuccess(future.getNow()); } } else { mainPromise.setFailure(future.cause()); } } }); }