-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move PeerFinder machinery to discovery package
- Loading branch information
1 parent
3608505
commit 2013f10
Showing
7 changed files
with
121 additions
and
84 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
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
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
108 changes: 108 additions & 0 deletions
108
server/src/test/java/org/elasticsearch/discovery/PeerFinderMessagesTests.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,108 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.discovery; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.coordination.PeersResponse; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.EqualsHashCodeTestUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
|
||
public class PeerFinderMessagesTests extends ESTestCase { | ||
private DiscoveryNode createNode(String id) { | ||
return new DiscoveryNode(id, buildNewFakeTransportAddress(), Version.CURRENT); | ||
} | ||
|
||
public void testPeersRequestEqualsHashCodeSerialization() { | ||
final PeersRequest initialPeersRequest = new PeersRequest(createNode(randomAlphaOfLength(10)), | ||
Arrays.stream(generateRandomStringArray(10, 10, false)).map(this::createNode).collect(Collectors.toList())); | ||
|
||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersRequest, | ||
publishRequest -> copyWriteable(publishRequest, writableRegistry(), PeersRequest::new), | ||
in -> { | ||
final List<DiscoveryNode> discoveryNodes = new ArrayList<>(in.getKnownPeers()); | ||
if (randomBoolean()) { | ||
return new PeersRequest(createNode(randomAlphaOfLength(10)), discoveryNodes); | ||
} else { | ||
return new PeersRequest(in.getSourceNode(), modifyDiscoveryNodesList(in.getKnownPeers(), true)); | ||
} | ||
}); | ||
} | ||
|
||
public void testPeersResponseEqualsHashCodeSerialization() { | ||
final long initialTerm = randomNonNegativeLong(); | ||
final PeersResponse initialPeersResponse; | ||
|
||
if (randomBoolean()) { | ||
initialPeersResponse = new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), initialTerm); | ||
} else { | ||
initialPeersResponse = new PeersResponse(Optional.empty(), | ||
Arrays.stream(generateRandomStringArray(10, 10, false, false)).map(this::createNode).collect(Collectors.toList()), | ||
initialTerm); | ||
} | ||
|
||
EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersResponse, | ||
publishResponse -> copyWriteable(publishResponse, writableRegistry(), PeersResponse::new), | ||
in -> { | ||
final long term = in.getTerm(); | ||
if (randomBoolean()) { | ||
return new PeersResponse(in.getMasterNode(), in.getKnownPeers(), | ||
randomValueOtherThan(term, ESTestCase::randomNonNegativeLong)); | ||
} else { | ||
if (in.getMasterNode().isPresent()) { | ||
if (randomBoolean()) { | ||
return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), in.getKnownPeers(), term); | ||
} else { | ||
return new PeersResponse(Optional.empty(), singletonList(createNode(randomAlphaOfLength(10))), term); | ||
} | ||
} else { | ||
if (randomBoolean()) { | ||
return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), term); | ||
} else { | ||
return new PeersResponse(in.getMasterNode(), modifyDiscoveryNodesList(in.getKnownPeers(), false), term); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
private List<DiscoveryNode> modifyDiscoveryNodesList(Collection<DiscoveryNode> originalNodes, boolean allowEmpty) { | ||
final List<DiscoveryNode> discoveryNodes = new ArrayList<>(originalNodes); | ||
if (discoveryNodes.isEmpty() == false && randomBoolean() && (allowEmpty || discoveryNodes.size() > 1)) { | ||
discoveryNodes.remove(randomIntBetween(0, discoveryNodes.size() - 1)); | ||
} else if (discoveryNodes.isEmpty() == false && randomBoolean()) { | ||
discoveryNodes.set(randomIntBetween(0, discoveryNodes.size() - 1), createNode(randomAlphaOfLength(10))); | ||
} else { | ||
discoveryNodes.add(createNode(randomAlphaOfLength(10))); | ||
} | ||
return discoveryNodes; | ||
} | ||
} |
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