From 2e82f83cc196d7a579208a70d8bb7f71e875ec02 Mon Sep 17 00:00:00 2001 From: Andrew Kerr Date: Wed, 23 Aug 2017 13:54:09 +0100 Subject: [PATCH] Logging for upstream selection strategy. Log strategy loading, warn of deprecation of ConnectRandomlyWithinServerGroupStrategy.java, log bad configs of ConnectRandomlyToServerGroupStrategy.java and UserDefinedConfigurationStrategy.java. Additionally parse config in UserDefinedConfigurationStrategy.java once at initialisation. --- .../ConnectRandomlyToServerGroupStrategy.java | 15 +++++++-- ...nectRandomlyWithinServerGroupStrategy.java | 1 + .../UpstreamDatabaseSelectionStrategy.java | 3 ++ .../UserDefinedConfigurationStrategy.java | 32 +++++++++++++++---- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupStrategy.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupStrategy.java index a7d88afac47b..340c5c72de7b 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupStrategy.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyToServerGroupStrategy.java @@ -21,20 +21,21 @@ import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; 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 { + static final String NAME = "connect-randomly-to-server-group"; private ConnectRandomlyToServerGroupImpl strategyImpl; public ConnectRandomlyToServerGroupStrategy() { - super( "connect-randomly-to-server-group" ); + super( NAME ); } @Override @@ -43,6 +44,16 @@ void init() List groups = config.get( CausalClusteringSettings.connect_randomly_to_server_group_strategy ); strategyImpl = new ConnectRandomlyToServerGroupImpl( groups, topologyService, myself ); + + if (groups.isEmpty()) + { + log.warn( "No server groups configured for upstream strategy " + readableName + ". Strategy will not find upstream servers." ); + } + else + { + String readableGroups = groups.stream().collect( Collectors.joining( ", " ) ); + log.info( "Upstream selection strategy " + readableName + " configured with server groups " + readableGroups ); + } } @Override diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyWithinServerGroupStrategy.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyWithinServerGroupStrategy.java index aa5011aad968..9bcb08950a78 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyWithinServerGroupStrategy.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/ConnectRandomlyWithinServerGroupStrategy.java @@ -42,6 +42,7 @@ void init() { List groups = config.get( CausalClusteringSettings.server_groups ); strategyImpl = new ConnectRandomlyToServerGroupImpl( groups, topologyService, myself ); + log.warn( "Upstream selection strategy " + readableName + " is deprecated. Consider using " + ConnectRandomlyToServerGroupStrategy.NAME + " instead." ); } @Override diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UpstreamDatabaseSelectionStrategy.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UpstreamDatabaseSelectionStrategy.java index 7b378666d7b9..f86ccbc75949 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UpstreamDatabaseSelectionStrategy.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UpstreamDatabaseSelectionStrategy.java @@ -36,6 +36,7 @@ public abstract class UpstreamDatabaseSelectionStrategy extends Service protected Config config; protected Log log; protected MemberId myself; + protected String readableName; public UpstreamDatabaseSelectionStrategy( String key, String... altKeys ) { @@ -50,6 +51,8 @@ void inject( TopologyService topologyService, Config config, LogProvider logProv this.log = logProvider.getLog( this.getClass() ); this.myself = myself; + readableName = StreamSupport.stream( getKeys().spliterator(), false ).collect( Collectors.joining( ", " ) ); + log.info( "Using upstream selection strategy " + readableName ); init(); } diff --git a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UserDefinedConfigurationStrategy.java b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UserDefinedConfigurationStrategy.java index 626a4c9d511e..4a11aa69914a 100644 --- a/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UserDefinedConfigurationStrategy.java +++ b/enterprise/causal-clustering/src/main/java/org/neo4j/causalclustering/readreplica/UserDefinedConfigurationStrategy.java @@ -39,29 +39,47 @@ @Service.Implementation( UpstreamDatabaseSelectionStrategy.class ) public class UserDefinedConfigurationStrategy extends UpstreamDatabaseSelectionStrategy { + + // Empty if provided filter config cannot be parsed. + // Ideally this class would not be created until config has been successfully parsed + // in which case there would be no need for Optional + private Optional> filters; + public UserDefinedConfigurationStrategy() { super( "user-defined" ); } @Override - public Optional upstreamDatabase() throws UpstreamDatabaseSelectionException + void init() { + String filterConfig = config.get( CausalClusteringSettings.user_defined_upstream_selection_strategy ); try { - Filter filters = FilterConfigParser.parse( config.get( CausalClusteringSettings.user_defined_upstream_selection_strategy ) ); + Filter parsed = FilterConfigParser.parse( filterConfig ); + filters = Optional.of( parsed ); + log.info( "Upstream selection strategy " + readableName + " configured with " + filterConfig ); + } + catch ( InvalidFilterSpecification invalidFilterSpecification ) + { + filters = Optional.empty(); + log.warn( "Cannot parse configuration '" + filterConfig + "' for upstream selection strategy " + + readableName + ". " + invalidFilterSpecification.getMessage() ); + } + } + @Override + public Optional upstreamDatabase() throws UpstreamDatabaseSelectionException + { + return filters.flatMap( filters -> + { Set possibleServers = possibleServers(); return filters.apply( possibleServers ).stream() .map( ServerInfo::memberId ) .filter( memberId -> !Objects.equals( myself, memberId ) ) .findFirst(); - } - catch ( InvalidFilterSpecification invalidFilterSpecification ) - { - return Optional.empty(); - } + } ); } private Set possibleServers()