/
phalanx_db.go
108 lines (96 loc) · 2.44 KB
/
phalanx_db.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
package phalanx
import (
"log"
"github.com/coreos/etcd/snap"
"github.com/getumen/doctrine/phalanx/phalanxpb"
"google.golang.org/protobuf/proto"
)
// DB is distributed embeddable db
type DB interface {
Get(key []byte) ([]byte, error)
Propose(command *phalanxpb.Command) error
}
type phananxDB struct {
regionName string
proposeC chan<- []byte // channel for proposing updates
stableStore StableStore
commandHander CommandHandler
snapshotter *snap.Snapshotter
}
// NewDB creates new db
func NewDB(
regionName string,
snapshotter *snap.Snapshotter,
proposeC chan []byte,
commitC chan []byte,
errorC chan error,
stableStore StableStore,
commandHander CommandHandler,
) DB {
db := &phananxDB{
regionName: regionName,
proposeC: proposeC,
stableStore: stableStore,
commandHander: commandHander,
snapshotter: snapshotter,
}
// replay log into key-value map
db.readCommits(commitC, errorC)
// read commits from raft into kvStore map until error
go db.readCommits(commitC, errorC)
return db
}
func (db *phananxDB) Get(key []byte) ([]byte, error) {
snapshot, err := db.stableStore.GetSnapshot()
if err != nil {
return nil, err
}
defer snapshot.Release()
return snapshot.Get(db.regionName, key)
}
func (db *phananxDB) Propose(command *phalanxpb.Command) error {
message, err := proto.Marshal(command)
if err != nil {
return err
}
db.proposeC <- message
return nil
}
func (db *phananxDB) readCommits(commitC chan []byte, errorC chan error) error {
for data := range commitC {
if data == nil {
// done replaying log; new data incoming
// OR signaled to load snapshot
snapshot, err := db.snapshotter.Load()
if err == snap.ErrNoSnapshot {
return nil
}
if err != nil {
log.Panic(err)
}
log.Printf("loading snapshot at term %d and index %d",
snapshot.Metadata.Term, snapshot.Metadata.Index)
if err := db.recoverFromSnapshot(snapshot.Data); err != nil {
log.Panic(err)
}
continue
}
var command phalanxpb.Command
err := proto.Unmarshal(data, &command)
if err != nil {
errorC <- err
continue
}
db.commandHander.Apply(db.regionName, &command, db.stableStore)
}
if err, ok := <-errorC; ok {
return err
}
return nil
}
func (db *phananxDB) GetSnapshot() ([]byte, error) {
return db.stableStore.CreateCheckpoint(db.regionName)
}
func (db *phananxDB) recoverFromSnapshot(snapshot []byte) error {
return db.stableStore.RestoreToCheckpoint(db.regionName, snapshot)
}