read_only.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2016 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 raft
  15. import pb "go.etcd.io/etcd/raft/raftpb"
  16. // ReadState provides state for read only query.
  17. // It's caller's responsibility to call ReadIndex first before getting
  18. // this state from ready, it's also caller's duty to differentiate if this
  19. // state is what it requests through RequestCtx, eg. given a unique id as
  20. // RequestCtx
  21. type ReadState struct {
  22. Index uint64
  23. RequestCtx []byte
  24. }
  25. type readIndexStatus struct {
  26. req pb.Message
  27. index uint64
  28. // NB: this never records 'false', but it's more convenient to use this
  29. // instead of a map[uint64]struct{} due to the API of quorum.VoteResult. If
  30. // this becomes performance sensitive enough (doubtful), quorum.VoteResult
  31. // can change to an API that is closer to that of CommittedIndex.
  32. acks map[uint64]bool
  33. }
  34. type readOnly struct {
  35. option ReadOnlyOption
  36. pendingReadIndex map[string]*readIndexStatus
  37. readIndexQueue []string
  38. }
  39. func newReadOnly(option ReadOnlyOption) *readOnly {
  40. return &readOnly{
  41. option: option,
  42. pendingReadIndex: make(map[string]*readIndexStatus),
  43. }
  44. }
  45. // addRequest adds a read only reuqest into readonly struct.
  46. // `index` is the commit index of the raft state machine when it received
  47. // the read only request.
  48. // `m` is the original read only request message from the local or remote node.
  49. func (ro *readOnly) addRequest(index uint64, m pb.Message) {
  50. s := string(m.Entries[0].Data)
  51. if _, ok := ro.pendingReadIndex[s]; ok {
  52. return
  53. }
  54. ro.pendingReadIndex[s] = &readIndexStatus{index: index, req: m, acks: make(map[uint64]bool)}
  55. ro.readIndexQueue = append(ro.readIndexQueue, s)
  56. }
  57. // recvAck notifies the readonly struct that the raft state machine received
  58. // an acknowledgment of the heartbeat that attached with the read only request
  59. // context.
  60. func (ro *readOnly) recvAck(id uint64, context []byte) map[uint64]bool {
  61. rs, ok := ro.pendingReadIndex[string(context)]
  62. if !ok {
  63. return nil
  64. }
  65. rs.acks[id] = true
  66. return rs.acks
  67. }
  68. // advance advances the read only request queue kept by the readonly struct.
  69. // It dequeues the requests until it finds the read only request that has
  70. // the same context as the given `m`.
  71. func (ro *readOnly) advance(m pb.Message) []*readIndexStatus {
  72. var (
  73. i int
  74. found bool
  75. )
  76. ctx := string(m.Context)
  77. rss := []*readIndexStatus{}
  78. for _, okctx := range ro.readIndexQueue {
  79. i++
  80. rs, ok := ro.pendingReadIndex[okctx]
  81. if !ok {
  82. panic("cannot find corresponding read state from pending map")
  83. }
  84. rss = append(rss, rs)
  85. if okctx == ctx {
  86. found = true
  87. break
  88. }
  89. }
  90. if found {
  91. ro.readIndexQueue = ro.readIndexQueue[i:]
  92. for _, rs := range rss {
  93. delete(ro.pendingReadIndex, string(rs.req.Entries[0].Data))
  94. }
  95. return rss
  96. }
  97. return nil
  98. }
  99. // lastPendingRequestCtx returns the context of the last pending read only
  100. // request in readonly struct.
  101. func (ro *readOnly) lastPendingRequestCtx() string {
  102. if len(ro.readIndexQueue) == 0 {
  103. return ""
  104. }
  105. return ro.readIndexQueue[len(ro.readIndexQueue)-1]
  106. }