Skip to content

Commit

Permalink
RedisSentinelBuilder, RedisSentinel adder
Browse files Browse the repository at this point in the history
  • Loading branch information
turu committed Jan 22, 2015
1 parent b5916b1 commit 98c124f
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 14 deletions.
11 changes: 2 additions & 9 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ abstract class AbstractRedisInstance implements RedisInstance {
private volatile boolean active = false;
private Process redisProcess;

public static RedisServerBuilder builder() {
return new RedisServerBuilder();
}

@Override
public boolean isActive() {
return active;
Expand All @@ -36,9 +32,8 @@ public synchronized void start() throws IOException {
}

private void awaitRedisServerReady() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()));
try {
String outputLine = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()))) {
String outputLine;
do {
outputLine = reader.readLine();

Expand All @@ -47,8 +42,6 @@ private void awaitRedisServerReady() throws IOException {
throw new RuntimeException("Can't start redis server. Check logs for details.");
}
} while (!outputLine.matches(REDIS_READY_PATTERN));
} finally {
reader.close();
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/redis/embedded/RedisSentinel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package redis.embedded;

import java.util.ArrayList;
import java.util.List;

/**
* Created by piotrturek on 22/01/15.
*/
public class RedisSentinel extends AbstractRedisInstance {
public RedisSentinel(List<String> args) {
this.args = new ArrayList<>(args);
}

public static RedisSentinelBuilder builder() { return new RedisSentinelBuilder(); }
}
143 changes: 143 additions & 0 deletions src/main/java/redis/embedded/RedisSentinelBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package redis.embedded;

import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import redis.embedded.util.JarUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
* Created by piotrturek on 22/01/15.
*/
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 1";
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 = "parallel-syncs %s %d";

private File executable;
private Integer port;
private int masterPort = 6379;
private String masterName = "mymaster";
private long downAfterMilliseconds = 60000L;
private long failoverTimeout = 180000L;
private int parallelSyncs = 1;
private String sentinelConf;

private StringBuilder redisConfigBuilder;

public RedisSentinelBuilder executable(File executable) {
this.executable = executable;
return this;
}

public RedisSentinelBuilder executable(String executable) {
this.executable = new File(executable);
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 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 RuntimeException("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 RuntimeException("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() throws IOException {
if (sentinelConf == null) {
resolveSentinelConf();
}
if (executable == null) {
executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
}

List<String> args = buildCommandArgs();
return new RedisSentinel(args);
}

private void resolveSentinelConf() throws IOException {
if (redisConfigBuilder == null) {
useDefaultConfig();
}
final String configString = redisConfigBuilder.toString();

File redisConfigFile = File.createTempFile(CONF_FILENAME, ".conf");
redisConfigFile.deleteOnExit();
Files.write(configString, redisConfigFile, Charset.forName("UTF-8"));
sentinelConf = redisConfigFile.getAbsolutePath();
}

private void useDefaultConfig() {
setting(String.format(MASTER_MONITOR_LINE, masterName, masterPort));
setting(String.format(DOWN_AFTER_LINE, masterName, downAfterMilliseconds));
setting(String.format(FAILOVER_LINE, masterName, failoverTimeout));
setting(String.format(PARALLEL_SYNCS_LINE, masterName, parallelSyncs));
}

private List<String> buildCommandArgs() {
Preconditions.checkNotNull(sentinelConf);

List<String> args = new ArrayList<>();
args.add(sentinelConf);
args.add("--sentinel");
args.add(executable.getAbsolutePath());

if (port != null) {
args.add("--port");
args.add(Integer.toString(port));
}

return args;
}
}
7 changes: 5 additions & 2 deletions src/main/java/redis/embedded/RedisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import java.util.List;

public class RedisServer extends AbstractRedisInstance {
RedisServer(Integer port) throws IOException {
public RedisServer(Integer port) throws IOException {
File executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
);
}

RedisServer(File executable, Integer port) {
public RedisServer(File executable, Integer port) {
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
Expand All @@ -28,4 +28,7 @@ public class RedisServer extends AbstractRedisInstance {
this.args = new ArrayList<>(args);
}

public static RedisServerBuilder builder() {
return new RedisServerBuilder();
}
}
10 changes: 7 additions & 3 deletions src/main/java/redis/embedded/RedisServerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public class RedisServerBuilder {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String CONF_FILENAME = "embedded-redis-server";

private File executable;
private Integer port;
Expand Down Expand Up @@ -73,7 +74,7 @@ public RedisServerBuilder setting(String configLine) {

public RedisServer build() throws IOException {
if (redisConf == null && redisConfigBuilder != null) {
File redisConfigFile = File.createTempFile("embedded-redis", ".conf");
File redisConfigFile = File.createTempFile(CONF_FILENAME, ".conf");
redisConfigFile.deleteOnExit();
Files.write(redisConfigBuilder.toString(), redisConfigFile, Charset.forName("UTF-8"));
redisConf = redisConfigFile.getAbsolutePath();
Expand All @@ -87,8 +88,11 @@ public RedisServer build() throws IOException {
return new RedisServer(args);
}

private List<String> buildCommandArgs() {
List<String> args = new ArrayList<String>();
private List<String> buildCommandArgs() throws IOException {
List<String> args = new ArrayList<>();
if (executable == null) {
executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
}
args.add(executable.getAbsolutePath());

if (!Strings.isNullOrEmpty(redisConf)) {
Expand Down

0 comments on commit 98c124f

Please sign in to comment.