Feature - bgSave(), scheduleBgSave(), save(), getLastSaveTime(), bgRewriteAOF() methods added to RedisNode interface. #5251

pull/5004/head^2
Nikita Koksharov 1 year ago
parent 329b485a6f
commit 160dc6e6cc

@ -18,6 +18,7 @@ package org.redisson.api.redisnode;
import org.redisson.client.protocol.Time;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -93,4 +94,41 @@ public interface RedisNode {
*/
void setConfig(String parameter, String value);
/**
* Runs the Redis database saving process in background.
*
*/
void bgSave();
/**
* Save the Redis database in background.
* If AOF rewrite process is in progress then
* the background save is scheduled to run upon its completion.
*
*/
void scheduleBgSave();
/**
* Save the Redis database.
*
*/
void save();
/**
* Returns time of the last successful
* Redis database save operation.
*
* @return time
*/
Instant getLastSaveTime();
/**
* Runs an Append Only File rewrite process.
* Starts only if there is no a background process doing persistence.
* <p>
* If fails no data gets lost
*
*/
void bgRewriteAOF();
}

@ -18,6 +18,7 @@ package org.redisson.api.redisnode;
import org.redisson.api.RFuture;
import org.redisson.client.protocol.Time;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -85,4 +86,41 @@ public interface RedisNodeAsync {
*/
RFuture<Void> setConfigAsync(String parameter, String value);
/**
* Runs the Redis database saving process in background.
*
*/
RFuture<Void> bgSaveAsync();
/**
* Save the Redis database in background.
* If AOF rewrite process is in progress then
* the background save is scheduled to run upon its completion.
*
*/
RFuture<Void> scheduleBgSaveAsync();
/**
* Save the Redis database.
*
*/
RFuture<Void> saveAsync();
/**
* Returns time of the last successful
* Redis database save operation.
*
* @return time
*/
RFuture<Instant> getLastSaveTimeAsync();
/**
* Runs an Append Only File rewrite process.
* Starts only if there is no a background process doing persistence.
* <p>
* If fails no data gets lost
*
*/
RFuture<Void> bgRewriteAOFAsync();
}

@ -29,6 +29,7 @@ import org.redisson.codec.CompositeCodec;
import org.redisson.misc.RedisURI;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
@ -467,6 +468,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> SAVE = new RedisStrictCommand<Void>("SAVE", new VoidReplayConvertor());
RedisStrictCommand<Long> LASTSAVE = new RedisStrictCommand<Long>("LASTSAVE");
RedisStrictCommand<Instant> LASTSAVE_INSTANT = new RedisStrictCommand<>("LASTSAVE", new InstantReplyConvertor());
RedisStrictCommand<Void> BGSAVE = new RedisStrictCommand<Void>("BGSAVE", new VoidReplayConvertor());
RedisStrictCommand<Void> BGREWRITEAOF = new RedisStrictCommand<Void>("BGREWRITEAOF", new VoidReplayConvertor());

@ -0,0 +1,30 @@
/**
* Copyright (c) 2013-2022 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.client.protocol.convertor;
import java.time.Instant;
/**
*
* @author Nikita Koksharov
*
*/
public class InstantReplyConvertor implements Convertor<Instant> {
@Override
public Instant convert(Object obj) {
return Instant.ofEpochSecond((Long)obj);
}
}

@ -30,6 +30,7 @@ import org.redisson.misc.CompletableFutureWrapper;
import org.redisson.misc.RedisURI;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -339,4 +340,53 @@ public class RedisNode implements RedisClusterMaster, RedisClusterSlave, RedisMa
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.CONFIG_SET, parameter, value);
}
@Override
public void bgSave() {
commandExecutor.get(bgSaveAsync());
}
@Override
public void scheduleBgSave() {
commandExecutor.get(scheduleBgSaveAsync());
}
@Override
public void save() {
commandExecutor.get(saveAsync());
}
@Override
public Instant getLastSaveTime() {
return commandExecutor.get(getLastSaveTimeAsync());
}
@Override
public RFuture<Void> bgSaveAsync() {
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.BGSAVE);
}
@Override
public RFuture<Void> scheduleBgSaveAsync() {
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.BGSAVE, "SCHEDULE");
}
@Override
public RFuture<Void> saveAsync() {
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.SAVE);
}
@Override
public RFuture<Instant> getLastSaveTimeAsync() {
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.LASTSAVE_INSTANT);
}
@Override
public void bgRewriteAOF() {
commandExecutor.get(bgRewriteAOFAsync());
}
@Override
public RFuture<Void> bgRewriteAOFAsync() {
return commandExecutor.writeAsync(client, StringCodec.INSTANCE, RedisCommands.BGREWRITEAOF);
}
}

@ -31,6 +31,7 @@ import org.redisson.misc.CompletableFutureWrapper;
import org.redisson.misc.RedisURI;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@ -270,4 +271,53 @@ public class SentinelRedisNode implements RedisSentinel, RedisSentinelAsync {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.CONFIG_SET, parameter, value);
}
@Override
public void bgSave() {
commandAsyncService.get(bgSaveAsync());
}
@Override
public void scheduleBgSave() {
commandAsyncService.get(scheduleBgSaveAsync());
}
@Override
public void save() {
commandAsyncService.get(saveAsync());
}
@Override
public Instant getLastSaveTime() {
return commandAsyncService.get(getLastSaveTimeAsync());
}
@Override
public RFuture<Void> bgSaveAsync() {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.BGSAVE);
}
@Override
public RFuture<Void> scheduleBgSaveAsync() {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.BGSAVE, "SCHEDULE");
}
@Override
public RFuture<Void> saveAsync() {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.SAVE);
}
@Override
public RFuture<Instant> getLastSaveTimeAsync() {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.LASTSAVE_INSTANT);
}
@Override
public void bgRewriteAOF() {
commandAsyncService.get(bgRewriteAOFAsync());
}
@Override
public RFuture<Void> bgRewriteAOFAsync() {
return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.BGREWRITEAOF);
}
}

@ -12,7 +12,9 @@ import org.redisson.ClusterRunner.ClusterProcesses;
import org.redisson.RedisRunner.RedisProcess;
import org.redisson.api.*;
import org.redisson.api.redisnode.RedisClusterMaster;
import org.redisson.api.redisnode.RedisMaster;
import org.redisson.api.redisnode.RedisNodes;
import org.redisson.api.redisnode.RedisSingle;
import org.redisson.client.*;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.codec.StringCodec;
@ -33,6 +35,7 @@ import org.redisson.connection.balancer.RandomLoadBalancer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
@ -866,6 +869,22 @@ public class RedissonTest extends BaseTest {
}
@Test
public void testSave() throws InterruptedException {
Instant s2 = Instant.now();
Thread.sleep(1000);
RedisSingle nodes = redisson.getRedisNodes(RedisNodes.SINGLE);
RedisMaster node = nodes.getInstance();
Instant time1 = node.getLastSaveTime();
assertThat(time1).isNotNull();
node.save();
Instant time2 = node.getLastSaveTime();
assertThat(time2.isAfter(s2)).isTrue();
node.bgSave();
node.bgRewriteAOF();
}
@Test
public void testShutdown() {
Config config = new Config();

Loading…
Cancel
Save