-
Notifications
You must be signed in to change notification settings - Fork 115
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
go/oasis-node/txsource: add parallel workload #2724
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go/oasis-node/txsource: add parallel workload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package workload | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
|
||
"github.com/oasislabs/oasis-core/go/common/crypto/signature" | ||
memorySigner "github.com/oasislabs/oasis-core/go/common/crypto/signature/signers/memory" | ||
"github.com/oasislabs/oasis-core/go/common/logging" | ||
consensus "github.com/oasislabs/oasis-core/go/consensus/api" | ||
"github.com/oasislabs/oasis-core/go/consensus/api/transaction" | ||
runtimeClient "github.com/oasislabs/oasis-core/go/runtime/client/api" | ||
staking "github.com/oasislabs/oasis-core/go/staking/api" | ||
) | ||
|
||
const ( | ||
NameParallel = "parallel" | ||
|
||
parallelSendWaitTimeoutInterval = 30 * time.Second | ||
parallelSendTimeoutInterval = 60 * time.Second | ||
parallelConcurency = 200 | ||
parallelTxGasAmount = 10 | ||
) | ||
|
||
var parallelLogger = logging.GetLogger("cmd/txsource/workload/parallel") | ||
|
||
type parallel struct{} | ||
|
||
func (parallel) Run(gracefulExit context.Context, rng *rand.Rand, conn *grpc.ClientConn, cnsc consensus.ClientBackend, rtc runtimeClient.RuntimeClient) error { | ||
ctx := context.Background() | ||
|
||
accounts := make([]signature.Signer, parallelConcurency) | ||
var err error | ||
fac := memorySigner.NewFactory() | ||
for i := range accounts { | ||
// NOTE: no balances are needed for now | ||
accounts[i], err = fac.Generate(signature.SignerEntity, rng) | ||
if err != nil { | ||
return fmt.Errorf("memory signer factory Generate account %d: %w", i, err) | ||
} | ||
} | ||
|
||
// A single global nonce is enough as we wait for all submissions to | ||
// complete before proceeding with a new batch. | ||
var nonce uint64 | ||
ptrus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fee := transaction.Fee{ | ||
Gas: parallelTxGasAmount, | ||
} | ||
|
||
for { | ||
errCh := make(chan error, parallelConcurency) | ||
var wg sync.WaitGroup | ||
wg.Add(parallelConcurency) | ||
|
||
for i := 0; i < parallelConcurency; i++ { | ||
go func(txSigner signature.Signer, nonce uint64) { | ||
defer wg.Done() | ||
|
||
// Transfer tx. | ||
transfer := staking.Transfer{ | ||
To: txSigner.Public(), | ||
} | ||
tx := staking.NewTransferTx(nonce, &fee, &transfer) | ||
|
||
signedTx, err := transaction.Sign(txSigner, tx) | ||
if err != nil { | ||
parallelLogger.Error("transaction.Sign error", "err", err) | ||
errCh <- fmt.Errorf("transaction.Sign: %w", err) | ||
return | ||
} | ||
|
||
parallelLogger.Debug("submitting self transfer", | ||
"account", txSigner.Public(), | ||
) | ||
if err = cnsc.SubmitTx(ctx, signedTx); err != nil { | ||
parallelLogger.Error("SubmitTx error", "err", err) | ||
errCh <- fmt.Errorf("cnsc.SubmitTx: %w", err) | ||
return | ||
} | ||
|
||
}(accounts[i], nonce) | ||
} | ||
|
||
// Wait for transactions. | ||
waitC := make(chan struct{}) | ||
go func() { | ||
defer close(waitC) | ||
wg.Wait() | ||
nonce++ | ||
}() | ||
|
||
select { | ||
case <-time.After(parallelSendWaitTimeoutInterval): | ||
parallelLogger.Error("transactions not completed within timeout") | ||
return fmt.Errorf("workload parallel: transactions not completed within timeout") | ||
|
||
case err := <-errCh: | ||
parallelLogger.Error("error subimit transaction", | ||
"err", err, | ||
) | ||
return fmt.Errorf("workload parallel: error submiting transaction: %w", err) | ||
|
||
case <-waitC: | ||
parallelLogger.Debug("all transfers successful", | ||
"concurency", parallelConcurency, | ||
) | ||
} | ||
|
||
select { | ||
case <-time.After(parallelSendTimeoutInterval): | ||
case <-gracefulExit.Done(): | ||
parallelLogger.Debug("time's up") | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume these accounts don't need any balance as they don't include any fees? If (when) you need fees, you would need to make sure to include accounts in genesis (defined in the txsource E2E test).
An alternative way (to avoid having each workload to include new accounts in genesis for the default seed) would be to only have a single (known, seed-independent) account in genesis and txsource support for funding workloads on start (e.g., a simple API to transfer into arbitrary accounts before the workload main loop starts).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't test networks include the test entity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah agreed and makes sense. Maybe it would be nicer if I do that in a separate PR though as it will also affect other workloads (at least the transfer one), and I can for now just keep Fees to zero here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah definitely in a separate PR. Could use the test entity for this yeah 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