-
Notifications
You must be signed in to change notification settings - Fork 222
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
min-resolved-ts: check dc label #944
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -547,27 +547,18 @@ func (s *KVStore) updateSafeTS(ctx context.Context) { | |
tikvClient := s.GetTiKVClient() | ||
wg := &sync.WaitGroup{} | ||
wg.Add(len(stores)) | ||
clusterTS, txnScopeMap, err := s.buildTxnScopeMap(ctx, stores) | ||
for _, store := range stores { | ||
storeID := store.StoreID() | ||
storeAddr := store.GetAddr() | ||
go func(ctx context.Context, wg *sync.WaitGroup, storeID uint64, storeAddr string) { | ||
defer wg.Done() | ||
|
||
var ( | ||
safeTS uint64 | ||
err error | ||
) | ||
storeIDStr := strconv.Itoa(int(storeID)) | ||
// Try to get the minimum resolved timestamp of the store from PD. | ||
if s.pdHttpClient != nil { | ||
safeTS, err = s.pdHttpClient.GetStoreMinResolvedTS(ctx, storeID) | ||
if err != nil { | ||
logutil.BgLogger().Debug("get resolved TS from PD failed", zap.Error(err), zap.Uint64("store-id", storeID)) | ||
} | ||
} | ||
safeTS := clusterTS | ||
// If getting the minimum resolved timestamp from PD failed or returned 0, try to get it from TiKV. | ||
if safeTS == 0 || err != nil { | ||
resp, err := tikvClient.SendRequest( | ||
resp, e := tikvClient.SendRequest( | ||
ctx, storeAddr, tikvrpc.NewRequest( | ||
tikvrpc.CmdStoreSafeTS, &kvrpcpb.StoreSafeTSRequest{ | ||
KeyRange: &kvrpcpb.KeyRange{ | ||
|
@@ -579,7 +570,7 @@ func (s *KVStore) updateSafeTS(ctx context.Context) { | |
}, | ||
), client.ReadTimeoutShort, | ||
) | ||
if err != nil { | ||
if e != nil { | ||
metrics.TiKVSafeTSUpdateCounter.WithLabelValues("fail", storeIDStr).Inc() | ||
logutil.BgLogger().Debug("update safeTS failed", zap.Error(err), zap.Uint64("store-id", storeID)) | ||
return | ||
|
@@ -601,18 +592,40 @@ func (s *KVStore) updateSafeTS(ctx context.Context) { | |
}(ctx, wg, storeID, storeAddr) | ||
} | ||
|
||
txnScopeMap := make(map[string][]uint64) | ||
if clusterTS != 0 && err == nil { | ||
s.minSafeTS.Store(oracle.GlobalTxnScope, clusterTS) | ||
} else { | ||
for txnScope, storeIDs := range txnScopeMap { | ||
s.updateMinSafeTS(txnScope, storeIDs) | ||
} | ||
} | ||
wg.Wait() | ||
} | ||
|
||
// build txnScopeMap and judge whether it is needed to get safeTS from PD. | ||
// - if stores label are global, return get cluster min resolved ts from pd. | ||
// - if contains dc label store, return try to get it from TiKV. | ||
func (s *KVStore) buildTxnScopeMap(ctx context.Context, stores []*Store) (safeTS uint64, txnScopeMap map[string][]uint64, err error) { | ||
isGlobal := true | ||
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. We do not need judge from the tikv labels, I think should judge from the config like the usage in tidb: 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. got it |
||
txnScopeMap = make(map[string][]uint64) | ||
for _, store := range stores { | ||
txnScopeMap[oracle.GlobalTxnScope] = append(txnScopeMap[oracle.GlobalTxnScope], store.StoreID()) | ||
|
||
if label, ok := store.GetLabelValue(DCLabelKey); ok { | ||
txnScopeMap[label] = append(txnScopeMap[label], store.StoreID()) | ||
isGlobal = false | ||
} | ||
} | ||
for txnScope, storeIDs := range txnScopeMap { | ||
s.updateMinSafeTS(txnScope, storeIDs) | ||
|
||
// Try to get the minimum resolved timestamp of the cluster from PD. | ||
if s.pdHttpClient != nil && isGlobal { | ||
safeTS, err = s.pdHttpClient.GetClusterMinResolvedTS(ctx) | ||
if err != nil { | ||
logutil.BgLogger().Debug("get resolved TS from PD failed", zap.Error(err)) | ||
} | ||
} | ||
wg.Wait() | ||
|
||
return safeTS, txnScopeMap, err | ||
} | ||
|
||
// Variables defines the variables used by TiKV storage. | ||
|
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.
Its better improve the readable like:
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.
good idea! fixed.