Skip to content

Commit dd3e8b2

Browse files
authored
Fix nodeUrls shuffle error (#16147)
1 parent b1deb6c commit dd3e8b2

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ public Session(
433433
if (nodeUrls.isEmpty()) {
434434
throw new IllegalArgumentException("nodeUrls shouldn't be empty.");
435435
}
436-
Collections.shuffle(nodeUrls);
436+
nodeUrls = shuffleNodeUrls(nodeUrls);
437437
this.nodeUrls = nodeUrls;
438438
this.username = username;
439439
this.password = password;
@@ -450,7 +450,7 @@ public Session(AbstractSessionBuilder builder) {
450450
if (builder.nodeUrls.isEmpty()) {
451451
throw new IllegalArgumentException("nodeUrls shouldn't be empty.");
452452
}
453-
Collections.shuffle(builder.nodeUrls);
453+
builder.nodeUrls = shuffleNodeUrls(builder.nodeUrls);
454454
this.nodeUrls = builder.nodeUrls;
455455
this.enableQueryRedirection = true;
456456
} else {
@@ -578,6 +578,16 @@ private void initThreadPool() {
578578
});
579579
}
580580

581+
private static List<String> shuffleNodeUrls(List<String> endPoints) {
582+
try {
583+
Collections.shuffle(endPoints);
584+
} catch (UnsupportedOperationException e) {
585+
endPoints = new ArrayList<>(endPoints);
586+
Collections.shuffle(endPoints);
587+
}
588+
return endPoints;
589+
}
590+
581591
private List<TEndPoint> getNodeUrls() {
582592
if (defaultEndPoint != null) {
583593
return Collections.singletonList(defaultEndPoint);

iotdb-client/session/src/test/java/org/apache/iotdb/session/SessionTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public void testBuildSession() {
105105
.username("username")
106106
.password("pwd")
107107
.build();
108+
session1 =
109+
new Session.Builder()
110+
.nodeUrls(Collections.nCopies(2, "host:port"))
111+
.username("username")
112+
.password("pwd")
113+
.build();
114+
session1 =
115+
new Session.Builder()
116+
.nodeUrls(Collections.unmodifiableList(Arrays.asList("host:port1", "host:port2")))
117+
.username("username")
118+
.password("pwd")
119+
.build();
108120
session1 =
109121
new Session.Builder()
110122
.host("host")

0 commit comments

Comments
 (0)