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