github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/jobop/backoff_manager.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 jobop 15 16 import "github.com/pingcap/tiflow/engine/pkg/clock" 17 18 // BackoffManager manages backoff of a target job set 19 type BackoffManager interface { 20 Terminate(jobID string) bool 21 Allow(jobID string) bool 22 JobOnline(jobID string) 23 JobFail(jobID string) 24 JobTerminate(jobID string) 25 } 26 27 // BackoffManagerImpl manages JobBackoff of all running or retrying jobs, it 28 // implement interface BackoffManager. 29 type BackoffManagerImpl struct { 30 jobs map[string]*JobBackoff 31 clocker clock.Clock 32 config *BackoffConfig 33 } 34 35 // NewBackoffManagerImpl creates a new backoff manager 36 func NewBackoffManagerImpl(clocker clock.Clock, config *BackoffConfig) *BackoffManagerImpl { 37 return &BackoffManagerImpl{ 38 jobs: make(map[string]*JobBackoff), 39 clocker: clocker, 40 config: config, 41 } 42 } 43 44 // Terminate checks whether this job should be terminated, terminated means 45 // job manager won't create this job any more. 46 func (m *BackoffManagerImpl) Terminate(jobID string) bool { 47 backoff, ok := m.jobs[jobID] 48 if !ok { 49 return false 50 } 51 return backoff.Terminate() 52 } 53 54 // Allow checks whether this job can be created now 55 func (m *BackoffManagerImpl) Allow(jobID string) bool { 56 backoff, ok := m.jobs[jobID] 57 if !ok { 58 return true 59 } 60 return backoff.Allow() 61 } 62 63 // JobOnline means a job is online 64 func (m *BackoffManagerImpl) JobOnline(jobID string) { 65 m.ensureJobBackoffExists(jobID) 66 m.jobs[jobID].Success() 67 } 68 69 // JobFail means a job is offline(with error) or dispatched with error 70 func (m *BackoffManagerImpl) JobFail(jobID string) { 71 m.ensureJobBackoffExists(jobID) 72 m.jobs[jobID].Fail() 73 } 74 75 // JobTerminate means a job is finished, canceled or failed 76 func (m *BackoffManagerImpl) JobTerminate(jobID string) { 77 delete(m.jobs, jobID) 78 } 79 80 func (m *BackoffManagerImpl) ensureJobBackoffExists(jobID string) { 81 if _, ok := m.jobs[jobID]; !ok { 82 m.jobs[jobID] = NewJobBackoff(jobID, m.clocker, m.config) 83 } 84 }