Skip to content

Commit

Permalink
Logging for upstream selection strategy.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewkerr9000 committed Aug 24, 2017
1 parent a0d6caf commit 2e82f83
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
Expand Up @@ -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
Expand All @@ -43,6 +44,16 @@ void init()
List<String> 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
Expand Down
Expand Up @@ -42,6 +42,7 @@ void init()
{
List<String> 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
Expand Down
Expand Up @@ -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 )
{
Expand All @@ -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();
}

Expand Down
Expand Up @@ -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<Filter<ServerInfo>> filters;

public UserDefinedConfigurationStrategy()
{
super( "user-defined" );
}

@Override
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException
void init()
{
String filterConfig = config.get( CausalClusteringSettings.user_defined_upstream_selection_strategy );
try
{
Filter<ServerInfo> filters = FilterConfigParser.parse( config.get( CausalClusteringSettings.user_defined_upstream_selection_strategy ) );
Filter<ServerInfo> 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<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException
{
return filters.flatMap( filters ->
{
Set<ServerInfo> 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<ServerInfo> possibleServers()
Expand Down

0 comments on commit 2e82f83

Please sign in to comment.