Repository: tikv/client-go
Pull request #305 (2021-09-13 08:11:40)

rename txnScope as readReplicaScope
Signed-off-by: yisaer <disxiaofei@163.com>

txnScope indicates tso scope and request scope, as the txnSope in request only indicates request scope. I think it's more proper to rename it as request scope in order to avoid confused meaning.


Patch:
diff --git a/tikvrpc/tikvrpc.go b/tikvrpc/tikvrpc.go
index 0b6f998..aa5d1f0 100644
--- a/tikvrpc/tikvrpc.go
+++ b/tikvrpc/tikvrpc.go
@@ -207,6 +207,8 @@ type Request struct {
 	Type CmdType
 	Req  interface{}
 	kvrpcpb.Context
+	ReadReplicaScope string
+	// remove txnScope after tidb removed txnScope
 	TxnScope        string
 	ReplicaReadType kv.ReplicaReadType // different from `kvrpcpb.Context.ReplicaRead`
 	ReplicaReadSeed *uint32            // pointer to follower read seed in snapshot/coprocessor
@@ -259,7 +261,10 @@ func (req *Request) EnableStaleRead() {
 
 // IsGlobalStaleRead checks if the request is a global stale read request.
 func (req *Request) IsGlobalStaleRead() bool {
-	return req.TxnScope == oracle.GlobalTxnScope && req.GetStaleRead()
+	return req.ReadReplicaScope == oracle.GlobalTxnScope &&
+		// remove txnScope after tidb remove it
+		req.TxnScope == oracle.GlobalTxnScope &&
+		req.GetStaleRead()
 }
 
 // IsDebugReq check whether the req is debug req.
diff --git a/txnkv/txnsnapshot/snapshot.go b/txnkv/txnsnapshot/snapshot.go
index 565a41f..07411b1 100644
--- a/txnkv/txnsnapshot/snapshot.go
+++ b/txnkv/txnsnapshot/snapshot.go
@@ -120,14 +120,14 @@ type KVSnapshot struct {
 	// It's OK as long as there are no zero-byte values in the protocol.
 	mu struct {
 		sync.RWMutex
-		hitCnt      int64
-		cached      map[string][]byte
-		cachedSize  int
-		stats       *SnapshotRuntimeStats
-		replicaRead kv.ReplicaReadType
-		taskID      uint64
-		isStaleness bool
-		txnScope    string
+		hitCnt           int64
+		cached           map[string][]byte
+		cachedSize       int
+		stats            *SnapshotRuntimeStats
+		replicaRead      kv.ReplicaReadType
+		taskID           uint64
+		isStaleness      bool
+		readReplicaScope string
 		// MatchStoreLabels indicates the labels the store should be matched
 		matchStoreLabels []*metapb.StoreLabel
 	}
@@ -353,11 +353,12 @@ func (s *KVSnapshot) batchGetSingleRegion(bo *retry.Backoffer, batch batchKeys,
 			TaskId:           s.mu.taskID,
 			ResourceGroupTag: s.resourceGroupTag,
 		})
-		txnScope := s.mu.txnScope
+		scope := s.mu.readReplicaScope
 		isStaleness := s.mu.isStaleness
 		matchStoreLabels := s.mu.matchStoreLabels
 		s.mu.RUnlock()
-		req.TxnScope = txnScope
+		req.TxnScope = scope
+		req.ReadReplicaScope = scope
 		if isStaleness {
 			req.EnableStaleRead()
 		}
@@ -521,9 +522,10 @@ func (s *KVSnapshot) get(ctx context.Context, bo *retry.Backoffer, k []byte) ([]
 		})
 	isStaleness := s.mu.isStaleness
 	matchStoreLabels := s.mu.matchStoreLabels
-	txnScope := s.mu.txnScope
+	scope := s.mu.readReplicaScope
 	s.mu.RUnlock()
-	req.TxnScope = txnScope
+	req.TxnScope = scope
+	req.ReadReplicaScope = scope
 	var ops []locate.StoreSelectorOption
 	if isStaleness {
 		req.EnableStaleRead()
@@ -683,11 +685,18 @@ func (s *KVSnapshot) SetRuntimeStats(stats *SnapshotRuntimeStats) {
 	s.mu.stats = stats
 }
 
-// SetTxnScope sets up the txn scope.
-func (s *KVSnapshot) SetTxnScope(txnScope string) {
+// SetTxnScope is same as SetReadReplicaScope, keep it in order to keep compatible for now.
+func (s *KVSnapshot) SetTxnScope(scope string) {
 	s.mu.Lock()
 	defer s.mu.Unlock()
-	s.mu.txnScope = txnScope
+	s.mu.readReplicaScope = scope
+}
+
+// SetReadReplicaScope set read replica scope
+func (s *KVSnapshot) SetReadReplicaScope(scope string) {
+	s.mu.Lock()
+	defer s.mu.Unlock()
+	s.mu.readReplicaScope = scope
 }
 
 // SetIsStatenessReadOnly indicates whether the transaction is staleness read only transaction


Test patch:
diff --git a/internal/locate/region_request3_test.go b/internal/locate/region_request3_test.go
index 51dc22e..ce249ac 100644
--- a/internal/locate/region_request3_test.go
+++ b/internal/locate/region_request3_test.go
@@ -791,6 +791,7 @@ func (s *testRegionRequestToThreeStoresSuite) TestSendReqWithReplicaSelector() {
 	s.cluster.ChangeLeader(region.Region.id, s.peerIDs[0])
 	reloadRegion()
 	req = tikvrpc.NewRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{Key: []byte("key")})
+	req.ReadReplicaScope = oracle.GlobalTxnScope
 	req.TxnScope = oracle.GlobalTxnScope
 	req.EnableStaleRead()
 	for i := 0; i < 5; i++ {