Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ConnectRandomlyToServerGroupStrategy
Shares logic with ConnectRandomlyWithinServerGroupStrategy. Replace dependency setters on UpstreamDatabaseSelectionStrategy with a single method that calls an init() method after setting fields. This makes null fields explicit in calling code, and enables post-injection set up. Use java-8-matchers for test assertions on Optionals
- Loading branch information
1 parent
a77d570
commit ef074fb
Showing
17 changed files
with
401 additions
and
177 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
70 changes: 70 additions & 0 deletions
70
...rc/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupImpl.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,70 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.causalclustering.readreplica; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Random; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
import org.neo4j.causalclustering.discovery.ReadReplicaInfo; | ||
import org.neo4j.causalclustering.discovery.TopologyService; | ||
import org.neo4j.causalclustering.identity.MemberId; | ||
|
||
class ConnectRandomlyToServerGroupImpl | ||
{ | ||
private final List<String> groups; | ||
private final TopologyService topologyService; | ||
private final MemberId myself; | ||
private final Random random = new Random(); | ||
|
||
ConnectRandomlyToServerGroupImpl( List<String> groups, TopologyService topologyService, MemberId myself ) | ||
{ | ||
this.groups = groups; | ||
this.topologyService = topologyService; | ||
this.myself = myself; | ||
} | ||
|
||
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException | ||
{ | ||
Map<MemberId, ReadReplicaInfo> replicas = topologyService.readReplicas().members(); | ||
|
||
List<MemberId> choices = groups.stream() | ||
.flatMap( group -> replicas.entrySet().stream().filter( isMyGroupAndNotMe( group ) ) ) | ||
.map( Map.Entry::getKey ) | ||
.collect( Collectors.toList() ); | ||
|
||
if ( choices.isEmpty() ) | ||
{ | ||
return Optional.empty(); | ||
} | ||
else | ||
{ | ||
return Optional.of( choices.get( random.nextInt( choices.size() ) ) ); | ||
} | ||
} | ||
|
||
private Predicate<Map.Entry<MemberId,ReadReplicaInfo>> isMyGroupAndNotMe( String group ) | ||
{ | ||
return entry -> entry.getValue().groups().contains( group ) && !entry.getKey().equals( myself ); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ain/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupStrategy.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,53 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.causalclustering.readreplica; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.neo4j.causalclustering.core.CausalClusteringSettings; | ||
import org.neo4j.causalclustering.identity.MemberId; | ||
import org.neo4j.helpers.Service; | ||
|
||
// TODO deprecation warning | ||
@Service.Implementation( UpstreamDatabaseSelectionStrategy.class ) | ||
public class ConnectRandomlyToServerGroupStrategy extends UpstreamDatabaseSelectionStrategy | ||
{ | ||
private ConnectRandomlyToServerGroupImpl strategyImpl; | ||
|
||
public ConnectRandomlyToServerGroupStrategy() | ||
{ | ||
super( "connect-randomly-to-server-group" ); | ||
} | ||
|
||
@Override | ||
void init() | ||
{ | ||
List<String> groups = config.get( CausalClusteringSettings.connect_randomly_to_server_group_strategy ); | ||
strategyImpl = new ConnectRandomlyToServerGroupImpl( groups, topologyService, | ||
myself ); | ||
} | ||
|
||
@Override | ||
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException | ||
{ | ||
return strategyImpl.upstreamDatabase(); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ETA-INF/services/org.neo4j.causalclustering.readreplica.UpstreamDatabaseSelectionStrategy
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.neo4j.causalclustering.readreplica.ConnectToRandomCoreServerStrategy | ||
org.neo4j.causalclustering.readreplica.ConnectRandomlyWithinServerGroupStrategy | ||
org.neo4j.causalclustering.readreplica.ConnectRandomlyToServerGroupStrategy |
Oops, something went wrong.