Skip to content

Commit

Permalink
Add state.Configure to load state/trees before migrations are started (
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardsn authored Sep 13, 2024
1 parent 4a44d6e commit 6e0d0db
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions network/dag/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var ErrPayloadNotFound = errors.New("payload not found")
type State interface {
core.Diagnosable
core.Migratable
core.Configurable

// WritePayload writes contents for the specified payload, identified by the given hash.
// It also calls observers and therefore requires the transaction.
Expand Down
14 changes: 14 additions & 0 deletions network/dag/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions network/dag/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (s *state) updateState(tx stoabs.WriteTx, transaction Transaction) error {
}

func (s *state) loadState(ctx context.Context) {
if err := s.db.Read(ctx, func(tx stoabs.ReadTx) error {
err := s.db.Read(ctx, func(tx stoabs.ReadTx) error {
s.lamportClockHigh.Store(s.graph.getHighestClockValue(tx))
if err := s.xorTree.read(tx); err != nil {
return fmt.Errorf("failed to read xorTree: %w", err)
Expand All @@ -243,10 +243,9 @@ func (s *state) loadState(ctx context.Context) {
return fmt.Errorf("failed to read ibltTree: %w", err)
}
return nil
}); err != nil {
log.Logger().
WithError(err).
Errorf("Failed to load the XOR and IBLT trees")
})
if err != nil {
log.Logger().WithError(err).Errorf("Failed to load the XOR and IBLT trees")
}
log.Logger().Trace("Loaded the XOR and IBLT trees")
}
Expand Down Expand Up @@ -394,6 +393,12 @@ func (s *state) CorrectStateDetected() {
s.xorTreeRepair.stateOK()
}

func (s *state) Configure(_ core.ServerConfig) error {
// state must be loaded before any migration takes place
s.loadState(context.Background())
return nil
}

func (s *state) Shutdown() error {
if s.transactionCount != nil {
prometheus.Unregister(s.transactionCount)
Expand All @@ -405,8 +410,6 @@ func (s *state) Shutdown() error {
}

func (s *state) Start() error {
s.loadState(context.Background())

err := s.db.Read(context.Background(), func(tx stoabs.ReadTx) error {
currentTXCount := s.graph.getNumberOfTransactions(tx)
s.transactionCount.Add(float64(currentTXCount))
Expand Down
5 changes: 5 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func (n *Network) Configure(config core.ServerConfig) error {
if n.state, err = dag.NewState(dagStore, dag.NewPrevTransactionsVerifier(), dag.NewTransactionSignatureVerifier(nutsKeyResolver)); err != nil {
return fmt.Errorf("failed to configure state: %w", err)
}
// load state
err = n.state.Configure(core.ServerConfig{})
if err != nil {
return err
}

n.strictMode = config.Strictmode
n.peerID = transport.PeerID(uuid.New().String())
Expand Down
4 changes: 3 additions & 1 deletion network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ func TestNetwork_Configure(t *testing.T) {
prov := storage.NewMockProvider(ctrl)
ctx.network.storeProvider = prov
ctx.network.connectionManager = nil
prov.EXPECT().GetKVStore(gomock.Any(), gomock.Any())
store := stoabs.NewMockKVStore(ctrl)
store.EXPECT().Read(gomock.Any(), gomock.Any())
prov.EXPECT().GetKVStore(gomock.Any(), gomock.Any()).Return(store, nil)
prov.EXPECT().GetKVStore(gomock.Any(), gomock.Any()).Return(nil, errors.New("failed"))

err := ctx.network.Configure(core.TestServerConfig(func(config *core.ServerConfig) {
Expand Down

0 comments on commit 6e0d0db

Please sign in to comment.