forked from buraksezer/olric
/
olricdb.go
360 lines (315 loc) · 8.07 KB
/
olricdb.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2018 Burak Sezer
//
// 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 olricdb provides embeddable, in-memory and distributed key/value store.*/
package olricdb
import (
"context"
"fmt"
"log"
"os"
"strconv"
"sync"
"github.com/buraksezer/consistent"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/logutils"
"github.com/hashicorp/memberlist"
"github.com/julienschmidt/httprouter"
)
// OlricDB represens an member in the cluster. All functions on the OlricDB structure are safe to call concurrently.
type OlricDB struct {
this host
config *Config
logger *log.Logger
hasher Hasher
discovery *discovery
consistent *consistent.Consistent
partitions map[uint64]*partition
backups map[uint64]*partition
transport *httpTransport
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
fsckMtx sync.Mutex
routingMtx sync.Mutex
// To control non-bootstrapped OlricDB instance
bctx context.Context
bcancel context.CancelFunc
}
type vdata struct {
Value interface{}
TTL int64
}
type dmap struct {
sync.RWMutex
locker *locker
// user's key/value pairs
d map[uint64]vdata
}
type partition struct {
sync.RWMutex
id uint64
backup bool
owners []host
// map name > map
m map[string]*dmap
}
// New creates a new OlricDB object, otherwise returns an error.
func New(c *Config) (*OlricDB, error) {
if c == nil {
c = &Config{}
}
if c.LogOutput != nil && c.Logger != nil {
return nil, fmt.Errorf("cannot specify both LogOutput and Logger")
}
if c.Logger == nil {
logDest := c.LogOutput
if logDest == nil {
logDest = os.Stderr
}
if c.LogLevel == "" {
c.LogLevel = DefaultLogLevel
}
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR", "INFO"},
MinLevel: logutils.LogLevel(c.LogLevel),
Writer: logDest,
}
c.Logger = log.New(logDest, "", log.LstdFlags)
c.Logger.SetOutput(filter)
}
if c.Hasher == nil {
c.Hasher = newDefaultHasher()
}
if c.Name == "" {
name, err := os.Hostname()
if err != nil {
return nil, err
}
c.Name = name + ":0"
}
if c.LoadFactor == 0 {
c.LoadFactor = DefaultLoadFactor
}
if c.PartitionCount == 0 {
c.PartitionCount = DefaultPartitionCount
}
if c.MemberlistConfig == nil {
c.MemberlistConfig = memberlist.DefaultLocalConfig()
}
cfg := consistent.Config{
Hasher: c.Hasher,
PartitionCount: int(c.PartitionCount),
ReplicationFactor: 20, // TODO: This also may be a configuration param.
Load: c.LoadFactor,
}
ctx, cancel := context.WithCancel(context.Background())
bctx, bcancel := context.WithTimeout(context.Background(), bootstrapTimeoutDuration)
db := &OlricDB{
ctx: ctx,
cancel: cancel,
logger: c.Logger,
config: c,
hasher: c.Hasher,
consistent: consistent.New(nil, cfg),
transport: newHTTPTransport(ctx, c),
partitions: make(map[uint64]*partition),
backups: make(map[uint64]*partition),
bctx: bctx,
bcancel: bcancel,
}
db.transport.db = db
// Create all the partitions. It's read-only. No need for locking.
for i := uint64(0); i < c.PartitionCount; i++ {
db.partitions[i] = &partition{id: i, m: make(map[string]*dmap)}
}
// Create all the backup partitions. It's read-only. No need for locking.
for i := uint64(0); i < c.PartitionCount; i++ {
db.backups[i] = &partition{
id: i,
backup: true,
m: make(map[string]*dmap),
}
}
return db, nil
}
func (db *OlricDB) prepare() error {
dsc, err := newDiscovery(db.config)
if err != nil {
return err
}
db.discovery = dsc
eventCh := db.discovery.subscribeNodeEvents()
db.discovery.join()
this, err := db.discovery.findMember(db.config.Name)
if err != nil {
db.logger.Printf("[DEBUG] Failed to get this node in cluster: %v", err)
serr := db.discovery.shutdown()
if serr != nil {
return serr
}
return err
}
db.this = this
db.consistent.Add(db.this)
if db.discovery.isCoordinator() {
db.distributePartitions()
// The coordinator bootstraps itself.
db.bcancel()
}
db.wg.Add(1)
go db.listenMemberlistEvents(eventCh)
return nil
}
// Start starts background servers and joins the cluster.
func (db *OlricDB) Start() error {
errCh := make(chan error, 1)
go func() {
errCh <- db.transport.start()
}()
if err := db.transport.checkAliveness(db.config.Server.Addr); err != nil {
return err
}
if err := db.prepare(); err != nil {
return err
}
db.wg.Add(2)
go db.updateRoutingPeriodically()
go db.evictKeysAtBackground()
return <-errCh
}
// Shutdown stops background servers and leaves the cluster.
func (db *OlricDB) Shutdown(ctx context.Context) error {
db.cancel()
var result error
cx := ctx
_, ok := ctx.Deadline()
if ok {
done := make(chan struct{})
go func() {
db.wg.Wait()
close(done)
}()
select {
case <-ctx.Done():
err := ctx.Err()
if err != nil {
result = multierror.Append(result, err)
}
case <-done:
}
} else {
db.wg.Wait()
cx = context.Background()
}
err := db.transport.server.Shutdown(cx)
if err != nil {
result = multierror.Append(result, err)
}
if db.discovery != nil {
err = db.discovery.memberlist.Shutdown()
if err != nil {
result = multierror.Append(result, err)
}
}
// The GC will flush all the data.
db.partitions = nil
db.backups = nil
return result
}
func (db *OlricDB) getPartitionID(hkey uint64) uint64 {
return hkey % db.config.PartitionCount
}
func (db *OlricDB) getPartition(hkey uint64) *partition {
partID := db.getPartitionID(hkey)
return db.partitions[partID]
}
func (db *OlricDB) getBackupPartition(hkey uint64) *partition {
partID := db.getPartitionID(hkey)
return db.backups[partID]
}
func (db *OlricDB) getBackupPartitionOwners(hkey uint64) []host {
bpart := db.getBackupPartition(hkey)
bpart.RLock()
defer bpart.RUnlock()
owners := append([]host{}, bpart.owners...)
return owners
}
func (db *OlricDB) getPartitionOwners(hkey uint64) []host {
part := db.getPartition(hkey)
part.RLock()
defer part.RUnlock()
owners := append([]host{}, part.owners...)
return owners
}
func (db *OlricDB) getHKey(name, key string) uint64 {
return db.hasher.Sum64([]byte(name + key))
}
func (db *OlricDB) locateHKey(hkey uint64) (host, error) {
<-db.bctx.Done()
if db.bctx.Err() == context.DeadlineExceeded {
return host{}, ErrOperationTimeout
}
part := db.getPartition(hkey)
part.RLock()
defer part.RUnlock()
if len(part.owners) == 0 {
return host{}, fmt.Errorf("no owner found for hkey: %d", hkey)
}
return part.owners[len(part.owners)-1], nil
}
func (db *OlricDB) locateKey(name, key string) (host, uint64, error) {
hkey := db.getHKey(name, key)
member, err := db.locateHKey(hkey)
if err != nil {
return host{}, 0, err
}
return member, hkey, nil
}
func (db *OlricDB) getDMap(name string, hkey uint64) *dmap {
part := db.getPartition(hkey)
part.Lock()
defer part.Unlock()
dmp, ok := part.m[name]
if ok {
return dmp
}
dmp = &dmap{
locker: newLocker(),
d: make(map[uint64]vdata),
}
part.m[name] = dmp
return dmp
}
// hostCmp returns true if o1 and o2 is the same.
func hostCmp(o1, o2 host) bool {
return o1.Name == o2.Name && o1.Birthdate == o2.Birthdate
}
func (db *OlricDB) getBackupDmap(name string, hkey uint64) *dmap {
bpart := db.getBackupPartition(hkey)
bpart.Lock()
defer bpart.Unlock()
dmp, ok := bpart.m[name]
if ok {
return dmp
}
dmp = &dmap{d: make(map[uint64]vdata)}
bpart.m[name] = dmp
return dmp
}
func printHKey(hkey uint64) string {
return strconv.FormatUint(hkey, 10)
}
func readHKey(ps httprouter.Params) (uint64, error) {
return strconv.ParseUint(ps.ByName("hkey"), 10, 64)
}