-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keysapce: wait region split when creating keyspace #6414
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package keyspace | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strconv" | ||
"time" | ||
|
@@ -55,6 +56,9 @@ const ( | |
// Config is the interface for keyspace config. | ||
type Config interface { | ||
GetPreAlloc() []string | ||
ToWaitRegionSplit() bool | ||
GetWaitRegionSplitTimeout() time.Duration | ||
GetCheckRegionSplitInterval() time.Duration | ||
} | ||
|
||
// Manager manages keyspace related data. | ||
|
@@ -86,6 +90,8 @@ type CreateKeyspaceRequest struct { | |
Config map[string]string | ||
// CreateTime is the timestamp used to record creation time. | ||
CreateTime int64 | ||
// IsPreAlloc indicates whether the keyspace is pre-allocated when the cluster starts. | ||
IsPreAlloc bool | ||
} | ||
|
||
// NewKeyspaceManager creates a Manager of keyspace related data. | ||
|
@@ -112,7 +118,7 @@ func NewKeyspaceManager( | |
// Bootstrap saves default keyspace info. | ||
func (manager *Manager) Bootstrap() error { | ||
// Split Keyspace Region for default keyspace. | ||
if err := manager.splitKeyspaceRegion(utils.DefaultKeyspaceID); err != nil { | ||
if err := manager.splitKeyspaceRegion(utils.DefaultKeyspaceID, false); err != nil { | ||
return err | ||
} | ||
now := time.Now().Unix() | ||
|
@@ -148,6 +154,7 @@ func (manager *Manager) Bootstrap() error { | |
req := &CreateKeyspaceRequest{ | ||
Name: keyspaceName, | ||
CreateTime: now, | ||
IsPreAlloc: true, | ||
Config: config, | ||
} | ||
keyspace, err := manager.CreateKeyspace(req) | ||
|
@@ -162,6 +169,11 @@ func (manager *Manager) Bootstrap() error { | |
return nil | ||
} | ||
|
||
// UpdateConfig update keyspace manager's config. | ||
func (manager *Manager) UpdateConfig(cfg Config) { | ||
manager.config = cfg | ||
} | ||
|
||
// CreateKeyspace create a keyspace meta with given config and save it to storage. | ||
func (manager *Manager) CreateKeyspace(request *CreateKeyspaceRequest) (*keyspacepb.KeyspaceMeta, error) { | ||
// Validate purposed name's legality. | ||
|
@@ -173,8 +185,11 @@ func (manager *Manager) CreateKeyspace(request *CreateKeyspaceRequest) (*keyspac | |
if err != nil { | ||
return nil, err | ||
} | ||
// If the request to create a keyspace is pre-allocated when the PD starts, | ||
// there is no need to wait for the region split, because TiKV has not started. | ||
waitRegionSplit := !request.IsPreAlloc && manager.config.ToWaitRegionSplit() | ||
// Split keyspace region. | ||
err = manager.splitKeyspaceRegion(newID) | ||
err = manager.splitKeyspaceRegion(newID, waitRegionSplit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -252,27 +267,81 @@ func (manager *Manager) saveNewKeyspace(keyspace *keyspacepb.KeyspaceMeta) error | |
|
||
// splitKeyspaceRegion add keyspace's boundaries to region label. The corresponding | ||
// region will then be split by Coordinator's patrolRegion. | ||
func (manager *Manager) splitKeyspaceRegion(id uint32) error { | ||
func (manager *Manager) splitKeyspaceRegion(id uint32, waitRegionSplit bool) (err error) { | ||
failpoint.Inject("skipSplitRegion", func() { | ||
failpoint.Return(nil) | ||
}) | ||
|
||
start := time.Now() | ||
keyspaceRule := makeLabelRule(id) | ||
if cl, ok := manager.cluster.(interface{ GetRegionLabeler() *labeler.RegionLabeler }); ok { | ||
err := cl.GetRegionLabeler().SetLabelRule(keyspaceRule) | ||
if err != nil { | ||
log.Warn("[keyspace] failed to add region label for keyspace", | ||
zap.Uint32("keyspaceID", id), | ||
zap.Error(err), | ||
) | ||
} | ||
log.Info("[keyspace] added region label for keyspace", | ||
cl, ok := manager.cluster.(interface{ GetRegionLabeler() *labeler.RegionLabeler }) | ||
if !ok { | ||
return errors.New("cluster does not support region label") | ||
} | ||
err = cl.GetRegionLabeler().SetLabelRule(keyspaceRule) | ||
if err != nil { | ||
log.Warn("[keyspace] failed to add region label for keyspace", | ||
zap.Uint32("keyspaceID", id), | ||
zap.Any("LabelRule", keyspaceRule), | ||
zap.Error(err), | ||
) | ||
return nil | ||
return err | ||
} | ||
return errors.New("cluster does not support region label") | ||
defer func() { | ||
if err != nil { | ||
cl.GetRegionLabeler().DeleteLabelRule(keyspaceRule.ID) | ||
} | ||
}() | ||
|
||
if waitRegionSplit { | ||
ranges := keyspaceRule.Data.([]*labeler.KeyRangeRule) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check the length? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added. cc @zeminzhou |
||
rawLeftBound, rawRightBound := ranges[0].StartKey, ranges[0].EndKey | ||
txnLeftBound, txnRightBound := ranges[1].StartKey, ranges[1].EndKey | ||
|
||
ticker := time.NewTicker(manager.config.GetCheckRegionSplitInterval()) | ||
timer := time.NewTimer(manager.config.GetWaitRegionSplitTimeout()) | ||
defer func() { | ||
ticker.Stop() | ||
timer.Stop() | ||
}() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
regionsInfo := manager.cluster.GetBasicCluster().RegionsInfo | ||
region := regionsInfo.GetRegionByKey(rawLeftBound) | ||
if region == nil || !bytes.Equal(region.GetStartKey(), rawLeftBound) { | ||
continue | ||
} | ||
region = regionsInfo.GetRegionByKey(rawRightBound) | ||
if region == nil || !bytes.Equal(region.GetStartKey(), rawRightBound) { | ||
continue | ||
} | ||
region = regionsInfo.GetRegionByKey(txnLeftBound) | ||
if region == nil || !bytes.Equal(region.GetStartKey(), txnLeftBound) { | ||
continue | ||
} | ||
region = regionsInfo.GetRegionByKey(txnRightBound) | ||
if region == nil || !bytes.Equal(region.GetStartKey(), txnRightBound) { | ||
continue | ||
} | ||
case <-timer.C: | ||
log.Warn("[keyspace] wait region split timeout", | ||
zap.Uint32("keyspaceID", id), | ||
zap.Error(err), | ||
) | ||
err = ErrRegionSplitTimeout | ||
return | ||
} | ||
log.Info("[keyspace] wait reigon split successfully", zap.Uint32("keyspaceID", id)) | ||
break | ||
} | ||
} | ||
|
||
log.Info("[keyspace] added region label for keyspace", | ||
zap.Uint32("keyspaceID", id), | ||
zap.Any("LabelRule", keyspaceRule), | ||
lhy1024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
zap.Duration("takes", time.Since(start)), | ||
) | ||
return | ||
} | ||
|
||
// LoadKeyspace returns the keyspace specified by name. | ||
|
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when meeting this error it will return nil in pd master, but it returns an error in pd-cse