All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.remote.AsyncRemoteProxy Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/**
 * Copyright (c) 2013-2024 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.remote;

import org.redisson.RedissonBucket;
import org.redisson.RedissonList;
import org.redisson.RedissonMap;
import org.redisson.api.RFuture;
import org.redisson.api.RList;
import org.redisson.api.RMap;
import org.redisson.api.RemoteInvocationOptions;
import org.redisson.client.RedisException;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.codec.CompositeCodec;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.executor.RemotePromise;
import org.redisson.misc.CompletableFutureWrapper;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public class AsyncRemoteProxy extends BaseRemoteProxy {

    protected final String cancelRequestMapName;
    
    public AsyncRemoteProxy(CommandAsyncExecutor commandExecutor, String name, String responseQueueName,
                            Codec codec, String executorId, String cancelRequestMapName, BaseRemoteService remoteService) {
        super(commandExecutor, name, responseQueueName, codec, executorId, remoteService);
        this.cancelRequestMapName = cancelRequestMapName;
    }
    
    protected List> permittedClasses() {
        return Arrays.asList(RFuture.class);
    }
    
    public  T create(Class remoteInterface, RemoteInvocationOptions options,
            Class syncInterface) {
        for (Method m : remoteInterface.getMethods()) {
            try {
                syncInterface.getMethod(m.getName(), m.getParameterTypes());
            } catch (NoSuchMethodException e) {
                throw new IllegalArgumentException("Method '" + m.getName() + "' with params '"
                        + Arrays.toString(m.getParameterTypes()) + "' isn't defined in " + syncInterface);
            } catch (SecurityException e) {
                throw new IllegalArgumentException(e);
            }
            
            boolean permitted = false;
            for (Class clazz : permittedClasses()) {
                if (clazz.isAssignableFrom(m.getReturnType())) {
                    permitted = true;
                    break;
                }
            }
            if (!permitted) {
                throw new IllegalArgumentException(
                        m.getReturnType().getClass() + " isn't allowed as return type");
            }
        }
        
        // local copy of the options, to prevent mutation
        RemoteInvocationOptions optionsCopy = new RemoteInvocationOptions(options);
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("toString")) {
                    return proxy.getClass().getName() + "-" + remoteInterface.getName();
                } else if (method.getName().equals("equals")) {
                    return proxy == args[0];
                } else if (method.getName().equals("hashCode")) {
                    return (proxy.getClass().getName() + "-" + remoteInterface.getName()).hashCode();
                }

                if (!optionsCopy.isResultExpected() && !(method.getReturnType().equals(Void.class)
                        || method.getReturnType().equals(Void.TYPE) || method.getReturnType().equals(RFuture.class))) {
                    throw new IllegalArgumentException("The noResult option only supports void return value");
                }

                String requestId = remoteService.generateRequestId(args);
                String requestQueueName = getRequestQueueName(syncInterface);
                Long ackTimeout = optionsCopy.getAckTimeoutInMillis();
                RemoteServiceRequest request = new RemoteServiceRequest(executorId, requestId, method.getName(),
                                                    remoteService.getMethodSignature(method), args, optionsCopy, System.currentTimeMillis());

                CompletableFuture ackFuture;
                if (optionsCopy.isAckExpected()) {
                    ackFuture = pollResponse(optionsCopy.getAckTimeoutInMillis(), requestId, false);
                } else {
                    ackFuture = null;
                }
                
                CompletableFuture responseFuture;
                if (optionsCopy.isResultExpected()) {
                    long timeout = remoteService.getTimeout(optionsCopy.getExecutionTimeoutInMillis(), request);
                    responseFuture = pollResponse(timeout, requestId, false);
                } else {
                    responseFuture = null;
                }

                RemotePromise result = createResultPromise(optionsCopy, requestId, requestQueueName,
                        ackTimeout);
                CompletableFuture addFuture = remoteService.addAsync(requestQueueName, request, result);
                addFuture.whenComplete((res, e) -> {
                        if (e != null) {
                            if (responseFuture != null) {
                                responseFuture.cancel(false);
                            }
                            if (ackFuture != null) {
                                ackFuture.cancel(false);
                            }
                            result.completeExceptionally(e);
                            return;
                        }
                        
                        if (!res) {
                            result.completeExceptionally(new RedisException("Task hasn't been added"));
                            if (responseFuture != null) {
                                responseFuture.cancel(false);
                            }
                            if (ackFuture != null) {
                                ackFuture.cancel(false);
                            }
                            return;
                        }

                                if (optionsCopy.isAckExpected()) {
                                    ackFuture.whenComplete((ack, ex) -> {
                                        if (ex != null) {
                                            if (responseFuture != null) {
                                                responseFuture.cancel(false);
                                            }

                                            result.completeExceptionally(ex);
                                            return;
                                        }

                                        if (ack == null) {
                                            String ackName = remoteService.getAckName(requestId);
                                            CompletionStage ackFutureAttempt =
                                                                        tryPollAckAgainAsync(optionsCopy, ackName, requestId);
                                            ackFutureAttempt.whenComplete((re, ex2) -> {
                                                if (ex2 != null) {
                                                    result.completeExceptionally(ex2);
                                                    return;
                                                }

                                                if (re == null) {
                                                    Exception exc = new RemoteServiceAckTimeoutException(
                                                            "No ACK response after "
                                                                    + optionsCopy.getAckTimeoutInMillis()
                                                                    + "ms for request: " + requestId);
                                                    result.completeExceptionally(exc);
                                                    return;
                                                }

                                                awaitResultAsync(optionsCopy, result, ackName, responseFuture);
                                            });
                                        } else {
                                            awaitResultAsync(optionsCopy, result, responseFuture);
                                        }
                                    });
                                } else {
                                    awaitResultAsync(optionsCopy, result, responseFuture);
                                }
                        });

                return convertResult(result, method.getReturnType());
            }

        };
        return (T) Proxy.newProxyInstance(remoteInterface.getClassLoader(), new Class[] { remoteInterface }, handler);
    }

    protected Object convertResult(RemotePromise result, Class returnType) {
        return new CompletableFutureWrapper<>(result);
    }
    
    private void awaitResultAsync(RemoteInvocationOptions optionsCopy, RemotePromise result,
            String ackName, CompletableFuture responseFuture) {
        RFuture deleteFuture = new RedissonBucket<>(commandExecutor, ackName).deleteAsync();
        deleteFuture.whenComplete((res, e) -> {
            if (e != null) {
                result.completeExceptionally(e);
                return;
            }

            awaitResultAsync(optionsCopy, result, responseFuture);
        });
    }
    
    protected void awaitResultAsync(RemoteInvocationOptions optionsCopy, RemotePromise result,
            CompletionStage responseFuture) {
        // poll for the response only if expected
        if (!optionsCopy.isResultExpected()) {
            return;
        }

        responseFuture.whenComplete((res, e) -> {
            if (e != null) {
                result.completeExceptionally(e);
                return;
            }

            if (res == null) {
                RemoteServiceTimeoutException ex = new RemoteServiceTimeoutException("No response after "
                        + optionsCopy.getExecutionTimeoutInMillis() + "ms for request: " + result.getRequestId());
                result.completeExceptionally(ex);
                return;
            }

            if (res instanceof RemoteServiceCancelResponse) {
                result.doCancel(true);
                return;
            }

            RemoteServiceResponse response = (RemoteServiceResponse) res;
            if (response.getError() != null) {
                result.completeExceptionally(response.getError());
                return;
            }

            result.complete(response.getResult());
        });
    }

    private RemotePromise createResultPromise(RemoteInvocationOptions optionsCopy,
                                                      String requestId, String requestQueueName, Long ackTimeout) {
        RemotePromise result = new RemotePromise(requestId) {

            @Override
            public boolean cancel(boolean mayInterruptIfRunning) {
                if (isCancelled()) {
                    return true;
                }
                
                if (isDone()) {
                    return false;
                }
                
                
                if (optionsCopy.isAckExpected()) {
                    String ackName = remoteService.getAckName(requestId);
                    RFuture future = commandExecutor.evalWriteAsync(responseQueueName, LongCodec.INSTANCE,
                            RedisCommands.EVAL_BOOLEAN,
                            "if redis.call('setnx', KEYS[1], 1) == 1 then "
                                + "redis.call('pexpire', KEYS[1], ARGV[1]);"
//                                        + "redis.call('lrem', KEYS[3], 1, ARGV[1]);"
//                                        + "redis.call('pexpire', KEYS[2], ARGV[2]);" 
                                + "return 1;" 
                            + "end;"
                            + "return 0;",
                            Arrays. asList(ackName),
//                                    Arrays. asList(ackName, responseQueueName, requestQueueName), 
                            ackTimeout);
                    
                    boolean ackNotSent = commandExecutor.get(future);
                    if (ackNotSent) {
                        RList list = new RedissonList<>(LongCodec.INSTANCE, commandExecutor, requestQueueName, null);
                        list.remove(requestId);
                        super.cancel(mayInterruptIfRunning);
                        return true;
                    }

                    return executeCancel(mayInterruptIfRunning);
                }

                Boolean removed = commandExecutor.get(remoteService.removeAsync(requestQueueName, requestId));
                if (removed == null || removed) {
                    super.cancel(mayInterruptIfRunning);
                    return true;
                }

                return executeCancel(mayInterruptIfRunning);
            }

            private boolean executeCancel(boolean mayInterruptIfRunning) {
                if (isCancelled()) {
                    return true;
                }
                
                if (isDone()) {
                    return false;
                }

                cancelExecution(optionsCopy, mayInterruptIfRunning, this, cancelRequestMapName);

                try {
                    toCompletableFuture().get(60, TimeUnit.SECONDS);
                } catch (Exception e) {
                    // skip
                }
                return isCancelled();
            }

            @Override
            public CompletableFuture cancelAsync(boolean mayInterruptIfRunning) {
                return AsyncRemoteProxy.this.cancelAsync(optionsCopy, this, requestId, requestQueueName, ackTimeout, mayInterruptIfRunning);
            }
        };
        return result;
    }

    private CompletableFuture cancelAsync(RemoteInvocationOptions optionsCopy, RemotePromise promise,
                                                   String requestId, String requestQueueName, Long ackTimeout, boolean mayInterruptIfRunning) {
        if (promise.isCancelled()) {
            return CompletableFuture.completedFuture(true);
        }

        if (promise.isDone()) {
            return CompletableFuture.completedFuture(false);
        }

        if (optionsCopy.isAckExpected()) {
            String ackName = remoteService.getAckName(requestId);
            RFuture f = commandExecutor.evalWriteNoRetryAsync(responseQueueName, LongCodec.INSTANCE,
                    RedisCommands.EVAL_BOOLEAN,
                    "if redis.call('setnx', KEYS[1], 1) == 1 then "
                        + "redis.call('pexpire', KEYS[1], ARGV[1]);"
//                                        + "redis.call('lrem', KEYS[3], 1, ARGV[1]);"
//                                        + "redis.call('pexpire', KEYS[2], ARGV[2]);"
                        + "return 1;"
                    + "end;"
                    + "return 0;",
                    Arrays. asList(ackName),
//                                    Arrays. asList(ackName, responseQueueName, requestQueueName),
                    ackTimeout);
            CompletableFuture future = f.toCompletableFuture();

            return future.thenCompose(ackNotSent -> {
                if (ackNotSent) {
                    RList list = new RedissonList<>(LongCodec.INSTANCE, commandExecutor, requestQueueName, null);
                    CompletableFuture removeFuture = list.removeAsync(requestId).toCompletableFuture();
                    return removeFuture.thenApply(res -> {
                        promise.doCancel(mayInterruptIfRunning);
                        return true;
                    });
                }

                return doCancelAsync(mayInterruptIfRunning, promise, optionsCopy);
            });
        }

        CompletableFuture removeFuture = remoteService.removeAsync(requestQueueName, requestId);
        return removeFuture.thenCompose(removed -> {
            if (removed == null || removed) {
                promise.doCancel(mayInterruptIfRunning);
            }

            return doCancelAsync(mayInterruptIfRunning, promise, optionsCopy);
        });
    }

    private CompletableFuture doCancelAsync(boolean mayInterruptIfRunning, RemotePromise promise, RemoteInvocationOptions optionsCopy) {
        if (promise.isCancelled()) {
            return CompletableFuture.completedFuture(true);
        }

        if (promise.isDone()) {
            return CompletableFuture.completedFuture(false);
        }

        cancelExecution(optionsCopy, mayInterruptIfRunning, promise, cancelRequestMapName);

        return promise.toCompletableFuture().thenApply(r -> promise.isCancelled());
    }

    private void cancelExecution(RemoteInvocationOptions optionsCopy,
            boolean mayInterruptIfRunning, RemotePromise remotePromise, String cancelRequestMapName) {
        RMap canceledRequests = new RedissonMap<>(new CompositeCodec(StringCodec.INSTANCE, codec, codec), commandExecutor, cancelRequestMapName, null, null, null);
        canceledRequests.fastPutAsync(remotePromise.getRequestId().toString(), new RemoteServiceCancelRequest(mayInterruptIfRunning, false));
        canceledRequests.expireAsync(60, TimeUnit.SECONDS);
        
        // subscribe for async result if it's not expected before
        if (!optionsCopy.isResultExpected()) {
            RemoteInvocationOptions options = new RemoteInvocationOptions(optionsCopy);
            options.expectResultWithin(60, TimeUnit.SECONDS);
            CompletionStage responseFuture = pollResponse(options.getExecutionTimeoutInMillis(), remotePromise.getRequestId(), false);
            awaitResultAsync(options, remotePromise, responseFuture);
        }
    }

}