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

disttask: fix cancel from grpc mark subtasks as failed #48339

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 4 additions & 0 deletions pkg/disttask/framework/scheduler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ go_library(
"//pkg/util/memory",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_uber_go_zap//:zap",
],
)
Expand All @@ -56,6 +58,8 @@ go_test(
"//pkg/resourcemanager/util",
"@com_github_pingcap_errors//:errors",
"@com_github_stretchr_testify//require",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_uber_go_mock//gomock",
],
)
1 change: 0 additions & 1 deletion pkg/disttask/framework/scheduler/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func (m *Manager) onRunnableTask(ctx context.Context, task *proto.Task) {
return
}
scheduler := factory(ctx, m.id, task, m.taskTable)

taskCtx, taskCancel := context.WithCancelCause(ctx)
m.registerCancelFunc(task.ID, taskCancel)
defer taskCancel(nil)
Expand Down
4 changes: 4 additions & 0 deletions pkg/disttask/framework/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/pingcap/tidb/pkg/util/memory"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
Expand Down Expand Up @@ -608,6 +610,8 @@ func (s *BaseScheduler) markSubTaskCanceledOrFailed(ctx context.Context, subtask
if ctx.Err() != nil && context.Cause(ctx) == ErrCancelSubtask {
logutil.Logger(s.logCtx).Warn("subtask canceled", zap.Error(err))
s.updateSubtaskStateAndError(subtask, proto.TaskStateCanceled, nil)
} else if status.Code(err) == codes.Canceled {
Copy link
Contributor

Choose a reason for hiding this comment

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

use errors.Cause to unwrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I follow the code in br/pkg/lightning/log/log.go:181.

Copy link
Contributor

Choose a reason for hiding this comment

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

no harm to do it, in case somewhere wrap it, ok to leave it

logutil.Logger(s.logCtx).Info("met context canceled in grpc", zap.Error(err))
} else if common.IsRetryableError(err) || isRetryableError(err) {
logutil.Logger(s.logCtx).Warn("met retryable error", zap.Error(err))
} else if errors.Cause(err) != context.Canceled {
Expand Down
20 changes: 18 additions & 2 deletions pkg/disttask/framework/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/pingcap/tidb/pkg/disttask/framework/proto"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var (
Expand Down Expand Up @@ -206,6 +208,21 @@ func TestSchedulerRun(t *testing.T) {
err = scheduler.Run(runCtx, task)
require.EqualError(t, err, context.Canceled.Error())

// 8. grpc cancel
mockSubtaskTable.EXPECT().GetSubtasksInStates("id", taskID, proto.StepOne,
unfinishedNormalSubtaskStates...).Return([]*proto.Subtask{{
ID: 2, Type: tp, Step: proto.StepOne, State: proto.TaskStatePending}}, nil)
mockSubtaskExecutor.EXPECT().Init(gomock.Any()).Return(nil)
mockSubtaskTable.EXPECT().GetFirstSubtaskInStates("id", taskID, proto.StepOne,
unfinishedNormalSubtaskStates...).Return(&proto.Subtask{
ID: 1, Type: tp, Step: proto.StepOne, State: proto.TaskStatePending}, nil)
mockSubtaskTable.EXPECT().StartSubtask(taskID).Return(nil)
grpcErr := status.Error(codes.Canceled, "test cancel")
mockSubtaskExecutor.EXPECT().RunSubtask(gomock.Any(), gomock.Any()).Return(grpcErr)
mockSubtaskExecutor.EXPECT().Cleanup(gomock.Any()).Return(nil)
err = scheduler.Run(runCtx, task)
require.EqualError(t, err, grpcErr.Error())

runCancel()
}

Expand Down Expand Up @@ -329,7 +346,7 @@ func TestScheduler(t *testing.T) {
mockSubtaskExecutor := mockexecute.NewMockSubtaskExecutor(ctrl)
mockExtension := mock.NewMockExtension(ctrl)
mockSubtaskTable.EXPECT().IsSchedulerCanceled(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes()

mockSubtaskTable.EXPECT().UpdateSubtaskStateAndError(taskID, proto.TaskStateFailed, gomock.Any()).Return(nil)
mockExtension.EXPECT().GetSubtaskExecutor(gomock.Any(), gomock.Any(), gomock.Any()).Return(mockSubtaskExecutor, nil).AnyTimes()

scheduler := NewBaseScheduler(ctx, "id", 1, mockSubtaskTable)
Expand All @@ -346,7 +363,6 @@ func TestScheduler(t *testing.T) {
ID: 1, Type: tp, Step: proto.StepOne, State: proto.TaskStatePending}, nil)
mockSubtaskTable.EXPECT().StartSubtask(taskID).Return(nil)
mockSubtaskExecutor.EXPECT().RunSubtask(gomock.Any(), gomock.Any()).Return(runSubtaskErr)
mockSubtaskTable.EXPECT().UpdateSubtaskStateAndError(taskID, proto.TaskStateFailed, gomock.Any()).Return(nil)
mockSubtaskExecutor.EXPECT().Cleanup(gomock.Any()).Return(nil)
err := scheduler.run(runCtx, &proto.Task{Step: proto.StepOne, Type: tp, ID: taskID, Concurrency: concurrency})
require.EqualError(t, err, runSubtaskErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion tests/realtikvtest/addindextest3/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "addindextest3_test",
timeout = "short",
timeout = "long",
srcs = [
"main_test.go",
"operator_test.go",
Expand Down
2 changes: 1 addition & 1 deletion tests/realtikvtest/addindextest4/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "addindextest4_test",
timeout = "short",
timeout = "long",
srcs = [
"ingest_test.go",
"main_test.go",
Expand Down