github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/mock-tikv/cluster_manipulate.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 "fmt"
    17  
    18  // BootstrapWithSingleStore initializes a Cluster with 1 Region and 1 Store.
    19  func BootstrapWithSingleStore(cluster *Cluster) (storeID, peerID, regionID uint64) {
    20  	ids := cluster.AllocIDs(3)
    21  	storeID, peerID, regionID = ids[0], ids[1], ids[2]
    22  	cluster.AddStore(storeID, fmt.Sprintf("store%d", storeID))
    23  	cluster.Bootstrap(regionID, []uint64{storeID}, []uint64{peerID}, peerID)
    24  	return
    25  }
    26  
    27  // BootstrapWithMultiStores initializes a Cluster with 1 Region and n Stores.
    28  func BootstrapWithMultiStores(cluster *Cluster, n int) (storeIDs, peerIDs []uint64, regionID uint64, leaderPeer uint64) {
    29  	storeIDs = cluster.AllocIDs(n)
    30  	peerIDs = cluster.AllocIDs(n)
    31  	leaderPeer = peerIDs[0]
    32  	regionID = cluster.AllocID()
    33  	for _, storeID := range storeIDs {
    34  		cluster.AddStore(storeID, fmt.Sprintf("store%d", storeID))
    35  	}
    36  	cluster.Bootstrap(regionID, storeIDs, peerIDs, leaderPeer)
    37  	return
    38  }
    39  
    40  // BootstrapWithMultiRegions initializes a Cluster with multiple Regions and 1
    41  // Store. The number of Regions will be len(splitKeys) + 1.
    42  func BootstrapWithMultiRegions(cluster *Cluster, splitKeys ...[]byte) (storeID uint64, regionIDs, peerIDs []uint64) {
    43  	var firstRegionID, firstPeerID uint64
    44  	storeID, firstPeerID, firstRegionID = BootstrapWithSingleStore(cluster)
    45  	regionIDs = append([]uint64{firstRegionID}, cluster.AllocIDs(len(splitKeys))...)
    46  	peerIDs = append([]uint64{firstPeerID}, cluster.AllocIDs(len(splitKeys))...)
    47  	for i, k := range splitKeys {
    48  		cluster.Split(regionIDs[i], regionIDs[i+1], k, []uint64{peerIDs[i]}, peerIDs[i])
    49  	}
    50  	return
    51  }