Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release preparation: Feature flag to disable configurable api parameters. #917

Merged
merged 3 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/disabled_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ func (dmc *DisabledMapConfig) validate(swag *openapi3.Swagger) error {

// MakeDisabledMapFromOA3 Creates a new disabled map from an openapi3 definition
func MakeDisabledMapFromOA3(swag *openapi3.Swagger, config *DisabledMapConfig) (*DisabledMap, error) {
if config == nil {
return nil, nil
}

err := config.validate(swag)

Expand Down
24 changes: 13 additions & 11 deletions api/disabled_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestSchemaCheck(t *testing.T) {
type testingStruct struct {
name string
ddm *DisplayDisabledMap
expectError string
expectError []string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was flaky because the error string is generated according to maps being returned in a randomized order. So sometimes the tests with 2 values were swapped in the error string.

}
tests := []testingStruct{
{"test param types - good",
Expand All @@ -87,7 +87,7 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled"}},
}},
},
"",
nil,
},

{"test param types - bad required",
Expand All @@ -97,7 +97,7 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled"}},
}},
},
"required-FAKE",
[]string{"required-FAKE"},
},

{"test param types - bad optional",
Expand All @@ -107,7 +107,7 @@ func TestSchemaCheck(t *testing.T) {
"optional-FAKE": {{"p3": "enabled"}},
}},
},
"optional-FAKE",
[]string{"optional-FAKE"},
},

{"test param types - bad both",
Expand All @@ -117,7 +117,7 @@ func TestSchemaCheck(t *testing.T) {
"optional-FAKE": {{"p3": "enabled"}},
}},
},
"required-FAKE optional-FAKE",
[]string{"required-FAKE", "optional-FAKE"},
},

{"test param status - good",
Expand All @@ -127,7 +127,7 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled"}},
}},
},
"",
nil,
},

{"test param status - bad required",
Expand All @@ -137,7 +137,7 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled"}},
}},
},
"p2",
[]string{"p2"},
},

{"test param status - bad optional",
Expand All @@ -147,7 +147,7 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled-FAKE"}},
}},
},
"p3",
[]string{"p3"},
},

{"test param status - bad both",
Expand All @@ -157,17 +157,19 @@ func TestSchemaCheck(t *testing.T) {
"optional": {{"p3": "enabled-FAKE"}},
}},
},
"p1",
[]string{"p1"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.ddm.validateSchema()

if test.expectError != "" {
if len(test.expectError) != 0 {
require.Error(t, err)
require.Contains(t, err.Error(), test.expectError)
for _, str := range test.expectError {
require.Contains(t, err.Error(), str)
}
} else {
require.NoError(t, err)
}
Expand Down
48 changes: 28 additions & 20 deletions cmd/algorand-indexer/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var (
enableAllParameters bool
)

const paramConfigEnableFlag = false

var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "run indexer daemon",
Expand Down Expand Up @@ -140,22 +142,6 @@ var daemonCmd = &cobra.Command{

options := makeOptions()

swag, err := generated.GetSwagger()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get swagger: %v", err)
os.Exit(1)
}

if suppliedAPIConfigFile != "" {
logger.Infof("supplied api configuration file located at: %s", suppliedAPIConfigFile)
potentialDisabledMapConfig, err := api.MakeDisabledMapConfigFromFile(swag, suppliedAPIConfigFile)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to created disabled map config from file: %v", err)
os.Exit(1)
}
options.DisabledMapConfig = potentialDisabledMapConfig
}

api.Serve(ctx, daemonServerAddr, db, bot, logger, options)
wg.Wait()
},
Expand All @@ -177,6 +163,10 @@ func init() {
daemonCmd.Flags().Uint32VarP(&maxConn, "max-conn", "", 0, "set the maximum connections allowed in the connection pool, if the maximum is reached subsequent connections will wait until a connection becomes available, or timeout according to the read-timeout setting")
daemonCmd.Flags().StringVar(&suppliedAPIConfigFile, "api-config-file", "", "supply an API config file to enable/disable parameters")
daemonCmd.Flags().BoolVar(&enableAllParameters, "enable-all-parameters", false, "override default configuration and enable all parameters. Can't be used with --api-config-file")
if !paramConfigEnableFlag {
daemonCmd.Flags().MarkHidden("api-config-file")
daemonCmd.Flags().MarkHidden("enable-all-parameters")
}

viper.RegisterAlias("algod", "algod-data-dir")
viper.RegisterAlias("algod-net", "algod-address")
Expand Down Expand Up @@ -205,10 +195,28 @@ func makeOptions() (options api.ExtraOptions) {
options.WriteTimeout = writeTimeout
options.ReadTimeout = readTimeout

if enableAllParameters {
options.DisabledMapConfig = api.MakeDisabledMapConfig()
} else {
options.DisabledMapConfig = api.GetDefaultDisabledMapConfigForPostgres()
if paramConfigEnableFlag {
if enableAllParameters {
options.DisabledMapConfig = api.MakeDisabledMapConfig()
} else {
options.DisabledMapConfig = api.GetDefaultDisabledMapConfigForPostgres()
}

if suppliedAPIConfigFile != "" {
swag, err := generated.GetSwagger()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get swagger: %v", err)
os.Exit(1)
}

logger.Infof("supplied api configuration file located at: %s", suppliedAPIConfigFile)
potentialDisabledMapConfig, err := api.MakeDisabledMapConfigFromFile(swag, suppliedAPIConfigFile)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to created disabled map config from file: %v", err)
os.Exit(1)
}
options.DisabledMapConfig = potentialDisabledMapConfig
}
}

return
Expand Down
4 changes: 3 additions & 1 deletion cmd/algorand-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ func init() {
rootCmd.AddCommand(importCmd)
importCmd.Hidden = true
rootCmd.AddCommand(daemonCmd)
rootCmd.AddCommand(apiConfigCmd)
if paramConfigEnableFlag {
rootCmd.AddCommand(apiConfigCmd)
}

// Not applied globally to avoid adding to utility commands.
addFlags := func(cmd *cobra.Command) {
Expand Down