From 95dcdc1c5e894a925bf6fa5e455f28eb4c7ddaff Mon Sep 17 00:00:00 2001 From: Warren He Date: Thu, 26 Dec 2019 14:50:51 -0800 Subject: [PATCH] go txsource: add time limit --- go/oasis-node/cmd/debug/txsource/txsource.go | 17 ++++++++++++++--- .../cmd/debug/txsource/workload/junk.go | 2 +- .../cmd/debug/txsource/workload/transfer.go | 9 ++++++++- .../cmd/debug/txsource/workload/workload.go | 7 ++++++- go/oasis-test-runner/scenario/e2e/txsource.go | 1 + scripts/txsource-wrapper.sh | 10 +++++++++- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/go/oasis-node/cmd/debug/txsource/txsource.go b/go/oasis-node/cmd/debug/txsource/txsource.go index 7f2e3d54a4f..b3ca274d50b 100644 --- a/go/oasis-node/cmd/debug/txsource/txsource.go +++ b/go/oasis-node/cmd/debug/txsource/txsource.go @@ -24,8 +24,9 @@ import ( ) const ( - CfgWorkload = "workload" - CfgSeed = "seed" + CfgWorkload = "workload" + CfgSeed = "seed" + CfgTimeLimit = "time_limit" ) var ( @@ -44,6 +45,15 @@ func doRun(cmd *cobra.Command, args []string) error { common.EarlyLogAndExit(err) } + // Set up the time limit. + ctx := context.Background() + timeLimit := viper.GetDuration(CfgTimeLimit) + if timeLimit != 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, timeLimit) + defer cancel() + } + // Set up the genesis system for the signature system's chain context. genesis, err := genesisFile.DefaultFileProvider() if err != nil { @@ -95,7 +105,7 @@ func doRun(cmd *cobra.Command, args []string) error { logger.Debug("node synced") logger.Debug("entering workload", "name", name) - if err = w.Run(rng, conn, cnsc, rtc); err != nil { + if err = w.Run(ctx, rng, conn, cnsc, rtc); err != nil { return fmt.Errorf("workload %s: %w", name, err) } logger.Debug("workload returned", "name", name) @@ -112,6 +122,7 @@ func init() { fs := flag.NewFlagSet("", flag.ContinueOnError) fs.String(CfgWorkload, workload.NameTransfer, "Name of the workload to run (see source for listing)") fs.String(CfgSeed, "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeed", "Seed to use for randomized workloads") + fs.Duration(CfgTimeLimit, 0, "Exit successfully after this long, or 0 to run forever") _ = viper.BindPFlags(fs) txsourceCmd.Flags().AddFlagSet(fs) diff --git a/go/oasis-node/cmd/debug/txsource/workload/junk.go b/go/oasis-node/cmd/debug/txsource/workload/junk.go index 9f007bb75b7..145d56ff315 100644 --- a/go/oasis-node/cmd/debug/txsource/workload/junk.go +++ b/go/oasis-node/cmd/debug/txsource/workload/junk.go @@ -15,7 +15,7 @@ var _ Workload = runtimePlaceholder{} type runtimePlaceholder struct{} -func (runtimePlaceholder) Run(_ *rand.Rand, _ *grpc.ClientConn, _ consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error { +func (runtimePlaceholder) Run(_ context.Context, _ *rand.Rand, _ *grpc.ClientConn, _ consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error { ctx := context.Background() var tx *runtimeClient.SubmitTxRequest // Placeholder for sending a runtime transaction from a workload. diff --git a/go/oasis-node/cmd/debug/txsource/workload/transfer.go b/go/oasis-node/cmd/debug/txsource/workload/transfer.go index d2f088960c0..12e94e3037a 100644 --- a/go/oasis-node/cmd/debug/txsource/workload/transfer.go +++ b/go/oasis-node/cmd/debug/txsource/workload/transfer.go @@ -27,7 +27,7 @@ var logger = logging.GetLogger("cmd/txsource/workload/transfer") type transfer struct{} -func (transfer) Run(rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.ClientBackend, _ runtimeClient.RuntimeClient) error { +func (transfer) Run(gracefulExit context.Context, rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error { // Load all the keys up front. Like, how annoyed would you be if down the line one of them turned out to be // corrupted or something, ya know? accounts := make([]struct { @@ -118,5 +118,12 @@ func (transfer) Run(rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.Client if err = to.reckonedBalance.Add(&transfer.Tokens); err != nil { return fmt.Errorf("to reckoned balance %v Add transfer tokens %v: %w", to.reckonedBalance, transfer.Tokens, err) } + + select { + case <-gracefulExit.Done(): + logger.Debug("time's up") + return nil + default: + } } } diff --git a/go/oasis-node/cmd/debug/txsource/workload/workload.go b/go/oasis-node/cmd/debug/txsource/workload/workload.go index c21a6763cbe..fd56c8c6ec7 100644 --- a/go/oasis-node/cmd/debug/txsource/workload/workload.go +++ b/go/oasis-node/cmd/debug/txsource/workload/workload.go @@ -1,6 +1,7 @@ package workload import ( + "context" "math/rand" "google.golang.org/grpc" @@ -10,7 +11,11 @@ import ( ) type Workload interface { - Run(rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error + // Run executes the workload. + // If `gracefulExit`'s deadline passes, it is not an error. + // Return `nil` after any short-ish amount of time in that case. + // Prefer to do at least one "iteration" even so. + Run(gracefulExit context.Context, rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error } // ByName is the registry of workloads that you can access with `--workload ` on the command line. diff --git a/go/oasis-test-runner/scenario/e2e/txsource.go b/go/oasis-test-runner/scenario/e2e/txsource.go index 63d64693105..01d1ef9dbff 100644 --- a/go/oasis-test-runner/scenario/e2e/txsource.go +++ b/go/oasis-test-runner/scenario/e2e/txsource.go @@ -40,6 +40,7 @@ func (sc *txSourceImpl) Run(childEnv *env.Env) error { cmd, err := startClient(childEnv, sc.net, sc.clientBinary, append([]string{ "--genesis-path", sc.net.GenesisPath(), + "--time-limit", "2m", // %%% low value for validation (: }, sc.clientArgs...)) if err != nil { return fmt.Errorf("startClient: %w", err) diff --git a/scripts/txsource-wrapper.sh b/scripts/txsource-wrapper.sh index e0998af06b6..ef67e1c3015 100755 --- a/scripts/txsource-wrapper.sh +++ b/scripts/txsource-wrapper.sh @@ -1,7 +1,8 @@ #!/bin/sh -eu usage() { - echo >&2 "usage: $0 --node-address --runtime-id --genesis-path " + echo >&2 "usage: $0 --node-address --runtime-id --genesis-path --time-limit " + # 0 1 2 3 4 5 6 7 8 exit 1 } @@ -16,8 +17,15 @@ else usage fi +if [ "$7" = "--time-limit" ]; then + time_limit=$8 +else + usage +fi + exec ./go/oasis-node/oasis-node debug txsource \ --workload transfer \ + --time_limit "$time_limit" \ --address "$node_address" \ --debug.allow_test_keys \ --debug.dont_blame_oasis \