Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.rep.impl;
  
 import static com.sleepycat.je.rep.impl.RepParams.GROUP_NAME;
 
 import java.util.Set;
 
 
 public class GroupService extends ExecutingService {
 
     /* The replication node */
     final RepNode repNode;
     final RepGroupProtocol protocol;

    
List of sockets for in-flight requests. The socket is in this collection while the request is being processed, and must be removed before sending any response.

 
     private final Collection<SocketChannelactiveChannels =
         new ArrayList<SocketChannel>();
 
     private final Logger logger;
 
     /* Identifies the Group Service. */
     public static final String SERVICE_NAME = "Group";
 
     public GroupService(ServiceDispatcher dispatcherRepNode repNode) {
         super(dispatcher);
         this. = repNode;
 
         final DbConfigManager configManager =
             repNode.getRepImpl().getConfigManager();
         String groupName = configManager.get();
          = new RepGroupProtocol(groupName,
                                         repNode.getNameIdPair(),
                                         repNode.getRepImpl());
          = LoggerUtils.getLogger(getClass());
     }
     
     @Override
     protected void cancel() {
         Collection<SocketChannelchannels;
         synchronized (this) {
             channels = new ArrayList<SocketChannel>();
             .clear();
         }
         if (!channels.isEmpty()) {
             LoggerUtils.warning
                 (.getRepImpl(),
                  "In-flight GroupService request(s) canceled: node shutdown");
         }
         for (SocketChannel channel : channels) {
             try {
                 PrintWriter out =
                     new PrintWriter(channel.socket().getOutputStream(), true);
                 ResponseMessage rm =
                     .new Fail(."shutting down");
                 out.println(rm.wireFormat());
             } catch (IOException e) {
                 LoggerUtils.warning
                     (.getRepImpl(),
                     "IO error on socket: " + e.getMessage());
            } finally {
                if (channel.isOpen()) {
                    try {
                        channel.close();
                    }
                    catch (IOException e) {
                        LoggerUtils.warning
                            (.getRepImpl(),
                             "IO error on socket close: " + e.getMessage());
                    }
                }
            }
        }
    }
    /* Dynamically invoked process methods */

    
Wraps the replication group as currently cached on this node in a Response message and returns it.
    @SuppressWarnings("unused")
    public ResponseMessage process(GroupRequest groupRequest) {
        RepGroupImpl group = .getGroup();
        if (group == null) {
            return .new Fail(."no group info yet");
        }
        return .new GroupResponse(group);
    }

    
Ensures that the Monitor node, as described in the request, is a member of the group.

Parameters:
ensureNode the request message describing the monitor node
Returns:
EnsureOK message if the monitor node is already part of the rep group, or was just made a part of the replication group. It returns a Fail message if it could not be made part of the group. The message associated with the response provides further details.
    public ResponseMessage process(EnsureNode ensureNode) {
        RepNodeImpl node = ensureNode.getNode();
        try {
            ensureMaster();
            .getRepGroupDB().ensureMember(node);
            RepNodeImpl enode =
                .getGroup().getMember(node.getName());
            return .new EnsureOK(enode.getNameIdPair());
        } catch (ReplicaStateException e) {
            return .new Fail(.e.getMessage());
        } catch (DatabaseException e) {
            return .new Fail(.e.getMessage());
        }
    }

    
Removes a current member from the group.

Parameters:
removeMember the request identifying the member to be removed.
Returns:
OK message if the member was removed from the group.
    public ResponseMessage process(RemoveMember removeMember) {
        final String nodeName = removeMember.getNodeName();
        try {
            ensureMaster();
            .removeMember(nodeName);
            return .new OK();
        } catch (MemberNotFoundException e) {
            return .new Fail(.,
                                     e.getMessage());
        } catch (MasterStateException e) {
            return .new Fail(.e.getMessage());
        } catch (ReplicaStateException e) {
            return .new Fail(.e.getMessage());
        }  catch (DatabaseException e) {
            return .new Fail(.e.getMessage());
        }
    }

    
Update the network address for a dead replica.

Parameters:
updateAddress the request identifying the new network address for the node.
Returns:
OK message if the address is successfully updated.
    public ResponseMessage process(UpdateAddress updateAddress) {
        try {
            ensureMaster();
            .updateAddress(updateAddress.getNodeName(),
                                  updateAddress.getNewHostName(),
                                  updateAddress.getNewPort());
            return .new OK();
        } catch (MemberNotFoundException e) {
            return .new Fail(.,
                                     e.getMessage());
        } catch (MasterStateException e) {
            return .new Fail(.e.getMessage());
        } catch (ReplicaStateException e) {
            return .new Fail(.e.getMessage());
        } catch (DatabaseException e) {
            return .new Fail(.e.getMessage());
        }
    }

    
Transfer the master role from the current master to one of the specified replicas.

Parameters:
transferMaster the request identifying nodes to be considered for the role of new master
Returns:
null
    public ResponseMessage process(TransferMaster transferMaster) {
        try {
            ensureMaster();
            final String nodeList = transferMaster.getNodeNameList();
            final Set<Stringreplicas = parseNodeList(nodeList);
            final long timeout = transferMaster.getTimeout();
            final boolean force = transferMaster.getForceFlag();
            String winner = .transferMaster(replicastimeoutforce);
            return .new TransferOK(winner);
        } catch (ReplicaStateException e) {
            return .new Fail(.e.getMessage());
        } catch (MasterTransferFailureException e) {
            return .new Fail(.e.getMessage());
        } catch (DatabaseException e) {
            return .new Fail(.e.getMessage());
        } catch (IllegalArgumentException e) {
            return .new Fail(.e.toString());
        } catch (IllegalStateException e) {
            return .new Fail(.e.toString());
        }
    }
    
    private Set<StringparseNodeList(String list) {
        Set<Stringset = new HashSet<String>();
        StringTokenizer st = new StringTokenizer(list",");
        while (st.hasMoreTokens()) {
            set.add(st.nextToken());
        }
        return set;
    }
    private void ensureMaster() throws ReplicaStateException {
        if (!.isMaster()) {
            throw new ReplicaStateException
                ("GroupService operation can only be performed at master");
        }
    }
    synchronized private void registerSocket(SocketChannel sc) {
        .add(sc);
    }

    
Removes the given SocketChannel from our list of active sockets.

Before sending any response on the socket, this method must be invoked to claim ownership of it. This avoids a potential race between the request processing thread in the normal case, and a thread calling cancel() at env shutdown time.

Returns:
true, if the socket is still active (usual case); false otherwise, presumably because the service was shut down.
    synchronized private boolean unregisterSocket(SocketChannel sc) {
        return .remove(sc);
    }
    @Override
    public Runnable getRunnable(SocketChannel socketChannel) {
        return new GroupServiceRunnable(socketChannel);
    }
    class GroupServiceRunnable extends ExecutingRunnable {
        GroupServiceRunnable(SocketChannel socketChannel
                             RepGroupProtocol protocol) {
            super(socketChannelprotocoltrue);
            registerSocket(socketChannel);
        }
        @Override
        protected ResponseMessage getResponse(RequestMessage request)  
            throws IOException {
            ResponseMessage rm = .process(GroupService.thisrequest);
            /*
             * If the socket has already been closed, before we got a chance to
             * produce the response, then just discard the tardy response and
             * return null.
             */
            return unregisterSocket() ? rm : null;
        }
        @Override
        protected void logMessage(String message) {
            LoggerUtils.warning(.getRepImpl(), message);
        }
    }
New to GrepCode? Check out our FAQ X