github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/dm/worker_test.go (about) 1 // Copyright 2022 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package dm 15 16 import ( 17 "bytes" 18 "context" 19 "testing" 20 "time" 21 22 "github.com/BurntSushi/toml" 23 dmconfig "github.com/pingcap/tiflow/dm/config" 24 "github.com/pingcap/tiflow/dm/config/dbconfig" 25 dmmaster "github.com/pingcap/tiflow/dm/master" 26 "github.com/pingcap/tiflow/dm/pb" 27 "github.com/pingcap/tiflow/engine/framework" 28 frameModel "github.com/pingcap/tiflow/engine/framework/model" 29 "github.com/pingcap/tiflow/engine/framework/registry" 30 "github.com/pingcap/tiflow/engine/jobmaster/dm/config" 31 "github.com/pingcap/tiflow/engine/jobmaster/dm/metadata" 32 dcontext "github.com/pingcap/tiflow/engine/pkg/context" 33 "github.com/pingcap/tiflow/engine/pkg/deps" 34 "github.com/pingcap/tiflow/engine/pkg/externalresource/broker" 35 kvmock "github.com/pingcap/tiflow/engine/pkg/meta/mock" 36 metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model" 37 pkgOrm "github.com/pingcap/tiflow/engine/pkg/orm" 38 "github.com/pingcap/tiflow/engine/pkg/p2p" 39 "github.com/pingcap/tiflow/pkg/errors" 40 "github.com/stretchr/testify/require" 41 "go.uber.org/dig" 42 ) 43 44 var jobTemplatePath = "../../jobmaster/dm/config/job_template.yaml" 45 46 type workerParamListForTest struct { 47 dig.Out 48 49 MessageHandlerManager p2p.MessageHandlerManager 50 MessageSender p2p.MessageSender 51 FrameMetaClient pkgOrm.Client 52 BusinessClientConn metaModel.ClientConn 53 ResourceBroker broker.Broker 54 } 55 56 // Init -> Poll -> Close 57 func TestFactory(t *testing.T) { 58 cli, err := pkgOrm.NewMockClient() 59 require.NoError(t, err) 60 dctx := dcontext.Background() 61 dp := deps.NewDeps() 62 dctx = dctx.WithDeps(dp) 63 messageHandlerManager := p2p.NewMockMessageHandlerManager() 64 depsForTest := workerParamListForTest{ 65 MessageHandlerManager: messageHandlerManager, 66 MessageSender: p2p.NewMockMessageSender(), 67 FrameMetaClient: cli, 68 BusinessClientConn: kvmock.NewMockClientConn(), 69 ResourceBroker: broker.NewBrokerForTesting("exector-id"), 70 } 71 defer depsForTest.ResourceBroker.Close() 72 require.NoError(t, dp.Provide(func() workerParamListForTest { 73 return depsForTest 74 })) 75 76 funcBackup := dmmaster.CheckAndAdjustSourceConfigFunc 77 dmmaster.CheckAndAdjustSourceConfigFunc = func(ctx context.Context, cfg *dmconfig.SourceConfig) error { return nil } 78 defer func() { 79 dmmaster.CheckAndAdjustSourceConfigFunc = funcBackup 80 }() 81 // test factory 82 var jobCfg config.JobCfg 83 require.NoError(t, jobCfg.DecodeFile(jobTemplatePath)) 84 taskCfg := jobCfg.ToTaskCfgs()["mysql-replica-01"] 85 var b bytes.Buffer 86 require.NoError(t, toml.NewEncoder(&b).Encode(taskCfg)) 87 content := b.Bytes() 88 RegisterWorker() 89 90 _, err = registry.GlobalWorkerRegistry().CreateWorker( 91 dctx, frameModel.WorkerDMDump, "worker-id", "dm-jobmaster-id", 92 content, int64(2)) 93 require.NoError(t, err) 94 _, err = registry.GlobalWorkerRegistry().CreateWorker( 95 dctx, frameModel.WorkerDMLoad, "worker-id", "dm-jobmaster-id", 96 content, int64(3)) 97 require.NoError(t, err) 98 _, err = registry.GlobalWorkerRegistry().CreateWorker( 99 dctx, frameModel.WorkerDMSync, "worker-id", "dm-jobmaster-id", 100 content, int64(4)) 101 require.NoError(t, err) 102 } 103 104 func TestWorker(t *testing.T) { 105 dctx := dcontext.Background() 106 dp := deps.NewDeps() 107 dctx = dctx.WithDeps(dp) 108 require.NoError(t, dp.Provide(func() p2p.MessageHandlerManager { 109 return p2p.NewMockMessageHandlerManager() 110 })) 111 taskCfg := &config.TaskCfg{ 112 JobCfg: config.JobCfg{ 113 TargetDB: &dbconfig.DBConfig{}, 114 Upstreams: []*config.UpstreamCfg{ 115 { 116 MySQLInstance: dmconfig.MySQLInstance{ 117 Mydumper: &dmconfig.MydumperConfig{}, 118 Loader: &dmconfig.LoaderConfig{}, 119 Syncer: &dmconfig.SyncerConfig{}, 120 SourceID: "task-id", 121 }, 122 DBCfg: &dbconfig.DBConfig{}, 123 }, 124 }, 125 }, 126 NeedExtStorage: true, 127 } 128 dmWorker, err := newDMWorker(dctx, "master-id", frameModel.WorkerDMDump, taskCfg) 129 require.NoError(t, err) 130 unitHolder := &mockUnitHolder{} 131 dmWorker.unitHolder = unitHolder 132 dmWorker.BaseWorker = framework.MockBaseWorker("worker-id", "master-id", dmWorker) 133 require.NoError(t, dmWorker.Init(context.Background())) 134 // tick 135 unitHolder.On("Stage").Return(metadata.StageRunning, nil).Twice() 136 unitHolder.On("CheckAndUpdateStatus") 137 require.NoError(t, dmWorker.Tick(context.Background())) 138 unitHolder.On("Stage").Return(metadata.StageRunning, nil).Twice() 139 require.NoError(t, dmWorker.Tick(context.Background())) 140 141 // auto resume error 142 unitHolder.On("Stage").Return(metadata.StageError, &pb.ProcessResult{Errors: []*pb.ProcessError{{ErrCode: 0}}}).Twice() 143 require.NoError(t, dmWorker.Tick(context.Background())) 144 time.Sleep(time.Second) 145 unitHolder.On("Stage").Return(metadata.StageError, &pb.ProcessResult{Errors: []*pb.ProcessError{{ErrCode: 0}}}).Once() 146 unitHolder.On("Resume").Return(errors.New("resume error")).Once() 147 require.EqualError(t, dmWorker.Tick(context.Background()), "resume error") 148 // auto resume normal 149 unitHolder.On("Stage").Return(metadata.StageError, &pb.ProcessResult{Errors: []*pb.ProcessError{{ErrCode: 0}}}).Once() 150 unitHolder.On("Stage").Return(metadata.StageRunning, nil).Once() 151 unitHolder.On("Resume").Return(nil).Once() 152 require.NoError(t, dmWorker.Tick(context.Background())) 153 154 // placeholder 155 require.NoError(t, dmWorker.OnMasterMessage(context.Background(), "", nil)) 156 157 // Finished 158 unitHolder.On("Stage").Return(metadata.StageFinished, nil).Times(3) 159 unitHolder.On("Status").Return(&pb.DumpStatus{}).Once() 160 require.True(t, errors.Is(dmWorker.Tick(context.Background()), errors.ErrWorkerFinish)) 161 162 unitHolder.AssertExpectations(t) 163 }