joint.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package quorum
  15. // JointConfig is a configuration of two groups of (possibly overlapping)
  16. // majority configurations. Decisions require the support of both majorities.
  17. type JointConfig [2]MajorityConfig
  18. func (c JointConfig) String() string {
  19. if len(c[1]) > 0 {
  20. return c[0].String() + "&&" + c[1].String()
  21. }
  22. return c[0].String()
  23. }
  24. // IDs returns a newly initialized map representing the set of voters present
  25. // in the joint configuration.
  26. func (c JointConfig) IDs() map[uint64]struct{} {
  27. m := map[uint64]struct{}{}
  28. for _, cc := range c {
  29. for id := range cc {
  30. m[id] = struct{}{}
  31. }
  32. }
  33. return m
  34. }
  35. // Describe returns a (multi-line) representation of the commit indexes for the
  36. // given lookuper.
  37. func (c JointConfig) Describe(l AckedIndexer) string {
  38. return MajorityConfig(c.IDs()).Describe(l)
  39. }
  40. // CommittedIndex returns the largest committed index for the given joint
  41. // quorum. An index is jointly committed if it is committed in both constituent
  42. // majorities.
  43. func (c JointConfig) CommittedIndex(l AckedIndexer) Index {
  44. idx0 := c[0].CommittedIndex(l)
  45. idx1 := c[1].CommittedIndex(l)
  46. if idx0 < idx1 {
  47. return idx0
  48. }
  49. return idx1
  50. }
  51. // VoteResult takes a mapping of voters to yes/no (true/false) votes and returns
  52. // a result indicating whether the vote is pending, lost, or won. A joint quorum
  53. // requires both majority quorums to vote in favor.
  54. func (c JointConfig) VoteResult(votes map[uint64]bool) VoteResult {
  55. r1 := c[0].VoteResult(votes)
  56. r2 := c[1].VoteResult(votes)
  57. if r1 == r2 {
  58. // If they agree, return the agreed state.
  59. return r1
  60. }
  61. if r1 == VoteLost || r2 == VoteLost {
  62. // If either config has lost, loss is the only possible outcome.
  63. return VoteLost
  64. }
  65. // One side won, the other one is pending, so the whole outcome is.
  66. return VotePending
  67. }