You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@curator.apache.org by Randgalt <gi...@git.apache.org> on 2015/09/28 00:00:36 UTC

[GitHub] curator pull request: First pass implementation of group membershi...

GitHub user Randgalt opened a pull request:

    https://github.com/apache/curator/pull/110

    First pass implementation of group membership

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/apache/curator CURATOR-267

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/curator/pull/110.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #110
    
----
commit 3029856c894a72ed2c5762e0c6acd2c0a8cd3937
Author: randgalt <ra...@apache.org>
Date:   2015-09-27T21:59:48Z

    First pass implementation of group membership

----


---
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.
---

[GitHub] curator pull request: [CURATOR-267] First pass implementation of g...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/curator/pull/110


---
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.
---

[GitHub] curator pull request: First pass implementation of group membershi...

Posted by cammckenzie <gi...@git.apache.org>.
Github user cammckenzie commented on the pull request:

    https://github.com/apache/curator/pull/110#issuecomment-145418756
  
    Other than my minor comment, this looks good to me.


---
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.
---

[GitHub] curator pull request: First pass implementation of group membershi...

Posted by cammckenzie <gi...@git.apache.org>.
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.
---