Skip to content

Commit

Permalink
replace
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <ihusharp@gmail.com>
  • Loading branch information
HuSharp committed Aug 27, 2024
1 parent 22b38ab commit 7b750ff
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/schedule/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,11 @@ func (o *Operator) Check(region *core.RegionInfo) OpStep {
defer func() { _ = o.CheckTimeout() }()
for step := atomic.LoadInt32(&o.currentStep); int(step) < len(o.steps); step++ {
if o.steps[int(step)].IsFinish(region) {
if atomic.CompareAndSwapInt64(&(o.stepsTime[step]), 0, time.Now().UnixNano()) {
current := time.Now()
if atomic.CompareAndSwapInt64(&(o.stepsTime[step]), 0, current.UnixNano()) {
startTime, _ := o.getCurrentTimeAndStep()
operatorStepDuration.WithLabelValues(reflect.TypeOf(o.steps[int(step)]).Name()).
Observe(time.Unix(0, o.stepsTime[step]).Sub(startTime).Seconds())
Observe(current.Sub(startTime).Seconds())
}
atomic.StoreInt32(&o.currentStep, step+1)
} else {
Expand Down
53 changes: 53 additions & 0 deletions pkg/schedule/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (
"context"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/core/constant"
Expand Down Expand Up @@ -529,3 +531,54 @@ func (suite *operatorTestSuite) TestRecord() {
suite.Equal(now, ob.FinishTime)
suite.Greater(ob.duration.Seconds(), time.Second.Seconds())
}

func TestOperatorCheckConcurrently(t *testing.T) {
re := require.New(t)
region := newTestRegion(1, 1, [2]uint64{1, 1}, [2]uint64{2, 2})
// addPeer1, transferLeader1, removePeer3
steps := []OpStep{
AddPeer{ToStore: 1, PeerID: 1},
TransferLeader{FromStore: 3, ToStore: 1},
RemovePeer{FromStore: 3},
}
op := NewTestOperator(1, &metapb.RegionEpoch{}, OpAdmin|OpLeader|OpRegion, steps...)
re.Equal(constant.Urgent, op.GetPriorityLevel())
checkSteps(re, op, steps)
op.Start()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
re.Nil(op.Check(region))
}()
}
wg.Wait()
}

func newTestRegion(regionID uint64, leaderPeer uint64, peers ...[2]uint64) *core.RegionInfo {
var (
region metapb.Region
leader *metapb.Peer
)
region.Id = regionID
for i := range peers {
peer := &metapb.Peer{
Id: peers[i][1],
StoreId: peers[i][0],
}
region.Peers = append(region.Peers, peer)
if peer.GetId() == leaderPeer {
leader = peer
}
}
regionInfo := core.NewRegionInfo(&region, leader, core.SetApproximateSize(50), core.SetApproximateKeys(50))
return regionInfo
}

func checkSteps(re *require.Assertions, op *Operator, steps []OpStep) {
re.Len(steps, op.Len())
for i := range steps {
re.Equal(steps[i], op.Step(i))
}
}

0 comments on commit 7b750ff

Please sign in to comment.