Skip to content

Commit

Permalink
Merge pull request #805 from DRK3/DatabaseTimeoutFlag
Browse files Browse the repository at this point in the history
feat: Database timeout flag
  • Loading branch information
fqutishat committed Oct 8, 2021
2 parents 6cb5f10 + 1f81796 commit 789f5f0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Flags:
--database-prefix string An optional prefix to be used when creating and retrieving underlying databases. Alternatively, this can be set with the following environment variable: DATABASE_PREFIX
-t, --database-type string The type of database to use for everything except key storage. Supported options: mem, couchdb, mongodb. Alternatively, this can be set with the following environment variable: DATABASE_TYPE
-v, --database-url string The URL of the database. Not needed if using memstore. For CouchDB, include the username:password@ text if required. Alternatively, this can be set with the following environment variable: DATABASE_URL
--database-timeout string The timeout for database requests. For example, '30s' for a 30 second timeout. Currently this setting only applies if you're using MongoDB. Alternatively, this can be set with the following environment variable: DATABASE_TIMEOUT
-a, --did-aliases stringArray Aliases for this did method. Alternatively, this can be set with the following environment variable: DID_ALIASES
-n, --did-namespace string DID Namespace.Alternatively, this can be set with the following environment variable: DID_NAMESPACE
--discovery-domain string Discovery domain for this domain. Format: HostName
Expand Down Expand Up @@ -154,16 +155,16 @@ Minimal configuration to run a service is:

## Databases

ORB uses Aries generic storage interface for storing data.
Backup should be done similarly to other trustbloc projects.
In ORB we support the following databases:
Orb uses the Aries generic storage interface for storing data.
Backup should be done similarly to other TrustBloc projects.
In Orb we support the following databases:
* CouchDB
* MongoDB
* Memory (backup is not supported)

Use the database-specific command to get all databases and filter them by `DATABASE_PREFIX` and `KMSSECRETS_DATABASE_PREFIX` env.
Use the database-specific command to get all databases and filter them by the `DATABASE_PREFIX` and `KMSSECRETS_DATABASE_PREFIX` environment variables.

NOTE: Service might use two different databases. In that case, do the procedure per-database and filter the output only by one prefix.
NOTE: The service might use two different databases. In that case, do the procedure per-database and filter the output only by one prefix.
`DATABASE_PREFIX` for `DATABASE_URL` and `KMSSECRETS_DATABASE_PREFIX` for `KMSSECRETS_DATABASE_URL`.

For instance, to get all databases for CouchDB use the following command:
Expand Down
55 changes: 26 additions & 29 deletions cmd/orb-server/startcmd/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
defaultActivityPubPageSize = 50
defaultNodeInfoRefreshInterval = 15 * time.Second
defaultIPFSTimeout = 20 * time.Second
defaultDatabaseTimeout = 10 * time.Second
mqDefaultMaxConnectionSubscriptions = 1000

