forked from ozimov/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redis building exception added; redis cluster builder implemented; re…
…dis sentinel builder made resettable; redis server builder made resettable; basic sentinel test;
- Loading branch information
Showing
6 changed files
with
207 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package redis.embedded; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Created by piotrturek on 22/01/15. | ||
*/ | ||
public class RedisClusterBuilder { | ||
private RedisSentinelBuilder sentinelBuilder = new RedisSentinelBuilder(); | ||
private RedisServerBuilder serverBuilder = new RedisServerBuilder(); | ||
private int sentinelCount = 1; | ||
private int quorumSize = 1; | ||
private int currentSentinelPort = 26379; | ||
private int currentReplicationGroupPort = 6379; | ||
private final List<ReplicationGroup> groups = new LinkedList<>(); | ||
|
||
public RedisClusterBuilder withSentinelBuilder(RedisSentinelBuilder sentinelBuilder) { | ||
this.sentinelBuilder = sentinelBuilder; | ||
return this; | ||
} | ||
|
||
public RedisClusterBuilder withServerBuilder(RedisServerBuilder serverBuilder) { | ||
this.serverBuilder = serverBuilder; | ||
return this; | ||
} | ||
|
||
public RedisClusterBuilder sentinelCount(int sentinelCount) { | ||
this.sentinelCount = sentinelCount; | ||
return this; | ||
} | ||
|
||
public RedisClusterBuilder quorumSize(int quorumSize) { | ||
this.quorumSize = quorumSize; | ||
return this; | ||
} | ||
|
||
public RedisClusterBuilder replicationGroup(String masterName, int slaveCount) { | ||
this.groups.add(new ReplicationGroup(masterName, slaveCount, nextReplicationGroupPort(slaveCount))); | ||
return this; | ||
} | ||
|
||
public RedisCluster build() { | ||
final List<Redis> sentinels = buildSentinels(); | ||
final List<Redis> servers = buildServers(); | ||
return new RedisCluster(sentinels, servers); | ||
} | ||
|
||
private List<Redis> buildServers() { | ||
return groups.stream().flatMap(g -> { | ||
final Stream.Builder<Redis> builder = Stream.builder(); | ||
builder.accept(buildMaster(g)); | ||
buildSlaves(builder, g); | ||
return builder.build(); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
private void buildSlaves(Stream.Builder<Redis> builder, ReplicationGroup g) { | ||
for (Integer slavePort : g.slavePorts) { | ||
serverBuilder.reset(); | ||
serverBuilder.port(slavePort); | ||
serverBuilder.slaveOf("localhost", g.masterPort); | ||
final RedisServer slave = serverBuilder.build(); | ||
builder.accept(slave); | ||
} | ||
} | ||
|
||
private Redis buildMaster(ReplicationGroup g) { | ||
serverBuilder.reset(); | ||
return serverBuilder.port(g.masterPort).build(); | ||
} | ||
|
||
private List<Redis> buildSentinels() { | ||
int toBuild = this.sentinelCount; | ||
final List<Redis> sentinels = new LinkedList<>(); | ||
while (toBuild-- > 0) { | ||
sentinels.add(buildSentinel()); | ||
} | ||
return sentinels; | ||
} | ||
|
||
private Redis buildSentinel() { | ||
sentinelBuilder.reset(); | ||
sentinelBuilder.port(nextSentinelPort()); | ||
groups.stream().forEach(g -> { | ||
sentinelBuilder.masterName(g.masterName); | ||
sentinelBuilder.masterPort(g.masterPort); | ||
sentinelBuilder.quorumSize(quorumSize); | ||
sentinelBuilder.addDefaultReplicationGroup(); | ||
}); | ||
return sentinelBuilder.build(); | ||
} | ||
|
||
private int nextSentinelPort() { | ||
return currentSentinelPort++; | ||
} | ||
|
||
private int nextReplicationGroupPort(int slaveCount) { | ||
final int toReturn = this.currentReplicationGroupPort; | ||
currentReplicationGroupPort += slaveCount + 1; | ||
return toReturn; | ||
} | ||
|
||
private static class ReplicationGroup { | ||
private final int slaveCount; | ||
private final String masterName; | ||
private final int masterPort; | ||
private final List<Integer> slavePorts = new LinkedList<>(); | ||
|
||
private ReplicationGroup(String masterName, int slaveCount, int portsStart) { | ||
this.masterName = masterName; | ||
this.slaveCount = slaveCount; | ||
masterPort = portsStart; | ||
while (slaveCount-- > 0) { | ||
portsStart += 1; | ||
slavePorts.add(portsStart); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/redis/embedded/exceptions/RedisBuildingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package redis.embedded.exceptions; | ||
|
||
/** | ||
* Created by piotrturek on 26/01/15. | ||
*/ | ||
public class RedisBuildingException extends RuntimeException { | ||
public RedisBuildingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters