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] Add closectx #1035

Merged

Conversation

Bolodya1997
Copy link

@Bolodya1997 Bolodya1997 commented Jul 22, 2021

Description

Adds closectx.New() postpone.ContextWithValues() to be used for Close/Unregister in failed Request/Register cases.

Issue link

#1026

How Has This Been Tested?

  • Added unit testing to cover
  • Tested manually
  • Tested by integration testing
  • Have not tested

Types of changes

  • Bug fix
  • New functionallity
  • Documentation
  • Refactoring
  • CI

@Bolodya1997 Bolodya1997 marked this pull request as draft July 23, 2021 09:53
@Bolodya1997 Bolodya1997 marked this pull request as ready for review July 23, 2021 15:23
@Bolodya1997 Bolodya1997 force-pushed the sdk#1026/close-context branch 2 times, most recently from 0b182de to cfcfbe4 Compare July 26, 2021 11:09
"github.com/networkservicemesh/sdk/pkg/tools/extend"
)

const closeTimeout = 15 * time.Second
Copy link
Member

Choose a reason for hiding this comment

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

Please don't put magic timer constants in the system.

Copy link
Author

Choose a reason for hiding this comment

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

So here comes a problem, what should we use as a timeout in such case? Or should we just make Close on a context without any deadline?

Copy link
Member

Choose a reason for hiding this comment

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

See below... derive the context from the previous duration of the Request... not from an arbitrary timer value.

Over reliance on (and tuning of) timer values leads to things like: networkservicemesh/cmd-admission-webhook-k8s#18 where we are now seeking to propagate the ability to over tune those timers further and further.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed with using timeout taken before the Request.

timeout = closeTimeout
}

ctx = extend.WithValuesFromContext(context.Background(), ctx)
Copy link
Member

Choose a reason for hiding this comment

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

We don't generally know if extending values is correct or incorrect here.

Copy link
Author

Choose a reason for hiding this comment

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

This method provides the same context with postponed deadline and disabled cancels from backward. So I suppose that extending values is correct here.

Copy link
Member

Choose a reason for hiding this comment

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

If we need to extend values... we can do that easily inline when using this as well... this is orthogonal to the issue of extending the timer.

Copy link
Author

Choose a reason for hiding this comment

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

Created 2 helper methods: Context and ContextWithValues.

if deadline, ok := ctx.Deadline(); ok {
timeout = clockTime.Until(deadline)
}
if timeout < closeTimeout {
Copy link
Member

Choose a reason for hiding this comment

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

This context is going to expire, if anything, faster the the initial context from which its taken... its not going to be appropriate for 'Have to call Close at some later point' ... the right answer here is closer to:

func FromContext(ctx context.Context) func() context.Context {
	ctxFunc := func() context.Context { return context.Background() }
	if deadline, ok := ctx.Deadline(); ok {
		duration := time.Until(deadline)
		ctxFunc = func() context.Context {
			ctx, _ := context.WithTimeout(context.Background(), duration)
			return ctx
		}
	}
	return ctxFunc
}

Copy link
Author

Choose a reason for hiding this comment

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

We increase timeout up to closeTimeout if it is less than that. So it does mean that new context is going to expire in the same time or slower than the initial context.
So using here pure ctx.Deadline() just leads us to the same old issue that should be fixed by closectx:

  1. Request context expires (or is very close to be expired).
  2. Error happens.
  3. Chain element calls Close on the expired (almost expired) context.

Copy link
Member

Choose a reason for hiding this comment

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

Not if you handle it like this:

func (c *client) Request(ctx context.Context,request *networkservice.Request,opts...grpc.CallOptions) {
    ctxFunc := ctxfunc.FromContext(ctx)
    conn, err := next.Client(ctx).Request(ctx,request,opts...)
    ...
    err = doSomething(...)
    if err != nil {
        _ = c.Close(extend.WithValues(ctxFunc(),ctx),conn)
       return nil, err
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

You capture the time at the beginning of the Request, and utilize it at the end of the Request for cleanup... all time spent calling down chain and doing something is recovered.

Copy link
Author

Choose a reason for hiding this comment

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

Looks great, done this in postpone.Context().

// limitations under the License.

// Package closectx provides helper method to create Close/Unregister context in case of Request/Register failure
package closectx
Copy link
Member

Choose a reason for hiding this comment

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

Could we name this something less bound to Close and more reflecting the fact that we are creating a mechanism generally appropriate to allow us to reasonably do things like Close after ourselves, and make other sorts of calls that start at some point in the future...

Copy link
Author

Choose a reason for hiding this comment

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

Probably we can name it something like postponectx? And so description would be:

// Package postponectx provides helper method to postpone context deadline.

Copy link
Member

Choose a reason for hiding this comment

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

This is moving in the right direction :)

As a side note: I don't like the current name I'm using (ctxFunc) all that much either... so please don't read to much into it :)

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps in the vein of postponectx we could have package postpone and the call be something like postpone.Context(ctx)

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@Bolodya1997 Bolodya1997 marked this pull request as draft July 27, 2021 13:28
@Bolodya1997 Bolodya1997 marked this pull request as ready for review July 28, 2021 13:41
@denis-tingaikin
Copy link
Member

@Bolodya1997 I don't quite follow how this PR relates with #1035.
Could you explain a motivation of this PR?

@Bolodya1997
Copy link
Author

@Bolodya1997 I don't quite follow how this PR relates with #1035.
Could you explain a motivation of this PR?

I don't quite follow your question - here is an issue related to this PR #1026 and so it is a motivation.

Vladimir Popov added 4 commits August 12, 2021 22:26
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
@Bolodya1997 Bolodya1997 force-pushed the sdk#1026/close-context branch from 9bb2925 to d17ec3a Compare August 12, 2021 15:32
@denis-tingaikin
Copy link
Member

denis-tingaikin commented Aug 12, 2021

I don't quite follow your question - here is an issue related to this PR #1026 and so it is a motivation.

@Bolodya1997 Sorry, I meant motivation of these changes. I'm intrested in why is this the best solution for the issue. Moreover I'm intrested on how is it related to #1035, as I see you pointed it a few days ago.

@edwarnicke Do you have any comments on this?
It looks like all comments are resolved

@edwarnicke edwarnicke merged commit a91779d into networkservicemesh:main Aug 17, 2021
nsmbot pushed a commit to networkservicemesh/cmd-nse-vfio that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-registry-memory that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsc-init that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsmgr that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-registry-proxy-dns that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/sdk-kernel that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nse-icmp-responder that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/cmd-nsmgr-proxy that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
nsmbot pushed a commit to networkservicemesh/sdk-k8s that referenced this pull request Aug 17, 2021
…k@main

PR link: networkservicemesh/sdk#1035

Commit: a91779d
Author: Vladimir Popov
Date: 2021-08-17 20:25:36 +0700
Message:
  - [sdk#1026] Add closectx (#1035)
* Add closectx

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Add unit tests for Close on canceled context

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Rework closectx to use context.Background()

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>

* Replace closectx.New() with postpone.ContextWithValues()

Signed-off-by: NSMBot <nsmbot@networkservicmesh.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants