From 0e35240fe9e887edef4eeb561b2f7f9937f42987 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Fri, 26 Jul 2019 21:01:02 +0200 Subject: [PATCH] Wire in context to VMContext License: MIT Signed-off-by: Jakub Sztandera --- chain/mining.go | 4 ++-- chain/sync.go | 12 ++++++------ chain/vm/vm.go | 34 +++++++++++++++++++++++++++++---- cmd/lotus-storage-miner/main.go | 22 +++++++++++++++++++++ go.sum | 2 ++ miner/miner.go | 3 +++ node/impl/full.go | 2 +- 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/chain/mining.go b/chain/mining.go index ec2396675..ae4dc59a5 100644 --- a/chain/mining.go +++ b/chain/mining.go @@ -21,7 +21,7 @@ func miningRewardForBlock(base *types.TipSet) types.BigInt { return types.NewInt(10000) } -func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*types.FullBlock, error) { +func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, miner address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*types.FullBlock, error) { st, err := cs.TipSetState(parents.Cids()) if err != nil { return nil, errors.Wrap(err, "failed to load tipset state") @@ -63,7 +63,7 @@ func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *type } else { msgCids = append(msgCids, msg.Cid()) } - rec, err := vm.ApplyMessage(&msg.Message) + rec, err := vm.ApplyMessage(ctx, &msg.Message) if err != nil { return nil, errors.Wrap(err, "apply message failure") } diff --git a/chain/sync.go b/chain/sync.go index bb1cc5023..dee9b84da 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -298,7 +298,7 @@ func (syncer *Syncer) SyncBootstrap() { return } - if err := syncer.ValidateTipSet(fts); err != nil { + if err := syncer.ValidateTipSet(context.TODO(), fts); err != nil { log.Errorf("failed to validate tipset: %s", err) return } @@ -482,7 +482,7 @@ func (syncer *Syncer) SyncCaughtUp(maybeHead *store.FullTipSet) error { for i := len(chain) - 1; i >= 0; i-- { ts := chain[i] - if err := syncer.ValidateTipSet(ts); err != nil { + if err := syncer.ValidateTipSet(context.TODO(), ts); err != nil { return errors.Wrap(err, "validate tipset failed") } @@ -500,21 +500,21 @@ func (syncer *Syncer) SyncCaughtUp(maybeHead *store.FullTipSet) error { return nil } -func (syncer *Syncer) ValidateTipSet(fts *store.FullTipSet) error { +func (syncer *Syncer) ValidateTipSet(ctx context.Context, fts *store.FullTipSet) error { ts := fts.TipSet() if ts.Equals(syncer.Genesis) { return nil } for _, b := range fts.Blocks { - if err := syncer.ValidateBlock(b); err != nil { + if err := syncer.ValidateBlock(ctx, b); err != nil { return err } } return nil } -func (syncer *Syncer) ValidateBlock(b *types.FullBlock) error { +func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) error { h := b.Header stateroot, err := syncer.store.TipSetState(h.Parents) if err != nil { @@ -537,7 +537,7 @@ func (syncer *Syncer) ValidateBlock(b *types.FullBlock) error { var receipts []interface{} for _, m := range b.Messages { - receipt, err := vm.ApplyMessage(&m.Message) + receipt, err := vm.ApplyMessage(ctx, &m.Message) if err != nil { return err } diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 9de1d5a85..f084aae9f 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -11,16 +11,22 @@ import ( "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/lib/bufbstore" + "go.opencensus.io/trace" bserv "github.com/ipfs/go-blockservice" cid "github.com/ipfs/go-cid" hamt "github.com/ipfs/go-hamt-ipld" ipld "github.com/ipfs/go-ipld-format" + logging "github.com/ipfs/go-log" dag "github.com/ipfs/go-merkledag" "golang.org/x/xerrors" ) +var log = logging.Logger("vm") + type VMContext struct { + ctx context.Context + vm *VM state *state.StateTree msg *types.Message @@ -88,6 +94,15 @@ func (vmc *VMContext) Origin() address.Address { // Send allows the current execution context to invoke methods on other actors in the system func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt, params []byte) ([]byte, aerrors.ActorError) { + ctx, span := trace.StartSpan(vmc.ctx, "vm.send") + defer span.End() + if span.IsRecordingEvents() { + span.AddAttributes( + trace.StringAttribute("to", to.String()), + trace.Int64Attribute("method", int64(method)), + trace.StringAttribute("value", value.String()), + ) + } msg := &types.Message{ From: vmc.msg.To, @@ -102,7 +117,7 @@ func (vmc *VMContext) Send(to address.Address, method uint64, value types.BigInt return nil, aerrors.Absorb(err, 2, "could not find actor for Send") } - nvmctx := vmc.vm.makeVMContext(toAct.Head, vmc.origin, msg) + nvmctx := vmc.vm.makeVMContext(ctx, toAct.Head, vmc.origin, msg) res, aerr := vmc.vm.Invoke(toAct, nvmctx, method, params) if aerr != nil { @@ -135,9 +150,10 @@ func (vmctx *VMContext) VerifySignature(sig types.Signature, act address.Address panic("NYI") } -func (vm *VM) makeVMContext(sroot cid.Cid, origin address.Address, msg *types.Message) *VMContext { +func (vm *VM) makeVMContext(ctx context.Context, sroot cid.Cid, origin address.Address, msg *types.Message) *VMContext { return &VMContext{ + ctx: ctx, vm: vm, state: vm.cstate, sroot: sroot, @@ -183,7 +199,10 @@ func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *store.ChainSt }, nil } -func (vm *VM) ApplyMessage(msg *types.Message) (*types.MessageReceipt, error) { +func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*types.MessageReceipt, error) { + ctx, span := trace.StartSpan(ctx, "vm.ApplyMessage") + defer span.End() + st := vm.cstate st.Snapshot() fromActor, err := st.GetActor(msg.From) @@ -220,7 +239,7 @@ func (vm *VM) ApplyMessage(msg *types.Message) (*types.MessageReceipt, error) { } DepositFunds(toActor, msg.Value) - vmctx := vm.makeVMContext(toActor.Head, msg.From, msg) + vmctx := vm.makeVMContext(ctx, toActor.Head, msg.From, msg) var errcode byte var ret []byte @@ -336,6 +355,13 @@ func (vm *VM) TransferFunds(from, to address.Address, amt types.BigInt) error { } func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params []byte) ([]byte, aerrors.ActorError) { + ctx, span := trace.StartSpan(vmctx.ctx, "vm.Invoke") + defer span.End() + var oldCtx context.Context + oldCtx, vmctx.ctx = vmctx.ctx, ctx + defer func() { + vmctx.ctx = oldCtx + }() ret, err := vm.inv.Invoke(act, vmctx, method, params) if err != nil { return nil, err diff --git a/cmd/lotus-storage-miner/main.go b/cmd/lotus-storage-miner/main.go index 3954c4c3d..390b50a37 100644 --- a/cmd/lotus-storage-miner/main.go +++ b/cmd/lotus-storage-miner/main.go @@ -4,10 +4,12 @@ import ( "os" logging "github.com/ipfs/go-log" + "go.opencensus.io/trace" "gopkg.in/urfave/cli.v2" "github.com/filecoin-project/go-lotus/build" lcli "github.com/filecoin-project/go-lotus/cli" + "github.com/filecoin-project/go-lotus/tracing" ) var log = logging.Logger("main") @@ -20,6 +22,26 @@ func main() { runCmd, initCmd, } + jaeger := tracing.SetupJaegerTracing("lotus") + defer func() { + if jaeger != nil { + jaeger.Flush() + } + }() + + for _, cmd := range local { + cmd := cmd + originBefore := cmd.Before + cmd.Before = func(cctx *cli.Context) error { + trace.UnregisterExporter(jaeger) + jaeger = tracing.SetupJaegerTracing("lotus/" + cmd.Name) + + if originBefore != nil { + return originBefore(cctx) + } + return nil + } + } app := &cli.App{ Name: "lotus-storage-miner", diff --git a/go.sum b/go.sum index 838fc3446..55488b54b 100644 --- a/go.sum +++ b/go.sum @@ -384,8 +384,10 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/marten-seemann/qtls v0.2.3 h1:0yWJ43C62LsZt08vuQJDK1uC1czUc3FJeCLPoNAI4vA= github.com/marten-seemann/qtls v0.2.3/go.mod h1:xzjG7avBwGGbdZ8dTGxlBnLArsVKLvwmjgmPuiQEcYk= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.1.4/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= diff --git a/miner/miner.go b/miner/miner.go index 35fb2da21..9e9e426f3 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -6,6 +6,7 @@ import ( logging "github.com/ipfs/go-log" "github.com/pkg/errors" + "go.opencensus.io/trace" chain "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/address" @@ -57,6 +58,8 @@ type Miner struct { } func (m *Miner) Mine(ctx context.Context) { + ctx, span := trace.StartSpan(ctx, "/mine") + defer span.End() for { base, err := m.GetBestMiningCandidate() if err != nil { diff --git a/node/impl/full.go b/node/impl/full.go index 99c75eb5f..8a467174f 100644 --- a/node/impl/full.go +++ b/node/impl/full.go @@ -100,7 +100,7 @@ func (a *FullNodeAPI) MinerStart(ctx context.Context, addr address.Address) erro } func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) { - fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs) + fblk, err := chain.MinerCreateBlock(ctx, a.Chain, addr, parents, tickets, proof, msgs) if err != nil { return nil, err }