Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,130 additions
and
17 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
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
68 changes: 68 additions & 0 deletions
68
protocols/primary-backup/src/test/java/io/atomix/cluster/TestClusterService.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,68 @@ | |||
/* | |||
* Copyright 2017-present Open Networking Foundation | |||
* | |||
* Licensed 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 io.atomix.cluster; | |||
|
|||
import io.atomix.messaging.Endpoint; | |||
|
|||
import java.util.Collection; | |||
import java.util.Set; | |||
import java.util.stream.Collectors; | |||
|
|||
/** | |||
* Test cluster service. | |||
*/ | |||
public class TestClusterService implements ClusterService { | |||
private final NodeId localNode; | |||
private final Collection<NodeId> nodes; | |||
|
|||
public TestClusterService(NodeId localNode, Collection<NodeId> nodes) { | |||
this.localNode = localNode; | |||
this.nodes = nodes; | |||
} | |||
|
|||
@Override | |||
public Node getLocalNode() { | |||
return Node.builder() | |||
.withId(localNode) | |||
.withEndpoint(Endpoint.from("localhost", localNode.hashCode())) | |||
.build(); | |||
} | |||
|
|||
@Override | |||
public Set<Node> getNodes() { | |||
return nodes.stream() | |||
.map(node -> Node.builder() | |||
.withId(node) | |||
.withEndpoint(Endpoint.from("localhost", node.hashCode())) | |||
.build()) | |||
.collect(Collectors.toSet()); | |||
} | |||
|
|||
@Override | |||
public Node getNode(NodeId nodeId) { | |||
return null; | |||
} | |||
|
|||
@Override | |||
public void addListener(ClusterEventListener listener) { | |||
|
|||
} | |||
|
|||
@Override | |||
public void removeListener(ClusterEventListener listener) { | |||
|
|||
} | |||
} |
72 changes: 72 additions & 0 deletions
72
...ocols/primary-backup/src/test/java/io/atomix/primitive/partition/TestPrimaryElection.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,72 @@ | |||
/* | |||
* Copyright 2017-present Open Networking Foundation | |||
* | |||
* Licensed 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 io.atomix.primitive.partition; | |||
|
|||
import com.google.common.collect.Sets; | |||
import io.atomix.cluster.NodeId; | |||
import io.atomix.primitive.partition.PrimaryElectionEvent.Type; | |||
|
|||
import java.util.ArrayList; | |||
import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Set; | |||
import java.util.concurrent.CompletableFuture; | |||
import java.util.stream.Collectors; | |||
|
|||
/** | |||
* Test primary election. | |||
*/ | |||
public class TestPrimaryElection implements PrimaryElection { | |||
private long counter; | |||
private PrimaryTerm term; | |||
private final List<NodeId> candidates = new ArrayList<>(); | |||
private final Set<PrimaryElectionEventListener> listeners = Sets.newConcurrentHashSet(); | |||
|
|||
@Override | |||
public CompletableFuture<PrimaryTerm> enter(NodeId nodeId) { | |||
candidates.add(nodeId); | |||
if (term == null) { | |||
term = new PrimaryTerm(++counter, nodeId, Collections.emptyList()); | |||
listeners.forEach(l -> l.onEvent(new PrimaryElectionEvent(Type.PRIMARY_AND_BACKUPS_CHANGED, term))); | |||
} else { | |||
term = new PrimaryTerm(term.term(), term.primary(), candidates.stream() | |||
.filter(candidate -> !candidate.equals(term.primary())) | |||
.collect(Collectors.toList())); | |||
listeners.forEach(l -> l.onEvent(new PrimaryElectionEvent(Type.BACKUPS_CHANGED, term))); | |||
} | |||
return CompletableFuture.completedFuture(term); | |||
} | |||
|
|||
@Override | |||
public CompletableFuture<PrimaryTerm> getTerm() { | |||
return CompletableFuture.completedFuture(term); | |||
} | |||
|
|||
@Override | |||
public void addListener(PrimaryElectionEventListener listener) { | |||
listeners.add(listener); | |||
} | |||
|
|||
@Override | |||
public void removeListener(PrimaryElectionEventListener listener) { | |||
listeners.remove(listener); | |||
} | |||
|
|||
@Override | |||
public void close() { | |||
|
|||
} | |||
} |
Oops, something went wrong.