forked from kstyrc/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisSentinelBuilder.java
More file actions
158 lines (130 loc) · 5.16 KB
/
RedisSentinelBuilder.java
File metadata and controls
158 lines (130 loc) · 5.16 KB
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
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.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 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;
public RedisSentinelBuilder redisExecProvider(RedisExecProvider redisExecProvider) {
this.redisExecProvider = redisExecProvider;
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;
}
public RedisSentinel build() {
tryResolveConfAndExec();
List<String> args = buildCommandArgs();
return new RedisSentinel(args, port);
}
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(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;
}
}