Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
refactor(motion): convert singularity to option
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jul 19, 2023
1 parent 3bdf067 commit 41b519b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
2 changes: 2 additions & 0 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions blob/local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions blob/singularity_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -23,13 +24,31 @@ 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")

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / All

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / All

Printf format %w reads arg #1, but call has only 0 args (SA5009)

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / ubuntu (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / windows (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / macos (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / All

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / All

Printf format %w reads arg #1, but call has only 0 args (SA5009)

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / ubuntu (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / windows (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 31 in blob/singularity_store.go

View workflow job for this annotation

GitHub Actions / macos (go 1.20.x)

fmt.Errorf format %w reads arg #1, but call has 0 args
}
local := NewLocalStore(dir)
return NewSingularityStore(local, singularityService), nil
}

func NewSingularityStore(local *LocalStore, singularity SingularityService) *SingularityStore {
return &SingularityStore{
local: local,
singularity: singularity,
}
}

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 {
Expand Down
49 changes: 18 additions & 31 deletions cmd/motion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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()
}

}
6 changes: 6 additions & 0 deletions motion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions singularity/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 41b519b

Please sign in to comment.