read_only.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. acks map[uint64]struct{}
  29. }
  30. type readOnly struct {
  31. option ReadOnlyOption
  32. pendingReadIndex map[string]*readIndexStatus
  33. readIndexQueue []string
  34. }
  35. func newReadOnly(option ReadOnlyOption) *readOnly {
  36. return &readOnly{
  37. option: option,
  38. pendingReadIndex: make(map[string]*readIndexStatus),
  39. }
  40. }
  41. // addRequest adds a read only reuqest into readonly struct.
  42. // `index` is the commit index of the raft state machine when it received
  43. // the read only request.
  44. // `m` is the original read only request message from the local or remote node.
  45. func (ro *readOnly) addRequest(index uint64, m pb.Message) {
  46. s := string(m.Entries[0].Data)
  47. if _, ok := ro.pendingReadIndex[s]; ok {
  48. return
  49. }
  50. ro.pendingReadIndex[s] = &readIndexStatus{index: index, req: m, acks: make(map[uint64]struct{})}
  51. ro.readIndexQueue = append(ro.readIndexQueue, s)
  52. }
  53. // recvAck notifies the readonly struct that the raft state machine received
  54. // an acknowledgment of the heartbeat that attached with the read only request
  55. // context.
  56. func (ro *readOnly) recvAck(id uint64, context []byte) map[uint64]struct{} {
  57. rs, ok := ro.pendingReadIndex[string(context)]
  58. if !ok {
  59. return nil
  60. }
  61. rs.acks[id] = struct{}{}
  62. return rs.acks
  63. }
  64. // advance advances the read only request queue kept by the readonly struct.
  65. // It dequeues the requests until it finds the read only request that has
  66. // the same context as the given `m`.
  67. func (ro *readOnly) advance(m pb.Message) []*readIndexStatus {
  68. var (
  69. i int
  70. found bool
  71. )
  72. ctx := string(m.Context)
  73. rss := []*readIndexStatus{}
  74. for _, okctx := range ro.readIndexQueue {
  75. i++
  76. rs, ok := ro.pendingReadIndex[okctx]
  77. if !ok {
  78. panic("cannot find corresponding read state from pending map")
  79. }
  80. rss = append(rss, rs)
  81. if okctx == ctx {
  82. found = true
  83. break
  84. }
  85. }
  86. if found {
  87. ro.readIndexQueue = ro.readIndexQueue[i:]
  88. for _, rs := range rss {
  89. delete(ro.pendingReadIndex, string(rs.req.Entries[0].Data))
  90. }
  91. return rss
  92. }
  93. return nil
  94. }
  95. // lastPendingRequestCtx returns the context of the last pending read only
  96. // request in readonly struct.
  97. func (ro *readOnly) lastPendingRequestCtx() string {
  98. if len(ro.readIndexQueue) == 0 {
  99. return ""
  100. }
  101. return ro.readIndexQueue[len(ro.readIndexQueue)-1]
  102. }