use of org.redisson.api.RFuture in project redisson by redisson.
the class RedissonBatchTest method testOrdering.
@Test
public void testOrdering() throws InterruptedException {
ExecutorService e = Executors.newFixedThreadPool(16);
final RBatch batch = redisson.createBatch();
final AtomicLong index = new AtomicLong(-1);
final List<RFuture<Long>> futures = new CopyOnWriteArrayList<>();
for (int i = 0; i < 500; i++) {
futures.add(null);
}
for (int i = 0; i < 500; i++) {
final int j = i;
e.execute(new Runnable() {
@Override
public void run() {
synchronized (RedissonBatchTest.this) {
int i = (int) index.incrementAndGet();
int ind = j % 3;
RFuture<Long> f1 = batch.getAtomicLong("test" + ind).addAndGetAsync(j);
futures.set(i, f1);
}
}
});
}
e.shutdown();
Assert.assertTrue(e.awaitTermination(30, TimeUnit.SECONDS));
List<?> s = batch.execute();
int i = 0;
for (Object element : s) {
RFuture<Long> a = futures.get(i);
Assert.assertEquals(a.getNow(), element);
i++;
}
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class RedissonExecutorService method poll.
private <T> Future<T> poll(List<Future<T>> futures, long timeout, TimeUnit timeUnit) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Future<T>> result = new AtomicReference<Future<T>>();
FutureListener<T> listener = new FutureListener<T>() {
@Override
public void operationComplete(io.netty.util.concurrent.Future<T> future) throws Exception {
latch.countDown();
result.compareAndSet(null, future);
}
};
for (Future<T> future : futures) {
RFuture<T> f = (RFuture<T>) future;
f.addListener(listener);
}
if (timeout == -1) {
latch.await();
} else {
latch.await(timeout, timeUnit);
}
for (Future<T> future : futures) {
RFuture<T> f = (RFuture<T>) future;
f.removeListener(listener);
}
return result.get();
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class RedissonKeys method deleteAsync.
@Override
public RFuture<Long> deleteAsync(String... keys) {
if (!commandExecutor.getConnectionManager().isClusterMode()) {
return commandExecutor.writeAsync(null, RedisCommands.DEL, keys);
}
Map<MasterSlaveEntry, List<String>> range2key = new HashMap<MasterSlaveEntry, List<String>>();
for (String key : keys) {
int slot = commandExecutor.getConnectionManager().calcSlot(key);
for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
List<String> list = range2key.get(entry);
if (list == null) {
list = new ArrayList<String>();
range2key.put(entry, list);
}
list.add(key);
}
}
final RPromise<Long> result = commandExecutor.getConnectionManager().newPromise();
final AtomicReference<Throwable> failed = new AtomicReference<Throwable>();
final AtomicLong count = new AtomicLong();
final AtomicLong executed = new AtomicLong(range2key.size());
FutureListener<List<?>> listener = new FutureListener<List<?>>() {
@Override
public void operationComplete(Future<List<?>> future) throws Exception {
if (future.isSuccess()) {
List<Long> result = (List<Long>) future.get();
for (Long res : result) {
count.addAndGet(res);
}
} else {
failed.set(future.cause());
}
checkExecution(result, failed, count, executed);
}
};
for (Entry<MasterSlaveEntry, List<String>> entry : range2key.entrySet()) {
// executes in batch due to CROSSLOT error
CommandBatchService executorService = new CommandBatchService(commandExecutor.getConnectionManager());
for (String key : entry.getValue()) {
executorService.writeAsync(entry.getKey(), null, RedisCommands.DEL, key);
}
RFuture<List<?>> future = executorService.executeAsync();
future.addListener(listener);
}
return result;
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class RedissonBlockingFairQueue method tryPollAsync.
private void tryPollAsync(final long startTime, final long timeout, final TimeUnit unit, final RFuture<RedissonLockEntry> subscribeFuture, final RPromise<V> promise) {
if (promise.isDone()) {
unsubscribe(subscribeFuture);
return;
}
long spentTime = System.currentTimeMillis() - startTime;
long remainTime = unit.toMillis(timeout) - spentTime;
if (remainTime <= 0) {
unsubscribe(subscribeFuture);
promise.trySuccess(null);
return;
}
RFuture<Long> tryAcquireFuture = tryAcquireAsync();
tryAcquireFuture.addListener(new FutureListener<Long>() {
@Override
public void operationComplete(Future<Long> future) throws Exception {
if (!future.isSuccess()) {
unsubscribe(subscribeFuture);
promise.tryFailure(future.cause());
return;
}
Long currentTimeout = future.getNow();
if (currentTimeout == null) {
long spentTime = System.currentTimeMillis() - startTime;
long remainTime = unit.toMillis(timeout) - spentTime;
if (remainTime > 0) {
final RFuture<V> pollFuture = RedissonBlockingFairQueue.super.pollAsync(remainTime, TimeUnit.MILLISECONDS);
pollFuture.addListener(new FutureListener<V>() {
@Override
public void operationComplete(Future<V> future) throws Exception {
unsubscribe(subscribeFuture);
if (!future.isSuccess()) {
promise.tryFailure(future.cause());
return;
}
promise.trySuccess(future.getNow());
}
});
} else {
unsubscribe(subscribeFuture);
promise.trySuccess(null);
}
} else {
final RedissonLockEntry entry = getEntry();
synchronized (entry) {
if (entry.getLatch().tryAcquire()) {
tryPollAsync(startTime, timeout, unit, subscribeFuture, promise);
} else {
final AtomicBoolean executed = new AtomicBoolean();
final AtomicReference<Timeout> futureRef = new AtomicReference<Timeout>();
final Runnable listener = new Runnable() {
@Override
public void run() {
executed.set(true);
if (futureRef.get() != null) {
futureRef.get().cancel();
}
tryPollAsync(startTime, timeout, unit, subscribeFuture, promise);
}
};
entry.addListener(listener);
if (!executed.get()) {
long spentTime = System.currentTimeMillis() - startTime;
long remainTime = unit.toMillis(timeout) - spentTime;
Timeout scheduledFuture = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override
public void run(Timeout t) throws Exception {
synchronized (entry) {
if (entry.removeListener(listener)) {
tryPollAsync(startTime, timeout, unit, subscribeFuture, promise);
}
}
}
}, remainTime, TimeUnit.MILLISECONDS);
futureRef.set(scheduledFuture);
}
}
}
}
}
;
});
}
use of org.redisson.api.RFuture in project redisson by redisson.
the class RedissonBaseAdder method resetAsync.
public RFuture<Void> resetAsync() {
String id = generateId();
RFuture<Long> future = topic.publishAsync(CLEAR_MSG + ":" + id);
RSemaphore semaphore = getSemaphore(id);
CompletionStage<Void> f = future.thenCompose(r -> semaphore.acquireAsync(r.intValue())).thenCompose(r -> semaphore.deleteAsync().thenApply(res -> null));
return new CompletableFutureWrapper<>(f);
}
Aggregations