forked from influxdata/influxdb
/
leveldb_shard_datastore.go
277 lines (239 loc) · 7.44 KB
/
leveldb_shard_datastore.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
package datastore
import (
"bytes"
"cluster"
"configuration"
"fmt"
"math"
"os"
"path/filepath"
"protocol"
"sync"
"time"
log "code.google.com/p/log4go"
"github.com/jmhodges/levigo"
)
type LevelDbShardDatastore struct {
baseDbDir string
config *configuration.Configuration
shards map[uint32]*LevelDbShard
lastAccess map[uint32]int64
shardRefCounts map[uint32]int
shardsToClose map[uint32]bool
shardsLock sync.RWMutex
levelDbOptions *levigo.Options
writeBuffer *cluster.WriteBuffer
maxOpenShards int
pointBatchSize int
}
const (
ONE_KILOBYTE = 1024
ONE_MEGABYTE = 1024 * 1024
ONE_GIGABYTE = ONE_MEGABYTE * 1024
TWO_FIFTY_SIX_KILOBYTES = 256 * 1024
SIXTY_FOUR_KILOBYTES = 64 * 1024
MAX_SERIES_SIZE = ONE_MEGABYTE
DATABASE_DIR = "db"
SHARD_BLOOM_FILTER_BITS_PER_KEY = 10
SHARD_DATABASE_DIR = "shard_db"
)
var (
// This datastore implements the PersistentAtomicInteger interface. All of the persistent
// integers start with this prefix, followed by their name
ATOMIC_INCREMENT_PREFIX = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFD}
// NEXT_ID_KEY holds the next id. ids are used to "intern" timeseries and column names
NEXT_ID_KEY = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
// SERIES_COLUMN_INDEX_PREFIX is the prefix of the series to column names index
SERIES_COLUMN_INDEX_PREFIX = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}
// DATABASE_SERIES_INDEX_PREFIX is the prefix of the database to series names index
DATABASE_SERIES_INDEX_PREFIX = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
MAX_SEQUENCE = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
// replicateWrite = protocol.Request_REPLICATION_WRITE
TRUE = true
)
type Field struct {
Id []byte
Name string
}
type rawColumnValue struct {
time []byte
sequence []byte
value []byte
}
func NewLevelDbShardDatastore(config *configuration.Configuration) (*LevelDbShardDatastore, error) {
baseDbDir := filepath.Join(config.DataDir, SHARD_DATABASE_DIR)
err := os.MkdirAll(baseDbDir, 0744)
if err != nil {
return nil, err
}
opts := levigo.NewOptions()
opts.SetCache(levigo.NewLRUCache(config.LevelDbLruCacheSize))
opts.SetCreateIfMissing(true)
opts.SetBlockSize(64 * ONE_KILOBYTE)
filter := levigo.NewBloomFilter(SHARD_BLOOM_FILTER_BITS_PER_KEY)
opts.SetFilterPolicy(filter)
opts.SetMaxOpenFiles(config.LevelDbMaxOpenFiles)
return &LevelDbShardDatastore{
baseDbDir: baseDbDir,
config: config,
shards: make(map[uint32]*LevelDbShard),
levelDbOptions: opts,
maxOpenShards: config.LevelDbMaxOpenShards,
lastAccess: make(map[uint32]int64),
shardRefCounts: make(map[uint32]int),
shardsToClose: make(map[uint32]bool),
pointBatchSize: config.LevelDbPointBatchSize,
}, nil
}
func (self *LevelDbShardDatastore) Close() {
self.shardsLock.Lock()
defer self.shardsLock.Unlock()
for _, shard := range self.shards {
shard.close()
}
}
func (self *LevelDbShardDatastore) GetOrCreateShard(id uint32) (cluster.LocalShardDb, error) {
now := time.Now().Unix()
self.shardsLock.Lock()
defer self.shardsLock.Unlock()
db := self.shards[id]
self.lastAccess[id] = now
if db != nil {
self.incrementShardRefCountAndCloseOldestIfNeeded(id)
return db, nil
}
dbDir := self.shardDir(id)
log.Info("DATASTORE: opening or creating shard %s", dbDir)
ldb, err := levigo.Open(dbDir, self.levelDbOptions)
if err != nil {
log.Error("Error opening shard: ", err)
return nil, err
}
db, err = NewLevelDbShard(ldb, self.pointBatchSize)
if err != nil {
log.Error("Error creating shard: ", err)
ldb.Close()
return nil, err
}
self.shards[id] = db
self.incrementShardRefCountAndCloseOldestIfNeeded(id)
return db, nil
}
func (self *LevelDbShardDatastore) incrementShardRefCountAndCloseOldestIfNeeded(id uint32) {
self.shardRefCounts[id] += 1
delete(self.shardsToClose, id)
if self.maxOpenShards > 0 && len(self.shards) > self.maxOpenShards {
for i := len(self.shards) - self.maxOpenShards; i > 0; i-- {
self.closeOldestShard()
}
}
}
func (self *LevelDbShardDatastore) ReturnShard(id uint32) {
self.shardsLock.Lock()
defer self.shardsLock.Unlock()
self.shardRefCounts[id] -= 1
if self.shardsToClose[id] && self.shardRefCounts[id] == 0 {
self.closeShard(id)
}
}
func (self *LevelDbShardDatastore) Write(request *protocol.Request) error {
shardDb, err := self.GetOrCreateShard(*request.ShardId)
if err != nil {
return err
}
defer self.ReturnShard(*request.ShardId)
for _, s := range request.MultiSeries {
err := shardDb.Write(*request.Database, s)
if err != nil {
return err
}
}
return nil
}
func (self *LevelDbShardDatastore) BufferWrite(request *protocol.Request) {
self.writeBuffer.Write(request)
}
func (self *LevelDbShardDatastore) SetWriteBuffer(writeBuffer *cluster.WriteBuffer) {
self.writeBuffer = writeBuffer
}
func (self *LevelDbShardDatastore) DeleteShard(shardId uint32) error {
self.shardsLock.Lock()
shardDb := self.shards[shardId]
delete(self.shards, shardId)
delete(self.lastAccess, shardId)
self.shardsLock.Unlock()
if shardDb != nil {
shardDb.close()
}
dir := self.shardDir(shardId)
log.Info("DATASTORE: dropping shard %s", dir)
return os.RemoveAll(dir)
}
func (self *LevelDbShardDatastore) shardDir(id uint32) string {
return filepath.Join(self.baseDbDir, fmt.Sprintf("%.5d", id))
}
func (self *LevelDbShardDatastore) closeOldestShard() {
var oldestId uint32
oldestAccess := int64(math.MaxInt64)
for id, lastAccess := range self.lastAccess {
if lastAccess < oldestAccess && self.shardsToClose[id] == false {
oldestId = id
oldestAccess = lastAccess
}
}
if self.shardRefCounts[oldestId] == 0 {
self.closeShard(oldestId)
} else {
self.shardsToClose[oldestId] = true
}
}
func (self *LevelDbShardDatastore) closeShard(id uint32) {
shard := self.shards[id]
if shard != nil {
shard.close()
}
delete(self.shardRefCounts, id)
delete(self.shards, id)
delete(self.lastAccess, id)
delete(self.shardsToClose, id)
log.Debug("DATASTORE: closing shard %s", self.shardDir(id))
}
// // returns true if the point has the correct field id and is
// // in the given time range
func isPointInRange(fieldId, startTime, endTime, point []byte) bool {
id := point[:8]
time := point[8:16]
return bytes.Equal(id, fieldId) && bytes.Compare(time, startTime) > -1 && bytes.Compare(time, endTime) < 1
}
type FieldLookupError struct {
message string
}
func (self FieldLookupError) Error() string {
return self.message
}
// depending on the query order (whether it's ascending or not) returns
// the min (or max in case of descending query) of the current
// [timestamp,sequence] and the self's [timestamp,sequence]
//
// This is used to determine what the next point's timestamp
// and sequence number should be.
func (self *rawColumnValue) updatePointTimeAndSequence(currentTimeRaw, currentSequenceRaw []byte, isAscendingQuery bool) ([]byte, []byte) {
if currentTimeRaw == nil {
return self.time, self.sequence
}
compareValue := 1
if isAscendingQuery {
compareValue = -1
}
timeCompare := bytes.Compare(self.time, currentTimeRaw)
if timeCompare == compareValue {
return self.time, self.sequence
}
if timeCompare != 0 {
return currentTimeRaw, currentSequenceRaw
}
if bytes.Compare(self.sequence, currentSequenceRaw) == compareValue {
return currentTimeRaw, self.sequence
}
return currentTimeRaw, currentSequenceRaw
}