-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor config: extract block, auth factories
config also holds factories. This creates dependency loops -- which Go's import cycle rules make hard to break. Prevent import loops by splitting packages that need a factory into: 1. The actual package (virtually unchanged, except to take a parameters object in its constructor); 1. A "params" (sub-)package, which holds parameter structs that config creates and factories consume; 1. A "factory" (sub-)package, which calls the appropriate config methods to get parameters and pass them to the constructor. Former-commit-id: 91351838726ff6ce10b9f6dcd9cd4218306e107a
- Loading branch information
1 parent
e9dc835
commit defa439
Showing
10 changed files
with
159 additions
and
109 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,10 @@ | ||
package params | ||
|
||
import "time" | ||
|
||
type ServiceCache struct { | ||
Enabled bool | ||
Size int | ||
TTL time.Duration | ||
EvictionJitter time.Duration | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package factory | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/treeverse/lakefs/block" | ||
"github.com/treeverse/lakefs/block/local" | ||
"github.com/treeverse/lakefs/block/mem" | ||
"github.com/treeverse/lakefs/block/params" | ||
s3a "github.com/treeverse/lakefs/block/s3" | ||
"github.com/treeverse/lakefs/block/transient" | ||
"github.com/treeverse/lakefs/config" | ||
"github.com/treeverse/lakefs/logging" | ||
) | ||
|
||
var ErrInvalidBlockStoreType = errors.New("invalid blockstore type") | ||
|
||
func BuildBlockAdapter(c *config.Config) (block.Adapter, error) { | ||
blockstore := c.GetBlockstoreType() | ||
logging.Default(). | ||
WithField("type", blockstore). | ||
Info("initialize blockstore adapter") | ||
switch blockstore { | ||
case local.BlockstoreType: | ||
params, err := c.GetBlockAdapterLocalParams() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buildLocalAdapter(params) | ||
case s3a.BlockstoreType: | ||
params, err := c.GetBlockAdapterS3Params() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buildS3Adapter(params) | ||
case mem.BlockstoreType, "memory": | ||
return mem.New(), nil | ||
case transient.BlockstoreType: | ||
return transient.New(), nil | ||
default: | ||
return nil, fmt.Errorf("%w '%s' please choose one of %s", | ||
ErrInvalidBlockStoreType, blockstore, []string{local.BlockstoreType, s3a.BlockstoreType, mem.BlockstoreType, transient.BlockstoreType}) | ||
} | ||
} | ||
|
||
func buildLocalAdapter(params params.Local) (*local.Adapter, error) { | ||
adapter, err := local.NewAdapter(params.Path) | ||
if err != nil { | ||
return nil, fmt.Errorf("got error opening a local block adapter with path %s: %w", params.Path, err) | ||
} | ||
logging.Default().WithFields(logging.Fields{ | ||
"type": "local", | ||
"path": params.Path, | ||
}).Info("initialized blockstore adapter") | ||
return adapter, nil | ||
} | ||
|
||
func buildS3Adapter(params params.S3) (*s3a.Adapter, error) { | ||
sess, err := session.NewSession(params.AwsConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sess.ClientConfig(s3.ServiceName) | ||
svc := s3.New(sess) | ||
adapter := s3a.NewAdapter(svc, | ||
s3a.WithStreamingChunkSize(params.StreamingChunkSize), | ||
s3a.WithStreamingChunkTimeout(params.StreamingChunkTimeout), | ||
) | ||
logging.Default().WithFields(logging.Fields{ | ||
"type": "s3", | ||
}).Info("initialized blockstore adapter") | ||
return adapter, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package params | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
) | ||
|
||
type Mem struct{} | ||
|
||
type Local struct { | ||
Path string | ||
} | ||
|
||
type S3 struct { | ||
AwsConfig *aws.Config | ||
StreamingChunkSize int | ||
StreamingChunkTimeout time.Duration | ||
} |
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
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
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,5 @@ | ||
package params | ||
|
||
type Database struct { | ||
DatabaseURI string | ||
} |