Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions pkg/aux_/store/raftstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const (
// repo is a full implementation of aux_.repos.Repository for Raft-based storage.
type repo struct {
consensus *consensus.Consensus
memStore *memstore.Store[repos.Repository]
memRepo repos.Repository
*memstore.Store[repos.Repository]
}

func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.Store[repos.Repository], error) {
Expand All @@ -39,7 +38,7 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.
return nil, stacktrace.Propagate(err, "failed to initialize aux memstore")
}

r := &repo{memStore: memStore, memRepo: memStore.GetRepo()}
r := &repo{Store: memStore}
store, err := raftstore.Init(ctx, logger.With(zap.String("service", "aux_")), locality, params, r, nil)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to initialize aux raftstore")
Expand All @@ -52,14 +51,6 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.

func (r *repo) GetRepo() repos.Repository { return r }

func (r *repo) GetSnapshot() ([]byte, error) {
return r.memStore.GetSnapshot()
}

func (r *repo) RestoreFromSnapshot(data []byte) error {
return r.memStore.RestoreFromSnapshot(data)
}

func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, error) {
switch proposal.RequestType {
case saveOwnMetadata:
Expand All @@ -68,18 +59,18 @@ func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, err
return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", saveOwnMetadata)
}

return nil, r.memRepo.SaveOwnMetadata(ctx, payload.Locality, payload.PublicEndpoint)
return nil, r.Store.GetRepo().SaveOwnMetadata(ctx, payload.Locality, payload.PublicEndpoint)

case getDSSMetadata:
return r.memRepo.GetDSSMetadata(ctx)
return r.Store.GetRepo().GetDSSMetadata(ctx)

case recordHeartbeat:
var heartbeat auxmodels.Heartbeat
if err := json.Unmarshal(proposal.Value, &heartbeat); err != nil {
return nil, stacktrace.Propagate(err, "failed to unmarshal %s payload", recordHeartbeat)
}

return nil, r.memRepo.RecordHeartbeat(ctx, heartbeat)
return nil, r.Store.GetRepo().RecordHeartbeat(ctx, heartbeat)

default:
return nil, stacktrace.NewError("unknown request type: %q", proposal.RequestType)
Expand Down
17 changes: 8 additions & 9 deletions pkg/raftstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/interuss/dss/pkg/locality"
"github.com/interuss/dss/pkg/logging"
"github.com/interuss/dss/pkg/memstore"
"github.com/interuss/dss/pkg/raftstore/consensus"
raftparams "github.com/interuss/dss/pkg/raftstore/params"
"github.com/interuss/dss/pkg/random"
Expand All @@ -15,19 +16,12 @@ import (
)

type RaftRepo[R any] interface {
GetRepo() R
memstore.MemRepo[R]

// Apply is called on every committed entry. The proposal must be applied atomically.
// The any return mirrors store.OperationHandler.Execute: different requests yield different
// concrete result types. Callers recover the type via store.TransactWithResult.
Apply(ctx context.Context, proposal consensus.Proposal) (any, error)

// GetSnapshot returns a serialized view of current state, suitable
// for restoring via RestoreFromSnapshot.
GetSnapshot() ([]byte, error)

// RestoreFromSnapshot replaces all state with the snapshot in data.
// data is always the output of a prior GetSnapshot.
RestoreFromSnapshot(data []byte) error
}

type Store[R any] struct {
Expand Down Expand Up @@ -119,7 +113,12 @@ func (s *Store[R]) processCommits(ctx context.Context, commitCh <-chan consensus
proposalCtx := timestamp.NewContext(ctx, commit.Prop.Timestamp)
proposalCtx = locality.NewContext(proposalCtx, commit.Prop.Locality)
proposalCtx = random.NewContext(proposalCtx, commit.Prop.Seed)
s.raftRepo.Checkpoint()
result, err := s.raftRepo.Apply(proposalCtx, commit.Prop)
if err != nil {
Comment thread
MariemBaccari marked this conversation as resolved.
s.logger.Warn("failed to apply proposal, rolling back", zap.String("proposal_id", commit.Prop.ID), zap.String("proposal_type", string(commit.Prop.RequestType)), zap.Error(err))
s.raftRepo.Restore()
}
commit.Done <- consensus.ProposalResult{Result: result, Error: err}
}
}
Expand Down
15 changes: 3 additions & 12 deletions pkg/rid/store/raftstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
// repo is a full implementation of rid.repos.Repository for Raft-based storage.
type repo struct {
consensus *consensus.Consensus
memStore *memstore.Store[repos.Repository]
memRepo repos.Repository
*memstore.Store[repos.Repository]
}

func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.Store[repos.Repository], error) {
Expand All @@ -32,7 +31,7 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.
return nil, stacktrace.Propagate(err, "failed to initialize rid memstore")
}

r := &repo{memStore: memStore, memRepo: memStore.GetRepo()}
r := &repo{Store: memStore}
store, err := raftstore.Init(ctx, logger.With(zap.String("service", "rid")), locality, params, r, operations.Registry)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to initialize rid raftstore")
Expand All @@ -45,14 +44,6 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.

func (r *repo) GetRepo() repos.Repository { return r }

func (r *repo) GetSnapshot() ([]byte, error) {
return r.memStore.GetSnapshot()
}

func (r *repo) RestoreFromSnapshot(data []byte) error {
return r.memStore.RestoreFromSnapshot(data)
}

func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, error) {
switch proposal.RequestType {

Expand All @@ -67,6 +58,6 @@ func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, err
return nil, stacktrace.Propagate(err, "failed to decode %s payload", proposal.RequestType)
}

return handler.Execute(ctx, r.memRepo, request)
return handler.Execute(ctx, r.Store.GetRepo(), request)
}
}
15 changes: 3 additions & 12 deletions pkg/scd/store/raftstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
// repo is a full implementation of scd.repos.Repository for Raft-based storage.
type repo struct {
consensus *consensus.Consensus
memStore *memstore.Store[repos.Repository]
memRepo repos.Repository
*memstore.Store[repos.Repository]
}

func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.Store[repos.Repository], error) {
Expand All @@ -32,7 +31,7 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.
return nil, stacktrace.Propagate(err, "failed to initialize scd memstore")
}

r := &repo{memStore: memStore, memRepo: memStore.GetRepo()}
r := &repo{Store: memStore}
store, err := raftstore.Init(ctx, logger.With(zap.String("service", "scd")), locality, params, r, operations.Registry)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to initialize scd raftstore")
Expand All @@ -45,14 +44,6 @@ func Init(ctx context.Context, logger *zap.Logger, locality string) (*raftstore.

func (r *repo) GetRepo() repos.Repository { return r }

func (r *repo) GetSnapshot() ([]byte, error) {
return r.memStore.GetSnapshot()
}

func (r *repo) RestoreFromSnapshot(data []byte) error {
return r.memStore.RestoreFromSnapshot(data)
}

func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, error) {
switch proposal.RequestType {

Expand All @@ -67,6 +58,6 @@ func (r *repo) Apply(ctx context.Context, proposal consensus.Proposal) (any, err
return nil, stacktrace.Propagate(err, "failed to decode %s payload", proposal.RequestType)
}

return handler.Execute(ctx, r.memRepo, request)
return handler.Execute(ctx, r.Store.GetRepo(), request)
}
}
Loading