@@ -33,8 +33,9 @@ import ( |
33 | 33 | "gitlab.com/thunderdb/ThunderDB/utils" |
34 | 34 | ) |
35 | 35 |
|
36 | | -const CMDSET = "set" |
| 36 | +const cmdset = "set" |
37 | 37 |
|
| 38 | +// LocalStorage holds consistent and storage struct |
38 | 39 | type LocalStorage struct { |
39 | 40 | consistent *consistent.Consistent |
40 | 41 | *storage.Storage |
@@ -61,6 +62,7 @@ func initStorage(dbFile string) (stor *LocalStorage, err error) { |
61 | 62 | return |
62 | 63 | } |
63 | 64 |
|
| 65 | +// Prepare implements twopc Worker.Prepare |
64 | 66 | func (s *LocalStorage) Prepare(ctx context.Context, wb twopc.WriteBatch) (err error) { |
65 | 67 | payload, err := s.decodeLog(wb) |
66 | 68 | if err != nil { |
@@ -75,6 +77,7 @@ func (s *LocalStorage) Prepare(ctx context.Context, wb twopc.WriteBatch) (err er |
75 | 77 | return s.Storage.Prepare(ctx, execLog) |
76 | 78 | } |
77 | 79 |
|
| 80 | +// Commit implements twopc Worker.Commit |
78 | 81 | func (s *LocalStorage) Commit(ctx context.Context, wb twopc.WriteBatch) (err error) { |
79 | 82 |
|
80 | 83 | payload, err := s.decodeLog(wb) |
@@ -104,6 +107,7 @@ func (s *LocalStorage) Commit(ctx context.Context, wb twopc.WriteBatch) (err err |
104 | 107 | return s.Storage.Commit(ctx, execLog) |
105 | 108 | } |
106 | 109 |
|
| 110 | +// Rollback implements twopc Worker.Rollback |
107 | 111 | func (s *LocalStorage) Rollback(ctx context.Context, wb twopc.WriteBatch) (err error) { |
108 | 112 | payload, err := s.decodeLog(wb) |
109 | 113 | if err != nil { |
@@ -121,7 +125,7 @@ func (s *LocalStorage) Rollback(ctx context.Context, wb twopc.WriteBatch) (err e |
121 | 125 |
|
122 | 126 | func (s *LocalStorage) compileExecLog(payload *KayakPayload) (execLog *storage.ExecLog, err error) { |
123 | 127 | switch payload.Command { |
124 | | - case CMDSET: |
| 128 | + case cmdset: |
125 | 129 | var nodeToSet proto.Node |
126 | 130 | err = utils.DecodeMsgPack(payload.Data, &nodeToSet) |
127 | 131 | if err != nil { |
@@ -179,28 +183,32 @@ func (s *LocalStorage) decodeLog(wb twopc.WriteBatch) (payload *KayakPayload, er |
179 | 183 | return |
180 | 184 | } |
181 | 185 |
|
| 186 | +// KayakKVServer holds kayak.Runtime and LocalStorage |
182 | 187 | type KayakKVServer struct { |
183 | 188 | Runtime *kayak.Runtime |
184 | 189 | Storage *LocalStorage |
185 | 190 | } |
186 | 191 |
|
| 192 | +// Init implements consistent.Persistence |
187 | 193 | func (s *KayakKVServer) Init(storePath string, initNode *proto.Node) (err error) { |
188 | 194 | return |
189 | 195 | } |
190 | 196 |
|
| 197 | +// KayakPayload is the payload used in kayak Leader and Follower |
191 | 198 | type KayakPayload struct { |
192 | 199 | Command string |
193 | 200 | Data []byte |
194 | 201 | } |
195 | 202 |
|
| 203 | +// SetNode implements consistent.Persistence |
196 | 204 | func (s *KayakKVServer) SetNode(node *proto.Node) (err error) { |
197 | 205 | nodeBuf, err := utils.EncodeMsgPack(node) |
198 | 206 | if err != nil { |
199 | 207 | log.Errorf("marshal node failed: %s", err) |
200 | 208 | return |
201 | 209 | } |
202 | 210 | payload := &KayakPayload{ |
203 | | - Command: CMDSET, |
| 211 | + Command: cmdset, |
204 | 212 | Data: nodeBuf.Bytes(), |
205 | 213 | } |
206 | 214 |
|
@@ -218,16 +226,19 @@ func (s *KayakKVServer) SetNode(node *proto.Node) (err error) { |
218 | 226 | return |
219 | 227 | } |
220 | 228 |
|
| 229 | +// DelNode implements consistent.Persistence |
221 | 230 | func (s *KayakKVServer) DelNode(nodeID proto.NodeID) (err error) { |
222 | 231 | // no need to del node currently |
223 | 232 | return |
224 | 233 | } |
225 | 234 |
|
| 235 | +// Reset implements consistent.Persistence |
226 | 236 | func (s *KayakKVServer) Reset() (err error) { |
227 | 237 | // no need to reset for kayak |
228 | 238 | return |
229 | 239 | } |
230 | 240 |
|
| 241 | +// GetAllNodeInfo implements consistent.Persistence |
231 | 242 | func (s *KayakKVServer) GetAllNodeInfo() (nodes []proto.Node, err error) { |
232 | 243 | var result [][]interface{} |
233 | 244 | mh := &codec.MsgpackHandle{} |
|