Skip to content

Commit 0e1d28a

Browse files
committed
code cleanup
1 parent fdc914a commit 0e1d28a

File tree

5 files changed

+113
-53
lines changed

5 files changed

+113
-53
lines changed

src/main/java/org/sourcelab/storm/spout/redis/RedisStreamSpoutConfig.java

Lines changed: 104 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static final class Builder {
185185
/**
186186
* Connection details.
187187
*/
188-
private final List<RedisServer> clusterServers = new ArrayList<>();
188+
private final List<RedisServer> clusterNodes = new ArrayList<>();
189189
private RedisServer redisServer = null;
190190

191191
/**
@@ -217,52 +217,94 @@ public static final class Builder {
217217
private Builder() {
218218
}
219219

220-
public Builder withServer(final RedisServer redisServer) {
221-
if (!clusterServers.isEmpty()) {
222-
// Cannot define both cluster servers and redis server instances.
223-
throw new IllegalArgumentException("TODO");
224-
}
225-
this.redisServer = Objects.requireNonNull(redisServer);
226-
return this;
227-
}
228-
229-
public Builder withServer(final String host, final int port, final String password) {
230-
return withServer(new RedisServer(host, port, password));
231-
}
232-
220+
/**
221+
* Define connection details for connecting to a single Redis server.
222+
*
223+
* NOTE: If you want to connect to a RedisCluster, {@link Builder#withClusterNode}.
224+
*
225+
* @param host Host of redis server to connect to.
226+
* @param port Port of redis server to connect to.
227+
* @return Builder.
228+
*/
233229
public Builder withServer(final String host, final int port) {
234230
return withServer(host, port, null);
235231
}
236232

237-
238-
public Builder withClusters(final RedisServer...clusterServers) {
239-
Arrays.stream(clusterServers)
240-
.forEach(this::withCluster);
241-
return this;
233+
/**
234+
* Define connection details for connecting to a single Redis server.
235+
*
236+
* NOTE: If you want to connect to a RedisCluster, {@link Builder#withClusterNode}.
237+
*
238+
* @param host Host of redis server to connect to.
239+
* @param port Port of redis server to connect to.
240+
* @param password (optional) Password for redis server, or NULL if no password required.
241+
* @return Builder.
242+
*/
243+
public Builder withServer(final String host, final int port, final String password) {
244+
return withServer(new RedisServer(host, port, password));
242245
}
243246

244-
public Builder withCluster(final RedisServer clusterServer) {
245-
if (redisServer != null) {
247+
/**
248+
* Define connection details for connecting to a single Redis server.
249+
*
250+
* NOTE: If you want to connect to a RedisCluster, {@link Builder#withClusterNode}.
251+
*
252+
* @param redisServer Defines a redis server to connect to.
253+
* @return Builder.
254+
*/
255+
private Builder withServer(final RedisServer redisServer) {
256+
if (!clusterNodes.isEmpty()) {
246257
// Cannot define both cluster servers and redis server instances.
247258
throw new IllegalArgumentException("TODO");
248259
}
249-
clusterServers.add(Objects.requireNonNull(clusterServer));
260+
this.redisServer = Objects.requireNonNull(redisServer);
250261
return this;
251262
}
252263

253-
public Builder withCluster(final String host, final int port, final String password) {
254-
return withCluster(new RedisServer(host, port, password));
264+
/**
265+
* Define connection details for connecting to a RedisCluster.
266+
* Call this method as many times as needed to add nodes in your cluster.
267+
*
268+
* NOTE: If you want to connect to a single redis instance, {@link Builder#withServer}.
269+
*
270+
* @param host Host of redis node.
271+
* @param port Port of redis node.
272+
* @return Builder.
273+
*/
274+
public Builder withClusterNode(final String host, final int port) {
275+
return withClusterNode(host, port, null);
255276
}
256277

257-
public Builder withCluster(final String host, final int port) {
258-
return withCluster(host, port, null);
278+
/**
279+
* Define connection details for connecting to a RedisCluster.
280+
* Call this method as many times as needed to add nodes in your cluster.
281+
*
282+
* NOTE: If you want to connect to a single redis instance, {@link Builder#withServer}.
283+
*
284+
* @param host Host of redis node.
285+
* @param port Port of redis node.
286+
* @param password (optional) Password for redis node, or NULL if no password required.
287+
* @return Builder.
288+
*/
289+
public Builder withClusterNode(final String host, final int port, final String password) {
290+
return withClusterNode(new RedisServer(host, port, password));
259291
}
260292

261-
public Builder withCluster(final RedisCluster redisCluster) {
262-
Objects.requireNonNull(redisCluster);
263-
264-
this.clusterServers.clear();
265-
this.clusterServers.addAll(redisCluster.getServers());
293+
/**
294+
* Define connection details for connecting to a RedisCluster.
295+
* Call this method as many times as needed to add nodes in your cluster.
296+
*
297+
* NOTE: If you want to connect to a single redis instance, {@link Builder#withServer}.
298+
*
299+
* @param node Defines a node in the RedisCluster.
300+
* @return Builder.
301+
*/
302+
private Builder withClusterNode(final RedisServer node) {
303+
if (redisServer != null) {
304+
// Cannot define both cluster servers and redis server instances.
305+
throw new IllegalArgumentException("TODO");
306+
}
307+
clusterNodes.add(Objects.requireNonNull(node));
266308
return this;
267309
}
268310

@@ -335,8 +377,8 @@ public Builder withMetricsEnabled(final boolean enabled) {
335377
*/
336378
public RedisStreamSpoutConfig build() {
337379
RedisCluster redisCluster = null;
338-
if (!clusterServers.isEmpty()) {
339-
redisCluster = new RedisCluster(clusterServers);
380+
if (!clusterNodes.isEmpty()) {
381+
redisCluster = new RedisCluster(clusterNodes);
340382
}
341383

342384
return new RedisStreamSpoutConfig(
@@ -354,13 +396,16 @@ public RedisStreamSpoutConfig build() {
354396
}
355397
}
356398

399+
/**
400+
* Defines a RedisCluster connection details.
401+
*/
357402
public static class RedisCluster {
358403
private final List<RedisServer> servers;
359404

360-
public RedisCluster(final RedisServer...servers) {
361-
this(Arrays.asList(servers));
362-
}
363-
405+
/**
406+
* Constructor.
407+
* @param servers One or more Nodes in the RedisCluster.
408+
*/
364409
public RedisCluster(final List<RedisServer> servers) {
365410
Objects.requireNonNull(servers);
366411
this.servers = Collections.unmodifiableList(new ArrayList<>(servers));
@@ -377,22 +422,40 @@ public String toString() {
377422
+ '}';
378423
}
379424

425+
/**
426+
* The URI for connecting to this RedisCluster.
427+
* @return URI for the cluster.
428+
*/
380429
public String getConnectString() {
381430
return getServers().stream()
382431
.map(RedisServer::getConnectString)
383432
.collect(Collectors.joining(","));
384433
}
385434
}
386435

436+
/**
437+
* Defines a Single RedisServer instance connection details.
438+
*/
387439
public static class RedisServer {
388440
private final String host;
389441
private final int port;
390442
private final String password;
391443

444+
/**
445+
* Constructor.
446+
* @param host hostname of redis server.
447+
* @param port port of redis server.
448+
*/
392449
public RedisServer(final String host, final int port) {
393450
this(host, port, null);
394451
}
395452

453+
/**
454+
* Constructor.
455+
* @param host hostname of redis server.
456+
* @param port port of redis server.
457+
* @param password (optional) password for server, or NULL if not required.
458+
*/
396459
public RedisServer(final String host, final int port, final String password) {
397460
this.host = host;
398461
this.port = port;
@@ -411,10 +474,10 @@ public String getPassword() {
411474
return password;
412475
}
413476

414-
public boolean hasPassword() {
415-
return password != null;
416-
}
417-
477+
/**
478+
* The URI for connecting to this Redis Server instance.
479+
* @return URI for the server.
480+
*/
418481
public String getConnectString() {
419482
String connectStr = "redis://";
420483

src/main/java/org/sourcelab/storm/spout/redis/client/LettuceClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public LettuceClient(final RedisStreamSpoutConfig config, final int instanceId)
5858
this(
5959
config,
6060
instanceId,
61-
config.isConnectingToCluster() ?
62-
new LettuceClusterClient(RedisClusterClient.create(config.getConnectString()))
61+
// Determine which adapter to use based on what type of redis instance we are
62+
// communicating with.
63+
config.isConnectingToCluster()
64+
? new LettuceClusterClient(RedisClusterClient.create(config.getConnectString()))
6365
: new LettuceRedisClient(RedisClient.create(config.getConnectString()))
6466
);
6567
}

src/main/java/org/sourcelab/storm/spout/redis/client/LettuceClusterClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.util.Objects;
88

99
/**
10-
*
10+
* Adapter for talking to a RedisCluster.
11+
* If you need to talk to a single Redis instance {@link LettuceRedisClient}.
1112
*/
1213
public class LettuceClusterClient implements LettuceAdapter {
1314
/**

src/main/java/org/sourcelab/storm/spout/redis/client/LettuceRedisClient.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import io.lettuce.core.RedisClient;
44
import io.lettuce.core.api.StatefulRedisConnection;
55
import io.lettuce.core.api.sync.RedisStreamCommands;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
86

97
import java.util.Objects;
108

119
/**
12-
* Adapter for RedisClient.
10+
* Adapter for talking to a single Redis instance.
11+
* If you need to talk to a RedisCluster {@link LettuceClusterClient}.
1312
*/
1413
public class LettuceRedisClient implements LettuceAdapter {
15-
private static final Logger logger = LoggerFactory.getLogger(LettuceRedisClient.class);
1614

1715
/**
1816
* The underlying Redis Client.

src/test/java/org/sourcelab/storm/spout/redis/util/test/RedisTestContainer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package org.sourcelab.storm.spout.redis.util.test;
22

3-
import org.jetbrains.annotations.NotNull;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
63
import org.sourcelab.storm.spout.redis.RedisStreamSpoutConfig;
74
import org.testcontainers.containers.FixedHostPortGenericContainer;
8-
import org.testcontainers.containers.GenericContainer;
95
import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy;
106

117
import java.time.Duration;
@@ -60,7 +56,7 @@ public String getConnectStr() {
6056

6157
public RedisStreamSpoutConfig.Builder addConnectionDetailsToConfig(final RedisStreamSpoutConfig.Builder builder) {
6258
if (isCluster) {
63-
builder.withCluster(getHost(), getPort());
59+
builder.withClusterNode(getHost(), getPort());
6460
} else {
6561
builder.withServer(getHost(), getPort());
6662
}

0 commit comments

Comments
 (0)