Skip to content

Commit d35375e

Browse files
christophstroblThomas Darimont
authored and
Thomas Darimont
committed
DATAREDIS-277 - Add support for 'SLAVEOF'.
'SLAVEOF' and 'SLAVEOF NO ONE' are available via 'RedisConnection' and 'RedisOperations' for 'jedis', 'jredis', 'lettuce' and 'srp'. Original Pull Request: #57
1 parent 2b280e5 commit d35375e

File tree

13 files changed

+404
-6
lines changed

13 files changed

+404
-6
lines changed

docs/src/reference/docbook/appendix/appendix-command-reference.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
<row><entry><code>SINTER</code></entry><entry>X</entry></row>
139139
<row><entry><code>SINTERSTORE</code></entry><entry>X</entry></row>
140140
<row><entry><code>SISMEMBER</code></entry><entry>X</entry></row>
141-
<row><entry><code>SLAVEOF</code></entry><entry>-</entry></row>
141+
<row><entry><code>SLAVEOF</code></entry><entry>X</entry></row>
142142
<row><entry><code>SLOWLOG</code></entry><entry>-</entry></row>
143143
<row><entry><code>SMEMBERS</code></entry><entry>X</entry></row>
144144
<row><entry><code>SMOVE</code></entry><entry>X</entry></row>

src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,24 @@ public List<RedisClientInfo> getClientList() {
21972197
return this.delegate.getClientList();
21982198
}
21992199

