Fixed - RObject.rename() method doesn't work in cluster #6058

pull/6077/head
Nikita Koksharov 8 months ago
parent 1911133b3b
commit 3f63e4b322

@ -33,14 +33,11 @@ import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.convertor.NumberConvertor;
import org.redisson.client.protocol.decoder.*;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandBatchService;
import org.redisson.connection.decoder.MapGetAllDecoder;
import org.redisson.iterator.RedissonMapIterator;
import org.redisson.iterator.RedissonMapKeyIterator;
import org.redisson.mapreduce.RedissonMapReduce;
import org.redisson.misc.CompletableFutureWrapper;
import org.redisson.reactive.CommandReactiveBatchService;
import org.redisson.rx.CommandRxBatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ -25,6 +25,7 @@ import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.pubsub.PubSubType;
import org.redisson.command.BatchService;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.config.Protocol;
import org.redisson.connection.ServiceManager;
@ -116,7 +117,7 @@ public abstract class RedissonObject implements RObject {
}
protected final void setName(String name) {
this.name = commandExecutor.getServiceManager().getConfig().getNameMapper().map(name);
this.name = mapName(name);
}
@Override
@ -156,16 +157,27 @@ public abstract class RedissonObject implements RObject {
}
protected final void checkNotBatch() {
if (commandExecutor instanceof CommandBatchService
|| commandExecutor instanceof CommandReactiveBatchService
|| commandExecutor instanceof CommandRxBatchService) {
if (commandExecutor instanceof BatchService) {
throw new IllegalStateException("This method doesn't work in batch mode.");
}
}
@Override
public RFuture<Void> renameAsync(String newName) {
RFuture<Void> future = commandExecutor.writeAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.RENAME, getRawName(), newName);
if (getServiceManager().getCfg().isClusterConfig()) {
checkNotBatch();
RFuture<byte[]> r = commandExecutor.evalWriteAsync(getRawName(), ByteArrayCodec.INSTANCE, RedisCommands.EVAL_OBJECT,
"local result = redis.call('dump', KEYS[1]);" +
"redis.call('del', KEYS[1]);" +
"return result;",
Arrays.asList(getRawName()));
CompletionStage<Void> f = r.thenCompose(val -> commandExecutor.writeAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.RESTORE, mapName(newName), 0, val))
.thenAccept(rr -> setName(newName));
return new CompletableFutureWrapper<>(f);
}
RFuture<Void> future = commandExecutor.writeAsync(getRawName(), StringCodec.INSTANCE, RedisCommands.RENAME, getRawName(), mapName(newName));
CompletionStage<Void> f = future.thenAccept(r -> setName(newName));
return new CompletableFutureWrapper<>(f);
}
@ -550,7 +562,7 @@ public abstract class RedissonObject implements RObject {
protected final List<String> map(String[] keys) {
return Arrays.stream(keys)
.map(k -> commandExecutor.getServiceManager().getConfig().getNameMapper().map(k))
.map(k -> mapName(k))
.collect(Collectors.toList());
}

@ -0,0 +1,24 @@
/**
* 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.command;
/**
*
* @author Nikita Koksharov
*
*/
public interface BatchService {
}

@ -50,7 +50,7 @@ import java.util.stream.Collectors;
* @author Nikita Koksharov
*
*/
public class CommandBatchService extends CommandAsyncService {
public class CommandBatchService extends CommandAsyncService implements BatchService {
public static class ConnectionEntry {

@ -20,6 +20,7 @@ import org.redisson.api.BatchResult;
import org.redisson.api.RFuture;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.command.BatchService;
import org.redisson.command.CommandBatchService;
import org.redisson.connection.ConnectionManager;
import org.redisson.connection.NodeSource;
@ -36,7 +37,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @author Nikita Koksharov
*
*/
public class CommandReactiveBatchService extends CommandReactiveService {
public class CommandReactiveBatchService extends CommandReactiveService implements BatchService {
private final CommandBatchService batchService;

@ -21,6 +21,7 @@ import org.redisson.api.BatchResult;
import org.redisson.api.RFuture;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.command.BatchService;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.command.CommandBatchService;
import org.redisson.connection.ConnectionManager;
@ -37,7 +38,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @author Nikita Koksharov
*
*/
public class CommandRxBatchService extends CommandRxService {
public class CommandRxBatchService extends CommandRxService implements BatchService {
private final CommandBatchService batchService;

@ -116,6 +116,20 @@ public class RedissonBucketTest extends RedisDockerTest {
rs.shutdown();
}
@Test
public void testRenameInCluster() {
testInCluster(rc -> {
RBucket<String> b = rc.getBucket("test1234");
b.set("123");
b.rename("test347834");
assertThat(b.getName()).isEqualTo("test347834");
RBucket<String> bs = rc.getBucket(b.getName());
assertThat(bs.get()).isEqualTo("123");
});
}
@ParameterizedTest
@MethodSource("trackingData")
public void testTrackingCluster(List<Object> params) {

Loading…
Cancel
Save