-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Engine-Specific Options in Open (#16)
- Loading branch information
Showing
12 changed files
with
145 additions
and
92 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 |
---|---|---|
@@ -1,16 +1,73 @@ | ||
package config | ||
|
||
import "time" | ||
import ( | ||
ldbopt "github.com/syndtr/goleveldb/leveldb/opt" | ||
) | ||
|
||
// ReplicaConfig specifies the information needed for a Replica and Version manager to | ||
// maintain global object versioning and provenance. | ||
// TODO: this configuration is pulled from a service context not a library context. | ||
// DefaultConfig is used if the user does not specify a configuration | ||
var DefaultConfig = Config{ | ||
Versions: ReplicaConfig{ | ||
PID: 1, | ||
Region: "local", | ||
Name: "localhost", | ||
}, | ||
} | ||
|
||
// New creates a configuration with the required options and can also be used to specify | ||
// optional configuration e.g. for engine-specific operations. | ||
func New(options ...Option) (_ Config, err error) { | ||
// Create the default configuration in editable mode | ||
conf := &Config{ | ||
Versions: ReplicaConfig{ | ||
PID: DefaultConfig.Versions.PID, | ||
Region: DefaultConfig.Versions.Region, | ||
Name: DefaultConfig.Versions.Name, | ||
}, | ||
} | ||
|
||
// Apply all options to the configuration | ||
for _, opt := range options { | ||
if err = opt(conf); err != nil { | ||
return Config{}, err | ||
} | ||
} | ||
|
||
// Return the value of the configuration | ||
return *conf, nil | ||
} | ||
|
||
// Config specifies the options necessary to open a Honu database. | ||
type Config struct { | ||
Versions ReplicaConfig | ||
LDBOptions *ldbopt.Options | ||
} | ||
|
||
// ReplicaConfig specifies the information needed for the Version manager to maintain | ||
// global object versioning and provenance. Honu is intended to support data replication | ||
// by versioning using Lamport scalars. These conflict-free version numbers are closely | ||
// tied to a replica's configuration (where a replica is a process that performs data | ||
// replication using Honu), e.g. the PID is the process ID of a running replica, the | ||
// region is where the replica is running, and the name is usually the hostname of the | ||
// replica. | ||
type ReplicaConfig struct { | ||
Enabled bool `split_words:"true" default:"true"` | ||
BindAddr string `split_words:"true" default:":4435"` | ||
PID uint64 `split_words:"true" required:"false"` | ||
Region string `split_words:"true" required:"false"` | ||
Name string `split_words:"true" required:"false"` | ||
GossipInterval time.Duration `split_words:"true" default:"1m"` | ||
GossipSigma time.Duration `split_words:"true" default:"5s"` | ||
PID uint64 `split_words:"true" required:"false"` | ||
Region string `split_words:"true" required:"false"` | ||
Name string `split_words:"true" required:"false"` | ||
} | ||
|
||
// Option modifies a configuration to add optional configuration items. | ||
type Option func(*Config) error | ||
|
||
func WithReplica(conf ReplicaConfig) Option { | ||
return func(cfg *Config) error { | ||
cfg.Versions = conf | ||
return nil | ||
} | ||
} | ||
|
||
func WithLevelDB(opt *ldbopt.Options) Option { | ||
return func(cfg *Config) error { | ||
cfg.LDBOptions = opt | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rotationalio/honu/config" | ||
"github.com/stretchr/testify/require" | ||
ldbopt "github.com/syndtr/goleveldb/leveldb/opt" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
// Test Default Config | ||
conf, err := config.New() | ||
require.NoError(t, err) | ||
require.Equal(t, config.DefaultConfig, conf) | ||
require.NotZero(t, conf.Versions.PID) | ||
require.NotEmpty(t, conf.Versions.Region) | ||
|
||
// Test WithVersions | ||
conf, err = config.New(config.WithReplica(config.ReplicaConfig{8, "us-antarctic-23", "research"})) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, conf.Versions) | ||
require.Equal(t, uint64(8), conf.Versions.PID) | ||
require.Equal(t, "us-antarctic-23", conf.Versions.Region) | ||
require.Equal(t, "research", conf.Versions.Name) | ||
|
||
// Test WithLevelDB Options | ||
conf, err = config.New(config.WithLevelDB(&ldbopt.Options{Strict: ldbopt.StrictJournal})) | ||
require.NoError(t, err) | ||
require.Equal(t, config.DefaultConfig.Versions, conf.Versions) | ||
require.NotNil(t, conf.LDBOptions) | ||
require.Equal(t, conf.LDBOptions.Strict, ldbopt.StrictJournal) | ||
} |
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
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
Oops, something went wrong.