Search in sources :

Example 1 with RedisException

use of org.redisson.client.RedisException in project redisson by redisson.

the class RedissonKeys method checkExecution.

private void checkExecution(final RPromise<Long> result, final AtomicReference<Throwable> failed, final AtomicLong count, final AtomicLong executed) {
    if (executed.decrementAndGet() == 0) {
        if (failed.get() != null) {
            if (count.get() > 0) {
                RedisException ex = new RedisException("" + count.get() + " keys has been deleted. But one or more nodes has an error", failed.get());
                result.tryFailure(ex);
            } else {
                result.tryFailure(failed.get());
            }
        } else {
            result.trySuccess(count.get());
        }
    }
}
Also used : RedisException(org.redisson.client.RedisException)

Example 2 with RedisException

use of org.redisson.client.RedisException in project redisson by redisson.

the class RedissonBloomFilter method contains.

@Override
public boolean contains(T object) {
    long[] hashes = hash(object);
    while (true) {
        if (size == 0) {
            readConfig();
        }
        int hashIterations = this.hashIterations;
        long size = this.size;
        long[] indexes = hash(hashes[0], hashes[1], hashIterations, size);
        CommandBatchService executorService = new CommandBatchService(commandExecutor);
        addConfigCheck(hashIterations, size, executorService);
        RBitSetAsync bs = createBitSet(executorService);
        for (int i = 0; i < indexes.length; i++) {
            bs.getAsync(indexes[i]);
        }
        try {
            List<Boolean> result = (List<Boolean>) executorService.execute().getResponses();
            for (Boolean val : result.subList(1, result.size() - 1)) {
                if (!val) {
                    return false;
                }
            }
            return true;
        } catch (RedisException e) {
            if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
                throw e;
            }
        }
    }
}
Also used : RBitSetAsync(org.redisson.api.RBitSetAsync) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) List(java.util.List)

Example 3 with RedisException

use of org.redisson.client.RedisException in project redisson by redisson.

the class RedissonBloomFilter method tryInit.

@Override
public boolean tryInit(long expectedInsertions, double falseProbability) {
    if (falseProbability > 1) {
        throw new IllegalArgumentException("Bloom filter false probability can't be greater than 1");
    }
    if (falseProbability < 0) {
        throw new IllegalArgumentException("Bloom filter false probability can't be negative");
    }
    size = optimalNumOfBits(expectedInsertions, falseProbability);
    if (size == 0) {
        throw new IllegalArgumentException("Bloom filter calculated size is " + size);
    }
    if (size > getMaxSize()) {
        throw new IllegalArgumentException("Bloom filter size can't be greater than " + getMaxSize() + ". But calculated size is " + size);
    }
    hashIterations = optimalNumOfHashFunctions(expectedInsertions, size);
    CommandBatchService executorService = new CommandBatchService(commandExecutor);
    executorService.evalReadAsync(configName, codec, RedisCommands.EVAL_VOID, "local size = redis.call('hget', KEYS[1], 'size');" + "local hashIterations = redis.call('hget', KEYS[1], 'hashIterations');" + "assert(size == false and hashIterations == false, 'Bloom filter config has been changed')", Arrays.<Object>asList(configName), size, hashIterations);
    executorService.writeAsync(configName, StringCodec.INSTANCE, new RedisCommand<Void>("HMSET", new VoidReplayConvertor()), configName, "size", size, "hashIterations", hashIterations, "expectedInsertions", expectedInsertions, "falseProbability", BigDecimal.valueOf(falseProbability).toPlainString());
    try {
        executorService.execute();
    } catch (RedisException e) {
        if (e.getMessage() == null || !e.getMessage().contains("Bloom filter config has been changed")) {
            throw e;
        }
        readConfig();
        return false;
    }
    return true;
}
Also used : VoidReplayConvertor(org.redisson.client.protocol.convertor.VoidReplayConvertor) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException)

Example 4 with RedisException

use of org.redisson.client.RedisException in project redisson by redisson.

the class RedissonKeys method deleteByPatternAsync.

