Skip to content

Commit 08b3150

Browse files
authored
[To dev/1.3] Fix nodeUrls shuffle error (#16167)
1 parent e9039a7 commit 08b3150

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

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
@@ -412,7 +412,7 @@ public Session(
412412
if (nodeUrls.isEmpty()) {
413413
throw new IllegalArgumentException("nodeUrls shouldn't be empty.");
414414
}
415-
Collections.shuffle(nodeUrls);
415+
nodeUrls = shuffleNodeUrls(nodeUrls);
416416
this.nodeUrls = nodeUrls;
417417
this.username = username;
418418
this.password = password;
@@ -429,7 +429,7 @@ public Session(Builder builder) {
429429
if (builder.nodeUrls.isEmpty()) {
430430
throw new IllegalArgumentException("nodeUrls shouldn't be empty.");
431431
}
432-
Collections.shuffle(builder.nodeUrls);
432+
builder.nodeUrls = shuffleNodeUrls(builder.nodeUrls);
433433
this.nodeUrls = builder.nodeUrls;
434434
this.enableQueryRedirection = true;
435435
} else {
@@ -550,6 +550,16 @@ private void initThreadPool() {
550550
});
551551
}
552552

553+
private static List<String> shuffleNodeUrls(List<String> endPoints) {
554+
try {
555+
Collections.shuffle(endPoints);
556+
} catch (UnsupportedOperationException e) {
557+
endPoints = new ArrayList<>(endPoints);
558+
Collections.shuffle(endPoints);
559+
}
560+
return endPoints;
561+
}
562+
553563
private List<TEndPoint> getNodeUrls() {
554564
if (defaultEndPoint != null) {
555565
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
@@ -104,6 +104,18 @@ public void testBuildSession() {
104104
.username("username")
105105
.password("pwd")
106106
.build();
107+
session1 =
108+
new Session.Builder()
109+
.nodeUrls(Collections.nCopies(2, "host:port"))
110+
.username("username")
111+
.password("pwd")
112+
.build();
113+
session1 =
114+
new Session.Builder()
115+
.nodeUrls(Collections.unmodifiableList(Arrays.asList("host:port1", "host:port2")))
116+
.username("username")
117+
.password("pwd")
118+
.build();
107119
session1 =
108120
new Session.Builder()
109121
.host("host")

0 commit comments

Comments
 (0)