forked from kstyrc/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathRedisSentinelBuilder.java
197 lines (166 loc) · 6.5 KB
/
RedisSentinelBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package redis.embedded;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import redis.embedded.exceptions.RedisBuildingException;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class RedisSentinelBuilder {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String CONF_FILENAME = "embedded-redis-sentinel";
private static final String MASTER_MONITOR_LINE = "sentinel monitor %s 127.0.0.1 %d %d";
private static final String DOWN_AFTER_LINE = "sentinel down-after-milliseconds %s %d";
private static final String FAILOVER_LINE = "sentinel failover-timeout %s %d";
private static final String PARALLEL_SYNCS_LINE = "sentinel parallel-syncs %s %d";
private static final String PORT_LINE = "port %d";
private File executable;
private RedisExecProvider redisExecProvider = RedisExecProvider.defaultProvider();
private String bind="127.0.0.1";
private Integer port = 26379;
private int masterPort = 6379;
private String masterName = "mymaster";
private long downAfterMilliseconds = 60000L;
private long failoverTimeout = 180000L;
private int parallelSyncs = 1;
private int quorumSize = 1;
private String sentinelConf;
private StringBuilder redisConfigBuilder;
private PrintStream out = null; //Ignore Redis output.
private PrintStream err = System.err; //Forward Redis error messages to STDERR.
public RedisSentinelBuilder redisExecProvider(RedisExecProvider redisExecProvider) {
this.redisExecProvider = redisExecProvider;
return this;
}
public RedisSentinelBuilder bind(String bind) {
this.bind = bind;
return this;
}
public RedisSentinelBuilder port(Integer port) {
this.port = port;
return this;
}
public RedisSentinelBuilder masterPort(Integer masterPort) {
this.masterPort = masterPort;
return this;
}
public RedisSentinelBuilder masterName(String masterName) {
this.masterName = masterName;
return this;
}
public RedisSentinelBuilder quorumSize(int quorumSize) {
this.quorumSize = quorumSize;
return this;
}
public RedisSentinelBuilder downAfterMilliseconds(Long downAfterMilliseconds) {
this.downAfterMilliseconds = downAfterMilliseconds;
return this;
}
public RedisSentinelBuilder failoverTimeout(Long failoverTimeout) {
this.failoverTimeout = failoverTimeout;
return this;
}
public RedisSentinelBuilder parallelSyncs(int parallelSyncs) {
this.parallelSyncs = parallelSyncs;
return this;
}
public RedisSentinelBuilder configFile(String redisConf) {
if (redisConfigBuilder != null) {
throw new RedisBuildingException("Redis configuration is already partially build using setting(String) method!");
}
this.sentinelConf = redisConf;
return this;
}
public RedisSentinelBuilder setting(String configLine) {
if (sentinelConf != null) {
throw new RedisBuildingException("Redis configuration is already set using redis conf file!");
}
if (redisConfigBuilder == null) {
redisConfigBuilder = new StringBuilder();
}
redisConfigBuilder.append(configLine);
redisConfigBuilder.append(LINE_SEPARATOR);
return this;
}
/**
* Redirect Redis server messages sent to STDOUT.
* Usage example:
* @code{
* RedisServerBuilder serverBuilder = RedisServer.builder().outTo(System.out).errTo(System.err);
RedisSentinelBuilder sentinelBuilder = new RedisSentinelBuilder().outTo(System.out).errTo(System.err);
RedisCluster.builder().withServerBuilder(serverBuilder).withSentinelBuilder(sentinelBuilder)... }
* @param out Stream to redirect Redis output to.
* @return {@code this}
*/
public RedisSentinelBuilder outTo(PrintStream out) {
this.out = out;
return this;
}
/**
* Redirect Redis server messages sent to STDERR.
* @param err Stream to redirect Redis output to.
* Usage example: @code{builder.errTo(System.err);}
* @return {@code itself}
*/
public RedisSentinelBuilder errTo(PrintStream err) {
this.err = err;
return this;
}
public RedisSentinel build() {
tryResolveConfAndExec();
List<String> args = buildCommandArgs();
RedisSentinel sentinel = new RedisSentinel(args, port);
sentinel.outTo(out);
sentinel.errTo(err);
return sentinel;
}
private void tryResolveConfAndExec() {
try {
if (sentinelConf == null) {
resolveSentinelConf();
}
executable = redisExecProvider.get();
} catch (Exception e) {
throw new RedisBuildingException("Could not build sentinel instance", e);
}
}
public void reset() {
this.redisConfigBuilder = null;
this.sentinelConf = null;
}
public void addDefaultReplicationGroup() {
setting(String.format(MASTER_MONITOR_LINE, masterName, masterPort, quorumSize));
setting(String.format(DOWN_AFTER_LINE, masterName, downAfterMilliseconds));
setting(String.format(FAILOVER_LINE, masterName, failoverTimeout));
setting(String.format(PARALLEL_SYNCS_LINE, masterName, parallelSyncs));
}
private void resolveSentinelConf() throws IOException {
if (redisConfigBuilder == null) {
addDefaultReplicationGroup();
}
setting("bind "+bind);
setting(String.format(PORT_LINE, port));
final String configString = redisConfigBuilder.toString();
File redisConfigFile = File.createTempFile(resolveConfigName(), ".conf");
redisConfigFile.deleteOnExit();
Files.write(configString, redisConfigFile, Charset.forName("UTF-8"));
sentinelConf = redisConfigFile.getAbsolutePath();
}
private String resolveConfigName() {
return CONF_FILENAME + "_" + port;
}
private List<String> buildCommandArgs() {
Preconditions.checkNotNull(sentinelConf);
List<String> args = new ArrayList<String>();
args.add(executable.getAbsolutePath());
args.add(sentinelConf);
args.add("--sentinel");
if (port != null) {
args.add("--port");
args.add(Integer.toString(port));
}
return args;
}
}