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: make task executor onerror log skip one more level of stack. #56618

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion pkg/disttask/framework/taskexecutor/task_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func (e *BaseTaskExecutor) onError(err error) {
return
}
err = errors.Trace(err)
e.logger.Error("onError", zap.Error(err), zap.Stack("stack"))
e.logger.Error("onError", zap.Error(err), zap.StackSkip("stack", 1))
e.mu.Lock()
defer e.mu.Unlock()

Expand Down
35 changes: 34 additions & 1 deletion pkg/disttask/framework/taskexecutor/task_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ package taskexecutor

import (
"context"
"strings"
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/disttask/framework/mock"
"github.com/pingcap/tidb/pkg/disttask/framework/mock/execute"
mockexecute "github.com/pingcap/tidb/pkg/disttask/framework/mock/execute"
"github.com/pingcap/tidb/pkg/disttask/framework/proto"
"github.com/pingcap/tidb/pkg/disttask/framework/storage"
"github.com/pingcap/tidb/pkg/disttask/framework/taskexecutor/execute"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -546,3 +549,33 @@ func TestInject(t *testing.T) {
got := e.GetResource()
require.Equal(t, r, got)
}

func callOnError(taskExecutor *BaseTaskExecutor) {
taskExecutor.onError(errors.Errorf("mock error"))
}

func TestExecutorOnErrorLog(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockSubtaskTable := mock.NewMockTaskTable(ctrl)
mockExtension := mock.NewMockExtension(ctrl)

ctx := context.Background()
task := &proto.Task{TaskBase: proto.TaskBase{Step: proto.StepOne, Type: "type", ID: 1, Concurrency: 1}}
taskExecutor := NewBaseTaskExecutor(ctx, "tidb1", task, mockSubtaskTable)
taskExecutor.Extension = mockExtension
lance6716 marked this conversation as resolved.
Show resolved Hide resolved

observedZapCore, observedLogs := observer.New(zap.ErrorLevel)
observedLogger := zap.New(observedZapCore)
taskExecutor.logger = observedLogger

callOnError(taskExecutor)
require.GreaterOrEqual(t, observedLogs.Len(), 1)
errLog := observedLogs.All()[0]
stack := errLog.ContextMap()["stack"]
require.IsType(t, "", stack)
stackStr := stack.(string)
require.Truef(t, strings.HasPrefix(stackStr,
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/pingcap/tidb/pkg/disttask/framework/taskexecutor.callOnError"),
"got log stack: %s", stackStr)
}