Skip to content

Commit

Permalink
Merge branch 'master' into fix_cluster_pinic
Browse files Browse the repository at this point in the history
  • Loading branch information
HuSharp authored Sep 8, 2023
2 parents 79ddabf + d031342 commit 9d815c1
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 71 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/petermattis/goid v0.0.0-20211229010228-4d14c490ee36 // indirect
github.com/pingcap/tipb v0.0.0-20220718022156-3e2483c20a9e // indirect
github.com/pkg/errors v0.9.1
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
26 changes: 12 additions & 14 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ const (
func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionCreateOption) *RegionInfo {
// Convert unit to MB.
// If region isn't empty and less than 1MB, use 1MB instead.
// The size of empty region will be correct by the previous RegionInfo.
regionSize := heartbeat.GetApproximateSize() / units.MiB
// Due to https://github.com/tikv/tikv/pull/11170, if region size is not initialized,
// approximate size will be zero, and region size is zero not EmptyRegionApproximateSize
if heartbeat.GetApproximateSize() > 0 && regionSize < EmptyRegionApproximateSize {
regionSize = EmptyRegionApproximateSize
}
Expand Down Expand Up @@ -193,19 +194,9 @@ func RegionFromHeartbeat(heartbeat *pdpb.RegionHeartbeatRequest, opts ...RegionC
return region
}

// Inherit inherits the buckets and region size from the parent region if bucket enabled.
// correct approximate size and buckets by the previous size if here exists a reported RegionInfo.
// See https://github.com/tikv/tikv/issues/11114
func (r *RegionInfo) Inherit(origin *RegionInfo, bucketEnable bool) {
// regionSize should not be zero if region is not empty.
if r.GetApproximateSize() == 0 {
if origin != nil {
r.approximateSize = origin.approximateSize
} else {
r.approximateSize = EmptyRegionApproximateSize
}
}
if bucketEnable && origin != nil && r.buckets == nil {
// InheritBuckets inherits the buckets from the parent region if bucket enabled.
func (r *RegionInfo) InheritBuckets(origin *RegionInfo) {
if origin != nil && r.buckets == nil {
r.buckets = origin.buckets
}
}
Expand Down Expand Up @@ -515,6 +506,13 @@ func (r *RegionInfo) GetApproximateSize() int64 {
return r.approximateSize
}

// IsEmptyRegion returns whether the region is empty.
func (r *RegionInfo) IsEmptyRegion() bool {
// When cluster resumes, the region size may be not initialized, but region heartbeat is send.
// So use `==` here.
return r.approximateSize == EmptyRegionApproximateSize
}

// GetStorePeerApproximateKeys returns the approximate keys of the peer on the specified store.
func (r *RegionInfo) GetStorePeerApproximateKeys(storeID uint64) int64 {
peer := r.GetStorePeer(storeID)
Expand Down
31 changes: 2 additions & 29 deletions pkg/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,35 +186,9 @@ func TestSortedEqual(t *testing.T) {
}
}

func TestInherit(t *testing.T) {
func TestInheritBuckets(t *testing.T) {
re := require.New(t)
// size in MB
// case for approximateSize
testCases := []struct {
originExists bool
originSize uint64
size uint64
expect uint64
}{
{false, 0, 0, 1},
{false, 0, 2, 2},
{true, 0, 2, 2},
{true, 1, 2, 2},
{true, 2, 0, 2},
}
for _, testCase := range testCases {
var origin *RegionInfo
if testCase.originExists {
origin = NewRegionInfo(&metapb.Region{Id: 100}, nil)
origin.approximateSize = int64(testCase.originSize)
}
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
r.approximateSize = int64(testCase.size)
r.Inherit(origin, false)
re.Equal(int64(testCase.expect), r.approximateSize)
}

// bucket
data := []struct {
originBuckets *metapb.Buckets
buckets *metapb.Buckets
Expand All @@ -227,12 +201,11 @@ func TestInherit(t *testing.T) {
for _, d := range data {
origin := NewRegionInfo(&metapb.Region{Id: 100}, nil, SetBuckets(d.originBuckets))
r := NewRegionInfo(&metapb.Region{Id: 100}, nil)
r.Inherit(origin, true)
r.InheritBuckets(origin)
re.Equal(d.originBuckets, r.GetBuckets())
// region will not inherit bucket keys.
if origin.GetBuckets() != nil {
newRegion := NewRegionInfo(&metapb.Region{Id: 100}, nil)
newRegion.Inherit(origin, false)
re.NotEqual(d.originBuckets, newRegion.GetBuckets())
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gc/safepoint_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"context"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/keyspacepb"
"github.com/pingcap/log"
"github.com/pkg/errors"
"github.com/tikv/pd/pkg/keyspace"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/storage/endpoint"
Expand Down
6 changes: 5 additions & 1 deletion pkg/mcs/scheduling/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (s *Server) updateAPIServerMemberLoop() {
ticker = time.NewTicker(100 * time.Millisecond)
})
defer ticker.Stop()
var curLeader uint64
for {
select {
case <-ctx.Done():
Expand All @@ -180,7 +181,10 @@ func (s *Server) updateAPIServerMemberLoop() {
log.Info("failed to get delegate client", errs.ZapError(err))
}
if s.cluster.SwitchAPIServerLeader(pdpb.NewPDClient(cc)) {
log.Info("switch leader", zap.String("leader-id", fmt.Sprintf("%x", ep.ID)), zap.String("endpoint", ep.ClientURLs[0]))
if status.Leader != curLeader {
log.Info("switch leader", zap.String("leader-id", fmt.Sprintf("%x", ep.ID)), zap.String("endpoint", ep.ClientURLs[0]))
}
curLeader = ep.ID
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/tso/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/tso"
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/tso/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"strings"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/pkg/errors"
bs "github.com/tikv/pd/pkg/basicserver"
"github.com/tikv/pd/pkg/mcs/registry"
"github.com/tikv/pd/pkg/utils/apiutil"
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/soheilhy/cmux"
Expand Down
2 changes: 1 addition & 1 deletion pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"time"

"github.com/docker/go-units"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/pkg/errors"
"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/core/storelimit"
"github.com/tikv/pd/pkg/errs"
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package config
import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pkg/errors"
"github.com/tikv/pd/pkg/core/storelimit"
"github.com/tikv/pd/pkg/utils/configutil"
"github.com/tikv/pd/pkg/utils/syncutil"
Expand Down
6 changes: 4 additions & 2 deletions pkg/statistics/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func (s *RegionStats) Observe(r *core.RegionInfo) {
approximateKeys := r.GetApproximateKeys()
approximateSize := r.GetApproximateSize()
approximateKvSize := r.GetApproximateKvSize()
if approximateSize <= core.EmptyRegionApproximateSize {
if approximateSize == core.EmptyRegionApproximateSize {
s.EmptyCount++
}
s.StorageSize += approximateSize
if !r.IsEmptyRegion() {
s.StorageSize += approximateSize
}
s.UserStorageSize += approximateKvSize
s.StorageKeys += approximateKeys
leader := r.GetLeader()
Expand Down
4 changes: 2 additions & 2 deletions pkg/statistics/region_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ func (r *RegionStatistics) Observe(region *core.RegionInfo, stores []*core.Store
return false
}(),
LearnerPeer: len(region.GetLearners()) > 0,
EmptyRegion: region.GetApproximateSize() <= core.EmptyRegionApproximateSize,
EmptyRegion: region.IsEmptyRegion(),
OversizedRegion: region.IsOversized(
int64(r.conf.GetRegionMaxSize()),
int64(r.conf.GetRegionMaxKeys()),
),
UndersizedRegion: region.NeedMerge(
int64(r.conf.GetMaxMergeRegionSize()),
int64(r.conf.GetMaxMergeRegionKeys()),
),
) && region.GetApproximateSize() >= core.EmptyRegionApproximateSize,
WitnessLeader: region.GetLeader().GetIsWitness(),
}
// Check if the region meets any of the conditions and update the corresponding info.
Expand Down
5 changes: 3 additions & 2 deletions pkg/statistics/region_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestRegionStatistics(t *testing.T) {
stores[3] = store3
r1 := &metapb.Region{Id: 1, Peers: peers, StartKey: []byte("aa"), EndKey: []byte("bb")}
r2 := &metapb.Region{Id: 2, Peers: peers[0:2], StartKey: []byte("cc"), EndKey: []byte("dd")}
region1 := core.NewRegionInfo(r1, peers[0])
region1 := core.NewRegionInfo(r1, peers[0], core.SetApproximateSize(1))
region2 := core.NewRegionInfo(r2, peers[0])
regionStats := NewRegionStatistics(nil, opt, manager)
regionStats.Observe(region1, stores)
Expand Down Expand Up @@ -97,7 +97,8 @@ func TestRegionStatistics(t *testing.T) {
re.Len(regionStats.stats[PendingPeer], 1)
re.Len(regionStats.stats[LearnerPeer], 1)
re.Len(regionStats.stats[OversizedRegion], 1)
re.Len(regionStats.stats[UndersizedRegion], 1)
re.Len(regionStats.stats[UndersizedRegion], 0)
re.Len(regionStats.stats[EmptyRegion], 0)
re.Len(regionStats.stats[OfflinePeer], 1)

region1 = region1.Clone(core.WithRemoveStorePeer(7))
Expand Down
7 changes: 4 additions & 3 deletions pkg/unsaferecovery/unsafe_recovery_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,11 @@ func (t *regionTree) insert(item *regionItem) (bool, error) {
return false, errors.Errorf("region %v shouldn't be updated twice", item.Region().GetId())
}

for _, old := range overlaps {
for _, newer := range overlaps {
log.Info("Unsafe recovery found overlap regions", logutil.ZapRedactStringer("newer-region-meta", core.RegionToHexMeta(newer.Region())), logutil.ZapRedactStringer("older-region-meta", core.RegionToHexMeta(item.Region())))
// it's ensured by the `buildUpFromReports` that peers are inserted in epoch descending order.
if old.IsEpochStale(item) {
return false, errors.Errorf("region %v's epoch shouldn't be staler than old ones %v", item, old)
if newer.IsEpochStale(item) {
return false, errors.Errorf("region %v's epoch shouldn't be staler than old ones %v", item, newer)
}
}
if len(overlaps) != 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/configutil/configutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/pkg/errors"
"github.com/pingcap/errors"
"github.com/spf13/pflag"
"github.com/tikv/pd/pkg/encryption"
"github.com/tikv/pd/pkg/utils/grpcutil"
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/grpcutil/grpcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"net/url"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pkg/errors"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/utils/logutil"
"go.etcd.io/etcd/pkg/transport"
Expand Down
4 changes: 2 additions & 2 deletions server/api/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (suite *statsTestSuite) TestRegionStats() {
statsAll := &statistics.RegionStats{
Count: 4,
EmptyCount: 1,
StorageSize: 351,
StorageSize: 350,
UserStorageSize: 291,
StorageKeys: 221,
StoreLeaderCount: map[uint64]int{1: 1, 4: 2, 5: 1},
Expand All @@ -156,7 +156,7 @@ func (suite *statsTestSuite) TestRegionStats() {
stats23 := &statistics.RegionStats{
Count: 2,
EmptyCount: 1,
StorageSize: 201,
StorageSize: 200,
UserStorageSize: 181,
StorageKeys: 151,
StoreLeaderCount: map[uint64]int{4: 1, 5: 1},
Expand Down
2 changes: 1 addition & 1 deletion server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"sync"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/pingcap/errors"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/mcs/utils"
"github.com/tikv/pd/pkg/slice"
Expand Down
4 changes: 3 additions & 1 deletion server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,9 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
if err != nil {
return err
}
region.Inherit(origin, c.GetStoreConfig().IsEnableRegionBucket())
if c.GetStoreConfig().IsEnableRegionBucket() {
region.InheritBuckets(origin)
}

if !c.isAPIServiceMode {
c.hotStat.CheckWriteAsync(statistics.NewCheckExpiredItemTask(region))
Expand Down
2 changes: 2 additions & 0 deletions tests/scheduling_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/require"
scheduling "github.com/tikv/pd/pkg/mcs/scheduling/server"
sc "github.com/tikv/pd/pkg/mcs/scheduling/server/config"
"github.com/tikv/pd/pkg/schedule/schedulers"
"github.com/tikv/pd/pkg/utils/tempurl"
"github.com/tikv/pd/pkg/utils/testutil"
)
Expand All @@ -36,6 +37,7 @@ type TestSchedulingCluster struct {

// NewTestSchedulingCluster creates a new scheduling test cluster.
func NewTestSchedulingCluster(ctx context.Context, initialServerCount int, backendEndpoints string) (tc *TestSchedulingCluster, err error) {
schedulers.Register()
tc = &TestSchedulingCluster{
ctx: ctx,
backendEndpoints: backendEndpoints,
Expand Down
34 changes: 34 additions & 0 deletions tools/pd-api-bench/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2023 TiKV Project Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ROOT_PATH := ../..
GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin
PATH := $(GO_TOOLS_BIN_PATH):$(PATH)
SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash)

install-tools:
cd $(ROOT_PATH) && $(MAKE) install-tools

static: install-tools
@ echo "gofmt ..."
@ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }'
@ echo "golangci-lint ..."
@ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners
@ echo "revive ..."
@ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./...

tidy:
@ go mod tidy
git diff go.mod go.sum | cat
git diff --quiet go.mod go.sum
Loading

0 comments on commit 9d815c1

Please sign in to comment.