Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split poet into registration and worker services #364

Merged
merged 29 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ca1be92
Split poet into registration and worker services
poszu Aug 25, 2023
93a983f
Merge remote-tracking branch 'origin/develop' into 355-registration-svc
poszu Sep 5, 2023
e1dced6
Close rounds DBs as soon as they are not needed
poszu Sep 5, 2023
b9487e2
Merge branch 'develop' into 355-registration-svc
poszu Sep 6, 2023
9c9a445
Teardown recovered round
poszu Sep 6, 2023
d4bf630
Improve logging in worker svc
poszu Sep 6, 2023
1a83cf7
Document inMemory transport
poszu Sep 6, 2023
384cbe3
inMemory transport UTs
poszu Sep 6, 2023
ce75c30
Merge branch 'develop' into 355-registration-svc
poszu Sep 6, 2023
da65d7e
Merge remote-tracking branch 'origin/develop' into 355-registration-svc
poszu Sep 7, 2023
cffea7f
Test for skipping past rounds
poszu Sep 7, 2023
4d21ff3
Test recovering finished round
poszu Sep 7, 2023
5b4f1ac
Test recovering round in progress
poszu Sep 7, 2023
5a527d6
Merge branch 'develop' into 355-registration-svc
poszu Sep 7, 2023
5f5e624
Remove keys from the worker svc
poszu Sep 7, 2023
810931e
Close server
poszu Sep 8, 2023
449c727
Merge branch 'develop' into 355-registration-svc
poszu Sep 8, 2023
0695efd
Close registration in tests + flake fix
poszu Sep 8, 2023
8ed1f4b
Update registration/doc.go
poszu Sep 11, 2023
949b764
Update registration/round.go
poszu Sep 11, 2023
cfe9805
renames after review feedback
poszu Sep 11, 2023
c60684f
Release openRoundMutex early
poszu Sep 11, 2023
dc0378b
Avoid flushing empty submit batch
poszu Sep 11, 2023
5858e0d
removed server/state.go
poszu Sep 11, 2023
60a4f14
fixes
poszu Sep 11, 2023
dd70ebf
Update registration/registration.go
poszu Sep 12, 2023
1b2cdd9
WiP: config refactor
fasmat Sep 12, 2023
8a8e0eb
Finish config refactor
poszu Sep 12, 2023
5d92ff8
Fix UTs
poszu Sep 12, 2023
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
37 changes: 0 additions & 37 deletions config/config_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions db/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@
}
defer targetDb.Close()

