From 41b519b2f188e1e4bcd1a2413953c39baf166dae Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Wed, 19 Jul 2023 16:37:51 -0700 Subject: [PATCH] refactor(motion): convert singularity to option --- blob/blob.go | 2 ++ blob/local_store.go | 8 +++++++ blob/singularity_store.go | 19 +++++++++++++++ cmd/motion/main.go | 49 ++++++++++++++------------------------- motion.go | 6 +++++ singularity/local.go | 5 ++++ 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/blob/blob.go b/blob/blob.go index 1803b6e..43fdce1 100644 --- a/blob/blob.go +++ b/blob/blob.go @@ -25,6 +25,8 @@ type ( ModificationTime time.Time } Store interface { + Start(context.Context) error + Shutdown(context.Context) error Put(context.Context, io.ReadCloser) (*Descriptor, error) Get(context.Context, ID) (io.ReadSeekCloser, *Descriptor, error) } diff --git a/blob/local_store.go b/blob/local_store.go index 78e12a0..b874764 100644 --- a/blob/local_store.go +++ b/blob/local_store.go @@ -54,6 +54,14 @@ func NewLocalStore(dir string) *LocalStore { } } +func (l *LocalStore) Start(_ context.Context) error { + return nil +} + +func (l *LocalStore) Shutdown(_ context.Context) error { + return nil +} + // Put reads the given reader fully and stores its content in the store directory as flat files. // The reader content is first stored in a temporary directory and upon successful storage is moved to the store directory. // The Descriptor.ModificationTime is set to the modification date of the file that corresponds to the content. diff --git a/blob/singularity_store.go b/blob/singularity_store.go index 40e2184..819b417 100644 --- a/blob/singularity_store.go +++ b/blob/singularity_store.go @@ -14,6 +14,7 @@ import ( ) type SingularityService interface { + Start(ctx context.Context, dir string) error GetItem(ctx context.Context, id uint64) (*model.Item, error) PushItem(ctx context.Context, path string) (*model.Item, error) } @@ -23,6 +24,16 @@ type SingularityStore struct { singularity SingularityService } +func OpenSingularityStore(dir string) (*SingularityStore, error) { + dbFile := path.Join(path.Clean(dir), "singularity.db") + singularityService, err := singularity.OpenSQLiteSingularity(dbFile) + if err != nil { + return nil, fmt.Errorf("failed to open singularity database: %w") + } + local := NewLocalStore(dir) + return NewSingularityStore(local, singularityService), nil +} + func NewSingularityStore(local *LocalStore, singularity SingularityService) *SingularityStore { return &SingularityStore{ local: local, @@ -30,6 +41,14 @@ func NewSingularityStore(local *LocalStore, singularity SingularityService) *Sin } } +func (l *SingularityStore) Start(ctx context.Context) error { + return l.singularity.Start(ctx, l.local.dir) +} + +func (l *SingularityStore) Shutdown(_ context.Context) error { + return nil +} + func (s *SingularityStore) Put(ctx context.Context, reader io.ReadCloser) (*Descriptor, error) { desc, err := s.local.Put(ctx, reader) if err != nil { diff --git a/cmd/motion/main.go b/cmd/motion/main.go index 93040e8..78d43f7 100644 --- a/cmd/motion/main.go +++ b/cmd/motion/main.go @@ -6,7 +6,6 @@ import ( "github.com/filecoin-project/motion" "github.com/filecoin-project/motion/blob" - "github.com/filecoin-project/motion/singularity" "github.com/ipfs/go-log/v2" "github.com/urfave/cli/v2" ) @@ -28,35 +27,34 @@ func main() { Value: os.TempDir(), EnvVars: []string{"MOTION_STORE_DIR"}, }, - &cli.StringFlag{ - Name: "dbFile", - Usage: "file path for sqlite database", - DefaultText: "in memory db", - Value: "", - EnvVars: []string{"MOTION_STORE_DIR"}, + &cli.BoolFlag{ + Name: "experimentalSingularityStore", + Usage: "whether to use experimental Singularity store as the storage and deal making engine", + DefaultText: "Local storage is used", }, }, Action: func(cctx *cli.Context) error { storeDir := cctx.String("storeDir") - logger.Infow("Using local blob store", "storeDir", storeDir) - dbFile := cctx.String("dbFile") - ctx := cctx.Context - - singularityService, err := initializeSingularity(dbFile) - if err != nil { - logger.Fatalw("Failed to open singularity database", "err", err) - } - err = singularityService.Start(ctx, storeDir) - if err != nil { - logger.Fatalw("Failed to setup singularity for motion", "err", err) + var store blob.Store + if cctx.Bool("experimentalSingularityStore") { + var err error + store, err = blob.OpenSingularityStore(storeDir) + if err != nil { + return err + } + logger.Infow("Using Singularity blob store", "storeDir", storeDir) + } else { + store = blob.NewLocalStore(storeDir) + logger.Infow("Using local blob store", "storeDir", storeDir) } - local := blob.NewLocalStore(storeDir) + m, err := motion.New( - motion.WithBlobStore(blob.NewSingularityStore(local, singularityService)), + motion.WithBlobStore(store), ) if err != nil { logger.Fatalw("Failed to instantiate Motion", "err", err) } + ctx := cctx.Context if err := m.Start(ctx); err != nil { logger.Fatalw("Failed to start Motion", "err", err) @@ -77,14 +75,3 @@ func main() { logger.Error(err) } } - -func initializeSingularity(dbFile string) (*singularity.LocalSingularity, error) { - - if dbFile != "" { - logger.Infow("Using SQLite database file", "dbFile", dbFile) - return singularity.OpenSQLiteSingularity(dbFile) - } else { - return singularity.OpenMemorySingularity() - } - -} diff --git a/motion.go b/motion.go index 6331638..ef2b050 100644 --- a/motion.go +++ b/motion.go @@ -36,10 +36,16 @@ func New(o ...Option) (*Motion, error) { // Start starts the motion services. func (m *Motion) Start(ctx context.Context) error { // TODO start other components like deal engine, wallets etc. + if err := m.blobStore.Start(ctx); err != nil { + return err + } return m.httpServer.Start(ctx) } // Shutdown Start shuts down the motion services. func (m *Motion) Shutdown(ctx context.Context) error { + if err := m.blobStore.Shutdown(ctx); err != nil { + return err + } return m.httpServer.Shutdown(ctx) } diff --git a/singularity/local.go b/singularity/local.go index d554ba0..e616c8e 100644 --- a/singularity/local.go +++ b/singularity/local.go @@ -54,6 +54,11 @@ func NewLocalSingularity(db *gorm.DB) *LocalSingularity { } func (l *LocalSingularity) Start(ctx context.Context, dir string) error { + // migrate the db + if err := model.AutoMigrate(l.db); err != nil { + return err + } + // insure the creation of the default dataset, source, and root directory return database.DoRetry(func() error { return l.db.Transaction(func(db *gorm.DB) error {