@Override
public RFuture<Long> deleteByPatternAsync(String pattern) {
    if (commandExecutor instanceof CommandBatchService || commandExecutor instanceof CommandReactiveBatchService || commandExecutor instanceof CommandRxBatchService) {
        if (getConnectionManager().isClusterMode()) {
            throw new IllegalStateException("This method doesn't work in batch for Redis cluster mode. For Redis cluster execute it as non-batch method");
        }
        return commandExecutor.evalWriteAsync((String) null, null, RedisCommands.EVAL_LONG, "local keys = redis.call('keys', ARGV[1]) " + "local n = 0 " + "for i=1, #keys,5000 do " + "n = n + redis.call('del', unpack(keys, i, math.min(i+4999, table.getn(keys)))) " + "end " + "return n;", Collections.emptyList(), pattern);
    }
    int batchSize = 500;
    List<CompletableFuture<Long>> futures = new ArrayList<>();
    for (MasterSlaveEntry entry : commandExecutor.getConnectionManager().getEntrySet()) {
        commandExecutor.getConnectionManager().getExecutor().execute(() -> {
            long count = 0;
            try {
                Iterator<String> keysIterator = createKeysIterator(entry, RedisCommands.SCAN, pattern, batchSize);
                List<String> keys = new ArrayList<>();
                while (keysIterator.hasNext()) {
                    String key = keysIterator.next();
                    keys.add(key);
                    if (keys.size() % batchSize == 0) {
                        count += delete(keys.toArray(new String[0]));
                        keys.clear();
                    }
                }
                if (!keys.isEmpty()) {
                    count += delete(keys.toArray(new String[0]));
                    keys.clear();
                }
                RFuture<Long> future = RedissonPromise.newSucceededFuture(count);
                futures.add(future.toCompletableFuture());
            } catch (Exception e) {
                CompletableFuture<Long> future = new CompletableFuture<>();
                future.completeExceptionally(e);
                futures.add(future);
            }
        });
    }
    CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
    CompletableFuture<Long> res = future.handle((r, e) -> {
        long cc = futures.stream().filter(f -> f.isDone()).mapToLong(f -> f.getNow(0L)).sum();
        if (e != null) {
            if (cc > 0) {
                RedisException ex = new RedisException(cc + " keys have been deleted. But one or more nodes has an error", e);
                throw new CompletionException(ex);
            } else {
                throw new CompletionException(e);
            }
        }
        return cc;
    });
    return new CompletableFutureWrapper<>(res);
}
Also used : RedisException(org.redisson.client.RedisException) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) StringCodec(org.redisson.client.codec.StringCodec) CommandBatchService(org.redisson.command.CommandBatchService) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) CompositeIterable(org.redisson.misc.CompositeIterable) RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) StreamSupport(java.util.stream.StreamSupport) RObject(org.redisson.api.RObject) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService) RType(org.redisson.api.RType) ConnectionManager(org.redisson.connection.ConnectionManager) RedissonPromise(org.redisson.misc.RedissonPromise) CompletionException(java.util.concurrent.CompletionException) RedissonBaseIterator(org.redisson.iterator.RedissonBaseIterator) RKeys(org.redisson.api.RKeys) RedisCommands(org.redisson.client.protocol.RedisCommands) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisCommand(org.redisson.client.protocol.RedisCommand) CommandBatchService(org.redisson.command.CommandBatchService) RedisException(org.redisson.client.RedisException) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) CommandRxBatchService(org.redisson.rx.CommandRxBatchService) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) CompletionException(java.util.concurrent.CompletionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RedisException(org.redisson.client.RedisException) CommandReactiveBatchService(org.redisson.reactive.CommandReactiveBatchService)

Example 5 with RedisException

use of org.redisson.client.RedisException in project redisson by redisson.

the class SyncRemoteProxy method create.

public <T> T create(Class<T> remoteInterface, RemoteInvocationOptions options) {
    // local copy of the options, to prevent mutation
    RemoteInvocationOptions optionsCopy = new RemoteInvocationOptions(options);
    String toString = getClass().getSimpleName() + "-" + remoteInterface.getSimpleName() + "-proxy-" + remoteService.generateRequestId();
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("toString")) {
                return toString;
            } else if (method.getName().equals("equals")) {
                return proxy == args[0];
            } else if (method.getName().equals("hashCode")) {
                return toString.hashCode();
            }
            if (!optionsCopy.isResultExpected() && !(method.getReturnType().equals(Void.class) || method.getReturnType().equals(Void.TYPE)))
                throw new IllegalArgumentException("The noResult option only supports void return value");
            RequestId requestId = remoteService.generateRequestId();
            String requestQueueName = getRequestQueueName(remoteInterface);
            RemoteServiceRequest request = new RemoteServiceRequest(executorId, requestId.toString(), method.getName(), remoteService.getMethodSignature(method), args, optionsCopy, System.currentTimeMillis());
            CompletableFuture<RemoteServiceAck> ackFuture;
            if (optionsCopy.isAckExpected()) {
                ackFuture = pollResponse(optionsCopy.getAckTimeoutInMillis(), requestId, false);
            } else {
                ackFuture = null;
            }
            CompletableFuture<RRemoteServiceResponse> responseFuture;
            if (optionsCopy.isResultExpected()) {
                long timeout = remoteService.getTimeout(optionsCopy.getExecutionTimeoutInMillis(), request);
                responseFuture = pollResponse(timeout, requestId, false);
            } else {
                responseFuture = null;
            }
            RemotePromise<Object> addPromise = new RemotePromise<Object>(requestId);
            CompletableFuture<Boolean> futureAdd = remoteService.addAsync(requestQueueName, request, addPromise);
            Boolean res;
            try {
                res = futureAdd.join();
            } catch (Exception e) {
                if (responseFuture != null) {
                    responseFuture.cancel(false);
                }
                if (ackFuture != null) {
                    ackFuture.cancel(false);
                }
                throw e.getCause();
            }
            if (!res) {
                if (responseFuture != null) {
                    responseFuture.cancel(false);
                }
                if (ackFuture != null) {
                    ackFuture.cancel(false);
                }
                throw new RedisException("Task hasn't been added");
            }
            // poll for the ack only if expected
            if (ackFuture != null) {
                String ackName = remoteService.getAckName(requestId);
                RemoteServiceAck ack = null;
                try {
                    ack = ackFuture.toCompletableFuture().get(optionsCopy.getAckTimeoutInMillis(), TimeUnit.MILLISECONDS);
                } catch (ExecutionException | TimeoutException e) {
                // skip
                }
                if (ack == null) {
                    CompletionStage<RemoteServiceAck> ackFutureAttempt = tryPollAckAgainAsync(optionsCopy, ackName, requestId);
                    try {
                        ack = ackFutureAttempt.toCompletableFuture().get(optionsCopy.getAckTimeoutInMillis(), TimeUnit.MILLISECONDS);
                    } catch (ExecutionException | TimeoutException e) {
                    // skip
                    }
                    if (ack == null) {
                        throw new RemoteServiceAckTimeoutException("No ACK response after " + optionsCopy.getAckTimeoutInMillis() + "ms for request: " + request);
                    }
                }
                new RedissonBucket<>(commandExecutor, ackName).delete();
            }
            // poll for the response only if expected
            if (responseFuture != null) {
                RemoteServiceResponse response = null;
                try {
                    response = (RemoteServiceResponse) responseFuture.toCompletableFuture().join();
                } catch (Exception e) {
                // skip
                }
                if (response == null) {
                    throw new RemoteServiceTimeoutException("No response after " + optionsCopy.getExecutionTimeoutInMillis() + "ms for request: " + request);
                }
                if (response.getError() != null) {
                    throw response.getError();
                }
                if (method.getReturnType().equals(Optional.class)) {
                    if (response.getResult() == null) {
                        return Optional.empty();
                    }
                    return Optional.of(response.getResult());
                }
                return response.getResult();
            }
            return null;
        }
    };
    return (T) Proxy.newProxyInstance(remoteInterface.getClassLoader(), new Class[] { remoteInterface }, handler);
}
Also used : RedisException(org.redisson.client.RedisException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) RedisException(org.redisson.client.RedisException) RemoteInvocationOptions(org.redisson.api.RemoteInvocationOptions) RemotePromise(org.redisson.executor.RemotePromise)

Aggregations

RedisException (org.redisson.client.RedisException)13 CommandBatchService (org.redisson.command.CommandBatchService)5 RFuture (org.redisson.api.RFuture)3 RedisClient (org.redisson.client.RedisClient)3 Future (io.netty.util.concurrent.Future)2 FutureListener (io.netty.util.concurrent.FutureListener)2 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 RBitSetAsync (org.redisson.api.RBitSetAsync)2 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)2 RedisConnection (org.redisson.client.RedisConnection)2 RedisConnectionException (org.redisson.client.RedisConnectionException)2 RPromise (org.redisson.misc.RPromise)2 ReqlError (com.rethinkdb.gen.exc.ReqlError)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ReplayingDecoder (io.netty.handler.codec.ReplayingDecoder)1