FileDocCategorySizeDatePackage
Membership.javaAPI DocApache Tomcat 6.0.1410533Fri Jul 20 04:20:30 BST 2007org.apache.catalina.tribes.membership

Membership

public class Membership extends Object
A membership implementation using simple multicast. This is the representation of a multicast membership. This class is responsible for maintaining a list of active cluster nodes in the cluster. If a node fails to send out a heartbeat, the node will be dismissed.
author
Filip Hanik
author
Peter Rossbach
version
$Revision: 500684 $, $Date: 2007-01-28 00:27:18 +0100 (dim., 28 janv. 2007) $

Fields Summary
protected static final MemberImpl[]
EMPTY_MEMBERS
protected MemberImpl
local
The name of this membership, has to be the same as the name for the local member
protected HashMap
map
A map of all the members in the cluster.
protected MemberImpl[]
members
A list of all the members in the cluster.
protected Comparator
memberComparator
sort members by alive time
Constructors Summary
public Membership(MemberImpl local, boolean includeLocal)
Constructs a new membership

param
name - has to be the name of the local member. Used to filter the local member from the cluster membership

        this.local = local;
        if ( includeLocal ) addMember(local);
    
public Membership(MemberImpl local)

        this(local,false);
    
public Membership(MemberImpl local, Comparator comp)

        this(local,comp,false);
    
public Membership(MemberImpl local, Comparator comp, boolean includeLocal)

        this(local,includeLocal);
        this.memberComparator = comp;
    
Methods Summary
public synchronized org.apache.catalina.tribes.membership.Membership$MbrEntryaddMember(MemberImpl member)
Add a member to this component and sort array with memberComparator

param
member The member to add

      synchronized (members) {
          MbrEntry entry = new MbrEntry(member);
          if (!map.containsKey(member) ) {
              map.put(member, entry);
              MemberImpl results[] = new MemberImpl[members.length + 1];
              for (int i = 0; i < members.length; i++) results[i] = members[i];
              results[members.length] = member;
              members = results;
              Arrays.sort(members, memberComparator);
          }
          return entry;
      }
    
public java.lang.Objectclone()


       
        synchronized (members) {
            Membership clone = new Membership(local, memberComparator);
            clone.map = (HashMap) map.clone();
            clone.members = new MemberImpl[members.length];
            System.arraycopy(members,0,clone.members,0,members.length);
            return clone;
        }
    
public booleancontains(org.apache.catalina.tribes.Member mbr)

 
        return getMember(mbr)!=null;
    
public synchronized MemberImpl[]expire(long maxtime)
Runs a refresh cycle and returns a list of members that has expired. This also removes the members from the membership, in such a way that getMembers() = getMembers() - expire()

param
maxtime - the max time a member can remain unannounced before it is considered dead.
return
the list of expired members

        if(!hasMembers() )
           return EMPTY_MEMBERS;
       
        ArrayList list = null;
        Iterator i = map.values().iterator();
        while(i.hasNext()) {
            MbrEntry entry = (MbrEntry)i.next();
            if( entry.hasExpired(maxtime) ) {
                if(list == null) // only need a list when members are expired (smaller gc)
                    list = new java.util.ArrayList();
                list.add(entry.getMember());
            }
        }
        
        if(list != null) {
            MemberImpl[] result = new MemberImpl[list.size()];
            list.toArray(result);
            for( int j=0; j<result.length; j++) {
                removeMember(result[j]);
            }
            return result;
        } else {
            return EMPTY_MEMBERS ;
        }
    
public MemberImplgetMember(org.apache.catalina.tribes.Member mbr)

        if(hasMembers()) {
            MemberImpl result = null;
            for ( int i=0; i<this.members.length && result==null; i++ ) {
                if ( members[i].equals(mbr) ) result = members[i];
            }//for
            return result;
        } else {
            return null;
        }
    
protected synchronized org.apache.catalina.tribes.membership.Membership$MbrEntry[]getMemberEntries()
get a copy from all member entries

        MbrEntry[] result = new MbrEntry[map.size()];
        java.util.Iterator i = map.entrySet().iterator();
        int pos = 0;
        while ( i.hasNext() )
            result[pos++] = ((MbrEntry)((java.util.Map.Entry)i.next()).getValue());
        return result;
    
public MemberImpl[]getMembers()
Returning a list of all the members in the membership We not need a copy: add and remove generate new arrays.

        if(hasMembers()) {
            return members;
        } else {
            return EMPTY_MEMBERS;
        }
    
public booleanhasMembers()
Returning that service has members or not

        return members.length > 0 ;
    
public synchronized booleanmemberAlive(MemberImpl member)
Notify the membership that this member has announced itself.

param
member - the member that just pinged us
return
- true if this member is new to the cluster, false otherwise.
return
- false if this member is the local member or updated.

        boolean result = false;
        //ignore ourselves
        if (  member.equals(local) ) return result;

        //return true if the membership has changed
        MbrEntry entry = (MbrEntry)map.get(member);
        if ( entry == null ) {
            entry = addMember(member);
            result = true;
       } else {
            //update the member alive time
            MemberImpl updateMember = entry.getMember() ;
            if(updateMember.getMemberAliveTime() != member.getMemberAliveTime()) {
                //update fields that can change
                updateMember.setMemberAliveTime(member.getMemberAliveTime());
                updateMember.setPayload(member.getPayload());
                updateMember.setCommand(member.getCommand());
                Arrays.sort(members, memberComparator);
            }
        }
        entry.accessed();
        return result;
    
public voidremoveMember(MemberImpl member)
Remove a member from this component.

param
member The member to remove

        map.remove(member);
        synchronized (members) {
            int n = -1;
            for (int i = 0; i < members.length; i++) {
                if (members[i] == member || members[i].equals(member)) {
                    n = i;
                    break;
                }
            }
            if (n < 0) return;
            MemberImpl results[] = new MemberImpl[members.length - 1];
            int j = 0;
            for (int i = 0; i < members.length; i++) {
                if (i != n)
                    results[j++] = members[i];
            }
            members = results;
        }
    
public synchronized voidreset()
Reset the membership and start over fresh. Ie, delete all the members and wait for them to ping again and join this membership

        map.clear();
        members = EMPTY_MEMBERS ;