-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from thingsboard/bugfix/jedis-cluster-topolog…
…y-refresh Added Redis cluster topology refresh options for Jedis implementation
- Loading branch information
Showing
14 changed files
with
337 additions
and
68 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
59 changes: 59 additions & 0 deletions
59
common/cache/src/main/java/org/thingsboard/mqtt/broker/cache/JedisClusterNodesUtil.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,59 @@ | ||
/** | ||
* Copyright © 2016-2024 The Thingsboard Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.thingsboard.mqtt.broker.cache; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.connection.RedisNode; | ||
|
||
@Slf4j | ||
public class JedisClusterNodesUtil { | ||
|
||
public static RedisNode parseClusterNodeLine(String line) { | ||
String[] parts = line.split(" "); | ||
if (parts.length < 8) { | ||
throw new IllegalArgumentException("Invalid cluster node line format: " + line); | ||
} | ||
try { | ||
// Node ID | ||
String id = parts[0]; | ||
|
||
// Extract host and port from <ip:port@cport> | ||
String[] hostPort = parts[1].split(":"); | ||
String host = hostPort[0]; | ||
int port = Integer.parseInt(hostPort[1].split("@")[0]); | ||
|
||
// Flags to determine node type | ||
String flags = parts[2]; | ||
RedisNode.NodeType type = flags.contains("master") ? | ||
RedisNode.NodeType.MASTER : RedisNode.NodeType.REPLICA; | ||
|
||
RedisNode.RedisNodeBuilder redisNodeBuilder = RedisNode.newRedisNode() | ||
.listeningAt(host, port) | ||
.withId(id) | ||
.promotedAs(type); | ||
|
||
String masterId = parts[3]; | ||
boolean masterIdUnknown = "-".equals(masterId); | ||
if (masterIdUnknown) { | ||
return redisNodeBuilder.build(); | ||
} | ||
return redisNodeBuilder.replicaOf(masterId).build(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error parsing cluster node line: " + line, e); | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
.../cache/src/main/java/org/thingsboard/mqtt/broker/cache/JedisClusterTopologyRefresher.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,96 @@ | ||
/** | ||
* Copyright © 2016-2024 The Thingsboard Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.thingsboard.mqtt.broker.cache; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.redis.connection.RedisClusterConfiguration; | ||
import org.springframework.data.redis.connection.RedisNode; | ||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import redis.clients.jedis.Jedis; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
// TODO: replace jedis from TBMQ implementation and use only lettuce. | ||
@Slf4j | ||
@Component | ||
@Profile("!install") | ||
@RequiredArgsConstructor | ||
@ConditionalOnExpression("'cluster'.equals('${redis.connection.type}') and 'true'.equals('${jedis.cluster.topology-refresh.enabled}')") | ||
public class JedisClusterTopologyRefresher { | ||
|
||
private final JedisConnectionFactory factory; | ||
|
||
@Scheduled(initialDelayString = "${jedis.cluster.topology-refresh.period}", fixedDelayString = "${jedis.cluster.topology-refresh.period}", timeUnit = TimeUnit.SECONDS) | ||
public void refreshTopology() { | ||
if (!factory.isRedisClusterAware()) { | ||
log.trace("Redis cluster configuration is not set!"); | ||
return; | ||
} | ||
try { | ||
RedisClusterConfiguration clusterConfig = factory.getClusterConfiguration(); | ||
Set<RedisNode> currentNodes = clusterConfig.getClusterNodes(); | ||
log.trace("Current Redis cluster nodes: {}", currentNodes); | ||
|
||
for (RedisNode node : currentNodes) { | ||
if (!node.hasValidHost()) { | ||
log.debug("Skip Redis node with invalid host: {}", node); | ||
continue; | ||
} | ||
if (node.getPort() == null) { | ||
log.debug("Skip Redis node with null port: {}", node); | ||
continue; | ||
} | ||
try (Jedis jedis = new Jedis(node.getHost(), node.getPort())) { | ||
if (factory.getPassword() != null) { | ||
jedis.auth(factory.getPassword()); | ||
} | ||
Set<RedisNode> redisNodes = getRedisNodes(node, jedis); | ||
if (currentNodes.equals(redisNodes)) { | ||
log.trace("Redis cluster topology is up to date!"); | ||
break; | ||
} | ||
clusterConfig.setClusterNodes(redisNodes); | ||
log.trace("Successfully updated Redis cluster topology, nodes: {}", redisNodes); | ||
break; | ||
} catch (Exception e) { | ||
log.debug("Failed to refresh cluster topology using node: {}", node.getHost(), e); | ||
} | ||
} | ||
} catch (Exception e) { | ||
log.warn("Failed to refresh cluster topology", e); | ||
} | ||
} | ||
|
||
private Set<RedisNode> getRedisNodes(RedisNode node, Jedis jedis) { | ||
String clusterNodes = jedis.clusterNodes(); | ||
log.trace("Caller Redis node: {}:{} CLUSTER NODES output:{}{}", node.getHost(), node.getPort(), System.lineSeparator(), clusterNodes); | ||
// Split the clusterNodes string into individual lines and parse each line | ||
// Each line is composed of the following fields: | ||
// <id> <ip:port@cport[,hostname]> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot> | ||
return Arrays.stream(clusterNodes.split(System.lineSeparator())) | ||
.map(JedisClusterNodesUtil::parseClusterNodeLine) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
} |
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
26 changes: 0 additions & 26 deletions
26
...n/cache/src/main/java/org/thingsboard/mqtt/broker/cache/LettuceTopologyRefreshConfig.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.