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.elections;
  
 import static com.sleepycat.je.rep.impl.RepParams.ELECTIONS_OPEN_TIMEOUT;
 import static com.sleepycat.je.rep.impl.RepParams.ELECTIONS_READ_TIMEOUT;
 
Defines the request/response messages used in the implementation of elections. From Proposer to Acceptor: Propose -> Promise | Reject Accept -> Accepted | Reject From Proposer initiator to Learners: Result -> none The following exchange is not part of the elections process, but is used by the Monitor to query a Learner for the latest election result it's aware of, when the Monitor first starts up. From Monitor to Learner MasterQuery -> MasterQueryResponse | None
 
 public class Protocol extends TextProtocol {
 
     /* Protocol version string. Format: <major version>.<minor version> */
     /* It's used to ensure compatibility across versions. */
     private static final String VERSION = "2.0";
 
     /* An instance of ProposalParser used to de-serialize proposals */
     private final ProposalParser proposalParser;
 
     /* An instance of ValueParser used to de-serialize values */
     private final ValueParser valueParser;
 
     /* Request Operations */
     public final MessageOp PROPOSE;
     public final MessageOp ACCEPT;
     public final MessageOp RESULT;
     public final MessageOp MASTER_QUERY;
     public final MessageOp SHUTDOWN;
 
     /* Response operations */
     public final MessageOp REJECT;
     public final MessageOp PROMISE;
     public final MessageOp ACCEPTED;
     public final MessageOp MASTER_QUERY_RESPONSE;

    
Creates an instance of the Protocol.

Parameters:
proposalParser parses a string into a Proposal object.
valueParser parses a string into a Value object.
nameIdPair a unique identifier for this election participant.
Parameter:
groupName the name of the group running the election process.
 
     public Protocol(ProposalParser proposalParser,
                     ValueParser valueParser,
                     String  groupName,
                     NameIdPair nameIdPair,
                     RepImpl repImpl) {
 
         /* Request operations */
         super(groupNamenameIdPairrepImpl);
 
          = new MessageOp("P"Propose.class);
          = new MessageOp("A"Accept.class);
          = new MessageOp("RE"Result.class);
          = new MessageOp("MQ"MasterQuery.class);
          = new MessageOp("X"Shutdown.class );
 
          = new MessageOp("R"Reject.class);
          = new MessageOp("PR"Promise.class);
          = new MessageOp("AD"Accepted.class);
          =
             new MessageOp("MQR"MasterQueryResponse.class);
 
         initializeMessageOps(new MessageOp[] {
                 ,
                 ,
                 ,
                 ,
                 ,
 
                ,
                ,
                ,
                ,
        });
        this. = proposalParser;
        this. = valueParser;
    }

    
Promise response message. It's sent in response to a Propose message.
    public class Promise extends ResponseMessage {
        private Proposal highestProposal = null;
        private Value acceptedValue = null;
        private Value suggestion = null;
        private long suggestionWeight = .;
        private final int priority;
        private int logVersion;
        private JEVersion jeVersion;
        public Promise(Proposal highestProposal,
                       Value value,
                       Value suggestion,
                       long suggestionWeight,
                       int priority
                       int logVersion,
                       JEVersion jeVersion) {
            this. = highestProposal;
            this. = value;
            this. = suggestion;
            this. = suggestionWeight;
            this. = priority;
            this. = logVersion;
            this. = jeVersion;
        }
        public Promise(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
             = .parse(nextPayloadToken());
             = .parse(nextPayloadToken());
            String weight = nextPayloadToken();
             =
                "".equals(weight) ?
                . :
                Long.parseLong(weight);
             = Integer.parseInt(nextPayloadToken());
            if (getMajorVersionNumber() > 1) {
                 = Integer.parseInt(nextPayloadToken());
                 = new JEVersion(nextPayloadToken());
            }
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + getOuterType().hashCode();
            result = prime * result
                    + (( == null) ? 0 : .hashCode());
            result = prime
                    * result
                    + (( == null) ? 0
                            : .hashCode());
            result = prime * result + ;
            result = prime * result
                    + (( == null) ? 0 : .hashCode());
            result = prime * result
                    + (int) ( ^ ( >>> 32));
            if (getMajorVersionNumber() > 1) {
                result += primeresult +  + .hashCode();
            }
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            Promise other = (Promiseobj;
            if (!getOuterType().equals(other.getOuterType())) {
                return false;
            }
            if ( == null) {
                if (other.acceptedValue != null) {
                    return false;
                }
            } else if (!.equals(other.acceptedValue)) {
                return false;
            }
            if ( == null) {
                if (other.highestProposal != null) {
                    return false;
                }
            } else if (!.equals(other.highestProposal)) {
                return false;
            }
            if ( != other.priority) {
                return false;
            }
            if (getMajorVersionNumber() > 1) {
                if ( != other.logVersion) {
                    return false;
                }
                if (.compareTo(other.jeVersion) != 0) {
                    return false;
                }
            }
            if ( == null) {
                if (other.suggestion != null) {
                    return false;
                }
            } else if (!.equals(other.suggestion)) {
                return false;
            }
            if ( != other.suggestionWeight) {
                return false;
            }
            return true;
        }
        public String wireFormat() {
            String line = 
                wireFormatPrefix() +
                 +
                (( != null) ?
                 .wireFormat() :
                 "") +
                 +
                (( != null) ? .wireFormat() : "") +
                 +
                (( != null) ?  .wireFormat() : "") +
                 +
                (( == .) ?
                 "" :
                 Long.toString()) +
                  +
                 ;
           
           if (getMajorVersionNumber() > 1) {
              line +=  +  +  +
                      .toString();
           }
           return line;
        }
        Proposal getHighestProposal() {
            return ;
        }
        Value getAcceptedValue() {
            return ;
        }
        Value getSuggestion() {
            return ;
        }
        long getSuggestionRanking() {
            return ;
        }
        int getPriority() {
            return ;
        }
        int getLogVersion() {
            return ;
        }
        JEVersion getJEVersion() {
            return ;
        }
        private Protocol getOuterType() {
            return Protocol.this;
        }
    }

    
Response to a successful Accept message.
    public class Accepted extends ResponseMessage {
        private final Proposal proposal;
        private final Value value;
        Accepted(Proposal proposalValue value) {
            assert(proposal!= null);
            assert(value != null);
            this. = proposal;
            this. = value;
        }
        public Accepted(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
             = .parse(nextPayloadToken());
              = .parse(nextPayloadToken());
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result
                    + (( == null) ? 0 : .hashCode());
            result = prime * result + (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (!(obj instanceof Accepted)) {
                return false;
            }
            final Accepted other = (Acceptedobj;
            if ( == null) {
                if (other.proposal != null) {
                    return false;
                }
            } else if (!.equals(other.proposal)) {
                return false;
            }
            if ( == null) {
                if (other.value != null) {
                    return false;
                }
            } else if (!.equals(other.value)) {
                return false;
            }
            return true;
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        public String wireFormat() {
            return wireFormatPrefix() +  + .wireFormat() +
                 + .wireFormat();
        }
        public Value getValue() {
            return ;
        }
        public Proposal getProposal() {
            return ;
        }
    }

    
The response to a Master Query request. It simply repackages the Accepted response.
    public class MasterQueryResponse extends Accepted {
        MasterQueryResponse(Proposal proposalValue value) {
            super(proposalvalue);
        }
        public MasterQueryResponse(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        @Override
        protected String getMessagePrefix() {
            return ;
        }
    }

    
Reject response to a message.
    public class Reject extends ResponseMessage {
        private final Proposal higherProposal;
        Reject(Proposal higherProposal) {
            this. = higherProposal;
        }
        public Reject(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
             = .parse(nextPayloadToken());
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + 
                (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (!(obj instanceof Reject)) {
                return false;
            }
            final Reject other = (Rejectobj;
            if ( == null) {
                if (other.higherProposal != null) {
                    return false;
                }
            } else if (!.equals(other.higherProposal)) {
                return false;
            }
            return true;
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        public String wireFormat() {
            return wireFormatPrefix() +  + .wireFormat();
        }
        Proposal getHigherProposal() {
            return ;
        }
    }

    
Propose request used in Phase 1 of Paxos
    public class Propose extends RequestMessage {
        protected final Proposal proposal;
        Propose(Proposal proposal) {
            this. = proposal;
        }
        public Propose(String requestLineString[] tokens)
            throws InvalidMessageException {
            super(requestLinetokens);
             = .parse(nextPayloadToken());
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result
                    + (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (!(obj instanceof Propose)) {
                return false;
            }
            final Propose other = (Proposeobj;
            if ( == null) {
                if (other.proposal != null) {
                    return false;
                }
            } else if (!.equals(other.proposal)) {
                return false;
            }
            return true;
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        public String wireFormat() {
            return wireFormatPrefix() +  +  .wireFormat();
        }
        Proposal getProposal() {
            return ;
        }
    }
    public class Shutdown extends RequestMessage {
        public Shutdown() {}
        public Shutdown(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        public String wireFormat() {
            return wireFormatPrefix();
        }
    }

    
Accept request issued in Phase 2 of paxos.
    public class Accept extends Propose {
        private final Value value;
        Accept(Proposal proposalValue value) {
            super(proposal);
            this. = value;
        }
        public Accept(String requestLineString[] tokens)
            throws InvalidMessageException {
            super(requestLinetokens);
             = .parse(nextPayloadToken());
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + (( == null) ? 0 : .hashCode());
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (!(obj instanceof Accept)) {
                return false;
            }
            final Accept other = (Acceptobj;
            if ( == null) {
                if (other.value != null) {
                    return false;
                }
            } else if (!.equals(other.value)) {
                return false;
            }
            return true;
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        @Override
        public String wireFormat() {
            return super.wireFormat() +  + .wireFormat();
        }
        Value getValue() {
            return ;
        }
    }

    
Used to inform Learners of a "chosen value".
    public class Result extends Accept {
        Result(Proposal proposalValue value) {
            super(proposalvalue);
        }
        public Result(String requestLineString[] tokens)
            throws InvalidMessageException {
            super(requestLinetokens);
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
    }

    
Used to query the Learner for a current master
    public class MasterQuery extends RequestMessage {
        public MasterQuery() {}
        public MasterQuery(String responseLineString[] tokens)
            throws InvalidMessageException {
            super(responseLinetokens);
        }
        @Override
        public MessageOp getOp() {
            return ;
        }
        @Override
        protected String getMessagePrefix() {
            return ;
        }
        public String wireFormat() {
            return wireFormatPrefix();
        }
        @Override
        public String toString() {
            return getOp() + " " + getMessagePrefix() + " " + wireFormat();
        }
    }
    /* Represents a Value in Paxos. */
    public interface Value extends WireFormatable  {
    }
    public interface ValueParser {
        
Converts the wire format back into a Value

Parameters:
wireFormat String representation of a Value
Returns:
the de-serialized Value
        abstract Value parse(String wireFormat);
    }

    
A String based value implementation used as the "default" Value
    public static class StringValue extends StringFormatable implements Value {
        StringValue() {
            super(null);
        }
        public StringValue(String s) {
            super(s);
        }
        @Override
        public String toString() {
            return "Value:" + ;
        }
        public String getString() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X