Skip to content

Commit

Permalink
feat: add experimental flag to enable features support
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent f2f6496 commit d99845d
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
BallastSizeInBytesFlag = "ballast-size"
NumscriptCacheMaxCountFlag = "numscript-cache-max-count"
AutoUpgradeFlag = "auto-upgrade"
ExperimentalFeaturesFlag = "experimental-features"
)

func NewServeCommand() *cobra.Command {
Expand All @@ -49,6 +50,11 @@ func NewServeCommand() *cobra.Command {
return err
}

experimentalFeatures, err := cmd.Flags().GetBool(ExperimentalFeaturesFlag)
if err != nil {
return err
}

options := []fx.Option{
fx.NopLogger,
otlp.FXModuleFromFlags(cmd),
Expand All @@ -66,6 +72,7 @@ func NewServeCommand() *cobra.Command {
MaxRetry: 10,
Delay: time.Millisecond * 100,
},
EnableFeatures: experimentalFeatures,
}),
bus.NewFxModule(),
ballast.Module(serveConfiguration.ballastSize),
Expand Down Expand Up @@ -104,6 +111,7 @@ func NewServeCommand() *cobra.Command {
cmd.Flags().Uint(NumscriptCacheMaxCountFlag, 1024, "Numscript cache max count")
cmd.Flags().Bool(AutoUpgradeFlag, false, "Automatically upgrade all schemas")
cmd.Flags().String(BindFlag, "0.0.0.0:3068", "API bind address")
cmd.Flags().Bool(ExperimentalFeaturesFlag, false, "Enable features configurability")

service.AddFlags(cmd.Flags())
bunconnect.AddFlags(cmd.Flags())
Expand Down
2 changes: 1 addition & 1 deletion internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ type BalancesByAssetsByAccounts map[string]BalancesByAssets
type Configuration struct {
Bucket string `json:"bucket" bun:"bucket,type:varchar(255)"`
Metadata metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb"`
Features map[string]string `json:"features" bun:"features,type:jsonb"`
Features FeatureSet `json:"features" bun:"features,type:jsonb"`
}
```

Expand Down
19 changes: 17 additions & 2 deletions internal/controller/system/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package system

import (
"context"
"reflect"
"time"

"go.opentelemetry.io/otel/metric"
Expand Down Expand Up @@ -36,8 +37,9 @@ type DefaultController struct {
registry *ledgercontroller.StateRegistry
databaseRetryConfiguration DatabaseRetryConfiguration

tracer trace.Tracer
meter metric.Meter
tracer trace.Tracer
meter metric.Meter
enableFeatures bool
}

func (ctrl *DefaultController) GetLedgerController(ctx context.Context, name string) (ledgercontroller.Controller, error) {
Expand Down Expand Up @@ -85,6 +87,13 @@ func (ctrl *DefaultController) GetLedgerController(ctx context.Context, name str
func (ctrl *DefaultController) CreateLedger(ctx context.Context, name string, configuration ledger.Configuration) error {
return tracing.SkipResult(tracing.Trace(ctx, ctrl.tracer, "CreateLedger", tracing.NoResult(func(ctx context.Context) error {
configuration.SetDefaults()

if !ctrl.enableFeatures {
if !reflect.DeepEqual(configuration.Features, ledger.DefaultFeatures) {
return ErrExperimentalFeaturesDisabled
}
}

l, err := ledger.New(name, configuration)
if err != nil {
return newErrInvalidLedgerConfiguration(err)
Expand Down Expand Up @@ -156,6 +165,12 @@ func WithTracer(t trace.Tracer) Option {
}
}

func WithEnableFeatures(v bool) Option {
return func(ctrl *DefaultController) {
ctrl.enableFeatures = v
}
}

var defaultOptions = []Option{
WithParser(ledgercontroller.NewDefaultNumscriptParser()),
WithMeter(noopmetrics.Meter{}),
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/system/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var (
ErrLedgerAlreadyExists = errors.New("ledger already exists")
ErrExperimentalFeaturesDisabled = errors.New("experimental features are disabled")
)

type ErrInvalidLedgerConfiguration struct {
Expand All @@ -26,4 +27,4 @@ func newErrInvalidLedgerConfiguration(err error) ErrInvalidLedgerConfiguration {
return ErrInvalidLedgerConfiguration{
err: err,
}
}
}
2 changes: 2 additions & 0 deletions internal/controller/system/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type DatabaseRetryConfiguration struct {
type ModuleConfiguration struct {
NSCacheConfiguration ledgercontroller.CacheConfiguration
DatabaseRetryConfiguration DatabaseRetryConfiguration
EnableFeatures bool
}

func NewFXModule(configuration ModuleConfiguration) fx.Option {
Expand Down Expand Up @@ -45,6 +46,7 @@ func NewFXModule(configuration ModuleConfiguration) fx.Option {
WithDatabaseRetryConfiguration(configuration.DatabaseRetryConfiguration),
WithMeter(meterProvider.Meter("core")),
WithTracer(tracerProvider.Tracer("core")),
WithEnableFeatures(configuration.EnableFeatures),
)...,
)
}),
Expand Down
2 changes: 1 addition & 1 deletion internal/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func shortenFeature(feature string) string {
type Configuration struct {
Bucket string `json:"bucket" bun:"bucket,type:varchar(255)"`
Metadata metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb"`
Features map[string]string `json:"features" bun:"features,type:jsonb"`
Features FeatureSet `json:"features" bun:"features,type:jsonb"`
}

func (c *Configuration) SetDefaults() {
Expand Down
7 changes: 7 additions & 0 deletions pkg/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Configuration struct {
Output io.Writer
Debug bool
OTLPConfig *OTLPConfig
ExperimentalFeatures bool
}

type Server struct {
Expand All @@ -66,6 +67,12 @@ func (s *Server) Start() {
"--" + bunconnect.PostgresMaxOpenConnsFlag, fmt.Sprint(s.configuration.PostgresConfiguration.MaxOpenConns),
"--" + bunconnect.PostgresConnMaxIdleTimeFlag, fmt.Sprint(s.configuration.PostgresConfiguration.ConnMaxIdleTime),
}
if s.configuration.ExperimentalFeatures {
args = append(
args,
"--"+cmd.ExperimentalFeaturesFlag,
)
}
if s.configuration.PostgresConfiguration.MaxIdleConns != 0 {
args = append(
args,
Expand Down
1 change: 1 addition & 0 deletions test/e2e/api_ledgers_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var _ = Context("Ledger engine tests", func() {
Output: GinkgoWriter,
Debug: debug,
NatsURL: natsServer.GetValue().ClientURL(),
ExperimentalFeatures: true,
}
})
When("creating a new ledger", func() {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/api_ledgers_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var _ = Context("Ledger engine tests", func() {
Output: GinkgoWriter,
Debug: debug,
NatsURL: natsServer.GetValue().ClientURL(),
ExperimentalFeatures: true,
}
})
When("creating a new ledger", func() {
Expand Down
1 change: 1 addition & 0 deletions test/stress/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var _ = Context("Ledger stress tests", func() {
PostgresConfiguration: db.GetValue().ConnectionOptions(),
Output: GinkgoWriter,
Debug: debug,
ExperimentalFeatures: true,
}
})

Expand Down

0 comments on commit d99845d

Please sign in to comment.