forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an automated cherry-pick of pingcap#45680
Signed-off-by: ti-chi-bot <[email protected]>
- Loading branch information
1 parent
c3f04c1
commit d57d450
Showing
14 changed files
with
2,230 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// 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. | ||
|
||
package local | ||
|
||
import ( | ||
"context" | ||
|
||
sstpb "github.com/pingcap/kvproto/pkg/import_sstpb" | ||
"github.com/pingcap/tidb/br/pkg/lightning/common" | ||
"github.com/pingcap/tidb/br/pkg/lightning/tikv" | ||
pd "github.com/tikv/pd/client" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// TiKVModeSwitcher is used to switch TiKV nodes between Import and Normal mode. | ||
type TiKVModeSwitcher interface { | ||
// ToImportMode switches all TiKV nodes to Import mode. | ||
ToImportMode(ctx context.Context, ranges ...*sstpb.Range) | ||
// ToNormalMode switches all TiKV nodes to Normal mode. | ||
ToNormalMode(ctx context.Context, ranges ...*sstpb.Range) | ||
} | ||
|
||
// TiKVModeSwitcher is used to switch TiKV nodes between Import and Normal mode. | ||
type switcher struct { | ||
tls *common.TLS | ||
pdCli pd.Client | ||
logger *zap.Logger | ||
} | ||
|
||
// NewTiKVModeSwitcher creates a new TiKVModeSwitcher. | ||
func NewTiKVModeSwitcher(tls *common.TLS, pdCli pd.Client, logger *zap.Logger) TiKVModeSwitcher { | ||
return &switcher{ | ||
tls: tls, | ||
pdCli: pdCli, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (rc *switcher) ToImportMode(ctx context.Context, ranges ...*sstpb.Range) { | ||
rc.switchTiKVMode(ctx, sstpb.SwitchMode_Import, ranges...) | ||
} | ||
|
||
func (rc *switcher) ToNormalMode(ctx context.Context, ranges ...*sstpb.Range) { | ||
rc.switchTiKVMode(ctx, sstpb.SwitchMode_Normal, ranges...) | ||
} | ||
|
||
func (rc *switcher) switchTiKVMode(ctx context.Context, mode sstpb.SwitchMode, ranges ...*sstpb.Range) { | ||
rc.logger.Info("switch tikv mode", zap.Stringer("mode", mode)) | ||
|
||
// It is fine if we miss some stores which did not switch to Import mode, | ||
// since we're running it periodically, so we exclude disconnected stores. | ||
// But it is essentially all stores be switched back to Normal mode to allow | ||
// normal operation. | ||
var minState tikv.StoreState | ||
if mode == sstpb.SwitchMode_Import { | ||
minState = tikv.StoreStateOffline | ||
} else { | ||
minState = tikv.StoreStateDisconnected | ||
} | ||
tls := rc.tls.WithHost(rc.pdCli.GetLeaderAddr()) | ||
// we ignore switch mode failure since it is not fatal. | ||
// no need log the error, it is done in kv.SwitchMode already. | ||
_ = tikv.ForAllStores( | ||
ctx, | ||
tls, | ||
minState, | ||
func(c context.Context, store *tikv.Store) error { | ||
return tikv.SwitchMode(c, tls, store.Address, mode, ranges...) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.