read_only.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "github.com/coreos/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. ctx := string(m.Entries[0].Data)
  47. if _, ok := ro.pendingReadIndex[ctx]; ok {
  48. return
  49. }
  50. ro.pendingReadIndex[ctx] = &readIndexStatus{index: index, req: m, acks: make(map[uint64]struct{})}
  51. ro.readIndexQueue = append(ro.readIndexQueue, ctx)
  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(m pb.Message) int {
  57. rs, ok := ro.pendingReadIndex[string(m.Context)]
  58. if !ok {
  59. return 0
  60. }
  61. rs.acks[m.From] = struct{}{}
  62. // add one to include an ack from local node
  63. return len(rs.acks) + 1
  64. }
  65. // advance advances the read only request queue kept by the readonly struct.
  66. // It dequeues the requests until it finds the read only request that has
  67. // the same context as the given `m`.
  68. func (ro *readOnly) advance(m pb.Message) []*readIndexStatus {
  69. var (
  70. i int
  71. found bool
  72. )
  73. ctx := string(m.Context)
  74. rss := []*readIndexStatus{}
  75. for _, okctx := range ro.readIndexQueue {
  76. i++
  77. rs, ok := ro.pendingReadIndex[okctx]
  78. if !ok {
  79. panic("cannot find corresponding read state from pending map")
  80. }
  81. rss = append(rss, rs)
  82. if okctx == ctx {
  83. found = true
  84. break
  85. }
  86. }
  87. if found {
  88. ro.readIndexQueue = ro.readIndexQueue[i:]
  89. for _, rs := range rss {
  90. delete(ro.pendingReadIndex, string(rs.req.Entries[0].Data))
  91. }
  92. return rss
  93. }
  94. return nil
  95. }
  96. // lastPendingRequestCtx returns the context of the last pending read only
  97. // request in readonly struct.
  98. func (ro *readOnly) lastPendingRequestCtx() string {
  99. if len(ro.readIndexQueue) == 0 {
  100. return ""
  101. }
  102. return ro.readIndexQueue[len(ro.readIndexQueue)-1]
  103. }