Skip to content

Commit

Permalink
feat: use the context to thread sessions through all commands
Browse files Browse the repository at this point in the history
* Ensure we have a single session per gateway request.
* Use a session in the ipfs refs command.
* Start a session before resolving the pin root (also fixes sessions while
  pinning).
* Update go-merkledag to reliably create sessions.

part of #7198
  • Loading branch information
Stebalien committed Apr 23, 2020
1 parent e296c61 commit 7374929
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 24 deletions.
17 changes: 7 additions & 10 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dagcmd

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -9,6 +10,7 @@ import (
"strings"
"time"

exchange "github.com/ipfs/go-ipfs-exchange-interface"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
"github.com/ipfs/go-ipfs/core/coredag"
iface "github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -18,7 +20,6 @@ import (
cmds "github.com/ipfs/go-ipfs-cmds"
files "github.com/ipfs/go-ipfs-files"
ipld "github.com/ipfs/go-ipld-format"
mdag "github.com/ipfs/go-merkledag"
ipfspath "github.com/ipfs/go-path"
"github.com/ipfs/interface-go-ipfs-core/options"
path "github.com/ipfs/interface-go-ipfs-core/path"
Expand Down Expand Up @@ -552,6 +553,10 @@ The output of blocks happens in strict DAG-traversal, first-seen, order.
return err
}

ctx, cancel := context.WithCancel(req.Context)
ctx = exchange.NewSession(req.Context)
defer cancel()

// Code disabled until descent-issue in go-ipld-prime is fixed
// https://github.com/ribasushi/gip-muddle-up
//
Expand Down Expand Up @@ -581,15 +586,7 @@ The output of blocks happens in strict DAG-traversal, first-seen, order.
close(errCh)
}()

if err := gocar.WriteCar(
req.Context,
mdag.NewSession(
req.Context,
node.DAG,
),
[]cid.Cid{c},
pipeW,
); err != nil {
if err := gocar.WriteCar(ctx, node.DAG, []cid.Cid{c}, pipeW); err != nil {
errCh <- err
}
}()
Expand Down
23 changes: 18 additions & 5 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
cid "github.com/ipfs/go-cid"
cidenc "github.com/ipfs/go-cidutil/cidenc"
cmds "github.com/ipfs/go-ipfs-cmds"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
pin "github.com/ipfs/go-ipfs-pinner"
ipld "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -186,20 +187,32 @@ var addPinCmd = &cmds.Command{
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
added := make([]string, len(paths))
for i, b := range paths {
rp, err := api.ResolvePath(ctx, path.New(b))
rp, err := pinAdd(ctx, api, path.New(b), recursive)
if err != nil {
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
return nil, err
}
added[i] = enc.Encode(rp.Cid())
}

return added, nil
}

func pinAdd(ctx context.Context, api coreiface.CoreAPI, path path.Path, recursive bool) (path.Resolved, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = exchange.NewSession(ctx)

rp, err := api.ResolvePath(ctx, path)
if err != nil {
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
return nil, err
}
return rp, nil
}

var rmPinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove pinned objects from local storage.",
Expand Down
7 changes: 7 additions & 0 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"strings"

exchange "github.com/ipfs/go-ipfs-exchange-interface"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"

cid "github.com/ipfs/go-cid"
Expand Down Expand Up @@ -102,6 +103,12 @@ NOTE: List all references recursively by using the flag '-r'.
format = "<src> -> <dst>"
}

// Assume all these requests are related and put them into a
// single session.
ctx, cancel := context.WithCancel(ctx)
ctx = exchange.NewSession(ctx)
defer cancel()

objs, err := objectsForPaths(ctx, api, req.Arguments)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
}

// getSession returns new api backed by the same node with a read-only session DAG
// TODO: Remove this once we can thread the context through everywhere we need
// to make requests relates to the session.
func (api *CoreAPI) getSession(ctx context.Context) *CoreAPI {
sesApi := *api

// TODO: We could also apply this to api.blocks, and compose into writable api,
// but this requires some changes in blockservice/merkledag
sesApi.dag = dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag))
sesApi.dag = dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag)) //nolint

return &sesApi
}
6 changes: 6 additions & 0 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package coreapi
import (
"context"
"fmt"

bserv "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
pin "github.com/ipfs/go-ipfs-pinner"
ipld "github.com/ipfs/go-ipld-format"
Expand All @@ -17,6 +19,8 @@ import (
type PinAPI CoreAPI

func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOption) error {
ctx = exchange.NewSession(ctx)

dagNode, err := api.core().ResolveNode(ctx, p)
if err != nil {
return fmt.Errorf("pin: %s", err)
Expand Down Expand Up @@ -80,6 +84,8 @@ func (api *PinAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.PinRmOpti
}

func (api *PinAPI) Update(ctx context.Context, from path.Path, to path.Path, opts ...caopts.PinUpdateOption) error {
ctx = exchange.NewSession(ctx)

settings, err := caopts.PinUpdateOptions(opts...)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

humanize "github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
files "github.com/ipfs/go-ipfs-files"
dag "github.com/ipfs/go-merkledag"
mfs "github.com/ipfs/go-mfs"
Expand Down Expand Up @@ -89,6 +90,11 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// the hour is a hard fallback, we don't expect it to happen, but just in case
ctx, cancel := context.WithTimeout(r.Context(), time.Hour)
defer cancel()

// Always create a root session for http requests.
// That way we do all path resolution etc in the same session.
ctx = exchange.NewSession(ctx)

r = r.WithContext(ctx)

defer func() {
Expand Down
2 changes: 1 addition & 1 deletion core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *syncDagService) Sync() error {
}

func (s *syncDagService) Session(ctx context.Context) format.NodeGetter {
return merkledag.NewSession(ctx, s.DAGService)
return merkledag.NewSession(ctx, s.DAGService) //nolint
}

// Dag creates new DAGService
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gogo/protobuf v1.3.1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/golang-lru v0.5.4
github.com/ipfs/go-bitswap v0.2.12
github.com/ipfs/go-bitswap v0.2.13-0.20200423051144-fa8848292027
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-cid v0.0.5
Expand All @@ -33,7 +33,7 @@ require (
github.com/ipfs/go-ipfs-cmds v0.2.2
github.com/ipfs/go-ipfs-config v0.5.2
github.com/ipfs/go-ipfs-ds-help v0.1.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/go-ipfs-pinner v0.0.4
Expand All @@ -46,7 +46,7 @@ require (
github.com/ipfs/go-ipld-git v0.0.3
github.com/ipfs/go-ipns v0.0.2
github.com/ipfs/go-log v1.0.4
github.com/ipfs/go-merkledag v0.3.2
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-metrics-prometheus v0.0.2
github.com/ipfs/go-mfs v0.1.1
Expand Down
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ github.com/ipfs/go-bitswap v0.1.0/go.mod h1:FFJEf18E9izuCqUtHxbWEvq+reg7o4CW5wSA
github.com/ipfs/go-bitswap v0.1.2/go.mod h1:qxSWS4NXGs7jQ6zQvoPY3+NmOfHHG47mhkiLzBpJQIs=
github.com/ipfs/go-bitswap v0.1.3/go.mod h1:YEQlFy0kkxops5Vy+OxWdRSEZIoS7I7KDIwoa5Chkps=
github.com/ipfs/go-bitswap v0.1.8/go.mod h1:TOWoxllhccevbWFUR2N7B1MTSVVge1s6XSMiCSA4MzM=
github.com/ipfs/go-bitswap v0.2.12 h1:BvCMhbGykW7C1z+TbEiwH4flkTCyUlfoVwmaYX8+b/s=
github.com/ipfs/go-bitswap v0.2.12/go.mod h1:SDXpLeKZagyVVc8/z7sGtmM/lz8lyAmSzrUx3Ge3GXw=
github.com/ipfs/go-bitswap v0.2.13-0.20200423051144-fa8848292027 h1:WyM+kTU7W2CkcwSR2BpS5Y7rNCoI9fWolnUYvA/7KP4=
github.com/ipfs/go-bitswap v0.2.13-0.20200423051144-fa8848292027/go.mod h1:2lkrFZYpk4VxBIC+blbKYSmqfAySh7bhbmEW/hT68+s=
github.com/ipfs/go-block-format v0.0.1/go.mod h1:DK/YYcsSUIVAFNwo/KZCdIIbpN0ROH/baNLgayt4pFc=
github.com/ipfs/go-block-format v0.0.2 h1:qPDvcP19izTjU8rgo6p7gTXZlkMkF5bz5G3fqIsSCPE=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down Expand Up @@ -302,6 +302,9 @@ github.com/ipfs/go-ipfs-ds-help v0.1.1 h1:IW/bXGeaAZV2VH0Kuok+Ohva/zHkHmeLFBxC1k
github.com/ipfs/go-ipfs-ds-help v0.1.1/go.mod h1:SbBafGJuGsPI/QL3j9Fc5YPLeAu+SzOkI0gFwAg+mOs=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1 h1:LJXIo9W7CAmugqI+uofioIpRb6rY30GUu7G6LUfpMvM=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1/go.mod h1:c8MwfHjtQjPoDyiy9cFquVtVHkO9b9Ob3FG91qJnWCM=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423051052-ccb5de3a8346/go.mod h1:uChpHomshgrKU+8Uh6y1bh3SKn0jBRrdz4tHGKdDYao=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43 h1:m6tthCWsKZHixR2WqO4O5Hq61nRIS13NVxPA/LsFWPE=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43/go.mod h1:uChpHomshgrKU+8Uh6y1bh3SKn0jBRrdz4tHGKdDYao=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1 h1:P56jYKZF7lDDOLx5SotVh5KFxoY6C81I1NSHW1FxGew=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1/go.mod h1:WhHSFCVYX36H/anEKQboAzpUws3x7UeEGkzQc3iNkM0=
github.com/ipfs/go-ipfs-files v0.0.2/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
Expand Down Expand Up @@ -367,8 +370,8 @@ github.com/ipfs/go-merkledag v0.3.0 h1:1bXv/ZRPZLVdij/a33CkXMVdxUdred9sz4xyph+0l
github.com/ipfs/go-merkledag v0.3.0/go.mod h1:4pymaZLhSLNVuiCITYrpViD6vmfZ/Ws4n/L9tfNv3S4=
github.com/ipfs/go-merkledag v0.3.1 h1:3UqWINBEr3/N+r6OwgFXAddDP/8zpQX/8J7IGVOCqRQ=
github.com/ipfs/go-merkledag v0.3.1/go.mod h1:fvkZNNZixVW6cKSZ/JfLlON5OlgTXNdRLz0p6QG/I2M=
github.com/ipfs/go-merkledag v0.3.2 h1:MRqj40QkrWkvPswXs4EfSslhZ4RVPRbxwX11js0t1xY=
github.com/ipfs/go-merkledag v0.3.2/go.mod h1:fvkZNNZixVW6cKSZ/JfLlON5OlgTXNdRLz0p6QG/I2M=
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0 h1:tomXjLNcY/Hr8wvtKKycQm8kaBFeFLsulCsenhY9xjM=
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0/go.mod h1:v/YrENXnd3fuLUb8USpUavtceC90CegoDif+wiY/ttw=
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY=
github.com/ipfs/go-metrics-prometheus v0.0.2 h1:9i2iljLg12S78OhC6UAiXi176xvQGiZaGVF1CUVdE+s=
Expand Down

0 comments on commit 7374929

Please sign in to comment.