Skip to content

Commit

Permalink
go txsource: add time limit
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Dec 26, 2019
1 parent 6e96616 commit 95dcdc1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
17 changes: 14 additions & 3 deletions go/oasis-node/cmd/debug/txsource/txsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
)

const (
CfgWorkload = "workload"
CfgSeed = "seed"
CfgWorkload = "workload"
CfgSeed = "seed"
CfgTimeLimit = "time_limit"
)

var (
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion go/oasis-node/cmd/debug/txsource/workload/junk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion go/oasis-node/cmd/debug/txsource/workload/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
}
}
}
7 changes: 6 additions & 1 deletion go/oasis-node/cmd/debug/txsource/workload/workload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package workload

import (
"context"
"math/rand"

"google.golang.org/grpc"
Expand All @@ -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 <name>` on the command line.
Expand Down
1 change: 1 addition & 0 deletions go/oasis-test-runner/scenario/e2e/txsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion scripts/txsource-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh -eu

usage() {
echo >&2 "usage: $0 --node-address <node_address> --runtime-id <unused> --genesis-path <genesis_path>"
echo >&2 "usage: $0 --node-address <node_address> --runtime-id <unused> --genesis-path <genesis_path> --time-limit <time_limit>"
# 0 1 2 3 4 5 6 7 8
exit 1
}

Expand All @@ -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 \
Expand Down

0 comments on commit 95dcdc1

Please sign in to comment.