trans, err := targetDb.OpenTransaction()
tx, err := targetDb.OpenTransaction()
if err != nil {
return fmt.Errorf("opening new DB transaction: %w", err)
}
iter := oldDb.NewIterator(nil, nil)
defer iter.Release()
for iter.Next() {
if err := trans.Put(iter.Key(), iter.Value(), nil); err != nil {
trans.Discard()
if err := tx.Put(iter.Key(), iter.Value(), nil); err != nil {
tx.Discard()

Check warning on line 53 in db/migrate.go

View check run for this annotation

Codecov / codecov/patch

db/migrate.go#L53

Added line #L53 was not covered by tests
return fmt.Errorf("migrating key %X: %w", iter.Key(), err)
}
}
iter.Release()
if err := trans.Commit(); err != nil {
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing DB transaction: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion db/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db_test
import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestSkipMigrateInPlace(t *testing.T) {
}

func TestSkipMigrateSrcDoesntExist(t *testing.T) {
require.NoError(t, db.Migrate(context.Background(), t.TempDir(), "i-dont-exist"))
require.NoError(t, db.Migrate(context.Background(), t.TempDir(), filepath.Join(t.TempDir(), "i-dont-exist")))
}

func TestDontMigrateIfTargetExists(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions migrations/00_dbdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"go.uber.org/zap"

"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/db"
"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/server"
)

func migrateDbDir(ctx context.Context, cfg *config.Config) error {
func migrateDbDir(ctx context.Context, cfg *server.Config) error {
if err := migrateProofsDb(ctx, cfg); err != nil {
return fmt.Errorf("migrating proofs DB: %w", err)
}
Expand All @@ -25,7 +25,7 @@ func migrateDbDir(ctx context.Context, cfg *config.Config) error {
return nil
}

func migrateRoundsDbs(ctx context.Context, cfg *config.Config) error {
func migrateRoundsDbs(ctx context.Context, cfg *server.Config) error {
roundsDataDir := filepath.Join(cfg.DataDir, "rounds")
// check if dir exists
if _, err := os.Stat(roundsDataDir); os.IsNotExist(err) {
Expand Down Expand Up @@ -57,7 +57,7 @@ func migrateRoundsDbs(ctx context.Context, cfg *config.Config) error {
return nil
}

func migrateProofsDb(ctx context.Context, cfg *config.Config) error {
func migrateProofsDb(ctx context.Context, cfg *server.Config) error {
proofsDbPath := filepath.Join(cfg.DbDir, "proofs")
oldProofsDbPath := filepath.Join(cfg.DataDir, "proofs")
if err := db.Migrate(ctx, proofsDbPath, oldProofsDbPath); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions migrations/00_dbdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb"

"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/server"
)

func TestMigrateRoundsDb(t *testing.T) {
// Prepare
cfg := config.Config{
cfg := server.Config{
DataDir: t.TempDir(),
DbDir: t.TempDir(),
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestMigrateRoundsDb(t *testing.T) {
}

func TestMigrateRoundsDb_NothingToMigrate(t *testing.T) {
cfg := config.Config{
cfg := server.Config{
DataDir: t.TempDir(),
DbDir: t.TempDir(),
}
Expand Down
4 changes: 2 additions & 2 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package migrations
import (
"context"

"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/server"
)

func Migrate(ctx context.Context, cfg *config.Config) error {
func Migrate(ctx context.Context, cfg *server.Config) error {
ctx = logging.NewContext(ctx, logging.FromContext(ctx).Named("migrations"))
if err := migrateDbDir(ctx, cfg); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

"github.com/stretchr/testify/require"

"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/migrations"
"github.com/spacemeshos/poet/server"
)

func TestMigrate(t *testing.T) {
cfg := config.DefaultConfig()
cfg := server.DefaultConfig()
cfg.PoetDir = t.TempDir()
cfg.DataDir = t.TempDir()
cfg.DbDir = t.TempDir()
Expand Down
30 changes: 20 additions & 10 deletions poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"

"github.com/spacemeshos/poet/config"
"github.com/spacemeshos/poet/logging"
"github.com/spacemeshos/poet/migrations"
"github.com/spacemeshos/poet/server"
Expand All @@ -33,26 +32,26 @@ var version = "unknown"
// os.Exit() is called.
func poetMain() (err error) {
// Start with a default Config with sane settings
cfg := config.DefaultConfig()
cfg := server.DefaultConfig()
// Pre-parse the command line to check for an alternative Config file
cfg, err = config.ParseFlags(cfg)
cfg, err = server.ParseFlags(cfg)
if err != nil {
return err
}
// Load configuration file overwriting defaults with any specified options
// Parse CLI options and overwrite/add any specified options
cfg, err = config.ReadConfigFile(cfg)
cfg, err = server.ReadConfigFile(cfg)
if err != nil {
return err
}

cfg, err = config.SetupConfig(cfg)
cfg, err = server.SetupConfig(cfg)
if err != nil {
return err
}
// Finally, parse the remaining command line options again to ensure
// they take precedence.
cfg, err = config.ParseFlags(cfg)
cfg, err = server.ParseFlags(cfg)
if err != nil {
return err
}
Expand All @@ -69,9 +68,15 @@ func poetMain() (err error) {
logger.Info("shutdown complete")
}()

// Show version at startup.
logger.Sugar().
Infof("version: %s, dir: %v, datadir: %v, genesis: %v", version, cfg.PoetDir, cfg.DataDir, cfg.Service.Genesis.Time())
logger.Info(
"starting up poet",
zap.String("version", version),
zap.String("poet dir", cfg.PoetDir),
zap.String("datadir", cfg.DataDir),
zap.String("dbdir", cfg.DbDir),
zap.Time("genesis", cfg.Genesis.Time()),
zap.Object("round config", cfg.Round),
)

// Migrate data if needed
if err := migrations.Migrate(ctx, cfg); err != nil {
Expand Down Expand Up @@ -131,10 +136,15 @@ func poetMain() (err error) {
if err != nil {
return fmt.Errorf("failed to create server: %w", err)
}

if err := server.Start(ctx); err != nil {
return fmt.Errorf("failure in server: %w", err)
errClosing := server.Close()
return fmt.Errorf("failure in server: %w (closing: %w)", err, errClosing)
}

if err := server.Close(); err != nil {
return fmt.Errorf("failed closing server: %w", err)
}
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions registration/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package registration

import "time"

func DefaultConfig() Config {
return Config{
MaxRoundMembers: 1 << 32,
SubmitFlushInterval: 100 * time.Millisecond,
MaxSubmitBatchSize: 1000,
}
}

type Config struct {
PowDifficulty uint `long:"pow-difficulty" description:"PoW difficulty (in the number of leading zero bits)"`

MaxRoundMembers int `long:"max-round-members" description:"the maximum number of members in a round"`
MaxSubmitBatchSize int `long:"max-submit-batch-size" description:"The maximum number of challenges to submit in a single batch"`
SubmitFlushInterval time.Duration `long:"submit-flush-interval" description:"The interval between flushes of the submit queue"`
}
Loading
Loading