This is an automated email from the ASF dual-hosted git repository. dragonyliu pushed a commit to branch branch-2 in repository https://gitbox.apache.org/repos/asf/ratis.git
commit 5c20c85b4a952f526252fb95cc8b20b07603f7a8 Author: leo65535 <[email protected]> AuthorDate: Wed Aug 17 12:06:19 2022 +0800 RATIS-1673. Verify duplicate peerid when init raft group (#714) (cherry picked from commit 713aa38fe2c996f7961d8f8c03e13db898927e9b) --- .../java/org/apache/ratis/protocol/RaftGroup.java | 1 + .../org/apache/ratis/protocol/TestRaftGroup.java | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/ratis-common/src/main/java/org/apache/ratis/protocol/RaftGroup.java b/ratis-common/src/main/java/org/apache/ratis/protocol/RaftGroup.java index a18aa5ce9..0612a16f9 100644 --- a/ratis-common/src/main/java/org/apache/ratis/protocol/RaftGroup.java +++ b/ratis-common/src/main/java/org/apache/ratis/protocol/RaftGroup.java @@ -61,6 +61,7 @@ public final class RaftGroup { if (peers == null || !peers.iterator().hasNext()) { this.peers = Collections.emptyMap(); } else { + Preconditions.assertUnique(peers); final Map<RaftPeerId, RaftPeer> map = new HashMap<>(); peers.forEach(p -> map.put(p.getId(), p)); this.peers = Collections.unmodifiableMap(map); diff --git a/ratis-test/src/test/java/org/apache/ratis/protocol/TestRaftGroup.java b/ratis-test/src/test/java/org/apache/ratis/protocol/TestRaftGroup.java new file mode 100644 index 000000000..5267b2238 --- /dev/null +++ b/ratis-test/src/test/java/org/apache/ratis/protocol/TestRaftGroup.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ratis.protocol; + +import org.apache.ratis.BaseTest; +import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; +import org.junit.Assert; +import org.junit.Test; + +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; + +public class TestRaftGroup extends BaseTest { + @Override + public int getGlobalTimeoutSeconds() { + return 1; + } + + @Test(expected = IllegalStateException.class) + public void testDuplicatePeerId() throws Exception { + UUID groupId = UUID.fromString("02511d47-d67c-49a3-9011-abb3109a44c1"); + + List<RaftPeer> peers = new LinkedList<>(); + peers.add(RaftPeer.newBuilder().setId("n0").build()); + peers.add(RaftPeer.newBuilder().setId("n0").build()); + RaftGroup.valueOf(RaftGroupId.valueOf(groupId), peers); + } +}
