Use and Analysis of [Curator] Group Member

Group Member

Group member management.

Add this instance to a group and maintain the member cache within the group.

1. Key API s

org.apache.curator.framework.recipes.nodes.GroupMember

org.apache.curator.framework.recipes.nodes.PersistentNode

org.apache.curator.framework.recipes.cache.PathChildrenCache

2. Mechanisms

For an introduction to PersistentNode, see The Use and Analysis of Persistent Ephemeral Node/Persistent Node

For an introduction to Path ChildrenCache, see The Use and Analysis of Path Cache

GroupMember s are essentially a combination of Persistent Ephemeral Node and Path ChildrenCache.

3. Usage

3.1 Creation

public GroupMember(CuratorFramework client,
                        String membershipPath,
                        String thisId,
                        byte[] payload)
  • thisId
    • ID identifier for group members
    • For members in the same group, their ID must be unique
  • payload
    • Group membership data
    • Write data to group member nodes

3.2 Use

GroupMember s must call group.start(); before use; and after use, group.close(); after use. close() causes the current instance to be removed from the group

You can call group.getCurrentMembers(); view membership information.

4. Error handling

The GroupMember instance handles all errors internally and recreates the nodes as needed.

5. Source code analysis

Class 5.1 Definitions

public class GroupMember implements Closeable {}
  • Implement java.io.Closeable interface

5.2 Membership Variables

public class GroupMember implements Closeable
{
    private final PersistentEphemeralNode pen;
    private final PathChildrenCache cache;
    private final String thisId;
}

5.3 Constructor

public GroupMember(CuratorFramework client, String membershipPath, String thisId)
{
    this(client, membershipPath, thisId, CuratorFrameworkFactory.getLocalAddress());
}

public GroupMember(CuratorFramework client, String membershipPath, String thisId, byte[] payload)
{
    this.thisId = Preconditions.checkNotNull(thisId, "thisId cannot be null");

    cache = newPathChildrenCache(client, membershipPath);
    pen = newPersistentEphemeralNode(client, membershipPath, thisId, payload);
}

protected PathChildrenCache newPathChildrenCache(CuratorFramework client, String membershipPath)
{
    return new PathChildrenCache(client, membershipPath, true);
}

protected PersistentEphemeralNode newPersistentEphemeralNode(CuratorFramework client, String membershipPath, String thisId, byte[] payload)
{
    return new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, ZKPaths.makePath(membershipPath, thisId), payload);
}
  • Mainly for the initialization of Persistent EphemeralNode and Path ChildrenCache
  • The default member node data is the local IP address
  • The created node is a temporary node
    • Temporary nodes are created: membershipPath/thisId
    • So thisId must be unique within a group

5.4 Start-up

Before GroupMember s can be used, start() must be called to start.

public void start()
{
    pen.start();
    try
    {
        cache.start();
    }
    catch ( Exception e )
    {
        ThreadUtils.checkInterrupted(e);
        Throwables.propagate(e);
    }
}
  1. Start by creating nodes through Persistent EphemeralNode
  2. Then call PathChildrenCache to get node data and cache it

5.5 Close

After GroupMember s are used up, close() must be called to close.

public void close()
    {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(pen);
    }

Contrary to the order at startup:

  1. Close the node data cache first
  2. Close the node again

5.6 Get the members of the group

The public Map< String, byte []> getCurrentMembers () method can be invoked to obtain information about members of the group.

  • Return Map with member ID as key
  • Take member's payload as value
public Map<String, byte[]> getCurrentMembers()
{
    ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
    boolean thisIdAdded = false;
    for ( ChildData data : cache.getCurrentData() )
    {
        String id = idFromPath(data.getPath());
        thisIdAdded = thisIdAdded || id.equals(thisId);
        builder.put(id, data.getData());
    }
    if ( !thisIdAdded )
    {
        builder.put(thisId, pen.getData());   // this instance is always a member
    }
    return builder.build();
}
  • If thisId already exists in the node, it will be overwritten with its own data.
  • So don't repeat thisId

6. Summary

In a sense, GroupMember is a distributed Map.

GroupMember s can be easily applied to the simple management of distributed multiple nodes.

However, the repeatability of thisId is not strictly regulated, so we should pay attention to it when using it. Regardless of repetition, the instance itself assumes that it must be in the group.

It uses temporary nodes, so other members can be found by getCurrentMembers() when the node unexpectedly exits. This can be done through GroupMember, multi-node scheduling scenario application.

Keywords: Apache Java

Added by killswitchfilter on Wed, 19 Jun 2019 22:18:22 +0300