1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2014 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.je.rep;
9 
10 import com.sleepycat.je.EnvironmentFailureException;
11 
12 /**
13  * The quorum policy determine the number of nodes that must participate to
14  * pick the winner of an election, and therefore the master of the group.
15  * The default quorum policy during the lifetime of the group is
16  * QuorumPolicy.SIMPLE_MAJORITY. The only time that the application needs to
17  * specify a specific quorum policy is at node startup time, by passing one
18  * to the {@link ReplicatedEnvironment} constructor.
19  *
20  * <p>Note that {@link NodeType#SECONDARY} nodes are not counted as part of
21  * master election quorums.
22  */
23 public enum QuorumPolicy {
24 
25     /**
26      * All participants are required to vote.
27      */
28     ALL,
29 
30      /**
31       *  A simple majority of participants is required to vote.
32       */
33     SIMPLE_MAJORITY;
34 
35     /**
36      * Returns the minimum number of nodes to needed meet the quorum policy.
37      *
38      * @param groupSize the number of election participants in the replication
39      *        group
40      *
41      * @return the number of nodes that are needed for a quorum for a group
42      *         with {@code groupSize} number of election participants
43      */
quorumSize(int groupSize)44     public int quorumSize(int groupSize) {
45         switch (this) {
46             case ALL:
47                 return groupSize;
48 
49             case SIMPLE_MAJORITY:
50                 return (groupSize / 2 + 1);
51 
52             default:
53                 throw EnvironmentFailureException.unexpectedState
54                     ("Unknown quorum:" + this);
55         }
56     }
57 }
58