diff --git a/itests/clusterdb_test.go b/itests/clusterdb_test.go index e1b29f691..9a2ddb2e1 100644 --- a/itests/clusterdb_test.go +++ b/itests/clusterdb_test.go @@ -10,35 +10,21 @@ import ( "github.com/filecoin-project/lotus/itests/kit" "github.com/filecoin-project/lotus/lib/sturdy/clusterdb" "github.com/filecoin-project/lotus/node" - "github.com/filecoin-project/lotus/node/config" + "github.com/filecoin-project/lotus/node/impl" ) -func staticConfig() config.ClusterDB { - return config.ClusterDB{ - Hosts: []string{"127.0.0.1"}, - Username: "yugabyte", - Password: "yugabyte", - Database: "yugabyte", - Port: "5433", - ITest: clusterdb.ITestNewID(), - } -} func withSetup(t *testing.T, f func(*kit.TestMiner)) { _, miner, _ := kit.EnsembleMinimal(t, kit.LatestActorsAt(-1), kit.MockProofs(), kit.ConstructorOpts( - node.Override(new(*clusterdb.DB), clusterdb.NewFromConfig), //Why does this not work? + node.Override(new(clusterdb.ITestID), func() clusterdb.ITestID { + return clusterdb.ITestNewID() + }), ), ) - var err error - miner.ClusterDB, err = clusterdb.NewFromConfig(staticConfig()) - if err != nil { - t.Fatal("Yugabyte connection error:", err) - } - - defer miner.ClusterDB.ITestDeleteAll() + defer miner.BaseAPI.(*impl.StorageMinerAPI).ClusterDB.ITestDeleteAll() f(miner) } @@ -47,7 +33,8 @@ func TestCrud(t *testing.T) { defer cancel() withSetup(t, func(miner *kit.TestMiner) { - err := miner.ClusterDB.Exec(ctx, ` + cdb := miner.BaseAPI.(*impl.StorageMinerAPI).ClusterDB + err := cdb.Exec(ctx, ` INSERT INTO itest_scratch (some_int, content) VALUES @@ -62,7 +49,7 @@ func TestCrud(t *testing.T) { Animal string `db:"content"` Unpopulated int } - err = miner.ClusterDB.Select(ctx, &ints, "SELECT content, some_int FROM itest_scratch") + err = cdb.Select(ctx, &ints, "SELECT content, some_int FROM itest_scratch") if err != nil { t.Fatal("Could not select: ", err) } @@ -84,17 +71,18 @@ func TestTransaction(t *testing.T) { defer cancel() withSetup(t, func(miner *kit.TestMiner) { - if err := miner.ClusterDB.Exec(ctx, "INSERT INTO itest_scratch (some_int) VALUES (4), (5), (6)"); err != nil { + cdb := miner.BaseAPI.(*impl.StorageMinerAPI).ClusterDB + if err := cdb.Exec(ctx, "INSERT INTO itest_scratch (some_int) VALUES (4), (5), (6)"); err != nil { t.Fatal("E0", err) } - miner.ClusterDB.BeginTransaction(ctx, func(tx *clusterdb.Transaction) (commit bool) { + cdb.BeginTransaction(ctx, func(tx *clusterdb.Transaction) (commit bool) { if err := tx.Exec(ctx, "INSERT INTO itest_scratch (some_int) VALUES (7), (8), (9)"); err != nil { t.Fatal("E1", err) } // sum1 is read from OUTSIDE the transaction so it's the old value var sum1 int - if err := miner.ClusterDB.QueryRow(ctx, "SELECT SUM(some_int) FROM itest_scratch").Scan(&sum1); err != nil { + if err := cdb.QueryRow(ctx, "SELECT SUM(some_int) FROM itest_scratch").Scan(&sum1); err != nil { t.Fatal("E2", err) } if sum1 != 4+5+6 { @@ -114,7 +102,7 @@ func TestTransaction(t *testing.T) { var sum2 int // Query() example (yes, QueryRow would be preferred here) - q, err := miner.ClusterDB.Query(ctx, "SELECT SUM(some_int) FROM itest_scratch") + q, err := cdb.Query(ctx, "SELECT SUM(some_int) FROM itest_scratch") if err != nil { t.Fatal("E4", err) } @@ -138,7 +126,8 @@ func TestPartialWalk(t *testing.T) { defer cancel() withSetup(t, func(miner *kit.TestMiner) { - if err := miner.ClusterDB.Exec(ctx, ` + cdb := miner.BaseAPI.(*impl.StorageMinerAPI).ClusterDB + if err := cdb.Exec(ctx, ` INSERT INTO itest_scratch (content, some_int) VALUES @@ -153,7 +142,7 @@ func TestPartialWalk(t *testing.T) { // TASK: FIND THE ID of the string with a specific SHA256 needle := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" - q, err := miner.ClusterDB.Query(ctx, `SELECT id, content FROM itest_scratch`) + q, err := cdb.Query(ctx, `SELECT id, content FROM itest_scratch`) if err != nil { t.Fatal("e2", err) } @@ -168,7 +157,7 @@ func TestPartialWalk(t *testing.T) { for q.Next() { if err := q.StructScan(&tmp); err != nil { - t.Fatal("") + t.Fatal("structscan err " + err.Error()) } bSha := sha256.Sum256([]byte(tmp.Src)) diff --git a/itests/kit/ensemble.go b/itests/kit/ensemble.go index 250caa640..19cc163af 100644 --- a/itests/kit/ensemble.go +++ b/itests/kit/ensemble.go @@ -49,7 +49,6 @@ import ( "github.com/filecoin-project/lotus/cmd/lotus-worker/sealworker" "github.com/filecoin-project/lotus/gateway" "github.com/filecoin-project/lotus/genesis" - "github.com/filecoin-project/lotus/lib/sturdy/clusterdb" "github.com/filecoin-project/lotus/markets/idxprov" "github.com/filecoin-project/lotus/markets/idxprov/idxprov_test" lotusminer "github.com/filecoin-project/lotus/miner" @@ -764,10 +763,6 @@ func (n *Ensemble) Start() *Ensemble { ) } - opts = append(opts, node.Override(new(*clusterdb.DB), func(cfg config.ClusterDB) (*clusterdb.DB, error) { - return clusterdb.NewFromConfig(cfg) - })) - // start node stop, err := node.New(ctx, opts...) require.NoError(n.t, err) diff --git a/itests/kit/node_miner.go b/itests/kit/node_miner.go index cdcc03b13..4b81c9df0 100644 --- a/itests/kit/node_miner.go +++ b/itests/kit/node_miner.go @@ -24,7 +24,6 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/wallet/key" - "github.com/filecoin-project/lotus/lib/sturdy/clusterdb" "github.com/filecoin-project/lotus/miner" sealing "github.com/filecoin-project/lotus/storage/pipeline" "github.com/filecoin-project/lotus/storage/sealer/storiface" @@ -88,8 +87,6 @@ type TestMiner struct { RemoteListener net.Listener options nodeOpts - - ClusterDB *clusterdb.DB } func (tm *TestMiner) PledgeSectors(ctx context.Context, n, existing int, blockNotif <-chan struct{}) { diff --git a/lib/sturdy/clusterdb/clusterdb.go b/lib/sturdy/clusterdb/clusterdb.go index 7b77b6365..13a5db209 100644 --- a/lib/sturdy/clusterdb/clusterdb.go +++ b/lib/sturdy/clusterdb/clusterdb.go @@ -1,7 +1,5 @@ package clusterdb -// TODO 2 - build integration tests - import ( "context" "embed" @@ -22,9 +20,11 @@ import ( "github.com/filecoin-project/lotus/node/config" ) +type ITestID string + // ItestNewID see ITestWithID doc -func ITestNewID() string { - return strconv.Itoa(rand.Intn(99999)) +func ITestNewID() ITestID { + return ITestID(strconv.Itoa(rand.Intn(99999))) } type DB struct { @@ -48,15 +48,30 @@ func NewFromConfig(cfg config.ClusterDB) (*DB, error) { cfg.Password, cfg.Database, cfg.Port, - cfg.ITest, + "", func(s string) { logger.Error(s) }, ) } +func NewFromConfigWithITestID(cfg config.ClusterDB) func(id ITestID) (*DB, error) { + return func(id ITestID) (*DB, error) { + return New( + cfg.Hosts, + cfg.Username, + cfg.Password, + cfg.Database, + cfg.Port, + id, + func(s string) { logger.Error(s) }, + ) + } +} + // New is to be called once per binary to establish the pool. // log() is for errors. It returns an upgraded database's connection. // This entry point serves both production and integration tests, so it's more DI. -func New(hosts []string, username, password, database, port, itest string, log func(string)) (*DB, error) { +func New(hosts []string, username, password, database, port string, itestID ITestID, log func(string)) (*DB, error) { + itest := string(itestID) connString := "" if len(hosts) > 0 { connString = "host=" + hosts[0] + " " diff --git a/node/builder_miner.go b/node/builder_miner.go index 69d567da8..de777f34f 100644 --- a/node/builder_miner.go +++ b/node/builder_miner.go @@ -232,7 +232,8 @@ func ConfigStorageMiner(c interface{}) Option { Override(new(config.SealerConfig), cfg.Storage), Override(new(config.ProvingConfig), cfg.Proving), Override(new(*ctladdr.AddressSelector), modules.AddressSelector(&cfg.Addresses)), - Override(new(*clusterdb.DB), clusterdb.NewFromConfig), + Override(new(*clusterdb.DB), clusterdb.NewFromConfigWithITestID(cfg.ClusterDB)), + Override(new(clusterdb.ITestID), ""), ) } diff --git a/node/impl/storminer.go b/node/impl/storminer.go index e4fa41c78..89837baff 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -45,6 +45,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/gen" "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/lib/sturdy/clusterdb" mktsdagstore "github.com/filecoin-project/lotus/markets/dagstore" "github.com/filecoin-project/lotus/markets/storageadapter" "github.com/filecoin-project/lotus/miner" @@ -122,6 +123,8 @@ type StorageMinerAPI struct { GetSealingConfigFunc dtypes.GetSealingConfigFunc `optional:"true"` GetExpectedSealDurationFunc dtypes.GetExpectedSealDurationFunc `optional:"true"` SetExpectedSealDurationFunc dtypes.SetExpectedSealDurationFunc `optional:"true"` + + ClusterDB *clusterdb.DB } var _ api.StorageMiner = &StorageMinerAPI{}