forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.go
301 lines (258 loc) · 6.51 KB
/
lists.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2015 DGraph Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package posting
import (
"flag"
"log"
"math/rand"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/context"
"github.com/dgraph-io/dgraph/commit"
"github.com/dgraph-io/dgraph/store"
"github.com/dgryski/go-farm"
"github.com/zond/gotomic"
)
var maxmemory = flag.Uint64("stw_ram_mb", 4096,
"If RAM usage exceeds this, we stop the world, and flush our buffers.")
type mergeRoutines struct {
sync.RWMutex
count int
}
func (mr *mergeRoutines) Count() int {
mr.RLock()
defer mr.RUnlock()
return mr.count
}
func (mr *mergeRoutines) Add(delta int) {
mr.Lock()
mr.count += delta
mr.Unlock()
}
type counters struct {
ticker *time.Ticker
added uint64
merged uint64
clean uint64
lastVal uint64
}
func (c *counters) periodicLog() {
for _ = range c.ticker.C {
c.log()
}
}
func (c *counters) log() {
added := atomic.LoadUint64(&c.added)
merged := atomic.LoadUint64(&c.merged)
lastVal := atomic.LoadUint64(&c.lastVal)
if merged == lastVal {
// Ignore.
return
}
atomic.StoreUint64(&c.lastVal, merged)
var pending uint64
if added > merged {
pending = added - merged
}
log.Printf("List merge counters. added: %d merged: %d clean: %d"+
" pending: %d mapsize: %d dirtysize: %d\n",
added, merged, atomic.LoadUint64(&c.clean),
pending, lhmap.Size(), dirtymap.Size())
}
func NewCounters() *counters {
c := new(counters)
c.ticker = time.NewTicker(time.Second)
return c
}
var MIB, MAX_MEMORY uint64
func aggressivelyEvict(ms runtime.MemStats) {
// Okay, we exceed the max memory threshold.
// Stop the world, and deal with this first.
stopTheWorld.Lock()
defer stopTheWorld.Unlock()
megs := ms.Alloc / MIB
log.Printf("Memory usage over threshold. STW. Allocated MB: %v\n", megs)
log.Println("Calling merge on all lists.")
MergeLists(100 * runtime.GOMAXPROCS(-1))
log.Println("Merged lists. Calling GC.")
runtime.GC() // Call GC to do some cleanup.
log.Println("Trying to free OS memory")
debug.FreeOSMemory()
runtime.ReadMemStats(&ms)
megs = ms.Alloc / MIB
log.Printf("Memory usage after calling GC. Allocated MB: %v", megs)
}
func gentlyMerge(mr *mergeRoutines) {
defer mr.Add(-1)
ctr := NewCounters()
defer ctr.ticker.Stop()
// Pick 7% of the dirty map or 400 keys, whichever is higher.
pick := int(float64(dirtymap.Size()) * 0.07)
if pick < 400 {
pick = 400
}
// We should start picking up elements from a randomly selected index,
// otherwise, the same keys would keep on getting merged, while the
// rest would never get a chance.
var start int
n := dirtymap.Size() - pick
if n <= 0 {
start = 0
} else {
start = rand.Intn(n)
}
var hs []gotomic.Hashable
idx := 0
dirtymap.Each(func(k gotomic.Hashable, v gotomic.Thing) bool {
if idx < start {
idx += 1
return false
}
hs = append(hs, k)
return len(hs) >= pick
})
for _, hid := range hs {
dirtymap.Delete(hid)
ret, ok := lhmap.Get(hid)
if !ok || ret == nil {
continue
}
// Not calling processOne, because we don't want to
// remove the postings list from the map, to avoid
// a race condition, where another caller re-creates the
// posting list before a merge happens.
l := ret.(*List)
if l == nil {
continue
}
mergeAndUpdate(l, ctr)
}
ctr.log()
}
func checkMemoryUsage() {
MIB = 1 << 20
MAX_MEMORY = *maxmemory * MIB
var mr mergeRoutines
for _ = range time.Tick(5 * time.Second) {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
if ms.Alloc > MAX_MEMORY {
aggressivelyEvict(ms)
} else {
// If merging is slow, we don't want to end up having too many goroutines
// merging the dirty list. This should keep them in check.
// With a value of 18 and duration of 5 seconds, some goroutines are
// taking over 1.5 mins to finish.
if mr.Count() > 18 {
log.Println("Skipping gentle merging.")
continue
}
mr.Add(1)
// gentlyMerge can take a while to finish. So, run it in a goroutine.
go gentlyMerge(&mr)
}
}
}
var stopTheWorld sync.RWMutex
var lhmap *gotomic.Hash
var dirtymap *gotomic.Hash
var clog *commit.Logger
func Init(log *commit.Logger) {
lhmap = gotomic.NewHash()
dirtymap = gotomic.NewHash()
clog = log
go checkMemoryUsage()
}
func GetOrCreate(key []byte, pstore *store.Store) *List {
stopTheWorld.RLock()
defer stopTheWorld.RUnlock()
uid := farm.Fingerprint64(key)
ukey := gotomic.IntKey(uid)
lp, _ := lhmap.Get(ukey)
if lp != nil {
return lp.(*List)
}
l := NewList()
if inserted := lhmap.PutIfMissing(ukey, l); inserted {
l.init(key, pstore, clog)
return l
} else {
lp, _ = lhmap.Get(ukey)
return lp.(*List)
}
}
func mergeAndUpdate(l *List, c *counters) {
if l == nil {
return
}
if merged, err := l.MergeIfDirty(context.Background()); err != nil {
log.Printf("Error while commiting dirty list: %v\n", err)
} else if merged {
atomic.AddUint64(&c.merged, 1)
} else {
atomic.AddUint64(&c.clean, 1)
}
}
func processOne(k gotomic.Hashable, c *counters) {
ret, _ := lhmap.Delete(k)
if ret == nil {
return
}
l := ret.(*List)
if l == nil {
return
}
l.SetForDeletion() // No more AddMutation.
mergeAndUpdate(l, c)
}
// For on-demand merging of all lists.
func process(ch chan gotomic.Hashable, c *counters, wg *sync.WaitGroup) {
// No need to go through dirtymap, because we're going through
// everything right now anyways.
for k := range ch {
processOne(k, c)
}
if wg != nil {
wg.Done()
}
}
func queueAll(ch chan gotomic.Hashable, c *counters) {
lhmap.Each(func(k gotomic.Hashable, v gotomic.Thing) bool {
ch <- k
atomic.AddUint64(&c.added, 1)
return false // If this returns true, Each would break.
})
close(ch)
}
func MergeLists(numRoutines int) {
// We're merging all the lists, so just create a new dirtymap.
dirtymap = gotomic.NewHash()
ch := make(chan gotomic.Hashable, 10000)
c := NewCounters()
go c.periodicLog()
defer c.ticker.Stop()
go queueAll(ch, c)
wg := new(sync.WaitGroup)
for i := 0; i < numRoutines; i++ {
wg.Add(1)
go process(ch, c, wg)
}
wg.Wait()
c.ticker.Stop()
}