Skip to content

Commit bef8318

Browse files
authored
fix: Remove the loop condition in the test code to fix CI (#3)
* [NEW] Added maven-ci.yml [NEW] Added WatcherConstant.java [NEW] Added LettuceRedisWatcher.java [NEW] Added LettuceSubscriber.java [NEW] Added LettuceSubThread.java [NEW] Added LettuceRedisWatcherTest.java [UPDATE] Updated .gitignore [NEW] Added .releaserc.json [NEW] Added maven-settings.xml [NEW] Added pom.xml [UPDATE] Updated README.md * Refactor LettuceRedisWatcherTest.java to use local IP address instead of remote IP address for testing. * Update LettuceRedisWatcherTest.java to simplify watcher initialization and update logic. * Refactor Redis connection initialization and add support for Redis Cluster. - Add Redis URI constants. - Update Redis connection initialization in LettuceRedisWatcher. - Add constructors for different Redis connection types. - Add support for Redis Cluster in LettuceRedisWatcher. - Update LettuceRedisWatcherTest to test Redis Cluster connection. * Update LettuceRedisWatcherTest.java to use localhost as the redis server instead of the specific IP address. * Update node version from 16 to 18 in maven-ci.yml and add new semantic.yml. * Refactor semantic.yml to disable validation for the PR title and all the commits.
1 parent 178bebf commit bef8318

File tree

5 files changed

+87
-46
lines changed

5 files changed

+87
-46
lines changed

.github/semantic.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Always validate the PR title AND all the commits
2+
titleAndCommits: false

.github/workflows/maven-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: Set up Node.js
4646
uses: actions/setup-node@v2
4747
with:
48-
node-version: 16
48+
node-version: 18
4949

5050
- name: Sematic Release
5151
run: |

src/main/java/org/casbin/watcher/lettuce/LettuceRedisWatcher.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package org.casbin.watcher.lettuce;
22

3-
import io.lettuce.core.AbstractRedisClient;
4-
import io.lettuce.core.ClientOptions;
5-
import io.lettuce.core.RedisClient;
6-
import io.lettuce.core.RedisURI;
3+
import io.lettuce.core.*;
74
import io.lettuce.core.cluster.ClusterClientOptions;
5+
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
86
import io.lettuce.core.cluster.RedisClusterClient;
7+
import io.lettuce.core.cluster.RedisClusterURIUtil;
98
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
109
import io.lettuce.core.resource.ClientResources;
1110
import io.lettuce.core.resource.DefaultClientResources;
1211
import org.apache.commons.lang3.StringUtils;
1312
import org.casbin.jcasbin.persist.Watcher;
1413
import org.casbin.watcher.lettuce.constants.WatcherConstant;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1516

17+
import java.net.URI;
1618
import java.time.Duration;
1719
import java.time.temporal.ChronoUnit;
20+
import java.util.List;
1821
import java.util.UUID;
1922
import java.util.function.Consumer;
2023

2124
public class LettuceRedisWatcher implements Watcher {
25+
private static final Logger logger = LoggerFactory.getLogger(LettuceRedisWatcher.class);
2226
private final String localId;
2327
private final String redisChannelName;
2428
private final AbstractRedisClient abstractRedisClient;
@@ -33,10 +37,9 @@ public class LettuceRedisWatcher implements Watcher {
3337
* @param redisChannelName Redis Channel
3438
* @param timeout Redis Timeout
3539
* @param password Redis Password
36-
* @param type Redis Type (standalone | cluster)
3740
*/
38-
public LettuceRedisWatcher(String redisIp, int redisPort, String redisChannelName, int timeout, String password, String type) {
39-
this.abstractRedisClient = this.getLettuceRedisClient(redisIp, redisPort, password, timeout, type);
41+
public LettuceRedisWatcher(String redisIp, Integer redisPort, String redisChannelName, int timeout, String password) {
42+
this.abstractRedisClient = this.getLettuceRedisClient(redisIp, redisPort, null, password, timeout, WatcherConstant.LETTUCE_REDIS_TYPE_STANDALONE);
4043
this.localId = UUID.randomUUID().toString();
4144
this.redisChannelName = redisChannelName;
4245
this.startSub();
@@ -48,10 +51,24 @@ public LettuceRedisWatcher(String redisIp, int redisPort, String redisChannelNam
4851
* @param redisIp Redis IP
4952
* @param redisPort Redis Port
5053
* @param redisChannelName Redis Channel
51-
* @param type Redis Type (standalone | cluster)
5254
*/
53-
public LettuceRedisWatcher(String redisIp, int redisPort, String redisChannelName, String type) {
54-
this(redisIp, redisPort, redisChannelName, 2000, null, type);
55+
public LettuceRedisWatcher(String redisIp, Integer redisPort, String redisChannelName) {
56+
this(redisIp, redisPort, redisChannelName, 2000, null);
57+
}
58+
59+
/**
60+
* Constructor
61+
*
62+
* @param nodes Redis Nodes
63+
* @param redisChannelName Redis Channel
64+
* @param timeout Redis Timeout
65+
* @param password Redis Password
66+
*/
67+
public LettuceRedisWatcher(String nodes, String redisChannelName, Integer timeout, String password) {
68+
this.abstractRedisClient = this.getLettuceRedisClient(null, null, nodes, password, timeout, WatcherConstant.LETTUCE_REDIS_TYPE_CLUSTER);
69+
this.localId = UUID.randomUUID().toString();
70+
this.redisChannelName = redisChannelName;
71+
this.startSub();
5572
}
5673

5774
@Override
@@ -86,37 +103,38 @@ private void startSub() {
86103
*
87104
* @param host Redis Host
88105
* @param port Redis Port
106+
* @param nodes Redis Nodes
89107
* @param password Redis Password
90108
* @param timeout Redis Timeout
91109
* @param type Redis Type (standalone | cluster) default:standalone
92110
* @return AbstractRedisClient
93111
*/
94-
private AbstractRedisClient getLettuceRedisClient(String host, int port, String password, int timeout, String type) {
112+
private AbstractRedisClient getLettuceRedisClient(String host, Integer port, String nodes, String password, int timeout, String type) {
95113
// todo default standalone ?
96114
// type = StringUtils.isEmpty(type) ? WatcherConstant.LETTUCE_REDIS_TYPE_STANDALONE : type;
97115
if (StringUtils.isNotEmpty(type) && StringUtils.equalsAnyIgnoreCase(type,
98116
WatcherConstant.LETTUCE_REDIS_TYPE_STANDALONE, WatcherConstant.LETTUCE_REDIS_TYPE_CLUSTER)) {
99-
RedisURI redisUri = null;
100-
if (StringUtils.isNotEmpty(password)) {
101-
redisUri = RedisURI.builder()
102-
.withHost(host)
103-
.withPort(port)
104-
.withPassword(password.toCharArray())
105-
.withTimeout(Duration.of(timeout, ChronoUnit.SECONDS))
106-
.build();
107-
} else {
108-
redisUri = RedisURI.builder()
109-
.withHost(host)
110-
.withPort(port)
111-
.withTimeout(Duration.of(timeout, ChronoUnit.SECONDS))
112-
.build();
113-
}
114117
ClientResources clientResources = DefaultClientResources.builder()
115118
.ioThreadPoolSize(4)
116119
.computationThreadPoolSize(4)
117120
.build();
118121
if (StringUtils.equalsIgnoreCase(type, WatcherConstant.LETTUCE_REDIS_TYPE_STANDALONE)) {
119122
// standalone
123+
RedisURI redisUri = null;
124+
if (StringUtils.isNotEmpty(password)) {
125+
redisUri = RedisURI.builder()
126+
.withHost(host)
127+
.withPort(port)
128+
.withPassword(password.toCharArray())
129+
.withTimeout(Duration.of(timeout, ChronoUnit.SECONDS))
130+
.build();
131+
} else {
132+
redisUri = RedisURI.builder()
133+
.withHost(host)
134+
.withPort(port)
135+
.withTimeout(Duration.of(timeout, ChronoUnit.SECONDS))
136+
.build();
137+
}
120138
ClientOptions clientOptions = ClientOptions.builder()
121139
.autoReconnect(true)
122140
.pingBeforeActivateConnection(true)
@@ -126,12 +144,26 @@ private AbstractRedisClient getLettuceRedisClient(String host, int port, String
126144
return redisClient;
127145
} else {
128146
// cluster
147+
TimeoutOptions timeoutOptions = TimeoutOptions.builder().fixedTimeout(Duration.of(timeout, ChronoUnit.SECONDS)).build();
148+
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
149+
.enablePeriodicRefresh(Duration.of(10, ChronoUnit.MINUTES))
150+
.enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
151+
.adaptiveRefreshTriggersTimeout(Duration.of(30, ChronoUnit.SECONDS))
152+
.build();
129153
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
130154
.autoReconnect(true)
155+
.timeoutOptions(timeoutOptions)
156+
.topologyRefreshOptions(topologyRefreshOptions)
131157
.pingBeforeActivateConnection(true)
132158
.validateClusterNodeMembership(true)
133159
.build();
134-
RedisClusterClient redisClusterClient = RedisClusterClient.create(clientResources, redisUri);
160+
// Redis Cluster Node
161+
String redisUri = StringUtils.isNotEmpty(password) ?
162+
WatcherConstant.REDIS_URI_PREFIX.concat(password).concat(WatcherConstant.REDIS_URI_PASSWORD_SPLIT).concat(nodes) :
163+
WatcherConstant.REDIS_URI_PREFIX.concat(nodes);
164+
logger.info("Redis Cluster Uri: {}", redisUri);
165+
List<RedisURI> redisURIList = RedisClusterURIUtil.toRedisURIs(URI.create(redisUri));
166+
RedisClusterClient redisClusterClient = RedisClusterClient.create(clientResources, redisURIList);
135167
redisClusterClient.setOptions(clusterClientOptions);
136168
return redisClusterClient;
137169
}

src/main/java/org/casbin/watcher/lettuce/constants/WatcherConstant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ public class WatcherConstant {
1616
*/
1717
public static final String LETTUCE_REDIS_TYPE_STANDALONE = "standalone";
1818
public static final String LETTUCE_REDIS_TYPE_CLUSTER = "cluster";
19+
20+
/**
21+
* Redis URI
22+
*/
23+
public static final String REDIS_URI_PREFIX = "redis://";
24+
public static final String REDIS_URI_PASSWORD_SPLIT = "@";
1925
}

src/test/java/org/casbin/test/LettuceRedisWatcherTest.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,47 @@ public class LettuceRedisWatcherTest {
1818
@Before
1919
public void initWatcher() {
2020
String redisTopic = "jcasbin-topic";
21-
this.lettuceRedisWatcher = new LettuceRedisWatcher("127.0.0.1", 6379, redisTopic, 2000, "foobared", "standalone");
21+
this.lettuceRedisWatcher = new LettuceRedisWatcher("127.0.0.1", 6379, redisTopic, 2000, "foobared");
22+
Enforcer enforcer = new Enforcer();
23+
enforcer.setWatcher(this.lettuceRedisWatcher);
24+
}
25+
26+
public void initClusterWatcher() {
27+
String redisTopic = "jcasbin-topic";
28+
// modify your cluster nodes
29+
this.lettuceRedisWatcher = new LettuceRedisWatcher("192.168.1.234:6380,192.168.1.234:6381,192.168.1.234:6382", redisTopic, 2000, "123456");
2230
Enforcer enforcer = new Enforcer();
2331
enforcer.setWatcher(this.lettuceRedisWatcher);
2432
}
2533

2634
@Test
2735
public void testUpdate() throws InterruptedException {
28-
this.initWatcher();
36+
// this.initClusterWatcher();
2937
this.lettuceRedisWatcher.update();
3038
Thread.sleep(100);
3139
}
3240

3341
@Test
3442
public void testConsumerCallback() throws InterruptedException {
35-
this.initWatcher();
36-
while (true) {
37-
this.lettuceRedisWatcher.setUpdateCallback((s) -> System.out.println(s));
38-
this.lettuceRedisWatcher.update();
39-
Thread.sleep(500);
40-
}
43+
// this.initClusterWatcher();
44+
// while (true) {
45+
this.lettuceRedisWatcher.setUpdateCallback((s) -> System.out.println(s));
46+
this.lettuceRedisWatcher.update();
47+
Thread.sleep(100);
48+
// }
4149
}
4250

4351
@Test
4452
public void testConnectWatcherWithoutPassword() {
4553
String redisTopic = "jcasbin-topic";
46-
LettuceRedisWatcher lettuceRedisWatcherWithoutPassword = new LettuceRedisWatcher("127.0.0.1", 6378, redisTopic, "standalone");
54+
LettuceRedisWatcher lettuceRedisWatcherWithoutPassword = new LettuceRedisWatcher("127.0.0.1", 6378, redisTopic);
4755
Assert.assertNotNull(lettuceRedisWatcherWithoutPassword);
4856
}
4957

5058
@Test
51-
public void testConnectWatcherWithType() {
59+
public void testConnectWatcherCluster() {
5260
String redisTopic = "jcasbin-topic";
53-
Assert.assertThrows(IllegalArgumentException.class, () -> {
54-
new LettuceRedisWatcher("127.0.0.1", 6378, redisTopic, "sentinel");
55-
});
56-
57-
LettuceRedisWatcher lettuceRedisWatcherStandalone = new LettuceRedisWatcher("127.0.0.1", 6378, redisTopic, "standalone");
58-
Assert.assertNotNull(lettuceRedisWatcherStandalone);
59-
60-
LettuceRedisWatcher lettuceRedisWatcherCluster = new LettuceRedisWatcher("127.0.0.1", 6378, redisTopic, "cluster");
61+
LettuceRedisWatcher lettuceRedisWatcherCluster = new LettuceRedisWatcher("127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382", redisTopic, 2000, null);
6162
Assert.assertNotNull(lettuceRedisWatcherCluster);
6263
}
6364
}

0 commit comments

Comments
 (0)