| @@ -1,20 +1,23 @@ |
| 1 | 1 | package wal |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | | − "bufio" |
| 4 | + "bytes" |
| 5 | 5 | "encoding/base64" |
| 6 | 6 | "encoding/binary" |
| 7 | 7 | "encoding/json" |
| 8 | 8 | "errors" |
| 9 | 9 | "fmt" |
| 10 | | − "io" |
| 11 | 10 | "io/ioutil" |
| 12 | 11 | "os" |
| 13 | 12 | "path/filepath" |
| 14 | 13 | "strconv" |
| 15 | 14 | "strings" |
| 15 | + "sync" |
| 16 | 16 | "unicode/utf8" |
| 17 | 17 | "unsafe" |
| 18 | + |
| 19 | + "github.com/tidwall/gjson" |
| 20 | + "github.com/tidwall/tinylru" |
| 18 | 21 | ) |
| 19 | 22 | |
| 20 | 23 | var ( |
| @@ -42,22 +45,6 @@ var ( · 21 unmodified lines |
| 42 | 45 | ErrOutOfRange = errors.New("out of range") |
| 43 | 46 | ) |
| 44 | 47 | |
| 45 | | −// Durability policy. |
| 46 | | −type Durability int8 |
| 47 | | − |
| 48 | | −const ( |
| 49 | | − // Low durability flushes data to operating system only when the buffer is |
| 50 | | − // at capacity. A process crash could cause data loss. |
| 51 | | − Low Durability = -1 |
| 52 | | − // Medium durability flushes data to the operating system after every |
| 53 | | − // new entry is addded to the log. A server crash could cause data loss. |
| 54 | | − Medium Durability = 0 |
| 55 | | − // High durability syncs data to disk after every new entry is added to |
| 56 | | − // the log. All entries are persisted to disk and crashes will not cause |
| 57 | | − // data loss. |
| 58 | | − High Durability = 1 |
| 59 | | −) |
| 60 | | − |
| 61 | 48 | // LogFormat is the format of the log files. |
| 62 | 49 | type LogFormat byte |
| 63 | 50 | |
| @@ -72,49 +59,54 @@ const ( · 8 unmodified lines |
| 72 | 59 | |
| 73 | 60 | // Options for Log |
| 74 | 61 | type Options struct { |
| 75 | | − // Durability policy. Default is High. |
| 76 | | − Durability Durability |
| 62 | + // NoSync disables fsync after writes. This is less durable and puts the |
| 63 | + // log at risk of data loss when there's a server crash. |
| 64 | + NoSync bool |
| 77 | 65 | // SegmentSize of each segment. This is just a target value, actual size |
| 78 | | − // may differ. Default is 50 MB. |
| 66 | + // may differ. Default is 20 MB. |
| 79 | 67 | SegmentSize int |
| 80 | 68 | // LogFormat is the format of the log files. Default is Binary. |
| 81 | 69 | LogFormat LogFormat |
| 70 | + // SegmentCacheSize is the maximum number of segments that will be held in |
| 71 | + // memory for caching. Increasing this value may enhance performance for |
| 72 | + // concurrent read operations. Default is 1 |
| 73 | + SegmentCacheSize int |
| 82 | 74 | } |
| 83 | 75 | |
| 84 | 76 | // DefaultOptions for Open(). |
| 85 | 77 | var DefaultOptions = &Options{ |
| 86 | | − Durability: High, // Fsync after every write |
| 87 | | − SegmentSize: 52428800, // 50 MB log segment files. |
| 88 | | − LogFormat: Binary, // Binary format is small and fast. |
| 78 | + NoSync: false, // Fsync after every write |
| 79 | + SegmentSize: 20971520, // 20 MB log segment files. |
| 80 | + LogFormat: Binary, // Binary format is small and fast. |
| 81 | + SegmentCacheSize: 1, // Number of cached in-memory segments |
| 89 | 82 | } |
| 90 | 83 | |
| 91 | | −const maxReaders = 8 // maximum number of opened readers. |
| 92 | | − |
| 93 | 84 | // Log represents a write ahead log |
| 94 | 85 | type Log struct { |
| 95 | | − path string // absolute path to log directory |
| 96 | | − opts Options // log options |
| 97 | | − closed bool // log is closed |
| 98 | | − segments []segment // all known log segments |
| 99 | | − firstIndex uint64 // index of the first entry in log |
| 100 | | − lastIndex uint64 // index of the last entry in log |
| 101 | | − file *os.File // tail segment file handle |
| 102 | | − buffer []byte // tail segment file write buffer |
| 103 | | − fileSize int // tail segment file size, including buffer |
| 104 | | − readers []*reader // all opened readers |
| 86 | + mu sync.Mutex |
| 87 | + path string // absolute path to log directory |
| 88 | + opts Options // log options |
| 89 | + closed bool // log is closed |
| 90 | + corrupt bool // log may be corrupt |
| 91 | + segments []*segment // all known log segments |
| 92 | + firstIndex uint64 // index of the first entry in log |
| 93 | + lastIndex uint64 // index of the last entry in log |
| 94 | + sfile *os.File // tail segment file handle |
| 95 | + wbatch Batch // reusable write batch |
| 96 | + scache tinylru.LRU // segment entries cache |
| 105 | 97 | } |
| 106 | 98 | |
| 107 | 99 | // segment represents a single segment file. |
| 108 | 100 | type segment struct { |
| 109 | 101 | path string // path of segment file |
| 110 | 102 | index uint64 // first index of segment |
| 103 | + ebuf []byte // cached entries buffer |
| 104 | + epos []bpos // cached entries positions in buffer |
| 111 | 105 | } |
| 112 | 106 | |
| 113 | | −type reader struct { |
| 114 | | − sindex int // segment index |
| 115 | | − nindex uint64 // next entry index |
| 116 | | − file *os.File // opened file |
| 117 | | − rd *bufio.Reader // reader |
| 107 | +type bpos struct { |
| 108 | + pos int // byte position |
| 109 | + end int // one byte past pos |
| 118 | 110 | } |
| 119 | 111 | |
| 120 | 112 | // Open a new write ahead log |
| @@ -122,13 +114,22 @@ func Open(path string, opts *Options) (*Log, error) { · 1 unmodified line |
| 122 | 114 | if opts == nil { |
| 123 | 115 | opts = DefaultOptions |
| 124 | 116 | } |
| 117 | + if opts.SegmentCacheSize <= 0 { |
| 118 | + opts.SegmentCacheSize = DefaultOptions.SegmentCacheSize |
| 119 | + } |
| 120 | + if opts.SegmentSize <= 0 { |
| 121 | + opts.SegmentSize = DefaultOptions.SegmentSize |
| 122 | + } |
| 125 | 123 | var err error |
| 126 | 124 | path, err = abs(path) |
| 127 | 125 | if err != nil { |
| 128 | 126 | return nil, err |
| 129 | 127 | } |
| 130 | 128 | l := &Log{path: path, opts: *opts} |
| 131 | | − _ = os.MkdirAll(path, 0777) |
| 129 | + l.scache.Resize(l.opts.SegmentCacheSize) |
| 130 | + if err := os.MkdirAll(path, 0777); err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 132 | 133 | if err := l.load(); err != nil { |
| 133 | 134 | return nil, err |
| 134 | 135 | } |
| @@ -142,6 +143,16 @@ func abs(path string) (string, error) { · 7 unmodified lines |
| 142 | 143 | return filepath.Abs(path) |
| 143 | 144 | } |
| 144 | 145 | |
| 146 | +func (l *Log) pushCache(segIdx int) { |
| 147 | + _, _, _, v, evicted := |
| 148 | + l.scache.SetEvicted(segIdx, l.segments[segIdx]) |
| 149 | + if evicted { |
| 150 | + s := v.(*segment) |
| 151 | + s.ebuf = nil |
| 152 | + s.epos = nil |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 145 | 156 | // load all the segments. This operation also cleansup any START/END segments. |
| 146 | 157 | func (l *Log) load() error { |
| 147 | 158 | fis, err := ioutil.ReadDir(l.path) |
| @@ -167,7 +178,7 @@ func (l *Log) load() error { · 19 unmodified lines |
| 167 | 178 | } else if isEnd && endIdx == -1 { |
| 168 | 179 | endIdx = len(l.segments) |
| 169 | 180 | } |
| 170 | | − l.segments = append(l.segments, segment{ |
| 181 | + l.segments = append(l.segments, &segment{ |
| 171 | 182 | index: index, |
| 172 | 183 | path: filepath.Join(l.path, name), |
| 173 | 184 | }) |
| @@ -175,13 +186,13 @@ func (l *Log) load() error { · 1 unmodified line |
| 175 | 186 | } |
| 176 | 187 | if len(l.segments) == 0 { |
| 177 | 188 | // Create a new log |
| 178 | | − l.segments = append(l.segments, segment{ |
| 189 | + l.segments = append(l.segments, &segment{ |
| 179 | 190 | index: 1, |
| 180 | 191 | path: filepath.Join(l.path, segmentName(1)), |
| 181 | 192 | }) |
| 182 | 193 | l.firstIndex = 1 |
| 183 | 194 | l.lastIndex = 0 |
| 184 | | − l.file, err = os.Create(l.segments[0].path) |
| 195 | + l.sfile, err = os.Create(l.segments[0].path) |
| 185 | 196 | return err |
| 186 | 197 | } |
| 187 | 198 | // Open existing log. Clean up log if START of END segments exists. |
| @@ -196,7 +207,7 @@ func (l *Log) load() error { · 8 unmodified lines |
| 196 | 207 | return err |
| 197 | 208 | } |
| 198 | 209 | } |
| 199 | | − l.segments = append([]segment{}, l.segments[startIdx:]...) |
| 210 | + l.segments = append([]*segment{}, l.segments[startIdx:]...) |
| 200 | 211 | // Rename the START segment |
| 201 | 212 | orgPath := l.segments[0].path |
| 202 | 213 | finalPath := orgPath[:len(orgPath)-len(".START")] |
| @@ -213,7 +224,7 @@ func (l *Log) load() error { · 10 unmodified lines |
| 213 | 224 | return err |
| 214 | 225 | } |
| 215 | 226 | } |
| 216 | | − l.segments = append([]segment{}, l.segments[:endIdx+1]...) |
| 227 | + l.segments = append([]*segment{}, l.segments[:endIdx+1]...) |
| 217 | 228 | if len(l.segments) > 1 && l.segments[len(l.segments)-2].index == |
| 218 | 229 | l.segments[len(l.segments)-1].index { |
| 219 | 230 | // remove the segment prior to the END segment because it shares |
| @@ -230,125 +241,108 @@ func (l *Log) load() error { · 10 unmodified lines |
| 230 | 241 | } |
| 231 | 242 | l.segments[len(l.segments)-1].path = finalPath |
| 232 | 243 | } |
| 233 | | − // Open the last segment for appending |
| 234 | 244 | l.firstIndex = l.segments[0].index |
| 235 | | − l.file, err = os.OpenFile(l.segments[len(l.segments)-1].path, |
| 236 | | − os.O_RDWR, 0666) |
| 245 | + // Open the last segment for appending |
| 246 | + lseg := l.segments[len(l.segments)-1] |
| 247 | + l.sfile, err = os.OpenFile(lseg.path, os.O_WRONLY, 0666) |
| 237 | 248 | if err != nil { |
| 238 | 249 | return err |
| 239 | 250 | } |
| 240 | | − // Read the last segment to the end of the log |
| 241 | | − rd := bufio.NewReader(l.file) |
| 242 | | − for { |
| 243 | | − idx, _, err := readEntry(rd, l.opts.LogFormat, true) |
| 244 | | − if err != nil { |
| 245 | | − if err == io.EOF { |
| 246 | | − break |
| 247 | | − } |
| 248 | | − return err |
| 249 | | − } |
| 250 | | − l.lastIndex = idx |
| 251 | + if _, err := l.sfile.Seek(0, 2); err != nil { |
| 252 | + return err |
| 251 | 253 | } |
| 252 | | − n, err := l.file.Seek(0, 2) |
| 253 | | − if err != nil { |
| 254 | + // Load the last segment entries |
| 255 | + if err := l.loadSegmentEntries(lseg); err != nil { |
| 254 | 256 | return err |
| 255 | 257 | } |
| 256 | | − l.fileSize = int(n) |
| 258 | + l.lastIndex = lseg.index + uint64(len(lseg.epos)) - 1 |
| 257 | 259 | return nil |
| 258 | 260 | } |
| 259 | 261 | |
| 260 | | −func must(v interface{}, err error) interface{} { |
| 261 | | − if err != nil { |
| 262 | | − panic(err) |
| 263 | | − } |
| 264 | | − return v |
| 265 | | −} |
| 266 | | − |
| 267 | 262 | // segmentName returns a 20-byte textual representation of an index |
| 268 | 263 | // for lexical ordering. This is used for the file names of log segments. |
| 269 | 264 | func segmentName(index uint64) string { |
| 270 | 265 | return fmt.Sprintf("%020d", index) |
| 271 | 266 | } |
| 272 | 267 | |
| 273 | | −// Close the log |
| 268 | +// Close the log. |
| 274 | 269 | func (l *Log) Close() error { |
| 270 | + l.mu.Lock() |
| 271 | + defer l.mu.Unlock() |
| 275 | 272 | if l.closed { |
| 273 | + if l.corrupt { |
| 274 | + return ErrCorrupt |
| 275 | + } |
| 276 | 276 | return ErrClosed |
| 277 | 277 | } |
| 278 | | − l.flush() |
| 279 | | − must(nil, l.file.Close()) |
| 280 | | − for len(l.readers) > 0 { |
| 281 | | − l.closeReader(l.readers[0]) |
| 278 | + if err := l.sfile.Sync(); err != nil { |
| 279 | + return err |
| 280 | + } |
| 281 | + if err := l.sfile.Close(); err != nil { |
| 282 | + return err |
| 282 | 283 | } |
| 283 | | − l.readers = nil |
| 284 | | − l.segments = nil |
| 285 | 284 | l.closed = true |
| 286 | | − return nil |
| 287 | | −} |
| 288 | | − |
| 289 | | −func (l *Log) flush() { |
| 290 | | − if len(l.buffer) > 0 { |
| 291 | | − must(l.file.Write(l.buffer)) |
| 292 | | − l.buffer = l.buffer[:0] |
| 293 | | − if l.opts.Durability >= High { |
| 294 | | − must(nil, l.file.Sync()) |
| 295 | | − } |
| 285 | + if l.corrupt { |
| 286 | + return ErrCorrupt |
| 296 | 287 | } |
| 288 | + return nil |
| 297 | 289 | } |
| 298 | 290 | |
| 299 | 291 | // Write an entry to the log. |
| 300 | 292 | func (l *Log) Write(index uint64, data []byte) error { |
| 301 | | − if l.closed { |
| 293 | + l.mu.Lock() |
| 294 | + defer l.mu.Unlock() |
| 295 | + if l.corrupt { |
| 296 | + return ErrCorrupt |
| 297 | + } else if l.closed { |
| 302 | 298 | return ErrClosed |
| 303 | 299 | } |
| 304 | | − if index != l.lastIndex+1 { |
| 305 | | − return ErrOutOfOrder |
| 306 | | − } |
| 307 | | − if l.fileSize >= l.opts.SegmentSize { |
| 308 | | − l.cycle() |
| 309 | | − } |
| 310 | | − l.appendEntry(index, data) |
| 311 | | − if l.opts.Durability >= Medium || len(l.buffer) >= 4096 { |
| 312 | | − l.flush() |
| 300 | + l.wbatch.Clear() |
| 301 | + l.wbatch.Write(index, data) |
| 302 | + return l.writeBatch(&l.wbatch) |
| 303 | +} |
| 304 | + |
| 305 | +func (l *Log) appendEntry(dst []byte, index uint64, data []byte) (out []byte, |
| 306 | + epos bpos) { |
| 307 | + if l.opts.LogFormat == JSON { |
| 308 | + return appendJSONEntry(dst, index, data) |
| 313 | 309 | } |
| 314 | | − l.lastIndex = index |
| 315 | | − return nil |
| 310 | + return appendBinaryEntry(dst, data) |
| 316 | 311 | } |
| 317 | 312 | |
| 318 | 313 | // Cycle the old segment for a new segment. |
| 319 | | −func (l *Log) cycle() { |
| 320 | | − l.flush() |
| 321 | | − must(nil, l.file.Close()) |
| 322 | | − s := segment{ |
| 314 | +func (l *Log) cycle() error { |
| 315 | + if err := l.sfile.Sync(); err != nil { |
| 316 | + return err |
| 317 | + } |
| 318 | + if err := l.sfile.Close(); err != nil { |
| 319 | + return err |
| 320 | + } |
| 321 | + // cache the previous segement |
| 322 | + l.pushCache(len(l.segments) - 1) |
| 323 | + s := &segment{ |
| 323 | 324 | index: l.lastIndex + 1, |
| 324 | 325 | path: filepath.Join(l.path, segmentName(l.lastIndex+1)), |
| 325 | 326 | } |
| 326 | | − l.file = must(os.Create(s.path)).(*os.File) |
| 327 | | − l.fileSize = 0 |
| 328 | | − l.segments = append(l.segments, s) |
| 329 | | − l.buffer = nil |
| 330 | | −} |
| 331 | | − |
| 332 | | −// appendEntry to the log buffer. This also increases the log segment fileSize. |
| 333 | | −func (l *Log) appendEntry(index uint64, data []byte) { |
| 334 | | − mark := len(l.buffer) |
| 335 | | − if l.opts.LogFormat == JSON { |
| 336 | | − l.buffer = appendJSONEntry(l.buffer, index, data) |
| 337 | | − } else { |
| 338 | | − l.buffer = appendBinaryEntry(l.buffer, index, data) |
| 327 | + var err error |
| 328 | + l.sfile, err = os.Create(s.path) |
| 329 | + if err != nil { |
| 330 | + return err |
| 339 | 331 | } |
| 340 | | − l.fileSize += len(l.buffer) - mark |
| 341 | | − |
| 332 | + l.segments = append(l.segments, s) |
| 333 | + return nil |
| 342 | 334 | } |
| 343 | 335 | |
| 344 | | −func appendJSONEntry(dst []byte, index uint64, data []byte) []byte { |
| 336 | +func appendJSONEntry(dst []byte, index uint64, data []byte) (out []byte, |
| 337 | + epos bpos) { |
| 345 | 338 | // {"index":number,"data":string} |
| 339 | + mark := len(dst) |
| 346 | 340 | dst = append(dst, `{"index":"`...) |
| 347 | 341 | dst = strconv.AppendUint(dst, index, 10) |
| 348 | 342 | dst = append(dst, `","data":`...) |
| 349 | 343 | dst = appendJSONData(dst, data) |
| 350 | 344 | dst = append(dst, '}', '\n') |
| 351 | | − return dst |
| 345 | + return dst, bpos{mark, len(dst)} |
| 352 | 346 | } |
| 353 | 347 | |
| 354 | 348 | func appendJSONData(dst []byte, s []byte) []byte { |
| @@ -362,12 +356,12 @@ func appendJSONData(dst []byte, s []byte) []byte { · 7 unmodified lines |
| 362 | 356 | return append(dst, '"') |
| 363 | 357 | } |
| 364 | 358 | |
| 365 | | −func appendBinaryEntry(dst []byte, index uint64, data []byte) []byte { |
| 366 | | − // index + data_size + data |
| 367 | | − dst = appendUvarint(dst, index) |
| 359 | +func appendBinaryEntry(dst []byte, data []byte) (out []byte, epos bpos) { |
| 360 | + // data_size + data |
| 361 | + pos := len(dst) |
| 368 | 362 | dst = appendUvarint(dst, uint64(len(data))) |
| 369 | 363 | dst = append(dst, data...) |
| 370 | | − return dst |
| 364 | + return dst, bpos{pos, len(dst)} |
| 371 | 365 | } |
| 372 | 366 | |
| 373 | 367 | func appendUvarint(dst []byte, x uint64) []byte { |
| @@ -379,53 +373,92 @@ func appendUvarint(dst []byte, x uint64) []byte { · 5 unmodified lines |
| 379 | 373 | |
| 380 | 374 | // Batch of entries. Used to write multiple entries at once using WriteBatch(). |
| 381 | 375 | type Batch struct { |
| 382 | | − indexes []uint64 |
| 383 | | − dataSizes []int |
| 384 | | − datas []byte |
| 376 | + entries []batchEntry |
| 377 | + datas []byte |
| 378 | +} |
| 379 | + |
| 380 | +type batchEntry struct { |
| 381 | + index uint64 |
| 382 | + size int |
| 385 | 383 | } |
| 386 | 384 | |
| 387 | 385 | // Write an entry to the batch |
| 388 | 386 | func (b *Batch) Write(index uint64, data []byte) { |
| 389 | | − b.indexes = append(b.indexes, index) |
| 390 | | − b.dataSizes = append(b.dataSizes, len(data)) |
| 387 | + b.entries = append(b.entries, batchEntry{index, len(data)}) |
| 391 | 388 | b.datas = append(b.datas, data...) |
| 392 | 389 | } |
| 393 | 390 | |
| 394 | 391 | // Clear the batch for reuse. |
| 395 | 392 | func (b *Batch) Clear() { |
| 396 | | − b.indexes = b.indexes[:0] |
| 397 | | − b.dataSizes = b.dataSizes[:0] |
| 393 | + b.entries = b.entries[:0] |
| 398 | 394 | b.datas = b.datas[:0] |
| 399 | 395 | } |
| 400 | 396 | |
| 401 | 397 | // WriteBatch writes the entries in the batch to the log in the order that they |
| 402 | 398 | // were added to the batch. The batch is cleared upon a successful return. |
| 403 | 399 | func (l *Log) WriteBatch(b *Batch) error { |
| 404 | | − if l.closed { |
| 400 | + l.mu.Lock() |
| 401 | + defer l.mu.Unlock() |
| 402 | + if l.corrupt { |
| 403 | + return ErrCorrupt |
| 404 | + } else if l.closed { |
| 405 | 405 | return ErrClosed |
| 406 | 406 | } |
| 407 | | − if len(b.indexes) == 0 { |
| 407 | + if len(b.entries) == 0 { |
| 408 | 408 | return nil |
| 409 | 409 | } |
| 410 | | − // check all indexes in batch |
| 411 | | − for i := 0; i < len(b.indexes); i++ { |
| 412 | | − if b.indexes[i] != l.lastIndex+uint64(i+1) { |
| 410 | + return l.writeBatch(b) |
| 411 | +} |
| 412 | + |
| 413 | +func (l *Log) writeBatch(b *Batch) error { |
| 414 | + // check that all indexes in batch are sane |
| 415 | + for i := 0; i < len(b.entries); i++ { |
| 416 | + if b.entries[i].index != l.lastIndex+uint64(i+1) { |
| 413 | 417 | return ErrOutOfOrder |
| 414 | 418 | } |
| 415 | 419 | } |
| 416 | | − if l.fileSize >= l.opts.SegmentSize { |
| 417 | | − l.cycle() |
| 420 | + // load the tail segment |
| 421 | + s := l.segments[len(l.segments)-1] |
| 422 | + if len(s.ebuf) > l.opts.SegmentSize { |
| 423 | + // tail segment has reached capacity. Close it and create a new one. |
| 424 | + if err := l.cycle(); err != nil { |
| 425 | + return err |
| 426 | + } |
| 427 | + s = l.segments[len(l.segments)-1] |
| 418 | 428 | } |
| 429 | + |
| 430 | + mark := len(s.ebuf) |
| 419 | 431 | datas := b.datas |
| 420 | | − for i := 0; i < len(b.indexes); i++ { |
| 421 | | − data := datas[:b.dataSizes[i]] |
| 422 | | − l.appendEntry(b.indexes[i], data) |
| 423 | | − datas = datas[b.dataSizes[i]:] |
| 432 | + for i := 0; i < len(b.entries); i++ { |
| 433 | + data := datas[:b.entries[i].size] |
| 434 | + var epos bpos |
| 435 | + s.ebuf, epos = l.appendEntry(s.ebuf, b.entries[i].index, data) |
| 436 | + s.epos = append(s.epos, epos) |
| 437 | + if len(s.ebuf) >= l.opts.SegmentSize { |
| 438 | + // segment has reached capacity, cycle now |
| 439 | + if _, err := l.sfile.Write(s.ebuf[mark:]); err != nil { |
| 440 | + return err |
| 441 | + } |
| 442 | + l.lastIndex = b.entries[i].index |
| 443 | + if err := l.cycle(); err != nil { |
| 444 | + return err |
| 445 | + } |
| 446 | + s = l.segments[len(l.segments)-1] |
| 447 | + mark = 0 |
| 448 | + } |
| 449 | + datas = datas[b.entries[i].size:] |
| 424 | 450 | } |
| 425 | | − if l.opts.Durability >= Medium || len(l.buffer) >= 4096 { |
| 426 | | − l.flush() |
| 451 | + if len(s.ebuf)-mark > 0 { |
| 452 | + if _, err := l.sfile.Write(s.ebuf[mark:]); err != nil { |
| 453 | + return err |
| 454 | + } |
| 455 | + l.lastIndex = b.entries[len(b.entries)-1].index |
| 456 | + } |
| 457 | + if !l.opts.NoSync { |
| 458 | + if err := l.sfile.Sync(); err != nil { |
| 459 | + return err |
| 460 | + } |
| 427 | 461 | } |
| 428 | | − l.lastIndex = b.indexes[len(b.indexes)-1] |
| 429 | 462 | b.Clear() |
| 430 | 463 | return nil |
| 431 | 464 | } |
| @@ -433,7 +466,11 @@ func (l *Log) WriteBatch(b *Batch) error { · 1 unmodified line |
| 433 | 466 | // FirstIndex returns the index of the first entry in the log. Returns zero |
| 434 | 467 | // when log has no entries. |
| 435 | 468 | func (l *Log) FirstIndex() (index uint64, err error) { |
| 436 | | − if l.closed { |
| 469 | + l.mu.Lock() |
| 470 | + defer l.mu.Unlock() |
| 471 | + if l.corrupt { |
| 472 | + return 0, ErrCorrupt |
| 473 | + } else if l.closed { |
| 437 | 474 | return 0, ErrClosed |
| 438 | 475 | } |
| 439 | 476 | // We check the lastIndex for zero because the firstIndex is always one or |
| @@ -447,7 +484,11 @@ func (l *Log) FirstIndex() (index uint64, err error) { · 7 unmodified lines |
| 447 | 484 | // LastIndex returns the index of the last entry in the log. Returns zero when |
| 448 | 485 | // log has no entries. |
| 449 | 486 | func (l *Log) LastIndex() (index uint64, err error) { |
| 450 | | − if l.closed { |
| 487 | + l.mu.Lock() |
| 488 | + defer l.mu.Unlock() |
| 489 | + if l.corrupt { |
| 490 | + return 0, ErrCorrupt |
| 491 | + } else if l.closed { |
| 451 | 492 | return 0, ErrClosed |
| 452 | 493 | } |
| 453 | 494 | if l.lastIndex == 0 { |
| @@ -470,425 +511,385 @@ func (l *Log) findSegment(index uint64) int { · 16 unmodified lines |
| 470 | 511 | return i - 1 |
| 471 | 512 | } |
| 472 | 513 | |
| 473 | | −// Read an entry from the log. This function reads an entry from disk and is |
| 474 | | −// optimized for sequential reads. Randomly accessing entries is slow. |
| 475 | | −func (l *Log) Read(index uint64) (data []byte, err error) { |
| 476 | | − if l.closed { |
| 477 | | − return nil, ErrClosed |
| 478 | | − } |
| 479 | | − if index == 0 || index < l.firstIndex || index > l.lastIndex { |
| 480 | | − return nil, ErrNotFound |
| 481 | | − } |
| 482 | | − |
| 483 | | − // find an opened reader |
| 484 | | − var r *reader |
| 485 | | − for _, or := range l.readers { |
| 486 | | − if or.nindex == index { |
| 487 | | − r = or |
| 488 | | − break |
| 489 | | − } |
| 490 | | − } |
| 491 | | − if r == nil { |
| 492 | | − // Reader not found, open a new reader and return the entry at index |
| 493 | | − return l.openReader(index) |
| 494 | | − } |
| 495 | | − |
| 496 | | − // Read next entry from reader |
| 497 | | − for { |
| 498 | | − eindex, edata, err := l.readEntry(r) |
| 499 | | − if err == io.EOF { |
| 500 | | − // Reached the end of the segment. |
| 501 | | − if r.sindex == len(l.segments)-1 { |
| 502 | | − // At the end of the last segment file. |
| 503 | | − if len(l.buffer) > 0 { |
| 504 | | − // But the segment has a buffer, flush it and try again |
| 505 | | − must(l.file.Write(l.buffer)) |
| 506 | | − l.buffer = l.buffer[:0] |
| 507 | | − continue |
| 508 | | − } |
| 509 | | − // Log is missing some final entries, consider is corrput |
| 510 | | − l.closeReader(r) |
| 511 | | − return nil, ErrCorrupt |
| 512 | | − } |
| 513 | | − // Close the old reader and open a new one |
| 514 | | − l.closeReader(r) |
| 515 | | − return l.openReader(index) |
| 516 | | − } |
| 517 | | − if eindex != index { |
| 518 | | − // Log has gaps or is out of order, corrupt file. |
| 519 | | − l.closeReader(r) |
| 520 | | − return nil, ErrCorrupt |
| 521 | | − } |
| 522 | | − r.nindex++ |
| 523 | | − if r.nindex == l.lastIndex+1 { |
| 524 | | − // read the last entry, close the reader |
| 525 | | − l.closeReader(r) |
| 526 | | − } |
| 527 | | − return edata, nil |
| 528 | | − } |
| 529 | | −} |
| 530 | | − |
| 531 | | −// openReader opens a new reader and returns the data belong to entry at index. |
| 532 | | −func (l *Log) openReader(index uint64) (data []byte, err error) { |
| 533 | | − // reader not found, open a new one |
| 534 | | − r := &reader{} |
| 535 | | − r.sindex = l.findSegment(index) |
| 536 | | − r.nindex = l.segments[r.sindex].index |
| 537 | | − r.file, err = os.Open(l.segments[r.sindex].path) |
| 514 | +func (l *Log) loadSegmentEntries(s *segment) error { |
| 515 | + data, err := ioutil.ReadFile(s.path) |
| 538 | 516 | if err != nil { |
| 539 | | − return nil, err |
| 517 | + return err |
| 540 | 518 | } |
| 541 | | − r.rd = bufio.NewReader(r.file) |
| 542 | | − if r.sindex == len(l.segments)-1 { |
| 543 | | − if len(l.buffer) > 0 { |
| 544 | | − // Reading from the last segment, which has a buffer, flush the |
| 545 | | − // buffer before reading from file. |
| 546 | | − must(l.file.Write(l.buffer)) |
| 547 | | − l.buffer = l.buffer[:0] |
| 519 | + ebuf := data |
| 520 | + var epos []bpos |
| 521 | + var pos int |
| 522 | + for exidx := s.index; len(data) > 0; exidx++ { |
| 523 | + var n int |
| 524 | + if l.opts.LogFormat == JSON { |
| 525 | + n, err = loadNextJSONEntry(data) |
| 526 | + } else { |
| 527 | + n, err = loadNextBinaryEntry(data) |
| 548 | 528 | } |
| 549 | | − } |
| 550 | | − // Scan the file for the entry at index. |
| 551 | | − for { |
| 552 | | − eindex, edata, err := l.readEntry(r) |
| 553 | 529 | if err != nil { |
| 554 | | − // Bad news. Likely a corrupt file. |
| 555 | | − r.file.Close() |
| 556 | | − return nil, err |
| 557 | | − } |
| 558 | | − if eindex != r.nindex { |
| 559 | | − // Log has gaps or is out of order, corrupt file. |
| 560 | | − r.file.Close() |
| 561 | | − return nil, ErrCorrupt |
| 562 | | − } |
| 563 | | − r.nindex = eindex + 1 |
| 564 | | − if eindex == index { |
| 565 | | − // Add the new reader to the front of the list of opened readers. |
| 566 | | − l.readers = append(l.readers, nil) |
| 567 | | − copy(l.readers[1:], l.readers[:len(l.readers)-1]) |
| 568 | | − l.readers[0] = r |
| 569 | | − for len(l.readers) > maxReaders { |
| 570 | | − // remove the oldest readers |
| 571 | | − l.closeReader(l.readers[len(l.readers)-1]) |
| 572 | | − } |
| 573 | | − return edata, nil |
| 530 | + return err |
| 574 | 531 | } |
| 532 | + data = data[n:] |
| 533 | + epos = append(epos, bpos{pos, pos + n}) |
| 534 | + pos += n |
| 575 | 535 | } |
| 536 | + s.ebuf = ebuf |
| 537 | + s.epos = epos |
| 538 | + return nil |
| 576 | 539 | } |
| 577 | 540 | |
| 578 | | −// closeReader closes the reader and removes it from the list of opened readers. |
| 579 | | −func (l *Log) closeReader(r *reader) { |
| 580 | | − for i, rr := range l.readers { |
| 581 | | − if rr == r { |
| 582 | | − must(nil, r.file.Close()) |
| 583 | | − l.readers[i] = l.readers[len(l.readers)-1] |
| 584 | | − l.readers[len(l.readers)-1] = nil |
| 585 | | − l.readers = l.readers[:len(l.readers)-1] |
| 586 | | − return |
| 541 | +func loadNextJSONEntry(data []byte) (n int, err error) { |
| 542 | + // {"index":number,"data":string} |
| 543 | + idx := bytes.IndexByte(data, '\n') |
| 544 | + if idx == -1 { |
| 545 | + return 0, ErrCorrupt |
| 546 | + } |
| 547 | + line := data[:idx] |
| 548 | + dres := gjson.Get(*(*string)(unsafe.Pointer(&line)), "data") |
| 549 | + if dres.Type != gjson.String { |
| 550 | + return 0, ErrCorrupt |
| 551 | + } |
| 552 | + return idx + 1, nil |
| 553 | +} |
| 554 | + |
| 555 | +func loadNextBinaryEntry(data []byte) (n int, err error) { |
| 556 | + // data_size + data |
| 557 | + size, n := binary.Uvarint(data) |
| 558 | + if n <= 0 { |
| 559 | + return 0, ErrCorrupt |
| 560 | + } |
| 561 | + if uint64(len(data)-n) < size { |
| 562 | + return 0, ErrCorrupt |
| 563 | + } |
| 564 | + return n + int(size), nil |
| 565 | +} |
| 566 | + |
| 567 | +// loadSegment loads the segment entries into memory, pushes it to the front |
| 568 | +// of the lru cache, and returns it. |
| 569 | +func (l *Log) loadSegment(index uint64) (*segment, error) { |
| 570 | + // check the last segment first. |
| 571 | + lseg := l.segments[len(l.segments)-1] |
| 572 | + if index >= lseg.index { |
| 573 | + return lseg, nil |
| 574 | + } |
| 575 | + // check the most recent cached segment |
| 576 | + var rseg *segment |
| 577 | + l.scache.Range(func(_, v interface{}) bool { |
| 578 | + s := v.(*segment) |
| 579 | + if index >= s.index && index < s.index+uint64(len(s.epos)) { |
| 580 | + rseg = s |
| 581 | + } |
| 582 | + return false |
| 583 | + }) |
| 584 | + if rseg != nil { |
| 585 | + return rseg, nil |
| 586 | + } |
| 587 | + // find in the segment array |
| 588 | + idx := l.findSegment(index) |
| 589 | + s := l.segments[idx] |
| 590 | + if len(s.epos) == 0 { |
| 591 | + // load the entries from cache |
| 592 | + if err := l.loadSegmentEntries(s); err != nil { |
| 593 | + return nil, err |
| 587 | 594 | } |
| 588 | 595 | } |
| 596 | + // push the segment to the front of the cache |
| 597 | + l.pushCache(idx) |
| 598 | + return s, nil |
| 589 | 599 | } |
| 590 | 600 | |
| 591 | | −// readEntry reads the next entry from reader. Returns io.EOF if the readers |
| 592 | | −// has reached the end of a segment. |
| 593 | | −func (l *Log) readEntry(r *reader) (index uint64, data []byte, err error) { |
| 594 | | − return readEntry(r.rd, l.opts.LogFormat, false) |
| 595 | | −} |
| 596 | | − |
| 597 | | −// readEntry from bufio.Reader. The discardData param will discard the entry |
| 598 | | −// data ane return nil. |
| 599 | | −func readEntry(rd *bufio.Reader, frmt LogFormat, discardData bool) ( |
| 600 | | − index uint64, data []byte, err error, |
| 601 | | −) { |
| 602 | | − if frmt == JSON { |
| 603 | | − line, err := rd.ReadBytes('\n') |
| 604 | | − if err != nil { |
| 605 | | − if err == io.EOF && len(line) > 0 { |
| 606 | | − return 0, nil, ErrCorrupt |
| 607 | | − } |
| 608 | | − return 0, nil, err |
| 609 | | − } |
| 610 | | − var m map[string]string |
| 611 | | − if err := json.Unmarshal(line, &m); err != nil { |
| 612 | | − return 0, nil, ErrCorrupt |
| 613 | | − } |
| 614 | | − index, err = strconv.ParseUint(m["index"], 10, 64) |
| 615 | | − if err != nil { |
| 616 | | − return 0, nil, ErrCorrupt |
| 617 | | − } |
| 618 | | − s, ok := m["data"] |
| 619 | | − if !ok || len(s) == 0 { |
| 620 | | − return 0, nil, ErrCorrupt |
| 621 | | − } |
| 622 | | − if !discardData { |
| 623 | | − switch s[0] { |
| 624 | | − case '$': |
| 625 | | − data, err = base64.URLEncoding.DecodeString(s[1:]) |
| 626 | | − if err != nil { |
| 627 | | − return 0, nil, ErrCorrupt |
| 628 | | − } |
| 629 | | − case '+': |
| 630 | | − data = []byte(s[1:]) |
| 631 | | − default: |
| 632 | | − return 0, nil, ErrCorrupt |
| 633 | | − } |
| 634 | | − } |
| 635 | | − return index, data, nil |
| 601 | +// Read an entry from the log. Returns a byte slice containing the data entry. |
| 602 | +func (l *Log) Read(index uint64) (data []byte, err error) { |
| 603 | + l.mu.Lock() |
| 604 | + defer l.mu.Unlock() |
| 605 | + if l.corrupt { |
| 606 | + return nil, ErrCorrupt |
| 607 | + } else if l.closed { |
| 608 | + return nil, ErrClosed |
| 636 | 609 | } |
| 637 | | − index, err = binary.ReadUvarint(rd) |
| 638 | | − if err != nil { |
| 639 | | − return 0, nil, err |
| 610 | + if index == 0 || index < l.firstIndex || index > l.lastIndex { |
| 611 | + return nil, ErrNotFound |
| 640 | 612 | } |
| 641 | | − dataSize, err := binary.ReadUvarint(rd) |
| 613 | + s, err := l.loadSegment(index) |
| 642 | 614 | if err != nil { |
| 643 | | − if err == io.EOF { |
| 644 | | − return 0, nil, ErrCorrupt |
| 645 | | − } |
| 646 | | − return 0, nil, err |
| 615 | + return nil, err |
| 647 | 616 | } |
| 648 | | − if discardData { |
| 649 | | − _, err = rd.Discard(int(dataSize)) |
| 617 | + epos := s.epos[index-s.index] |
| 618 | + edata := s.ebuf[epos.pos:epos.end] |
| 619 | + if l.opts.LogFormat == JSON { |
| 620 | + s := gjson.Get(*(*string)(unsafe.Pointer(&edata)), "data").String() |
| 621 | + if len(s) > 0 && s[0] == '$' { |
| 622 | + var err error |
| 623 | + data, err = base64.URLEncoding.DecodeString(s[1:]) |
| 624 | + if err != nil { |
| 625 | + return nil, ErrCorrupt |
| 626 | + } |
| 627 | + } else if len(s) > 0 && s[0] == '+' { |
| 628 | + data = make([]byte, len(s[1:])) |
| 629 | + copy(data, s[1:]) |
| 630 | + } else { |
| 631 | + return nil, ErrCorrupt |
| 632 | + } |
| 650 | 633 | } else { |
| 651 | | − data = make([]byte, dataSize) |
| 652 | | − _, err = io.ReadFull(rd, data) |
| 653 | | − } |
| 654 | | − if err != nil { |
| 655 | | − if err == io.EOF { |
| 656 | | − return 0, nil, ErrCorrupt |
| 634 | + size, n := binary.Uvarint(edata) |
| 635 | + if n <= 0 { |
| 636 | + return nil, ErrCorrupt |
| 657 | 637 | } |
| 658 | | − return 0, nil, err |
| 638 | + if uint64(len(edata)-n) < size { |
| 639 | + return nil, ErrCorrupt |
| 640 | + } |
| 641 | + data = make([]byte, size) |
| 642 | + copy(data, edata[n:]) |
| 659 | 643 | } |
| 660 | | − return index, data, nil |
| 644 | + return data, nil |
| 661 | 645 | } |
| 662 | 646 | |
| 663 | | −// TruncateFront truncates the front of the log by removing all entries that |
| 664 | | −// are before the provided `firstIndex`. In other words the entry at |
| 665 | | −// `firstIndex` becomes the first entry in the log. |
| 666 | | −func (l *Log) TruncateFront(firstIndex uint64) error { |
| 667 | | − if l.closed { |
| 647 | +// ClearCache clears the segment cache |
| 648 | +func (l *Log) ClearCache() error { |
| 649 | + l.mu.Lock() |
| 650 | + defer l.mu.Unlock() |
| 651 | + if l.corrupt { |
| 652 | + return ErrCorrupt |
| 653 | + } else if l.closed { |
| 668 | 654 | return ErrClosed |
| 669 | 655 | } |
| 656 | + l.clearCache() |
| 657 | + return nil |
| 658 | +} |
| 659 | +func (l *Log) clearCache() { |
| 660 | + l.scache.Range(func(_, v interface{}) bool { |
| 661 | + s := v.(*segment) |
| 662 | + s.ebuf = nil |
| 663 | + s.epos = nil |
| 664 | + return true |
| 665 | + }) |
| 666 | + l.scache = tinylru.LRU{} |
| 667 | + l.scache.Resize(l.opts.SegmentCacheSize) |
| 668 | +} |
| 670 | 669 | |
| 671 | | − index := firstIndex // alias |
| 672 | | − |
| 670 | +// TruncateFront truncates the front of the log by removing all entries that |
| 671 | +// are before the provided `index`. In other words the entry at |
| 672 | +// `index` becomes the first entry in the log. |
| 673 | +func (l *Log) TruncateFront(index uint64) error { |
| 674 | + l.mu.Lock() |
| 675 | + defer l.mu.Unlock() |
| 676 | + if l.corrupt { |
| 677 | + return ErrCorrupt |
| 678 | + } else if l.closed { |
| 679 | + return ErrClosed |
| 680 | + } |
| 681 | + return l.truncateFront(index) |
| 682 | +} |
| 683 | +func (l *Log) truncateFront(index uint64) (err error) { |
| 673 | 684 | if index == 0 || l.lastIndex == 0 || |
| 674 | 685 | index < l.firstIndex || index > l.lastIndex { |
| 675 | 686 | return ErrOutOfRange |
| 676 | 687 | } |
| 677 | | − |
| 678 | | − // Flush the buffer, if needed. |
| 679 | | − if len(l.buffer) > 0 { |
| 680 | | − must(l.file.Write(l.buffer)) |
| 681 | | − l.buffer = nil |
| 682 | | − } |
| 683 | | − // Close all readers |
| 684 | | − for len(l.readers) > 0 { |
| 685 | | − l.closeReader(l.readers[0]) |
| 686 | | − } |
| 687 | | − |
| 688 | 688 | if index == l.firstIndex { |
| 689 | 689 | // nothing to truncate |
| 690 | 690 | return nil |
| 691 | 691 | } |
| 692 | | − |
| 693 | | − // Find which segment has the entry with the index. |
| 694 | | − sidx := l.findSegment(index) |
| 695 | | − // Find the file offset of the first byte belonging to the entry at index. |
| 696 | | − f, err := os.Open(l.segments[sidx].path) |
| 692 | + segIdx := l.findSegment(index) |
| 693 | + var s *segment |
| 694 | + s, err = l.loadSegment(index) |
| 697 | 695 | if err != nil { |
| 698 | 696 | return err |
| 699 | 697 | } |
| 700 | | − defer f.Close() |
| 701 | | − if index > l.segments[sidx].index { |
| 702 | | − // Read all entries prior to entry at index. |
| 703 | | − rd := bufio.NewReader(f) |
| 704 | | − var found bool |
| 705 | | − for { |
| 706 | | − ridx, _, err := readEntry(rd, l.opts.LogFormat, true) |
| 707 | | − if err != nil { |
| 708 | | − return err |
| 709 | | − } |
| 710 | | − if ridx == index-1 { |
| 711 | | − // Seek to exact position of entry, the reader likely overread |
| 712 | | − // the file, so we need to back up what has been buffered. |
| 713 | | − if _, err := f.Seek(0-int64(rd.Buffered()), 1); err != nil { |
| 714 | | − return err |
| 715 | | − } |
| 716 | | − found = true |
| 717 | | − break |
| 718 | | − } |
| 698 | + epos := s.epos[index-s.index:] |
| 699 | + ebuf := s.ebuf[epos[0].pos:] |
| 700 | + // Create a temp file contains the truncated segment. |
| 701 | + tempName := filepath.Join(l.path, "TEMP") |
| 702 | + err = func() error { |
| 703 | + f, err := os.Create(tempName) |
| 704 | + if err != nil { |
| 705 | + return err |
| 719 | 706 | } |
| 720 | | − if !found { |
| 721 | | − return ErrCorrupt |
| 707 | + defer f.Close() |
| 708 | + if _, err := f.Write(ebuf); err != nil { |
| 709 | + return err |
| 722 | 710 | } |
| 723 | | − } |
| 724 | | − // Create a temp file in the same log directory and copy all of the data |
| 725 | | − // starting at the seeked offset position. |
| 726 | | − tempName := filepath.Join(l.path, "TEMP") |
| 727 | | − ftmp, err := os.Create(tempName) |
| 728 | | − if err != nil { |
| 711 | + if err := f.Sync(); err != nil { |
| 712 | + return err |
| 713 | + } |
| 714 | + return f.Close() |
| 715 | + }() |
| 716 | + // Rename the TEMP file to it's START file name. |
| 717 | + startName := filepath.Join(l.path, segmentName(index)+".START") |
| 718 | + if err = os.Rename(tempName, startName); err != nil { |
| 729 | 719 | return err |
| 730 | 720 | } |
| 721 | + // The log was truncated but still needs some file cleanup. Any errors |
| 722 | + // following this message will not cause an on-disk data ocorruption, but |
| 723 | + // may cause an inconsistency with the current program, so we'll return |
| 724 | + // ErrCorrupt so the the user can attempt a recover by calling Close() |
| 725 | + // followed by Open(). |
| 731 | 726 | defer func() { |
| 732 | | − ftmp.Close() |
| 733 | | − os.Remove(tempName) |
| 727 | + if v := recover(); v != nil { |
| 728 | + err = ErrCorrupt |
| 729 | + l.corrupt = true |
| 730 | + } |
| 734 | 731 | }() |
| 735 | | − if _, err := io.Copy(ftmp, f); err != nil { |
| 736 | | − return err |
| 737 | | − } |
| 738 | | − if err := f.Close(); err != nil { |
| 739 | | − return err |
| 732 | + if segIdx == len(l.segments)-1 { |
| 733 | + // Close the tail segment file |
| 734 | + if err = l.sfile.Close(); err != nil { |
| 735 | + return err |
| 736 | + } |
| 740 | 737 | } |
| 741 | | − if err := ftmp.Close(); err != nil { |
| 742 | | − return err |
| 738 | + // Delete truncated segment files |
| 739 | + for i := 0; i <= segIdx; i++ { |
| 740 | + if err = os.Remove(l.segments[i].path); err != nil { |
| 741 | + return err |
| 742 | + } |
| 743 | 743 | } |
| 744 | | − // Then rename the TEMP file to it's START file name. |
| 745 | | − startName := filepath.Join(l.path, segmentName(index)+".START") |
| 746 | | − if err := os.Rename(tempName, startName); err != nil { |
| 744 | + // Rename the START file to the final truncated segment name. |
| 745 | + newName := filepath.Join(l.path, segmentName(index)) |
| 746 | + if os.Rename(startName, newName); err != nil { |
| 747 | 747 | return err |
| 748 | 748 | } |
| 749 | | − // All operations from this point on are syscalls and must succeed, |
| 750 | | − // otherwise we panic. |
| 751 | | − // Remove all of the unneeded segments. |
| 752 | | − for i := 0; i <= sidx; i++ { |
| 753 | | − must(nil, os.Remove(l.segments[i].path)) |
| 754 | | − } |
| 755 | | − // Remove the ".START" suffix from the file name. |
| 756 | | − finalName := startName[:len(startName)-len(".START")] |
| 757 | | − must(nil, os.Rename(startName, finalName)) |
| 758 | | − // Modify the segments list. |
| 759 | | − l.segments = append([]segment{segment{path: finalName, index: index}}, |
| 760 | | − l.segments[sidx+1:]...) |
| 761 | | − if len(l.segments) == 1 { |
| 762 | | − // The last segment has been changed which means we need to reopen the |
| 763 | | − // appendable file and seek to the end. |
| 764 | | − must(nil, l.file.Close()) |
| 765 | | − l.file = must(os.OpenFile(finalName, os.O_RDWR, 0666)).(*os.File) |
| 766 | | − l.fileSize = int(must(l.file.Seek(0, 2)).(int64)) |
| 767 | | − l.buffer = nil |
| 749 | + s.path = newName |
| 750 | + s.index = index |
| 751 | + if segIdx == len(l.segments)-1 { |
| 752 | + // Reopen the tail segment file |
| 753 | + if l.sfile, err = os.OpenFile(newName, os.O_WRONLY, 0666); err != nil { |
| 754 | + return err |
| 755 | + } |
| 756 | + var n int64 |
| 757 | + if n, err = l.sfile.Seek(0, 2); err != nil { |
| 758 | + return err |
| 759 | + } |
| 760 | + if n != int64(len(ebuf)) { |
| 761 | + err = errors.New("invalid seek") |
| 762 | + return err |
| 763 | + } |
| 764 | + // Load the last segment entries |
| 765 | + if err = l.loadSegmentEntries(s); err != nil { |
| 766 | + return err |
| 767 | + } |
| 768 | 768 | } |
| 769 | + l.segments = append([]*segment{}, l.segments[segIdx:]...) |
| 769 | 770 | l.firstIndex = index |
| 771 | + l.clearCache() |
| 770 | 772 | return nil |
| 771 | 773 | } |
| 772 | 774 | |
| 773 | 775 | // TruncateBack truncates the back of the log by removing all entries that |
| 774 | | −// are after the provided `lastIndex`. In other words the entry at `lastIndex` |
| 776 | +// are after the provided `index`. In other words the entry at `index` |
| 775 | 777 | // becomes the last entry in the log. |
| 776 | | −func (l *Log) TruncateBack(lastIndex uint64) error { |
| 777 | | − if l.closed { |
| 778 | +func (l *Log) TruncateBack(index uint64) error { |
| 779 | + l.mu.Lock() |
| 780 | + defer l.mu.Unlock() |
| 781 | + if l.corrupt { |
| 782 | + return ErrCorrupt |
| 783 | + } else if l.closed { |
| 778 | 784 | return ErrClosed |
| 779 | 785 | } |
| 786 | + return l.truncateBack(index) |
| 787 | +} |
| 780 | 788 | |
| 781 | | − index := lastIndex // alias |
| 782 | | − |
| 789 | +func (l *Log) truncateBack(index uint64) (err error) { |
| 783 | 790 | if index == 0 || l.lastIndex == 0 || |
| 784 | | − index > l.lastIndex || index < l.firstIndex { |
| 791 | + index < l.firstIndex || index > l.lastIndex { |
| 785 | 792 | return ErrOutOfRange |
| 786 | 793 | } |
| 787 | | − |
| 788 | | − // Flush the buffer, if needed. |
| 789 | | − if len(l.buffer) > 0 { |
| 790 | | − must(l.file.Write(l.buffer)) |
| 791 | | − l.buffer = nil |
| 792 | | − } |
| 793 | | − // Close all readers |
| 794 | | − for len(l.readers) > 0 { |
| 795 | | − l.closeReader(l.readers[0]) |
| 796 | | − } |
| 797 | | − |
| 798 | 794 | if index == l.lastIndex { |
| 799 | 795 | // nothing to truncate |
| 800 | 796 | return nil |
| 801 | 797 | } |
| 802 | | − |
| 803 | | − // Find which segment has the entry with the index. |
| 804 | | − sidx := l.findSegment(index) |
| 805 | | − // Find the file offset of the first byte belonging to the entry at index. |
| 806 | | − f, err := os.Open(l.segments[sidx].path) |
| 798 | + segIdx := l.findSegment(index) |
| 799 | + var s *segment |
| 800 | + s, err = l.loadSegment(index) |
| 807 | 801 | if err != nil { |
| 808 | 802 | return err |
| 809 | 803 | } |
| 810 | | − defer f.Close() |
| 811 | | − // Read all entries prior to entry at index. |
| 812 | | − rd := bufio.NewReader(f) |
| 813 | | − var found bool |
| 814 | | − var offset int64 |
| 815 | | − for { |
| 816 | | − ridx, _, err := readEntry(rd, l.opts.LogFormat, true) |
| 804 | + epos := s.epos[:index-s.index+1] |
| 805 | + ebuf := s.ebuf[:epos[len(epos)-1].end] |
| 806 | + // Create a temp file contains the truncated segment. |
| 807 | + tempName := filepath.Join(l.path, "TEMP") |
| 808 | + err = func() error { |
| 809 | + f, err := os.Create(tempName) |
| 817 | 810 | if err != nil { |
| 818 | 811 | return err |
| 819 | 812 | } |
| 820 | | − if ridx == index { |
| 821 | | − // Seek to exact position of entry, the reader likely overread |
| 822 | | − // the file, so we need to back up what has been buffered. |
| 823 | | − offset, err = f.Seek(0-int64(rd.Buffered()), 1) |
| 824 | | − if err != nil { |
| 825 | | − return err |
| 826 | | − } |
| 827 | | − found = true |
| 828 | | − // Rewind the segment |
| 829 | | − if _, err := f.Seek(0, 0); err != nil { |
| 830 | | − return err |
| 831 | | − } |
| 832 | | − break |
| 813 | + defer f.Close() |
| 814 | + if _, err := f.Write(ebuf); err != nil { |
| 815 | + return err |
| 833 | 816 | } |
| 834 | | − } |
| 835 | | − if !found { |
| 836 | | − return ErrCorrupt |
| 837 | | − } |
| 838 | | − // Create a temp file in the same log directory and copy all of the data |
| 839 | | − // up to the offset. |
| 840 | | − tempName := filepath.Join(l.path, "TEMP") |
| 841 | | − ftmp, err := os.Create(tempName) |
| 842 | | − if err != nil { |
| 817 | + if err := f.Sync(); err != nil { |
| 818 | + return err |
| 819 | + } |
| 820 | + return f.Close() |
| 821 | + }() |
| 822 | + // Rename the TEMP file to it's END file name. |
| 823 | + endName := filepath.Join(l.path, segmentName(s.index)+".END") |
| 824 | + if err = os.Rename(tempName, endName); err != nil { |
| 843 | 825 | return err |
| 844 | 826 | } |
| 827 | + // The log was truncated but still needs some file cleanup. Any errors |
| 828 | + // following this message will not cause an on-disk data ocorruption, but |
| 829 | + // may cause an inconsistency with the current program, so we'll return |
| 830 | + // ErrCorrupt so the the user can attempt a recover by calling Close() |
| 831 | + // followed by Open(). |
| 845 | 832 | defer func() { |
| 846 | | − ftmp.Close() |
| 847 | | − os.Remove(tempName) |
| 833 | + if v := recover(); v != nil { |
| 834 | + err = ErrCorrupt |
| 835 | + l.corrupt = true |
| 836 | + } |
| 848 | 837 | }() |
| 849 | | − if _, err := io.CopyN(ftmp, f, offset); err != nil { |
| 838 | + |
| 839 | + // Close the tail segment file |
| 840 | + if err = l.sfile.Close(); err != nil { |
| 850 | 841 | return err |
| 851 | 842 | } |
| 852 | | − if err := f.Close(); err != nil { |
| 843 | + // Delete truncated segment files |
| 844 | + for i := segIdx; i < len(l.segments); i++ { |
| 845 | + if err = os.Remove(l.segments[i].path); err != nil { |
| 846 | + return err |
| 847 | + } |
| 848 | + } |
| 849 | + // Rename the END file to the final truncated segment name. |
| 850 | + newName := filepath.Join(l.path, segmentName(s.index)) |
| 851 | + if err = os.Rename(endName, newName); err != nil { |
| 852 | + return err |
| 853 | + } |
| 854 | + // Reopen the tail segment file |
| 855 | + if l.sfile, err = os.OpenFile(newName, os.O_WRONLY, 0666); err != nil { |
| 853 | 856 | return err |
| 854 | 857 | } |
| 855 | | − if err := ftmp.Close(); err != nil { |
| 858 | + var n int64 |
| 859 | + n, err = l.sfile.Seek(0, 2) |
| 860 | + if err != nil { |
| 856 | 861 | return err |
| 857 | 862 | } |
| 858 | | − // Then rename the TEMP file to it's END file name. |
| 859 | | − endName := l.segments[sidx].path + ".END" |
| 860 | | − if err := os.Rename(tempName, endName); err != nil { |
| 863 | + if n != int64(len(ebuf)) { |
| 864 | + err = errors.New("invalid seek") |
| 861 | 865 | return err |
| 862 | 866 | } |
| 863 | | − // All operations from this point on are syscalls and must succeed, |
| 864 | | − // otherwise we panic. |
| 865 | | − // Remove all of the unneeded segments. |
| 866 | | − for i := len(l.segments) - 1; i >= sidx; i-- { |
| 867 | | − must(nil, os.Remove(l.segments[i].path)) |
| 868 | | − } |
| 869 | | − // Modify the segments list. |
| 870 | | − l.segments = append([]segment{}, l.segments[:sidx+1]...) |
| 871 | | − // Remove the ".END" suffix from the file name. |
| 872 | | − finalName := endName[:len(endName)-len(".END")] |
| 873 | | − must(nil, os.Rename(endName, finalName)) |
| 874 | | − // The last segment has been changed which means we need to reopen the |
| 875 | | − // appendable file and seek to the end. |
| 876 | | − must(nil, l.file.Close()) |
| 877 | | − l.file = must(os.OpenFile(finalName, os.O_RDWR, 0666)).(*os.File) |
| 878 | | − l.fileSize = int(must(l.file.Seek(0, 2)).(int64)) |
| 879 | | − l.buffer = nil |
| 867 | + s.path = newName |
| 868 | + l.segments = append([]*segment{}, l.segments[:segIdx+1]...) |
| 880 | 869 | l.lastIndex = index |
| 870 | + l.clearCache() |
| 871 | + if err = l.loadSegmentEntries(s); err != nil { |
| 872 | + return err |
| 873 | + } |
| 881 | 874 | return nil |
| 882 | 875 | } |
| 883 | 876 | |
| 884 | 877 | // Sync performs an fsync on the log. This is not necessary when the |
| 885 | | −// durability is set to High. |
| 886 | | −func (l *Log) Sync() { |
| 887 | | − if len(l.buffer) > 0 { |
| 888 | | − must(l.file.Write(l.buffer)) |
| 889 | | − l.buffer = l.buffer[:0] |
| 878 | +// NoSync option is set to false. |
| 879 | +func (l *Log) Sync() error { |
| 880 | + l.mu.Lock() |
| 881 | + defer l.mu.Unlock() |
| 882 | + if l.corrupt { |
| 883 | + return ErrCorrupt |
| 884 | + } else if l.closed { |
| 885 | + return ErrClosed |
| 890 | 886 | } |
| 891 | | − if l.opts.Durability < High { |
| 892 | | − must(nil, l.file.Sync()) |
| 887 | + return l.sfile.Sync() |
| 888 | +} |
| 889 | + |
| 890 | +func must(v interface{}, err error) interface{} { |
| 891 | + if err != nil { |
| 892 | + panic(err) |
| 893 | 893 | } |
| 894 | + return v |
| 894 | 895 | } |