curator-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cammckenzie <...@git.apache.org>
Subject [GitHub] curator pull request: First pass implementation of group membershi...
Date Mon, 05 Oct 2015 03:05:06 GMT
Github user cammckenzie commented on a diff in the pull request:

    https://github.com/apache/curator/pull/110#discussion_r41107895
  
    --- Diff: curator-recipes/src/main/java/org/apache/curator/framework/recipes/nodes/GroupMember.java
---
    @@ -0,0 +1,154 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.curator.framework.recipes.nodes;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.base.Throwables;
    +import com.google.common.collect.ImmutableMap;
    +import org.apache.curator.framework.CuratorFramework;
    +import org.apache.curator.framework.CuratorFrameworkFactory;
    +import org.apache.curator.framework.recipes.cache.ChildData;
    +import org.apache.curator.framework.recipes.cache.PathChildrenCache;
    +import org.apache.curator.utils.CloseableUtils;
    +import org.apache.curator.utils.ZKPaths;
    +import java.io.Closeable;
    +import java.util.Map;
    +
    +/**
    + * Group membership management. Adds this instance into a group and
    + * keeps a cache of members in the group
    + */
    +public class GroupMember implements Closeable
    +{
    +    private final PersistentEphemeralNode pen;
    +    private final PathChildrenCache cache;
    +    private final String thisId;
    +
    +    /**
    +     * @param client client
    +     * @param membershipPath the path to use for membership
    +     * @param thisId ID of this group member. MUST be unique for the group
    +     */
    +    public GroupMember(CuratorFramework client, String membershipPath, String thisId)
    +    {
    +        this(client, membershipPath, thisId, CuratorFrameworkFactory.getLocalAddress());
    +    }
    +
    +    /**
    +     * @param client client
    +     * @param membershipPath the path to use for membership
    +     * @param thisId ID of this group member. MUST be unique for the group
    +     * @param payload the payload to write in our member node
    +     */
    +    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);
    +    }
    +
    +    /**
    +     * Start the group membership. Register thisId as a member and begin
    +     * caching all members
    +     */
    +    public void start()
    +    {
    +        pen.start();
    +        try
    +        {
    +            cache.start();
    +        }
    +        catch ( Exception e )
    +        {
    +            Throwables.propagate(e);
    +        }
    +    }
    +
    +    /**
    +     * Change the data stored in this instance's node
    +     *
    +     * @param data new data (cannot be null)
    +     */
    +    public void setThisData(byte[] data)
    +    {
    +        try
    +        {
    +            pen.setData(data);
    +        }
    +        catch ( Exception e )
    +        {
    +            Throwables.propagate(e);
    +        }
    +    }
    +
    +    /**
    +     * Have thisId leave the group and stop caching membership
    +     */
    +    @Override
    +    public void close()
    +    {
    +        CloseableUtils.closeQuietly(cache);
    +        CloseableUtils.closeQuietly(pen);
    +    }
    +
    +    /**
    +     * Return the current view of membership. The keys are the IDs
    +     * of the members. The values are each member's payload
    +     *
    +     * @return membership
    +     */
    +    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();
    +    }
    +
    +    /**
    +     * Given a full ZNode path, return the member ID
    +     *
    +     * @param path full ZNode path
    +     * @return id
    +     */
    +    public String idFromPath(String path)
    --- End diff --
    
    Any reason this is public?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message