/
entry.go
267 lines (234 loc) · 9.72 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package lookup
import (
"sync"
"sync/atomic"
"github.com/m3db/m3/src/dbnode/storage/index"
"github.com/m3db/m3/src/dbnode/storage/series"
xtime "github.com/m3db/m3/src/x/time"
)
const (
maxUint64 = ^uint64(0)
maxInt64 = int64(maxUint64 >> 1)
)
// Entry is the entry in the shard ident.ID -> series map. It has additional
// members to track lifecycle and minimize indexing overhead.
// NB: users are expected to use `NewEntry` to construct these objects.
type Entry struct {
Series series.DatabaseSeries
Index uint64
curReadWriters int32
reverseIndex entryIndexState
}
// OnReleaseReadWriteRef is a callback that can release
// a strongly held series read/write ref.
type OnReleaseReadWriteRef interface {
OnReleaseReadWriteRef()
}
// ensure Entry satifies the `OnReleaseReadWriteRef` interface.
var _ OnReleaseReadWriteRef = &Entry{}
// ensure Entry satisfies the `index.OnIndexSeries` interface.
var _ index.OnIndexSeries = &Entry{}
// NewEntry returns a new Entry.
func NewEntry(series series.DatabaseSeries, index uint64) *Entry {
entry := &Entry{
Series: series,
Index: index,
}
entry.reverseIndex.states = entry.reverseIndex._staticAloc[:0]
return entry
}
// ReaderWriterCount returns the current ref count on the Entry.
func (entry *Entry) ReaderWriterCount() int32 {
return atomic.LoadInt32(&entry.curReadWriters)
}
// IncrementReaderWriterCount increments the ref count on the Entry.
func (entry *Entry) IncrementReaderWriterCount() {
atomic.AddInt32(&entry.curReadWriters, 1)
}
// DecrementReaderWriterCount decrements the ref count on the Entry.
func (entry *Entry) DecrementReaderWriterCount() {
atomic.AddInt32(&entry.curReadWriters, -1)
}
// OnReleaseReadWriteRef decrements a read/write ref, it's named
// differently to decouple the concrete task needed when a ref
// is released and the intent to release the ref (simpler for
// caller readability/reasoning).
func (entry *Entry) OnReleaseReadWriteRef() {
// All we do when we release a read/write ref is decrement.
entry.DecrementReaderWriterCount()
}
// IndexedForBlockStart returns a bool to indicate if the Entry has been successfully
// indexed for the given index blockstart.
func (entry *Entry) IndexedForBlockStart(indexBlockStart xtime.UnixNano) bool {
entry.reverseIndex.RLock()
isIndexed := entry.reverseIndex.indexedWithRLock(indexBlockStart)
entry.reverseIndex.RUnlock()
return isIndexed
}
// NeedsIndexUpdate returns a bool to indicate if the Entry needs to be indexed
// for the provided blockStart. It only allows a single index attempt at a time
// for a single entry.
// NB(prateek): NeedsIndexUpdate is a CAS, i.e. when this method returns true, it
// also sets state on the entry to indicate that a write for the given blockStart
// is going to be sent to the index, and other go routines should not attempt the
// same write. Callers are expected to ensure they follow this guideline.
// Further, every call to NeedsIndexUpdate which returns true needs to have a corresponding
// OnIndexFinalze() call. This is required for correct lifecycle maintenance.
func (entry *Entry) NeedsIndexUpdate(indexBlockStartForWrite xtime.UnixNano) bool {
// first we try the low-cost path: acquire a RLock and see if the given block start
// has been marked successful or that we've attempted it.
entry.reverseIndex.RLock()
alreadyIndexedOrAttempted := entry.reverseIndex.indexedOrAttemptedWithRLock(indexBlockStartForWrite)
entry.reverseIndex.RUnlock()
if alreadyIndexedOrAttempted {
// if so, the entry does not need to be indexed.
return false
}
// now acquire a write lock and set that we're going to attempt to do this so we don't try
// multiple times.
entry.reverseIndex.Lock()
// NB(prateek): not defer-ing here, need to avoid the the extra ~150ns to minimize contention.
// but first, we have to ensure no one has done so since we released the read lock
alreadyIndexedOrAttempted = entry.reverseIndex.indexedOrAttemptedWithRLock(indexBlockStartForWrite)
if alreadyIndexedOrAttempted {
entry.reverseIndex.Unlock()
return false
}
entry.reverseIndex.setAttemptWithWLock(indexBlockStartForWrite, true)
entry.reverseIndex.Unlock()
return true
}
// OnIndexPrepare prepares the Entry to be handed off to the indexing sub-system.
// NB(prateek): we retain the ref count on the entry while the indexing is pending,
// the callback executed on the entry once the indexing is completed releases this
// reference.
func (entry *Entry) OnIndexPrepare() {
entry.IncrementReaderWriterCount()
}
// OnIndexSuccess marks the given block start as successfully indexed.
func (entry *Entry) OnIndexSuccess(blockStartNanos xtime.UnixNano) {
entry.reverseIndex.Lock()
entry.reverseIndex.setSuccessWithWLock(blockStartNanos)
entry.reverseIndex.Unlock()
}
// OnIndexFinalize marks any attempt for the given block start as finished
// and decrements the entry ref count.
func (entry *Entry) OnIndexFinalize(blockStartNanos xtime.UnixNano) {
entry.reverseIndex.Lock()
entry.reverseIndex.setAttemptWithWLock(blockStartNanos, false)
entry.reverseIndex.Unlock()
// indicate the index has released held reference for provided write
entry.DecrementReaderWriterCount()
}
// entryIndexState is used to capture the state of indexing for a single shard
// entry. It's used to prevent redundant indexing operations.
// NB(prateek): We need this amount of state because in the worst case, as we can have 3 active blocks being
// written to. Albeit that's an edge case due to bad configuration. Even outside of that, 2 blocks can
// be written to due to delayed, out of order writes. Consider an index block size of 2h, and buffer
// past of 10m. Say a write comes in at 2.05p (wallclock) for 2.05p (timestamp in the write), we'd index
// the entry, and update the entry to have a success for 4p. Now imagine another write
// comes in at 2.06p (wallclock) for 1.57p (timestamp in the write). We need to differentiate that we don't
// have a write for the 12-2p block from the 2-4p block, or we'd drop the late write.
type entryIndexState struct {
sync.RWMutex
states []entryIndexBlockState
// NB(prateek): we alloc an array (not slice) of size 3, as that is
// the most we will need (only 3 blocks should ever be written to
// simultaneously in the worst case). We allocate it like we're doing
// to ensure it's along side the rest of the struct in memory. But
// we only access it through `states`, to ensure that it can be
// grown/shrunk as needed. Do not acccess it directly.
_staticAloc [3]entryIndexBlockState
}
// entryIndexBlockState is used to capture the state of indexing for a single shard
// entry for a given index block start. It's used to prevent attempts at double indexing
// for the same block start.
type entryIndexBlockState struct {
blockStart xtime.UnixNano
attempt bool
success bool
}
func (s *entryIndexState) indexedWithRLock(t xtime.UnixNano) bool {
for i := range s.states {
if s.states[i].blockStart.Equal(t) {
return s.states[i].success
}
}
return false
}
func (s *entryIndexState) indexedOrAttemptedWithRLock(t xtime.UnixNano) bool {
for i := range s.states {
if s.states[i].blockStart.Equal(t) {
return s.states[i].success || s.states[i].attempt
}
}
return false
}
func (s *entryIndexState) setSuccessWithWLock(t xtime.UnixNano) {
for i := range s.states {
if s.states[i].blockStart.Equal(t) {
s.states[i].success = true
return
}
}
// NB(r): If not inserted state yet that means we need to make an insertion,
// this will happen if synchronously indexing and we haven't called
// NeedIndexUpdate before we indexed the series.
s.insertBlockState(entryIndexBlockState{
blockStart: t,
success: true,
})
}
func (s *entryIndexState) setAttemptWithWLock(t xtime.UnixNano, attempt bool) {
// first check if we have the block start in the slice already
for i := range s.states {
if s.states[i].blockStart.Equal(t) {
s.states[i].attempt = attempt
return
}
}
s.insertBlockState(entryIndexBlockState{
blockStart: t,
attempt: attempt,
})
}
func (s *entryIndexState) insertBlockState(newState entryIndexBlockState) {
// i.e. we don't have the block start in the slice
// if we have less than 3 elements, we can just insert an element to the slice.
if len(s.states) < 3 {
s.states = append(s.states, newState)
return
}
// i.e. len(s.states) == 3, in this case, we update the entry with the lowest block start
// as we know only 3 writes can be active at any point. Think of this as a lazy compaction.
var (
minIdx = -1
minBlockStart = xtime.UnixNano(maxInt64)
)
for idx, blockState := range s.states {
if blockState.blockStart < minBlockStart {
minIdx = idx
minBlockStart = blockState.blockStart
}
}
s.states[minIdx] = newState
}