Skip to content

Commit cd33977

Browse files
author
李金松
committed
Implemented read-write separation based on JedisSentineled
1 parent 5a324c1 commit cd33977

File tree

12 files changed

+457
-139
lines changed

12 files changed

+457
-139
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@
199199
<scope>test</scope>
200200
</dependency>
201201

202+
<dependency>
203+
<groupId>org.powermock</groupId>
204+
<artifactId>powermock-module-junit4</artifactId>
205+
<version>2.0.2</version>
206+
<scope>test</scope>
207+
</dependency>
208+
202209
<!-- circuit breaker / failover -->
203210
<dependency>
204211
<groupId>io.github.resilience4j</groupId>
@@ -239,6 +246,13 @@
239246
</resource>
240247
</resources>
241248
<plugins>
249+
<plugin>
250+
<groupId>org.apache.maven.plugins</groupId>
251+
<artifactId>maven-surefire-plugin</artifactId>
252+
<configuration>
253+
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
254+
</configuration>
255+
</plugin>
242256
<plugin>
243257
<groupId>org.jacoco</groupId>
244258
<artifactId>jacoco-maven-plugin</artifactId>

src/main/java/redis/clients/jedis/JedisSentineled.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo
3737
masterClientConfig.getRedisProtocol());
3838
}
3939

40+
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig,
41+
final GenericObjectPoolConfig<Connection> poolConfig,
42+
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig, boolean fallbackToMaster) {
43+
super(new SentineledConnectionProvider(masterName, masterClientConfig, poolConfig, sentinels, sentinelClientConfig, fallbackToMaster),
44+
masterClientConfig.getRedisProtocol());
45+
}
46+
4047
@Experimental
4148
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache,
4249
final GenericObjectPoolConfig<Connection> poolConfig,

