Skip to content

Commit

Permalink
Merge pull request #338 from sandrask/issue-337
Browse files Browse the repository at this point in the history
chore: Add delay for starting additional servers within domain
  • Loading branch information
sandrask authored May 4, 2021
2 parents 9a4e77c + 8277dd9 commit 9b3f2a2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
23 changes: 23 additions & 0 deletions cmd/orb-server/startcmd/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const (
hostURLFlagUsage = "URL to run the orb-server instance on. Format: HostName:Port."
hostURLEnvKey = "ORB_HOST_URL"

startupDelayFlagName = "startup-delay"
startupDelayEnvKey = "ORB_STARTUP_DELAY"
startupDelayFlagShorthand = "j"
startupDelayFlagUsage = "Orb server start-up delay (in seconds). " + commonEnvVarUsageText + startupDelayEnvKey

vctURLFlagName = "vct-url"
vctURLFlagUsage = "Verifiable credential transparency URL."
vctURLEnvKey = "ORB_VCT_URL"
Expand Down Expand Up @@ -204,6 +209,7 @@ type orbParameters struct {
discoveryDomains []string
discoveryMinimumResolvers int
maxWitnessDelay time.Duration
startupDelay time.Duration
}

type anchorCredentialParams struct {
Expand Down Expand Up @@ -295,6 +301,21 @@ func getOrbParameters(cmd *cobra.Command) (*orbParameters, error) {
maxWitnessDelay = time.Duration(delay) * time.Second
}

startupDelayStr, err := cmdutils.GetUserSetVarFromString(cmd, startupDelayFlagName, startupDelayEnvKey, true)
if err != nil {
return nil, err
}

startupDelay := noStartupDelay
if startupDelayStr != "" {
delay, parseErr := strconv.ParseUint(startupDelayStr, 10, 32)
if parseErr != nil {
return nil, fmt.Errorf("invalid start-up delay format: %s", parseErr.Error())
}

startupDelay = time.Duration(delay) * time.Second
}

didNamespace, err := cmdutils.GetUserSetVarFromString(cmd, didNamespaceFlagName, didNamespaceEnvKey, false)
if err != nil {
return nil, err
Expand Down Expand Up @@ -362,6 +383,7 @@ func getOrbParameters(cmd *cobra.Command) (*orbParameters, error) {
discoveryDomains: discoveryDomains,
discoveryMinimumResolvers: discoveryMinimumResolvers,
maxWitnessDelay: maxWitnessDelay,
startupDelay: startupDelay,
}, nil
}

Expand Down Expand Up @@ -445,6 +467,7 @@ func getDBParameters(cmd *cobra.Command, kmOptional bool) (*dbParameters, error)

func createFlags(startCmd *cobra.Command) {
startCmd.Flags().StringP(hostURLFlagName, hostURLFlagShorthand, "", hostURLFlagUsage)
startCmd.Flags().StringP(startupDelayFlagName, startupDelayFlagShorthand, "", startupDelayFlagUsage)
startCmd.Flags().String(vctURLFlagName, "", vctURLFlagUsage)
startCmd.Flags().String(kmsStoreEndpointFlagName, "", kmsStoreEndpointFlagUsage)
startCmd.Flags().String(kmsEndpointFlagName, "", kmsEndpointFlagUsage)
Expand Down
29 changes: 29 additions & 0 deletions cmd/orb-server/startcmd/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ func TestStartCmdWithMissingArg(t *testing.T) {
require.Contains(t, err.Error(), "invalid max witness delay format")
})

t.Run("test invalid startup delay format", func(t *testing.T) {
startCmd := GetStartCmd()

args := []string{
"--" + hostURLFlagName, "localhost:8247",
"--" + vctURLFlagName, "localhost:8081",
"--" + externalEndpointFlagName, "orb.example.com",
"--" + casURLFlagName, "localhost:8081",
"--" + startupDelayFlagName, "abc",
"--" + didNamespaceFlagName, "namespace", "--" + databaseTypeFlagName, databaseTypeMemOption,
"--" + kmsSecretsDatabaseTypeFlagName, databaseTypeMemOption, "--" + tokenFlagName, "tk1",
"--" + anchorCredentialSignatureSuiteFlagName, "suite",
"--" + anchorCredentialDomainFlagName, "domain.com",
"--" + anchorCredentialIssuerFlagName, "issuer.com",
"--" + anchorCredentialURLFlagName, "peer.com",
"--" + LogLevelFlagName, log.ParseString(log.ERROR),
}

startCmd.SetArgs(args)

err := startCmd.Execute()

require.Error(t, err)
require.Contains(t, err.Error(), "invalid start-up delay format")
})
}

func TestStartCmdWithBlankEnvVar(t *testing.T) {
Expand Down Expand Up @@ -391,6 +416,7 @@ func TestStartCmdValidArgs(t *testing.T) {
"--" + casURLFlagName, "localhost:8081",
"--" + batchWriterTimeoutFlagName, "700",
"--" + maxWitnessDelayFlagName, "600",
"--" + startupDelayFlagName, "1",
"--" + didNamespaceFlagName, "namespace", "--" + databaseTypeFlagName, databaseTypeMemOption,
"--" + kmsSecretsDatabaseTypeFlagName, databaseTypeMemOption, "--" + tokenFlagName, "tk1",
"--" + anchorCredentialSignatureSuiteFlagName, "suite",
Expand Down Expand Up @@ -428,6 +454,9 @@ func setEnvVars(t *testing.T, databaseType string) {
err = os.Setenv(maxWitnessDelayEnvKey, "600")
require.NoError(t, err)

err = os.Setenv(startupDelayEnvKey, "1")
require.NoError(t, err)

err = os.Setenv(didNamespaceEnvKey, "namespace")
require.NoError(t, err)

Expand Down
7 changes: 7 additions & 0 deletions cmd/orb-server/startcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const (
chBuffer = 100

defaultMaxWitnessDelay = 600 * time.Second // 10 minutes

noStartupDelay = 0 * time.Second // no delay
)

var logger = log.New("orb-server")
Expand Down Expand Up @@ -235,6 +237,11 @@ func startOrbServices(parameters *orbParameters) error {
SetDefaultLogLevel(logger, parameters.logLevel)
}

if parameters.startupDelay != noStartupDelay {
logger.Infof("delaying server start-up for duration: %s", parameters.startupDelay)
time.Sleep(parameters.startupDelay)
}

storeProviders, err := createStoreProviders(parameters)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion test/bdd/features/did-sidetree.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Feature:
When client sends request to "https://orb.domain1.com/sidetree/v1/identifiers" to resolve DID document with canonical id
Then check success response contains "#canonicalId"

When client sends request to "https://orb.domain1.com/sidetree/v1/identifiers" to resolve DID document with alias "did:alias.com"
When client sends request to "https://orb.domain1.com/sidetree/v1/identifiers" to resolve DID document with alias "did:orb:alias.com"
Then check success response contains "#canonicalId"
Then check success response contains "#aliasdid"

Expand Down
2 changes: 2 additions & 0 deletions test/bdd/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ services:
- LOG_LEVEL=DEBUG
- ORB_VCT_URL=http://orb.vct:8077
- ORB_HOST_URL=0.0.0.0:443
# add delay for starting additional servers within same domain (in seconds)
- ORB_STARTUP_DELAY=5
# ORB_EXTERNAL_ENDPOINT is the endpoint that external clients use to invoke services. This endpoint is used
# to generate IDs of anchor credentials and ActivityPub objects and should be resolvable by external
# clients. This endpoint does not (typically) target a single node in the cluster but instead, a load
Expand Down

0 comments on commit 9b3f2a2

Please sign in to comment.