File
Blob: kv/aof/log.go
| 1 | package aof |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "hash/crc64" |
| 7 | |
| 8 | "go.miragespace.co/specter/kv/aof/proto" |
| 9 | "go.miragespace.co/specter/spec/chord" |
| 10 | |
| 11 | bufPool "github.com/libp2p/go-buffer-pool" |
| 12 | "go.uber.org/zap" |
| 13 | ) |
| 14 | |
| 15 | var crcTable = crc64.MakeTable(crc64.ECMA) |
| 16 | |
| 17 | func (d *DiskKV) replayLogs() error { |
| 18 | index, err := d.log.LastIndex() |
| 19 | if err != nil { |
| 20 | return fmt.Errorf("error reading last log index: %w", err) |
| 21 | } |
| 22 | d.logger.Info("Replaying mutation logs", zap.Uint64("index", index)) |
| 23 | mut := &proto.Mutation{} |
| 24 | entry := &proto.LogEntry{} |
| 25 | for i := uint64(1); i <= index; i++ { |
| 26 | buf, err := d.log.Read(i) |
| 27 | if err != nil { |
| 28 | return fmt.Errorf("error reading log at index %d: %w", i, err) |
| 29 | } |
| 30 | if err := entry.UnmarshalVT(buf); err != nil { |
| 31 | return fmt.Errorf("error deserializing log at index %d: %w", i, err) |
| 32 | } |
| 33 | if err := d.decodeEntry(entry, mut); err != nil { |
| 34 | return fmt.Errorf("error decoding entry to mutation at index %d: %w", i, err) |
| 35 | } |
| 36 | if err := d.handleMutation(mut); err != nil { |
| 37 | // A rejected duplicate append may survive a crash before rollback. |
| 38 | // Its child already exists, so replaying it leaves the state unchanged. |
| 39 | if mut.GetType() != proto.MutationType_PREFIX_APPEND || !errors.Is(err, chord.ErrKVPrefixConflict) { |
| 40 | return fmt.Errorf("error apply mutation to memory state at index %d: %w", i, err) |
| 41 | } |
| 42 | } |
| 43 | entry.Reset() |
| 44 | mut.Reset() |
| 45 | } |
| 46 | d.counter = index + 1 |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | func (d *DiskKV) decodeEntry(entry *proto.LogEntry, mut *proto.Mutation) (err error) { |
| 51 | if entry.GetChecksum() != crc64.Checksum(entry.GetData(), crcTable) { |
| 52 | err = fmt.Errorf("log entry checksum does not match, possibly corrupted log") |
| 53 | return |
| 54 | } |
| 55 | switch entry.GetVersion() { |
| 56 | case proto.LogVersion_V1: |
| 57 | // uncompressed |
| 58 | err = mut.UnmarshalVT(entry.Data) |
| 59 | default: |
| 60 | err = fmt.Errorf("unknown log version: %s", entry.GetVersion()) |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | func (d *DiskKV) appendLog(mut *proto.Mutation) error { |
| 66 | mutBuf := bufPool.Get(mut.SizeVT()) |
| 67 | defer bufPool.Put(mutBuf) |
| 68 | |
| 69 | _, err := mut.MarshalToSizedBufferVT(mutBuf) |
| 70 | if err != nil { |
| 71 | d.logger.Error("Error serializing mutation", zap.String("mutation", mut.GetType().String()), zap.Error(err)) |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | entry := proto.LogEntryFromVTPool() |
| 76 | defer entry.ReturnToVTPool() |
| 77 | |
| 78 | entry.Version = proto.LogVersion_V1 |
| 79 | entry.Data = mutBuf |
| 80 | entry.Checksum = crc64.Checksum(mutBuf, crcTable) |
| 81 | |
| 82 | logBuf := bufPool.Get(entry.SizeVT()) |
| 83 | defer bufPool.Put(logBuf) |
| 84 | |
| 85 | _, err = entry.MarshalToSizedBufferVT(logBuf) |
| 86 | if err != nil { |
| 87 | d.logger.Error("Error serializing log entry", zap.String("version", entry.GetVersion().String()), zap.String("mutation", mut.GetType().String()), zap.Error(err)) |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | if err := d.log.Write(d.counter, logBuf); err != nil { |
| 92 | d.logger.Error("Error appending to log", zap.Uint64("counter", d.counter), zap.String("mutation", mut.GetType().String()), zap.Error(err)) |
| 93 | return err |
| 94 | } |
| 95 | d.counter += 1 |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | func (d *DiskKV) rollbackOne(mut *proto.Mutation, err error) { |
| 100 | d.logger.Warn("Rolling back last mutation because of an error", |
| 101 | zap.String("mutation", mut.GetType().String()), |
| 102 | zap.Uint64("truncate", d.counter-2), |
| 103 | zap.Uint64("index", d.counter-1), |
| 104 | zap.Error(err), |
| 105 | ) |
| 106 | d.counter -= 1 |
| 107 | if err := d.log.TruncateBack(d.counter - 1); err != nil { |
| 108 | d.logger.Error("Error applying rollback to the last mutation", |
| 109 | zap.Error(err)) |
| 110 | } |
| 111 | } |