Skip to content

Commit

Permalink
Simplifies read replica info retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
hugofirth committed Oct 5, 2018
1 parent 667f02f commit 21d46ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Expand Up @@ -19,6 +19,8 @@
*/ */
package org.neo4j.helpers.collection; package org.neo4j.helpers.collection;


import java.util.Map;

/** /**
* Utility to handle pairs of objects. * Utility to handle pairs of objects.
* *
Expand Down
Expand Up @@ -187,23 +187,12 @@ static boolean casClusterId( HazelcastInstance hazelcastInstance, ClusterId clus


private static Map<MemberId,ReadReplicaInfo> readReplicas( HazelcastInstance hazelcastInstance, Log log ) private static Map<MemberId,ReadReplicaInfo> readReplicas( HazelcastInstance hazelcastInstance, Log log )
{ {
Set<String> missingAttrKeys = new HashSet<>(); Pair<Set<String>,Map<String,IMap<String,String>>> validatedSimpleAttrMaps = validatedSimpleAttrMaps( hazelcastInstance );
Map<String,IMap<String,String>> validatedSimpleAttrMaps = new HashMap<>(); Set<String> missingAttrKeys = validatedSimpleAttrMaps.first();

Map<String,IMap<String,String>> simpleAttrMaps = validatedSimpleAttrMaps.other();
for ( String attrMapKey : simpleRRAttrMapKeys )
{
IMap<String,String> attrMap = hazelcastInstance.getMap( attrMapKey );
if ( attrMap == null )
{
missingAttrKeys.add( attrMapKey );
}
else
{
validatedSimpleAttrMaps.put( attrMapKey, attrMap );
}
}


MultiMap<String,String> serverGroupsMap = hazelcastInstance.getMultiMap( SERVER_GROUPS_MULTIMAP ); MultiMap<String,String> serverGroupsMap = hazelcastInstance.getMultiMap( SERVER_GROUPS_MULTIMAP );

if ( serverGroupsMap == null ) if ( serverGroupsMap == null )
{ {
missingAttrKeys.add( SERVER_GROUPS_MULTIMAP ); missingAttrKeys.add( SERVER_GROUPS_MULTIMAP );
Expand All @@ -222,22 +211,50 @@ private static Map<MemberId,ReadReplicaInfo> readReplicas( HazelcastInstance haz
return emptyMap(); return emptyMap();
} }


Stream<String> readReplicaHzIds = validatedSimpleAttrMaps.get( READ_REPLICA_BOLT_ADDRESS_MAP ).keySet().stream(); Stream<String> readReplicaHzIds = simpleAttrMaps.get( READ_REPLICA_BOLT_ADDRESS_MAP ).keySet().stream();


Map<MemberId,ReadReplicaInfo> validatedReadReplicas = readReplicaHzIds Map<MemberId,ReadReplicaInfo> validatedReadReplicas = readReplicaHzIds
.flatMap( hzId -> Streams.ofNullable( buildReadReplicaFromAttrMap( hzId, validatedSimpleAttrMaps, serverGroupsMap, log ) ) ) .flatMap( hzId -> Streams.ofNullable( buildReadReplicaFromAttrMap( hzId, simpleAttrMaps, serverGroupsMap, log ) ) )
.collect( Collectors.toMap( Pair::first, Pair::other ) ); .collect( Collectors.toMap( Pair::first, Pair::other ) );


return validatedReadReplicas; return validatedReadReplicas;
} }


private static Pair<MemberId,ReadReplicaInfo> buildReadReplicaFromAttrMap( String hzId, Map<String,IMap<String,String>> validatedSimpleAttrMaps, /**
* Retrieves the various maps containing attributes about read replicas from hazelcast. If any maps do not exist, keep track of their keys for logging.
*/
private static Pair<Set<String>,Map<String,IMap<String,String>>> validatedSimpleAttrMaps( HazelcastInstance hazelcastInstance )
{
Set<String> missingAttrKeys = new HashSet<>();
Map<String,IMap<String,String>> validatedSimpleAttrMaps = new HashMap<>();

for ( String attrMapKey : simpleRRAttrMapKeys )
{
IMap<String,String> attrMap = hazelcastInstance.getMap( attrMapKey );
if ( attrMap == null )
{
missingAttrKeys.add( attrMapKey );
}
else
{
validatedSimpleAttrMaps.put( attrMapKey, attrMap );
}
}

return Pair.of( missingAttrKeys, validatedSimpleAttrMaps );
}