commonEnvVarUsageText = "Alternatively, this can be set with the following environment variable: "
Expand Down Expand Up @@ -199,6 +200,12 @@ const (
kmsSecretsDatabasePrefixFlagUsage = "An optional prefix to be used when creating and retrieving " +
"the underlying KMS secrets database. " + commonEnvVarUsageText + kmsSecretsDatabasePrefixEnvKey

databaseTimeoutFlagName = "database-timeout"
databaseTimeoutEnvKey = "DATABASE_TIMEOUT"
databaseTimeoutFlagUsage = "The timeout for database requests. For example, '30s' for a 30 second timeout. " +
"Currently this setting only applies if you're using MongoDB. " +
commonEnvVarUsageText + databaseTimeoutEnvKey

databaseTypeMemOption = "mem"
databaseTypeCouchDBOption = "couchdb"
databaseTypeMongoDBOption = "mongodb"
Expand Down Expand Up @@ -385,6 +392,7 @@ type orbParameters struct {
enableDevMode bool
nodeInfoRefreshInterval time.Duration
ipfsTimeout time.Duration
databaseTimeout time.Duration
contextProviderURLs []string
}

Expand Down Expand Up @@ -739,16 +747,22 @@ func getOrbParameters(cmd *cobra.Command) (*orbParameters, error) {
return nil, fmt.Errorf("%s: %w", activityPubPageSizeFlagName, err)
}

nodeInfoRefreshInterval, err := getNodeInfoRefreshInterval(cmd)
nodeInfoRefreshInterval, err := getDuration(cmd, nodeInfoRefreshIntervalFlagName,
nodeInfoRefreshIntervalEnvKey, defaultNodeInfoRefreshInterval)
if err != nil {
return nil, fmt.Errorf("%s: %w", nodeInfoRefreshIntervalFlagName, err)
}

ipfsTimeout, err := getIPFSTimeout(cmd)
ipfsTimeout, err := getDuration(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, defaultIPFSTimeout)
if err != nil {
return nil, fmt.Errorf("%s: %w", ipfsTimeoutFlagName, err)
}

databaseTimeout, err := getDuration(cmd, databaseURLFlagName, databaseTimeoutEnvKey, defaultDatabaseTimeout)
if err != nil {
return nil, fmt.Errorf("%s: %w", databaseTimeoutFlagName, err)
}

contextProviderURLs, err := cmdutils.GetUserSetVarFromArrayString(cmd, contextProviderFlagName, contextProviderEnvKey, true)
if err != nil {
return nil, fmt.Errorf("%s: %w", contextProviderFlagName, err)
Expand Down Expand Up @@ -801,6 +815,7 @@ func getOrbParameters(cmd *cobra.Command) (*orbParameters, error) {
enableDevMode: enableDevMode,
nodeInfoRefreshInterval: nodeInfoRefreshInterval,
ipfsTimeout: ipfsTimeout,
databaseTimeout: databaseTimeout,
contextProviderURLs: contextProviderURLs,
}, nil
}
Expand Down Expand Up @@ -982,41 +997,23 @@ func getActivityPubPageSize(cmd *cobra.Command) (int, error) {
return activityPubPageSize, nil
}

func getNodeInfoRefreshInterval(cmd *cobra.Command) (time.Duration, error) {
nodeInfoRefreshIntervalStr, err := cmdutils.GetUserSetVarFromString(cmd, nodeInfoRefreshIntervalFlagName,
nodeInfoRefreshIntervalEnvKey, true)
if err != nil {
return 0, err
}

if nodeInfoRefreshIntervalStr == "" {
return defaultNodeInfoRefreshInterval, nil
}

nodeInfoRefreshInterval, err := time.ParseDuration(nodeInfoRefreshIntervalStr)
if err != nil {
return 0, fmt.Errorf("invalid value [%s]: %w", nodeInfoRefreshIntervalStr, err)
}

return nodeInfoRefreshInterval, nil
}

func getIPFSTimeout(cmd *cobra.Command) (time.Duration, error) {
ipfsTimeoutStr, err := cmdutils.GetUserSetVarFromString(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, true)
func getDuration(cmd *cobra.Command, flagName, envKey string,
defaultDuration time.Duration) (time.Duration, error) {
timeoutStr, err := cmdutils.GetUserSetVarFromString(cmd, flagName, envKey, true)
if err != nil {
return 0, err
return -1, err
}

if ipfsTimeoutStr == "" {
return defaultIPFSTimeout, nil
if timeoutStr == "" {
return defaultDuration, nil
}

ipfsTimeout, err := time.ParseDuration(ipfsTimeoutStr)
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
return 0, fmt.Errorf("invalid value [%s]: %w", ipfsTimeoutStr, err)
return -1, fmt.Errorf("invalid value [%s]: %w", timeoutStr, err)
}

return ipfsTimeout, nil
return timeout, nil
}

func getMQParameters(cmd *cobra.Command) (mqURL string, mqOpPoolSize int, mqObserverPoolSize int,
Expand Down
21 changes: 17 additions & 4 deletions cmd/orb-server/startcmd/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,19 @@ func TestStartCmdWithMissingArg(t *testing.T) {
require.Contains(t, err.Error(), "missing unit in duration")
})

t.Run("Invalid database timeout", func(t *testing.T) {
restoreEnv := setEnv(t, databaseTimeoutEnvKey, "5")
defer restoreEnv()

startCmd := GetStartCmd()

startCmd.SetArgs(getTestArgs("localhost:8081", "local", "false", databaseTypeMemOption, ""))

err := startCmd.Execute()
require.Error(t, err)
require.Contains(t, err.Error(), "missing unit in duration")
})

t.Run("Invalid max connection subscriptions", func(t *testing.T) {
restoreEnv := setEnv(t, mqMaxConnectionSubscriptionsEnvKey, "xxx")
defer restoreEnv()
Expand Down Expand Up @@ -957,23 +970,23 @@ func TestGetIPFSTimeout(t *testing.T) {
t.Run("Not specified -> default value", func(t *testing.T) {
cmd := getTestCmd(t)

timeout, err := getIPFSTimeout(cmd)
timeout, err := getDuration(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, defaultIPFSTimeout)
require.NoError(t, err)
require.Equal(t, defaultIPFSTimeout, timeout)
})

t.Run("Invalid value -> error", func(t *testing.T) {
cmd := getTestCmd(t, "--"+ipfsTimeoutFlagName, "xxx")

_, err := getIPFSTimeout(cmd)
_, err := getDuration(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, defaultIPFSTimeout)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid value")
})

t.Run("Valid value -> success", func(t *testing.T) {
cmd := getTestCmd(t, "--"+ipfsTimeoutFlagName, "30s")

timeout, err := getIPFSTimeout(cmd)
timeout, err := getDuration(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, defaultIPFSTimeout)
require.NoError(t, err)
require.Equal(t, 30*time.Second, timeout)
})
Expand All @@ -984,7 +997,7 @@ func TestGetIPFSTimeout(t *testing.T) {

cmd := getTestCmd(t)

timeout, err := getIPFSTimeout(cmd)
timeout, err := getDuration(cmd, ipfsTimeoutFlagName, ipfsTimeoutEnvKey, defaultIPFSTimeout)
require.NoError(t, err)
require.Equal(t, 40*time.Second, timeout)
})
Expand Down
9 changes: 6 additions & 3 deletions cmd/orb-server/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,8 @@ func createActivityPubStore(parameters *orbParameters, serviceEndpoint string) (

mongoDBProvider, err := ariesmongodbstorage.NewProvider(parameters.dbParameters.databaseURL,
ariesmongodbstorage.WithDBPrefix(databasePrefix),
ariesmongodbstorage.WithLogger(logger))
ariesmongodbstorage.WithLogger(logger),
ariesmongodbstorage.WithTimeout(parameters.databaseTimeout))
if err != nil {
return nil, fmt.Errorf("create MongoDB storage provider for ActivityPub: %w", err)
}
Expand Down Expand Up @@ -1022,7 +1023,8 @@ func createStoreProviders(parameters *orbParameters) (*storageProviders, error)
case strings.EqualFold(parameters.dbParameters.databaseType, databaseTypeMongoDBOption):
mongoDBProvider, err := ariesmongodbstorage.NewProvider(parameters.dbParameters.databaseURL,
ariesmongodbstorage.WithDBPrefix(parameters.dbParameters.databasePrefix),
ariesmongodbstorage.WithLogger(logger))
ariesmongodbstorage.WithLogger(logger),
ariesmongodbstorage.WithTimeout(parameters.databaseTimeout))
if err != nil {
return nil, fmt.Errorf("create MongoDB storage provider: %w", err)
}
Expand Down Expand Up @@ -1053,7 +1055,8 @@ func createStoreProviders(parameters *orbParameters) (*storageProviders, error)
case strings.EqualFold(parameters.dbParameters.kmsSecretsDatabaseType, databaseTypeMongoDBOption):
mongoDBProvider, err := ariesmongodbstorage.NewProvider(parameters.dbParameters.databaseURL,
ariesmongodbstorage.WithDBPrefix(parameters.dbParameters.databasePrefix),
ariesmongodbstorage.WithLogger(logger))
ariesmongodbstorage.WithLogger(logger),
ariesmongodbstorage.WithTimeout(parameters.databaseTimeout))
if err != nil {
return nil, fmt.Errorf("create MongoDB storage provider: %w", err)
}
Expand Down

0 comments on commit 789f5f0

Please sign in to comment.