src/main/java/redis/clients/jedis/Protocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public final class Protocol {
2323

2424
public static final String DEFAULT_HOST = "127.0.0.1";
2525
public static final int DEFAULT_PORT = 6379;
26-
public static final int DEFAULT_SENTINEL_PORT = 26379;
26+
public static final int DEFAULT_SENTINEL_PORT = 26378;
2727
public static final int DEFAULT_TIMEOUT = 2000;
2828
public static final int DEFAULT_DATABASE = 0;
2929
public static final int CLUSTER_HASHSLOTS = 16384;

src/main/java/redis/clients/jedis/bloom/RedisBloomProtocol.java

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,113 +8,149 @@ public class RedisBloomProtocol {
88

99
public enum BloomFilterCommand implements ProtocolCommand {
1010

11-
RESERVE("BF.RESERVE"),
12-
ADD("BF.ADD"),
13-
MADD("BF.MADD"),
14-
EXISTS("BF.EXISTS"),
15-
MEXISTS("BF.MEXISTS"),
16-
INSERT("BF.INSERT"),
17-
SCANDUMP("BF.SCANDUMP"),
18-
LOADCHUNK("BF.LOADCHUNK"),
19-
CARD("BF.CARD"),
20-
INFO("BF.INFO");
11+
RESERVE("BF.RESERVE", true),
12+
ADD("BF.ADD", true),
13+
MADD("BF.MADD", true),
14+
EXISTS("BF.EXISTS", false),
15+
MEXISTS("BF.MEXISTS", false),
16+
INSERT("BF.INSERT", true),
17+
SCANDUMP("BF.SCANDUMP", true),
18+
LOADCHUNK("BF.LOADCHUNK", true),
19+
CARD("BF.CARD", false),
20+
INFO("BF.INFO", false);
2121

2222
private final byte[] raw;
23+
private final boolean isWriteCommand;
2324

24-
private BloomFilterCommand(String alt) {
25+
private BloomFilterCommand(String alt, boolean isWriteCommand) {
2526
raw = SafeEncoder.encode(alt);
27+
this.isWriteCommand = isWriteCommand;
2628
}
2729

2830
@Override
2931
public byte[] getRaw() {
3032
return raw;
3133
}
34+
35+
@Override
36+
public boolean isWriteCommand() {
37+
return isWriteCommand;
38+
}
3239
}
3340

3441
public enum CuckooFilterCommand implements ProtocolCommand {
3542

36-
RESERVE("CF.RESERVE"), //
37-
ADD("CF.ADD"), //
38-
ADDNX("CF.ADDNX"), //
39-
INSERT("CF.INSERT"), //
40-
INSERTNX("CF.INSERTNX"), //
41-
EXISTS("CF.EXISTS"), //
42-
MEXISTS("CF.MEXISTS"), //
43-
DEL("CF.DEL"), //
44-
COUNT("CF.COUNT"), //
45-
SCANDUMP("CF.SCANDUMP"), //
46-
LOADCHUNK("CF.LOADCHUNK"), //
47-
INFO("CF.INFO");
43+
RESERVE("CF.RESERVE", true), //
44+
ADD("CF.ADD", true), //
45+
ADDNX("CF.ADDNX", true), //
46+
INSERT("CF.INSERT", true), //
47+
INSERTNX("CF.INSERTNX", true), //
48+
EXISTS("CF.EXISTS", false), //
49+
MEXISTS("CF.MEXISTS", false), //
50+
DEL("CF.DEL", true), //
51+
COUNT("CF.COUNT", false), //
52+
SCANDUMP("CF.SCANDUMP", true), //
53+
LOADCHUNK("CF.LOADCHUNK", true), //
54+
INFO("CF.INFO", false);
4855

4956
private final byte[] raw;
57+
private final boolean isWriteCommand;
5058

51-
private CuckooFilterCommand(String alt) {
59+
private CuckooFilterCommand(String alt, boolean isWriteCommand) {
5260
raw = SafeEncoder.encode(alt);
61+
this.isWriteCommand = isWriteCommand;
5362
}
5463

5564
@Override
5665
public byte[] getRaw() {
5766
return raw;
5867
}
68+
69+
@Override
70+
public boolean isWriteCommand() {
71+
return isWriteCommand;
72+
}
5973
}
6074

6175
public enum CountMinSketchCommand implements ProtocolCommand {
6276

63-
INITBYDIM("CMS.INITBYDIM"), //
64-
INITBYPROB("CMS.INITBYPROB"), //
65-
INCRBY("CMS.INCRBY"), //
66-
QUERY("CMS.QUERY"), //
67-
MERGE("CMS.MERGE"), //
68-
INFO("CMS.INFO");
77+
INITBYDIM("CMS.INITBYDIM", true), //
78+
INITBYPROB("CMS.INITBYPROB", true), //
79+
INCRBY("CMS.INCRBY", true), //
80+
QUERY("CMS.QUERY", false), //
81+
MERGE("CMS.MERGE", true), //
82+
INFO("CMS.INFO", false);
6983

7084
private final byte[] raw;
85+
private final boolean isWriteCommand;
7186

72-
private CountMinSketchCommand(String alt) {
87+
private CountMinSketchCommand(String alt, boolean isWriteCommand) {
7388
raw = SafeEncoder.encode(alt);
89+
this.isWriteCommand = isWriteCommand;
7490
}
7591

7692
@Override
7793
public byte[] getRaw() {
7894
return raw;
7995
}
96+
@Override
97+
public boolean isWriteCommand() {
98+
return isWriteCommand;
99+
}
80100
}
81101

82102
public enum TopKCommand implements ProtocolCommand {
83103

84-
RESERVE("TOPK.RESERVE"),
85-
ADD("TOPK.ADD"),
86-
INCRBY("TOPK.INCRBY"),
87-
QUERY("TOPK.QUERY"),
88-
LIST("TOPK.LIST"),
89-
INFO("TOPK.INFO");
104+
RESERVE("TOPK.RESERVE", true),
105+
ADD("TOPK.ADD", true),
106+
INCRBY("TOPK.INCRBY", true),
107+
QUERY("TOPK.QUERY", false),
108+
LIST("TOPK.LIST", false),
109+
INFO("TOPK.INFO", false);
90110

91111
private final byte[] raw;
112+
private final boolean isWriteCommand;
92113

93-
private TopKCommand(String alt) {
114+
private TopKCommand(String alt, boolean isWriteCommand) {
94115
raw = SafeEncoder.encode(alt);
116+
this.isWriteCommand = isWriteCommand;
95117
}
96118

97119
@Override
98120
public byte[] getRaw() {
99121
return raw;
100122
}
123+
124+
@Override
125+
public boolean isWriteCommand() {
126+
return isWriteCommand;
127+
}
101128
}
102129

103130
public enum TDigestCommand implements ProtocolCommand {
104131

105-
CREATE, INFO, ADD, RESET, MERGE, CDF, QUANTILE, MIN, MAX, TRIMMED_MEAN,
106-
RANK, REVRANK, BYRANK, BYREVRANK;
132+
CREATE(true), INFO(false), ADD(true), RESET(true), MERGE(true), CDF(false),
133+
QUANTILE(false), MIN(false), MAX(false), TRIMMED_MEAN(false),
134+
RANK(false), REVRANK(false), BYRANK(false), BYREVRANK(false);
107135

108136
private final byte[] raw;
109137

110-
private TDigestCommand() {
138+
private final boolean isWriteCommand;
139+
140+
private TDigestCommand(boolean isWriteCommand) {
111141
raw = SafeEncoder.encode("TDIGEST." + name());
142+
this.isWriteCommand = isWriteCommand;
112143
}
113144

114145
@Override
115146
public byte[] getRaw() {
116147
return raw;
117148
}
149+
150+
@Override
151+
public boolean isWriteCommand() {
152+
return isWriteCommand;
153+
}
118154
}
119155

120156
public enum RedisBloomKeyword implements Rawable {

src/main/java/redis/clients/jedis/json/JsonProtocol.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,45 @@
66
public class JsonProtocol {
77

88
public enum JsonCommand implements ProtocolCommand {
9-
DEL("JSON.DEL"),
10-
GET("JSON.GET"),
11-
MGET("JSON.MGET"),
12-
MERGE("JSON.MERGE"),
13-
SET("JSON.SET"),
14-
TYPE("JSON.TYPE"),
15-
STRAPPEND("JSON.STRAPPEND"),
16-
STRLEN("JSON.STRLEN"),
17-
NUMINCRBY("JSON.NUMINCRBY"),
18-
ARRAPPEND("JSON.ARRAPPEND"),
19-
ARRINDEX("JSON.ARRINDEX"),
20-
ARRINSERT("JSON.ARRINSERT"),
21-
ARRLEN("JSON.ARRLEN"),
22-
ARRPOP("JSON.ARRPOP"),
23-
ARRTRIM("JSON.ARRTRIM"),
24-
CLEAR("JSON.CLEAR"),
25-
TOGGLE("JSON.TOGGLE"),
26-
OBJKEYS("JSON.OBJKEYS"),
27-
OBJLEN("JSON.OBJLEN"),
28-
DEBUG("JSON.DEBUG"),
29-
RESP("JSON.RESP");
9+
DEL("JSON.DEL", true),
10+
GET("JSON.GET", false),
11+
MGET("JSON.MGET", false),
12+
MERGE("JSON.MERGE", true),
13+
SET("JSON.SET", true),
14+
TYPE("JSON.TYPE", false),
15+
STRAPPEND("JSON.STRAPPEND", true),
16+
STRLEN("JSON.STRLEN", false),
17+
NUMINCRBY("JSON.NUMINCRBY", true),
18+
ARRAPPEND("JSON.ARRAPPEND", true),
19+
ARRINDEX("JSON.ARRINDEX", false),
20+
ARRINSERT("JSON.ARRINSERT", true),
21+
ARRLEN("JSON.ARRLEN", false),
22+
ARRPOP("JSON.ARRPOP", true),
23+
ARRTRIM("JSON.ARRTRIM", true),
24+
CLEAR("JSON.CLEAR", true),
25+
TOGGLE("JSON.TOGGLE", true),
26+
OBJKEYS("JSON.OBJKEYS", false),
27+
OBJLEN("JSON.OBJLEN", false),
28+
DEBUG("JSON.DEBUG", false),
29+
RESP("JSON.RESP", false);
3030

3131
private final byte[] raw;
3232

33-
private JsonCommand(String alt) {
33+
private final boolean isWriteCommand;
34+
35+
private JsonCommand(String alt, boolean isWriteCommand) {
3436
raw = SafeEncoder.encode(alt);
37+
this.isWriteCommand = isWriteCommand;
3538
}
3639

3740
@Override
3841
public byte[] getRaw() {
3942
return raw;
4043
}
44+
45+
@Override
46+
public boolean isWriteCommand() {
47+
return isWriteCommand;
48+
}
4149
}
4250
}

0 commit comments

Comments
 (0)