/**
* Given a hazelcast member id and a set of non-null attribute maps, this method builds a discovery representation of a read replica
* (i.e. `Pair<MemberId,ReadReplicaInfo>`). Any missing attributes which are missing for a given hazelcast member id are logged and this
* method will return null.
*/
private static Pair<MemberId,ReadReplicaInfo> buildReadReplicaFromAttrMap( String hzId, Map<String,IMap<String,String>> simpleAttrMaps,
MultiMap<String,String> serverGroupsMap, Log log ) MultiMap<String,String> serverGroupsMap, Log log )
{ {


Map<String,String> memberAttrs = validatedSimpleAttrMaps.entrySet().stream() Map<String,String> memberAttrs = simpleAttrMaps.entrySet().stream().collect( Collectors.toMap( Map.Entry::getKey, e -> e.getValue().get( hzId ) ) );
.collect( Collectors.toMap( Map.Entry::getKey, e -> e.getValue().get( hzId ) ) ); Collection<String> memberServerGroups = serverGroupsMap.get( hzId );
Collection<String> serverGroups = serverGroupsMap.get( hzId );


for ( Map.Entry<String,String> attr : memberAttrs.entrySet() ) for ( Map.Entry<String,String> attr : memberAttrs.entrySet() )
{ {
Expand All @@ -248,7 +265,7 @@ private static Pair<MemberId,ReadReplicaInfo> buildReadReplicaFromAttrMap( Strin
} }
} }


if ( serverGroups == null ) if ( memberServerGroups == null )
{ {
log.warn( "Missing attribute %s for read replica with hz id %s", SERVER_GROUPS_MULTIMAP, hzId ); log.warn( "Missing attribute %s for read replica with hz id %s", SERVER_GROUPS_MULTIMAP, hzId );
return null; return null;
Expand All @@ -258,7 +275,7 @@ private static Pair<MemberId,ReadReplicaInfo> buildReadReplicaFromAttrMap( Strin
AdvertisedSocketAddress catchupAddress = socketAddress( memberAttrs.get( READ_REPLICA_TRANSACTION_SERVER_ADDRESS_MAP ), AdvertisedSocketAddress::new ); AdvertisedSocketAddress catchupAddress = socketAddress( memberAttrs.get( READ_REPLICA_TRANSACTION_SERVER_ADDRESS_MAP ), AdvertisedSocketAddress::new );
MemberId memberId = new MemberId( UUID.fromString( memberAttrs.get( READ_REPLICA_MEMBER_ID_MAP ) ) ); MemberId memberId = new MemberId( UUID.fromString( memberAttrs.get( READ_REPLICA_MEMBER_ID_MAP ) ) );
String memberDbName = memberAttrs.get( READ_REPLICAS_DB_NAME_MAP ); String memberDbName = memberAttrs.get( READ_REPLICAS_DB_NAME_MAP );
Set<String> serverGroupSet = asSet( serverGroups ); Set<String> serverGroupSet = asSet( memberServerGroups );


ReadReplicaInfo rrInfo = new ReadReplicaInfo( boltAddresses, catchupAddress, serverGroupSet, memberDbName ); ReadReplicaInfo rrInfo = new ReadReplicaInfo( boltAddresses, catchupAddress, serverGroupSet, memberDbName );
return Pair.of( memberId, rrInfo ); return Pair.of( memberId, rrInfo );
Expand Down

0 comments on commit 21d46ca

Please sign in to comment.