2200+
/*
2201+
* (non-Javadoc)
2202+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
2203+
*/
2204+
@Override
2205+
public void slaveOf(String host, int port) {
2206+
this.delegate.slaveOf(host, port);
2207+
}
2208+
2209+
/*
2210+
* (non-Javadoc)
2211+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
2212+
*/
2213+
@Override
2214+
public void slaveOfNoOne() {
2215+
this.delegate.slaveOfNoOne();
2216+
}
2217+
22002218
/**
22012219
* Specifies if pipelined and tx results should be deserialized to Strings. If false, results of
22022220
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the underlying connection

src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public enum ShutdownOption {
165165
Long time();
166166

167167
/**
168-
* Closes a given client connection identified by {@literal ip:port}.
168+
* <<<<<<< HEAD Closes a given client connection identified by {@literal ip:port}.
169169
*
170170
* @param host of connection to close.
171171
* @param port of connection to close
@@ -197,4 +197,22 @@ public enum ShutdownOption {
197197
* @see http://redis.io/commands/client-list
198198
*/
199199
List<RedisClientInfo> getClientList();
200+
201+
/**
202+
* Change redis replication setting to new master.
203+
*
204+
* @param host
205+
* @param port
206+
* @since 1.3
207+
* @see http://redis.io/commands/slaveof
208+
*/
209+
void slaveOf(String host, int port);
210+
211+
/**
212+
* Change server into master.
213+
*
214+
* @since 1.3
215+
* @see http://redis.io/commands/slaveof
216+
*/
217+
void slaveOfNoOne();
200218
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,8 +2051,7 @@ public Double zIncrBy(byte[] key, double increment, byte[] value) {
20512051

20522052
public Long zInterStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
20532053
try {
2054-
ZParams zparams = new ZParams().weights(weights).aggregate(
2055-
redis.clients.jedis.ZParams.Aggregate.valueOf(aggregate.name()));
2054+
ZParams zparams = new ZParams().weights(weights).aggregate(ZParams.Aggregate.valueOf(aggregate.name()));
20562055

20572056
if (isPipelined()) {
20582057
pipeline(new JedisResult(pipeline.zinterstore(destKey, zparams, sets)));
@@ -2382,8 +2381,7 @@ public Double zScore(byte[] key, byte[] value) {
23822381

23832382
public Long zUnionStore(byte[] destKey, Aggregate aggregate, int[] weights, byte[]... sets) {
23842383
try {
2385-
ZParams zparams = new ZParams().weights(weights).aggregate(
2386-
redis.clients.jedis.ZParams.Aggregate.valueOf(aggregate.name()));
2384+
ZParams zparams = new ZParams().weights(weights).aggregate(ZParams.Aggregate.valueOf(aggregate.name()));
23872385

23882386
if (isPipelined()) {
23892387
pipeline(new JedisResult(pipeline.zunionstore(destKey, zparams, sets)));
@@ -2830,6 +2828,23 @@ public void killClient(String host, int port) {
28302828
}
28312829
}
28322830

2831+
/*
2832+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
2833+
*/
2834+
@Override
2835+
public void slaveOf(String host, int port) {
2836+
2837+
Assert.hasText(host, "Host must not be null for 'SLAVEOF' command.");
2838+
if (isQueueing() || isPipelined()) {
2839+
throw new UnsupportedOperationException("'SLAVEOF' cannot be called in pipline / transaction mode.");
2840+
}
2841+
try {
2842+
this.jedis.slaveof(host, port);
2843+
} catch (Exception e) {
2844+
throw convertJedisAccessException(e);
2845+
}
2846+
}
2847+
28332848
/*
28342849
* @see org.springframework.data.redis.connection.RedisServerCommands#setClientName(java.lang.String)
28352850
*/
@@ -2868,6 +2883,23 @@ public List<RedisClientInfo> getClientList() {
28682883
return JedisConverters.toListOfRedisClientInformation(this.jedis.clientList());
28692884
}
28702885

2886+
/*
2887+
* (non-Javadoc)
2888+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
2889+
*/
2890+
@Override
2891+
public void slaveOfNoOne() {
2892+
2893+
if (isQueueing() || isPipelined()) {
2894+
throw new UnsupportedOperationException("'SLAVEOF' cannot be called in pipline / transaction mode.");
2895+
}
2896+
try {
2897+
this.jedis.slaveofNoOne();
2898+
} catch (Exception e) {
2899+
throw convertJedisAccessException(e);
2900+
}
2901+
}
2902+
28712903
/**
28722904
* Specifies if pipelined results should be converted to the expected data type. If false, results of
28732905
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Jedis driver

src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,20 @@ public void setClientName(byte[] name) {
11971197
throw new UnsupportedOperationException("'CLIENT SETNAME' is not supported by the JRedis driver.");
11981198
}
11991199

1200+
/*
1201+
* (non-Javadoc)
1202+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
1203+
*/
1204+
@Override
1205+
public void slaveOf(String host, int port) {
1206+
1207+
try {
1208+
this.jredis.slaveof(host, port);
1209+
} catch (Exception e) {
1210+
throw convertJredisAccessException(e);
1211+
}
1212+
}
1213+
12001214
/*
12011215
* (non-Javadoc)
12021216
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
@@ -1209,4 +1223,17 @@ public String getClientName() {
12091223
public List<RedisClientInfo> getClientList() {
12101224
throw new UnsupportedOperationException();
12111225
}
1226+
1227+
/*
1228+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
1229+
*/
1230+
@Override
1231+
public void slaveOfNoOne() {
1232+
1233+
try {
1234+
this.jredis.slaveofnone();
1235+
} catch (Exception e) {
1236+
throw convertJredisAccessException(e);
1237+
}
1238+
}
12121239
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,6 +2939,29 @@ public void setClientName(byte[] name) {
29392939
getAsyncConnection().clientSetname(name);
29402940
}
29412941

2942+
/*
2943+
* (non-Javadoc)
2944+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
2945+
*/
2946+
@Override
2947+
public void slaveOf(String host, int port) {
2948+
2949+
Assert.hasText(host, "Host must not be null for 'SLAVEOF' command.");
2950+
try {
2951+
if (isPipelined()) {
2952+
pipeline(new LettuceStatusResult(getAsyncConnection().slaveof(host, port)));
2953+
return;
2954+
}
2955+
if (isQueueing()) {
2956+
transaction(new LettuceTxResult(getConnection().slaveof(host, port)));
2957+
return;
2958+
}
2959+
getConnection().slaveof(host, port);
2960+
} catch (Exception ex) {
2961+
throw convertLettuceAccessException(ex);
2962+
}
2963+
}
2964+
29422965
/*
29432966
* (non-Javadoc)
29442967
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
@@ -2978,6 +3001,27 @@ public List<RedisClientInfo> getClientList() {
29783001
return LettuceConverters.toListOfRedisClientInformation(getConnection().clientList());
29793002
}
29803003

3004+
/*
3005+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
3006+
*/
3007+
@Override
3008+
public void slaveOfNoOne() {
3009+
3010+
try {
3011+
if (isPipelined()) {
3012+
pipeline(new LettuceStatusResult(getAsyncConnection().slaveofNoOne()));
3013+
return;
3014+
}
3015+
if (isQueueing()) {
3016+
transaction(new LettuceTxResult(getConnection().slaveofNoOne()));
3017+
return;
3018+
}
3019+
getConnection().slaveofNoOne();
3020+
} catch (Exception ex) {
3021+
throw convertLettuceAccessException(ex);
3022+
}
3023+
}
3024+
29813025
/**
29823026
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
29833027
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver

src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,25 @@ public void setClientName(byte[] name) {
22622262
}
22632263
}
22642264

2265+
/*
2266+
* (non-Javadoc)
2267+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOf(java.lang.String, int)
2268+
*/
2269+
@Override
2270+
public void slaveOf(String host, int port) {
2271+
2272+
Assert.hasText(host, "Host must not be null for 'SLAVEOF' command.");
2273+
try {
2274+
if (isPipelined()) {
2275+
pipeline(new SrpStatusResult(pipeline.slaveof(host, port)));
2276+
return;
2277+
}
2278+
client.slaveof(host, port);
2279+
} catch (Exception e) {
2280+
throw convertSrpAccessException(e);
2281+
}
2282+
}
2283+
22652284
/*
22662285
* (non-Javadoc)
22672286
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
@@ -2295,6 +2314,23 @@ public List<RedisClientInfo> getClientList() {
22952314
return SrpConverters.toListOfRedisClientInformation(this.client.client_list());
22962315
}
22972316

2317+
/*
2318+
* @see org.springframework.data.redis.connection.RedisServerCommands#slaveOfNoOne()
2319+
*/
2320+
@Override
2321+
public void slaveOfNoOne() {
2322+
2323+
try {
2324+
if (isPipelined()) {
2325+
pipeline(new SrpStatusResult(pipeline.slaveof("NO", "ONE")));
2326+
return;
2327+
}
2328+
client.slaveof("NO", "ONE");
2329+
} catch (Exception e) {
2330+
throw convertSrpAccessException(e);
2331+
}
2332+
}
2333+
22982334
private List<Object> closeTransaction() {
22992335
List<Object> results = Collections.emptyList();
23002336
if (txTracker != null) {

src/main/java/org/springframework/data/redis/core/RedisOperations.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,20 @@ <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSer
303303
* @since 1.3
304304
*/
305305
void killClient(String host, int port);
306+
307+
/**
308+
* Change redis replication setting to new master.
309+
*
310+
* @param host
311+
* @param port
312+
* @since 1.3
313+
*/
314+
void slaveOf(String host, int port);
315+
316+
/**
317+
* Change server into master.
318+
*
319+
* @since 1.3
320+
*/
321+
void slaveOfNoOne();
306322
}

src/main/java/org/springframework/data/redis/core/RedisTemplate.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,4 +1019,38 @@ public List<RedisClientInfo> doInRedis(RedisConnection connection) throws DataAc
10191019
}
10201020
});
10211021
}
1022+
1023+
/*
1024+
* @see org.springframework.data.redis.core.RedisOperations#slaveOf(java.lang.String, int)
1025+
*/
1026+
@Override
1027+
public void slaveOf(final String host, final int port) {
1028+
1029+
execute(new RedisCallback<Void>() {
1030+
@Override
1031+
public Void doInRedis(RedisConnection connection) throws DataAccessException {
1032+
1033+
connection.slaveOf(host, port);
1034+
return null;
1035+
}
1036+
1037+
});
1038+
}
1039+
1040+
/*
1041+
* (non-Javadoc)
1042+
* @see org.springframework.data.redis.core.RedisOperations#slaveOfNoOne()
1043+
*/
1044+
@Override
1045+
public void slaveOfNoOne() {
1046+
1047+
execute(new RedisCallback<Void>() {
1048+
1049+
@Override
1050+
public Void doInRedis(RedisConnection connection) throws DataAccessException {
1051+
connection.slaveOfNoOne();
1052+
return null;
1053+
}
1054+
});
1055+
}
10221056
}

0 commit comments

Comments
 (0)