github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/mock-tikv/pd.go (about) 1 // Copyright 2016 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package mocktikv 15 16 import ( 17 "sync" 18 "time" 19 20 "github.com/insionng/yougam/libraries/juju/errors" 21 "github.com/insionng/yougam/libraries/pingcap/kvproto/pkg/metapb" 22 "github.com/insionng/yougam/libraries/pingcap/pd/pd-client" 23 ) 24 25 // Use global variables to prevent pdClients from creating duplicate timestamps. 26 var ( 27 tsMu sync.Mutex 28 physicalTS int64 29 logicalTS int64 30 ) 31 32 type pdClient struct { 33 cluster *Cluster 34 } 35 36 // NewPDClient creates a mock pd.Client that uses local timestamp and meta data 37 // from a Cluster. 38 func NewPDClient(cluster *Cluster) pd.Client { 39 return &pdClient{ 40 cluster: cluster, 41 } 42 } 43 44 func (c *pdClient) GetTS() (int64, int64, error) { 45 tsMu.Lock() 46 defer tsMu.Unlock() 47 48 ts := time.Now().UnixNano() / int64(time.Millisecond) 49 if physicalTS >= ts { 50 logicalTS++ 51 } else { 52 physicalTS = ts 53 logicalTS = 0 54 } 55 return physicalTS, logicalTS, nil 56 } 57 58 func (c *pdClient) GetRegion(key []byte) (*metapb.Region, error) { 59 region := c.cluster.GetRegionByKey(key) 60 if region == nil { 61 return nil, errors.New("not found") 62 } 63 return region, nil 64 } 65 66 func (c *pdClient) GetStore(storeID uint64) (*metapb.Store, error) { 67 store := c.cluster.GetStore(storeID) 68 if store == nil { 69 return nil, errors.New("not found") 70 } 71 return store, nil 72 } 73 74 func (c *pdClient) Close() { 75 }