Skip to content

Commit f2b35af

Browse files
Bugfix/cluster info handle ipv6 (#4652)
* Fix parseNodesFromClusterInfoReply to be able to handle non XXX.XXX.X.XX:PPPP formated ips. For example, ipv6 ips. * Add unit tests related to ipv6. * update documentation.
1 parent 4ca85dd commit f2b35af

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

redisinsight/api/src/__mocks__/redis-info.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ export const mockRedisClusterNodesResponse: string =
109109
'07c37dfeb235213a872192d90877d0cd55635b91 127.0.0.1:30004@31004 slave e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 0 1426238317239 4 connected\n' +
110110
'e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 127.0.0.1:30001@31001 myself,master - 0 0 1 connected 0-16383';
111111

112+
// eslint-disable-next-line max-len
113+
export const mockRedisClusterNodesResponseIPv6: string =
114+
'07c37dfeb235213a872192d90877d0cd55635b91 2001:db8::1:7001@17001 slave e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 0 1426238317239 4 connected\n' +
115+
'e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 2001:db8::2:7002@17002 myself,master - 0 0 1 connected 0-16383';
116+
112117
export const mockStandaloneRedisInfoReply: string = `${
113118
mockRedisServerInfoResponse
114119
}\r\n${mockRedisClientsInfoResponse}\r\n${mockRedisMemoryInfoResponse}\r\n${

redisinsight/api/src/modules/redis/utils/reply.util.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
mockRedisClusterNodesResponse,
3+
mockRedisClusterNodesResponseIPv6,
34
mockRedisServerInfoResponse,
45
} from 'src/__mocks__';
56
import { flatMap } from 'lodash';
@@ -38,6 +39,28 @@ const mockRedisClusterNodes: IRedisClusterNode[] = [
3839
},
3940
];
4041

42+
// IPv6 expected results
43+
const mockRedisClusterNodesIPv6: IRedisClusterNode[] = [
44+
{
45+
id: '07c37dfeb235213a872192d90877d0cd55635b91',
46+
host: '2001:db8::1',
47+
port: 7001,
48+
replicaOf: 'e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca',
49+
linkState: RedisClusterNodeLinkState.Connected,
50+
slot: undefined,
51+
},
52+
{
53+
id: 'e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca',
54+
host: '2001:db8::2',
55+
port: 7002,
56+
replicaOf: undefined,
57+
linkState: RedisClusterNodeLinkState.Connected,
58+
slot: '0-16383',
59+
},
60+
];
61+
62+
63+
4164
const mockIncorrectString = '$6\r\nfoobar\r\n';
4265

4366
describe('convertArrayReplyToObject', () => {
@@ -86,4 +109,11 @@ describe('parseNodesFromClusterInfoReply', () => {
86109

87110
expect(result).toEqual([]);
88111
});
112+
it('should parse IPv6 addresses correctly', async () => {
113+
const result = parseNodesFromClusterInfoReply(
114+
mockRedisClusterNodesResponseIPv6,
115+
);
116+
117+
expect(result).toEqual(mockRedisClusterNodesIPv6);
118+
});
89119
});

redisinsight/api/src/modules/redis/utils/reply.util.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ export const convertMultilineReplyToObject = (
8686
* Parse and return all endpoints from the nodes list returned by "cluster info" command
8787
* @Input
8888
* ```
89-
* 08418e3514990489e48fa05d642efc33e205f5 172.31.100.211:6379@16379 myself,master - 0 1698694904000 1 connected 0-5460\n
90-
* d2dee846c715a917ec9a4963e8885b06130f9f 172.31.100.212:6379@16379 master - 0 1698694905285 2 connected 5461-10922\n
89+
* 08418e3514990489e48fa05d642efc33e205f5 172.31.100.211:6379@16379 myself,master - 0 1698694904000 1 connected 0-5460
90+
* d2dee846c715a917ec9a4963e8885b06130f9f 172.31.100.212:6379@16379 master - 0 1698694905285 2 connected 5461-10922
91+
* 3e92457ab813ad7a62dacf768ec7309210feaf [2001:db8::1]:7001@17001 master - 0 1698694906000 3 connected 10923-16383
9192
* ```
9293
* @Output
9394
* ```
@@ -99,6 +100,10 @@ export const convertMultilineReplyToObject = (
99100
* {
100101
* host: "172.31.100.212",
101102
* port: 6379
103+
* },
104+
* {
105+
* host: "2001:db8::1",
106+
* port: 7001
102107
* }
103108
* ]
104109
* ```
@@ -115,8 +120,12 @@ export const parseNodesFromClusterInfoReply = (
115120
// fields = [id, endpoint, flags, master, pingSent, pongRecv, configEpoch, linkState, slot]
116121
const fields = line.split(' ');
117122
const [id, endpoint, , master, , , , linkState, slot] = fields;
118-
const host = endpoint.split(':')[0];
119-
const port = endpoint.split(':')[1].split('@')[0];
123+
124+
const hostAndPort = endpoint.split('@')[0]
125+
const lastColonIndex = hostAndPort.lastIndexOf(':');
126+
127+
const host = hostAndPort.substring(0, lastColonIndex);
128+
const port = hostAndPort.substring(lastColonIndex + 1);
120129
nodes.push({
121130
id,
122131
host,

0 commit comments

Comments
 (0)