com.github.lontime.shaded.org.redisson.command.CommandBatchService Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson.command;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import com.github.lontime.shaded.org.redisson.api.BatchOptions;
import com.github.lontime.shaded.org.redisson.api.BatchOptions.ExecutionMode;
import com.github.lontime.shaded.org.redisson.api.BatchResult;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.client.RedisConnection;
import com.github.lontime.shaded.org.redisson.client.RedisTimeoutException;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.protocol.BatchCommandData;
import com.github.lontime.shaded.org.redisson.client.protocol.CommandData;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommand;
import com.github.lontime.shaded.org.redisson.client.protocol.RedisCommands;
import com.github.lontime.shaded.org.redisson.connection.ConnectionManager;
import com.github.lontime.shaded.org.redisson.connection.MasterSlaveEntry;
import com.github.lontime.shaded.org.redisson.connection.NodeSource;
import com.github.lontime.shaded.org.redisson.liveobject.core.RedissonObjectBuilder;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
*
* @author Nikita Koksharov
*
*/
public class CommandBatchService extends CommandAsyncService {
public static class ConnectionEntry {
boolean firstCommand = true;
CompletableFuture connectionFuture;
Runnable cancelCallback;
public CompletableFuture getConnectionFuture() {
return connectionFuture;
}
public void setConnectionFuture(CompletableFuture connectionFuture) {
this.connectionFuture = connectionFuture;
}
public boolean isFirstCommand() {
return firstCommand;
}
public void setFirstCommand(boolean firstCommand) {
this.firstCommand = firstCommand;
}
public Runnable getCancelCallback() {
return cancelCallback;
}
public void setCancelCallback(Runnable cancelCallback) {
this.cancelCallback = cancelCallback;
}
}
public static class Entry {
Deque> commands = new LinkedBlockingDeque<>();
volatile boolean readOnlyMode = true;
public Deque> getCommands() {
return commands;
}
public void setReadOnlyMode(boolean readOnlyMode) {
this.readOnlyMode = readOnlyMode;
}
public boolean isReadOnlyMode() {
return readOnlyMode;
}
public void clearErrors() {
for (BatchCommandData, ?> commandEntry : commands) {
commandEntry.clearError();
}
}
}
private final AtomicInteger index = new AtomicInteger();
private final ConcurrentMap commands = new ConcurrentHashMap<>();
private final ConcurrentMap connections = new ConcurrentHashMap<>();
private final BatchOptions options;
private final Map, List> nestedServices = new ConcurrentHashMap<>();
private final AtomicBoolean executed = new AtomicBoolean();
public CommandBatchService(CommandAsyncExecutor executor) {
this(executor, RedissonObjectBuilder.ReferenceType.DEFAULT);
}
public CommandBatchService(CommandAsyncExecutor executor, RedissonObjectBuilder.ReferenceType referenceType) {
this(executor.getConnectionManager(), BatchOptions.defaults(), executor.getObjectBuilder(), referenceType);
}
public CommandBatchService(CommandAsyncExecutor executor, BatchOptions options) {
this(executor.getConnectionManager(), options, executor.getObjectBuilder(), RedissonObjectBuilder.ReferenceType.DEFAULT);
}
public CommandBatchService(CommandAsyncExecutor executor, BatchOptions options, RedissonObjectBuilder.ReferenceType referenceType) {
this(executor.getConnectionManager(), options, executor.getObjectBuilder(), referenceType);
}
private CommandBatchService(ConnectionManager connectionManager, BatchOptions options,
RedissonObjectBuilder objectBuilder, RedissonObjectBuilder.ReferenceType referenceType) {
super(connectionManager, objectBuilder, referenceType);
this.options = options;
}
public BatchOptions getOptions() {
return options;
}
public void add(RFuture> future, List services) {
nestedServices.put(future, services);
}
@Override
public RFuture async(boolean readOnlyMode, NodeSource nodeSource,
Codec codec, RedisCommand command, Object[] params, boolean ignoreRedirect, boolean noRetry) {
CompletableFuture mainPromise = createPromise();
if (isRedisBasedQueue()) {
boolean isReadOnly = options.getExecutionMode() == ExecutionMode.REDIS_READ_ATOMIC;
RedisExecutor executor = new RedisQueuedBatchExecutor<>(isReadOnly, nodeSource, codec, command, params, mainPromise,
false, connectionManager, objectBuilder, commands, connections, options, index, executed, referenceType, noRetry);
executor.execute();
} else {
RedisExecutor executor = new RedisBatchExecutor<>(readOnlyMode, nodeSource, codec, command, params, mainPromise,
false, connectionManager, objectBuilder, commands, options, index, executed, referenceType, noRetry);
executor.execute();
}
return new CompletableFutureWrapper<>(mainPromise);
}
@Override
public CompletableFuture createPromise() {
if (isRedisBasedQueue()) {
return new BatchPromise<>();
}
return new CompletableFuture<>();
}
public void discard() {
get(discardAsync());
}
public RFuture discardAsync() {
if (executed.get()) {
throw new IllegalStateException("Batch already executed!");
}
executed.set(true);
if (isRedisBasedQueue()) {
return writeAllVoidAsync(RedisCommands.DISCARD);
}
commands.values().stream()
.flatMap(e -> e.getCommands().stream())
.flatMap(c -> Arrays.stream(c.getParams()))
.forEach(obj -> ReferenceCountUtil.safeRelease(obj));
return new CompletableFutureWrapper<>((Void) null);
}
public BatchResult> execute() {
RFuture> f = executeAsync();
return get(f);
}
public RFuture executeAsyncVoid() {
CompletableFuture> resFuture = executeAsync().toCompletableFuture();
CompletableFuture s = resFuture.thenApply(res -> null);
return new CompletableFutureWrapper<>(s);
}
public boolean isExecuted() {
return executed.get();
}
public RFuture> executeAsync() {
if (executed.get()) {
throw new IllegalStateException("Batch already executed!");
}
if (commands.isEmpty()) {
executed.set(true);
BatchResult