@@ -3,37 +3,69 @@ package raftstore
33import (
44 "context"
55
6- dsserr "github.com/interuss/dss/pkg/errors "
6+ "github.com/interuss/dss/pkg/memstore "
77 "github.com/interuss/dss/pkg/raftstore"
88 "github.com/interuss/dss/pkg/raftstore/consensus"
99 "github.com/interuss/dss/pkg/rid/actions"
1010 "github.com/interuss/dss/pkg/rid/repos"
11+ ridmemstore "github.com/interuss/dss/pkg/rid/store/memstore"
1112 ridraftparams "github.com/interuss/dss/pkg/rid/store/raftstore/params"
1213 "github.com/interuss/stacktrace"
1314 "go.uber.org/zap"
1415)
1516
1617// repo is a full implementation of rid.repos.Repository for Raft-based storage.
17- type repo struct {}
18+ type repo struct {
19+ consensus * consensus.Consensus
20+ memStore * memstore.Store [repos.Repository ]
21+ memRepo repos.Repository
22+ }
1823
1924func Init (ctx context.Context , logger * zap.Logger ) (* raftstore.Store [repos.Repository ], error ) {
2025 params , err := ridraftparams .GetConnectParameters ()
2126 if err != nil {
2227 return nil , stacktrace .Propagate (err , "failed to get rid raft parameters" )
2328 }
24- return raftstore .Init (ctx , logger .With (zap .String ("service" , "rid" )), params , & repo {}, actions .Registry )
29+ memStore , err := ridmemstore .Init (ctx , logger )
30+ if err != nil {
31+ return nil , stacktrace .Propagate (err , "failed to initialize rid memstore" )
32+ }
33+
34+ r := & repo {memStore : memStore , memRepo : memStore .GetRepo ()}
35+ store , err := raftstore .Init (ctx , logger .With (zap .String ("service" , "rid" )), params , r , actions .Registry )
36+ if err != nil {
37+ return nil , stacktrace .Propagate (err , "failed to initialize rid raftstore" )
38+ }
39+
40+ r .consensus = store .Consensus
41+
42+ return store , nil
2543}
2644
2745func (r * repo ) GetRepo () repos.Repository { return r }
2846
2947func (r * repo ) GetSnapshot () ([]byte , error ) {
30- return nil , stacktrace . NewErrorWithCode ( dsserr . NotImplemented , "not implemented yet" )
48+ return r . memStore . GetSnapshot ( )
3149}
3250
33- func (r * repo ) RestoreFromSnapshot ([]byte ) error {
34- return stacktrace . NewErrorWithCode ( dsserr . NotImplemented , "not implemented yet" )
51+ func (r * repo ) RestoreFromSnapshot (data []byte ) error {
52+ return r . memStore . RestoreFromSnapshot ( data )
3553}
3654
37- func (r * repo ) Apply (_ context.Context , _ consensus.Proposal ) (any , error ) {
38- return nil , stacktrace .NewErrorWithCode (dsserr .NotImplemented , "not implemented yet" )
55+ func (r * repo ) Apply (ctx context.Context , proposal consensus.Proposal ) (any , error ) {
56+ switch proposal .RequestType {
57+
58+ default :
59+ handler , ok := actions .Registry [string (proposal .RequestType )]
60+ if ! ok {
61+ return nil , stacktrace .NewError ("unrecognized request type: %s" , proposal .RequestType )
62+ }
63+
64+ request , err := handler .Decode (proposal .Value )
65+ if err != nil {
66+ return nil , stacktrace .Propagate (err , "failed to decode %s payload" , proposal .RequestType )
67+ }
68+
69+ return handler .Execute (ctx , r .memRepo , request )
70+ }
3971}
0 commit comments