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

fix: use max of ctx.deadline and requestDuration for healing #1006

Merged
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions pkg/networkservice/chains/nsmgr/heal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,19 @@ func TestNSMGR_CloseHeal(t *testing.T) {

nsc := domain.Nodes[0].NewClient(nscCtx, sandbox.GenerateTestToken)

reqCtx, reqCancel := context.WithTimeout(ctx, time.Second)
defer reqCancel()

// 1. Request
conn, err := nsc.Request(ctx, request.Clone())
conn, err := nsc.Request(reqCtx, request.Clone())
require.NoError(t, err)

ignoreCurrent := goleak.IgnoreCurrent()

// 2. Refresh
request.Connection = conn

conn, err = nsc.Request(ctx, request.Clone())
conn, err = nsc.Request(reqCtx, request.Clone())
require.NoError(t, err)

// 3. Stop endpoint and wait for the heal to start
Expand Down
9 changes: 6 additions & 3 deletions pkg/networkservice/chains/nsmgr/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ func (s *nsmgrSuite) Test_ConnectToDeadNSEUsecase() {

request := defaultRequest(nsReg.Name)

conn, err := nsc.Request(ctx, request.Clone())
reqCtx, reqCancel := context.WithTimeout(ctx, time.Second)
defer reqCancel()

conn, err := nsc.Request(reqCtx, request.Clone())
require.NoError(t, err)
require.NotNil(t, conn)
require.Equal(t, int32(1), atomic.LoadInt32(&counter.Requests))
Expand All @@ -338,9 +341,9 @@ func (s *nsmgrSuite) Test_ConnectToDeadNSEUsecase() {
refreshRequest := request.Clone()
refreshRequest.Connection = conn.Clone()

_, err = nsc.Request(ctx, refreshRequest)
_, err = nsc.Request(reqCtx, refreshRequest)
require.Error(t, err)
require.NoError(t, ctx.Err())
require.NoError(t, reqCtx.Err())

// Close
_, _ = nsc.Close(ctx, conn)
Expand Down
14 changes: 12 additions & 2 deletions pkg/networkservice/common/heal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (f *healServer) Request(ctx context.Context, request *networkservice.Networ
clockTime := clock.FromContext(ctx)
ctx = f.withHandlers(ctx)

requestTimeout := time.Duration(0)
if deadline, ok := ctx.Deadline(); ok {
requestTimeout = clockTime.Until(deadline)
}

requestStart := clockTime.Now()

conn, err := next.Server(ctx).Request(ctx, request)
Expand All @@ -92,7 +97,10 @@ func (f *healServer) Request(ctx context.Context, request *networkservice.Networ
// difference between these times was 3x on packet cluster (0.5s local vs 1.5s remote). So taking 5x value would be
// enough to cover such local to remote case and not too much in terms of blocking subsequent Request/Close events
// (7.5s for the worst remote case).
requestTimeout := clockTime.Since(requestStart) * 5
requestDuration := clockTime.Since(requestStart) * 5
if requestDuration > requestTimeout {
requestTimeout = requestDuration
}

cw, loaded := f.healContextMap.LoadOrStore(request.GetConnection().GetId(), &ctxWrapper{
request: request.Clone(),
Expand All @@ -108,7 +116,9 @@ func (f *healServer) Request(ctx context.Context, request *networkservice.Networ
cw.cancel = nil
}
cw.request = request.Clone()
cw.requestTimeout = requestTimeout
if requestTimeout > cw.requestTimeout {
cw.requestTimeout = requestTimeout
}
cw.ctx = f.createHealContext(ctx, cw.ctx)
}

Expand Down