Skip to content
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

[sdk#1026] Use postpone.ContextWithValues() #248

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions pkg/networkservice/common/resourcepool/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,12 +23,14 @@ import (
"sync"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/common"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

"github.com/networkservicemesh/sdk-sriov/pkg/sriov"
"github.com/networkservicemesh/sdk-sriov/pkg/sriov/config"
Expand Down Expand Up @@ -60,6 +64,8 @@ func (i *resourcePoolClient) Request(ctx context.Context, request *networkservic
oldPCIAddress := request.GetConnection().GetMechanism().GetParameters()[common.PCIAddressKey]
oldTokenID := request.GetConnection().GetMechanism().GetParameters()[TokenIDKey]

postponeCtxFunc := postpone.ContextWithValues(ctx)

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
Expand All @@ -73,10 +79,13 @@ func (i *resourcePoolClient) Request(ctx context.Context, request *networkservic

err = assignVF(ctx, logger, conn, tokenID, i.resourcePool)
if err != nil {
_ = i.resourcePool.close(conn)
if _, closeErr := next.Client(ctx).Close(ctx, conn, opts...); closeErr != nil {
logger.Errorf("failed to close failed connection: %s %s", conn.GetId(), closeErr.Error())
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bolodya1997 I'm not sure if I fully understand this solution, are you trying to postpone the invocation of i.Close with closeCtx ? what is the purpose of this ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have met an issue in our integration tests - networkservicemesh/sdk#1026.
In short - sometimes Request context can be expired/canceled and so Close on failure cannot reach next hop.
So here it is actually a pattern we are planning to use to solve this issue.

// 1. Capture ctx values and timeout before the Request.
postponeCtxFunc := postpone.ContextWithValues(ctx)

// 2. Perform Request.
conn, err = next.Client(ctx).Request(ctx, request)
if err != nil {
    return nil, err
}

if err := doSomething(); err != nil {
    // 3. Create Close context using stored timeout, values.
    closeCtx, cancelClose := postponeCtxFunc()
    defer cancelClose()

    // 4. Perform Close using Close context.
    if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
        err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
    }
    return nil, err
}

return conn, nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! ok, interesting change. is i.Close invoked with closeCtx which has extended deadline ?
should cancelClose() be called at the end ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes closeCtx has postponed deadline in comparison with ctx.
cancelClose() is called with defer, so closeCtx would be gracefully closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah! yes, correct. with this closeCtx's Done channel closed gracefully only after executing new hop's close handlers.


if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}

Expand Down