-
Notifications
You must be signed in to change notification settings - Fork 971
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(share/availability/full): Introduce Q4 trimming for archival nod…
…es (#4028)
- Loading branch information
Showing
13 changed files
with
467 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pruner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
|
||
fullavail "github.com/celestiaorg/celestia-node/share/availability/full" | ||
) | ||
|
||
// TODO @renaynay: remove this file after a few releases -- this utility serves as a temporary solution | ||
// to detect if the node has been run with pruning enabled before on previous version(s), and disallow | ||
// running as an archival node. | ||
|
||
var ( | ||
storePrefix = datastore.NewKey("full_avail") | ||
previousModeKey = datastore.NewKey("previous_mode") | ||
) | ||
|
||
// detectFirstRun is a temporary function that serves to assist migration to the refactored pruner | ||
// implementation (v0.21.0). It checks if the node has been run with pruning enabled before by checking | ||
// if the pruner service ran before, and disallows running as an archival node in the case it has. | ||
func detectFirstRun(ctx context.Context, cfg *Config, ds datastore.Datastore, lastPrunedHeight uint64) error { | ||
ds = namespace.Wrap(ds, storePrefix) | ||
|
||
exists, err := ds.Has(ctx, previousModeKey) | ||
if err != nil { | ||
return fmt.Errorf("share/availability/full: failed to check previous pruned run in "+ | ||
"datastore: %w", err) | ||
} | ||
if exists { | ||
// node has already been run on current version, no migration is necessary | ||
return nil | ||
} | ||
|
||
isArchival := !cfg.EnableService | ||
|
||
// if the node has been pruned before on a previous version, it cannot revert | ||
// to archival mode | ||
if isArchival && lastPrunedHeight > 1 { | ||
return fullavail.ErrDisallowRevertToArchival | ||
} | ||
|
||
return recordFirstRun(ctx, ds, isArchival) | ||
} | ||
|
||
// recordFirstRun exists to assist migration to new pruner implementation (v0.21.0) by recording | ||
// the first run of the pruner service in the full availability's datastore. It assumes the datastore | ||
// is already namespace-wrapped. | ||
func recordFirstRun(ctx context.Context, ds datastore.Datastore, isArchival bool) error { | ||
mode := []byte("pruned") | ||
if isArchival { | ||
mode = []byte("archival") | ||
} | ||
|
||
return ds.Put(ctx, previousModeKey, mode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pruner | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-datastore/namespace" | ||
ds_sync "github.com/ipfs/go-datastore/sync" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
fullavail "github.com/celestiaorg/celestia-node/share/availability/full" | ||
) | ||
|
||
func TestDetectFirstRun(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
t.Run("FirstRunArchival", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
|
||
cfg := &Config{EnableService: false} | ||
|
||
err := detectFirstRun(ctx, cfg, ds, 1) | ||
assert.NoError(t, err) | ||
|
||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, []byte("archival"), prevMode) | ||
}) | ||
|
||
t.Run("FirstRunPruned", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
|
||
cfg := &Config{EnableService: true} | ||
|
||
err := detectFirstRun(ctx, cfg, ds, 1) | ||
assert.NoError(t, err) | ||
|
||
nsWrapped := namespace.Wrap(ds, storePrefix) | ||
prevMode, err := nsWrapped.Get(ctx, previousModeKey) | ||
require.NoError(t, err) | ||
assert.Equal(t, []byte("pruned"), prevMode) | ||
}) | ||
|
||
t.Run("RevertToArchivalNotAllowed", func(t *testing.T) { | ||
ds := ds_sync.MutexWrap(datastore.NewMapDatastore()) | ||
|
||
// create archival node instance over a node that has been pruned before | ||
// (height 500) | ||
cfg := &Config{EnableService: false} | ||
lastPrunedHeight := uint64(500) | ||
|
||
err := detectFirstRun(ctx, cfg, ds, lastPrunedHeight) | ||
assert.Error(t, err) | ||
assert.ErrorIs(t, err, fullavail.ErrDisallowRevertToArchival) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.