本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/roachpb.StoreID函数的典型用法代码示例。如果您正苦于以下问题:Golang StoreID函数的具体用法?Golang StoreID怎么用?Golang StoreID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了StoreID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestStoresVisitStores
func TestStoresVisitStores(t *testing.T) {
defer leaktest.AfterTest(t)()
ls := NewStores(log.AmbientContext{}, hlc.NewClock(hlc.UnixNano))
numStores := 10
for i := 0; i < numStores; i++ {
ls.AddStore(&Store{Ident: roachpb.StoreIdent{StoreID: roachpb.StoreID(i)}})
}
visit := make([]bool, numStores)
err := ls.VisitStores(func(s *Store) error { visit[s.Ident.StoreID] = true; return nil })
if err != nil {
t.Errorf("unexpected error on visit: %s", err.Error())
}
for i, visited := range visit {
if !visited {
t.Errorf("store %d was not visited", i)
}
}
errBoom := errors.New("boom")
if err := ls.VisitStores(func(s *Store) error {
return errBoom
}); err != errBoom {
t.Errorf("got unexpected error %v", err)
}
}
示例2: createReplicaSlice
func createReplicaSlice() ReplicaSlice {
rs := ReplicaSlice(nil)
for i := 0; i < 5; i++ {
rs = append(rs, ReplicaInfo{ReplicaDescriptor: roachpb.ReplicaDescriptor{StoreID: roachpb.StoreID(i + 1)}})
}
return rs
}
示例3: TestStorePoolGetStoreDetails
func TestStorePoolGetStoreDetails(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp := createTestStorePool(TestTimeUntilStoreDeadOff, false /* deterministic */)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(uniqueStore, t)
sp.mu.Lock()
defer sp.mu.Unlock()
if detail := sp.getStoreDetailLocked(roachpb.StoreID(1)); detail.dead {
t.Errorf("Present storeDetail came back as dead, expected it to be alive. %+v", detail)
}
if detail := sp.getStoreDetailLocked(roachpb.StoreID(2)); detail.dead {
t.Errorf("Absent storeDetail came back as dead, expected it to be alive. %+v", detail)
}
}
示例4: TestStorePoolGetStoreDetails
func TestStorePoolGetStoreDetails(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp, _ := createTestStorePool(
TestTimeUntilStoreDead, false /* deterministic */, false /* defaultNodeLiveness */)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(uniqueStore, t)
sp.mu.Lock()
defer sp.mu.Unlock()
if detail := sp.getStoreDetailLocked(roachpb.StoreID(1)); detail.desc != nil {
t.Errorf("unexpected fetched store ID 1: %+v", detail.desc)
}
if detail := sp.getStoreDetailLocked(roachpb.StoreID(2)); detail.desc == nil {
t.Errorf("failed to fetch store ID 2")
}
}
示例5: allocateStoreIDs
// allocateStoreIDs increments the store id generator key for the
// specified node to allocate "inc" new, unique store ids. The
// first ID in a contiguous range is returned on success.
func allocateStoreIDs(
ctx context.Context, nodeID roachpb.NodeID, inc int64, db *client.DB,
) (roachpb.StoreID, error) {
r, err := db.Inc(ctx, keys.StoreIDGenerator, inc)
if err != nil {
return 0, errors.Errorf("unable to allocate %d store IDs for node %d: %s", inc, nodeID, err)
}
return roachpb.StoreID(r.ValueInt() - inc + 1), nil
}
示例6: getRemoveTarget
// getRemoveTarget queries the allocator for the store that contains a replica
// that can be removed.
func (r *Range) getRemoveTarget() (roachpb.StoreID, error) {
// Pass in an invalid store ID since we don't consider range leases as part
// of the simulator.
removeStore, err := r.allocator.RemoveTarget(r.desc.Replicas, roachpb.StoreID(-1))
if err != nil {
return 0, err
}
return removeStore.StoreID, nil
}
示例7: GetFirstStoreID
// GetFirstStoreID is a utility function returning the StoreID of the first
// store on this node.
func (ts *TestServer) GetFirstStoreID() roachpb.StoreID {
firstStoreID := roachpb.StoreID(-1)
err := ts.Stores().VisitStores(func(s *storage.Store) error {
if firstStoreID == -1 {
firstStoreID = s.Ident.StoreID
}
return nil
})
if err != nil {
panic(err)
}
return firstStoreID
}
示例8: TestBookieReserveMaxBytes
// TestBookieReserveMaxBytes ensures that over-booking doesn't occur when trying
// to reserve more bytes than maxReservedBytes.
func TestBookieReserveMaxBytes(t *testing.T) {
defer leaktest.AfterTest(t)()
previousReservedBytes := 10
stopper, _, b := createTestBookie(time.Hour, previousReservedBytes*2, int64(previousReservedBytes))
defer stopper.Stop()
// Load up reservations with a size of 1 each.
for i := 1; i <= previousReservedBytes; i++ {
req := ReservationRequest{
StoreRequestHeader: StoreRequestHeader{
StoreID: roachpb.StoreID(i),
NodeID: roachpb.NodeID(i),
},
RangeID: roachpb.RangeID(i),
RangeSize: 1,
}
if !b.Reserve(context.Background(), req, nil).Reserved {
t.Errorf("%d: could not add reservation", i)
}
verifyBookie(t, b, i, i, int64(i))
}
overbookedReq := ReservationRequest{
StoreRequestHeader: StoreRequestHeader{
StoreID: roachpb.StoreID(previousReservedBytes + 1),
NodeID: roachpb.NodeID(previousReservedBytes + 1),
},
RangeID: roachpb.RangeID(previousReservedBytes + 1),
RangeSize: 1,
}
if b.Reserve(context.Background(), overbookedReq, nil).Reserved {
t.Errorf("expected reservation to fail due to too many already existing reservations, but it succeeded")
}
// The same numbers from the last call to verifyBookie.
verifyBookie(t, b, previousReservedBytes, previousReservedBytes, int64(previousReservedBytes))
}
示例9: TestStoresRemoveStore
func TestStoresRemoveStore(t *testing.T) {
defer leaktest.AfterTest(t)()
ls := NewStores(log.AmbientContext{}, hlc.NewClock(hlc.UnixNano))
storeID := roachpb.StoreID(89)
ls.AddStore(&Store{Ident: roachpb.StoreIdent{StoreID: storeID}})
ls.RemoveStore(&Store{Ident: roachpb.StoreIdent{StoreID: storeID}})
if ls.HasStore(storeID) {
t.Errorf("expted local sender to remove storeID=%d", storeID)
}
}
示例10: TestStoresGetStoreCount
func TestStoresGetStoreCount(t *testing.T) {
defer leaktest.AfterTest(t)()
ls := NewStores(log.AmbientContext{}, hlc.NewClock(hlc.UnixNano))
if ls.GetStoreCount() != 0 {
t.Errorf("expected 0 stores in new local sender")
}
expectedCount := 10
for i := 0; i < expectedCount; i++ {
ls.AddStore(&Store{Ident: roachpb.StoreIdent{StoreID: roachpb.StoreID(i)}})
}
if count := ls.GetStoreCount(); count != expectedCount {
t.Errorf("expected store count to be %d but was %d", expectedCount, count)
}
}
示例11: TestRuleSolver
// TODO(bram): This test suite is not even close to exhaustive. The scores are
// not checked and each rule should have many more test cases. Also add a
// corrupt replica test and remove the 0 range ID used when calling
// getStoreList.
func TestRuleSolver(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, _, _, storePool := createTestStorePool(
TestTimeUntilStoreDeadOff,
/* deterministic */ false,
)
defer stopper.Stop()
storeUSa15 := roachpb.StoreID(1) // us-a-1-5
storeUSa1 := roachpb.StoreID(2) // us-a-1
storeUSb := roachpb.StoreID(3) // us-b
storeDead := roachpb.StoreID(4)
storeEurope := roachpb.StoreID(5) // eur-a-1-5
mockStorePool(storePool, []roachpb.StoreID{storeUSa15, storeUSa1, storeUSb, storeEurope}, []roachpb.StoreID{storeDead}, nil)
// tierSetup returns a tier struct constructed using the passed in values.
// If any value is an empty string, it is not included.
tierSetup := func(datacenter, floor, rack, slot string) []roachpb.Tier {
var tiers []roachpb.Tier
if datacenter != "" {
tiers = append(tiers, roachpb.Tier{Key: "datacenter", Value: datacenter})
}
if floor != "" {
tiers = append(tiers, roachpb.Tier{Key: "floor", Value: floor})
}
if rack != "" {
tiers = append(tiers, roachpb.Tier{Key: "rack", Value: rack})
}
if slot != "" {
tiers = append(tiers, roachpb.Tier{Key: "slot", Value: slot})
}
return tiers
}
// capacitySetup returns a store capacity in which the total capacity is
// always 100 and available and range count are passed in.
capacitySetup := func(available int64, rangeCount int32) roachpb.StoreCapacity {
return roachpb.StoreCapacity{
Capacity: 100,
Available: available,
RangeCount: rangeCount,
}
}
storePool.mu.Lock()
storePool.mu.storeDetails[storeUSa15].desc.Attrs.Attrs = []string{"a"}
storePool.mu.storeDetails[storeUSa15].desc.Node.Locality.Tiers = tierSetup("us", "a", "1", "5")
storePool.mu.storeDetails[storeUSa15].desc.Capacity = capacitySetup(1, 99)
storePool.mu.nodeLocalities[roachpb.NodeID(storeUSa15)] = storePool.mu.storeDetails[storeUSa15].desc.Node.Locality
storePool.mu.storeDetails[storeUSa1].desc.Attrs.Attrs = []string{"a", "b"}
storePool.mu.storeDetails[storeUSa1].desc.Node.Locality.Tiers = tierSetup("us", "a", "1", "")
storePool.mu.storeDetails[storeUSa1].desc.Capacity = capacitySetup(100, 0)
storePool.mu.nodeLocalities[roachpb.NodeID(storeUSa1)] = storePool.mu.storeDetails[storeUSa1].desc.Node.Locality
storePool.mu.storeDetails[storeUSb].desc.Attrs.Attrs = []string{"a", "b", "c"}
storePool.mu.storeDetails[storeUSb].desc.Node.Locality.Tiers = tierSetup("us", "b", "", "")
storePool.mu.storeDetails[storeUSb].desc.Capacity = capacitySetup(50, 50)
storePool.mu.nodeLocalities[roachpb.NodeID(storeUSb)] = storePool.mu.storeDetails[storeUSb].desc.Node.Locality
storePool.mu.storeDetails[storeEurope].desc.Node.Locality.Tiers = tierSetup("eur", "a", "1", "5")
storePool.mu.storeDetails[storeEurope].desc.Capacity = capacitySetup(60, 40)
storePool.mu.nodeLocalities[roachpb.NodeID(storeEurope)] = storePool.mu.storeDetails[storeEurope].desc.Node.Locality
storePool.mu.Unlock()
testCases := []struct {
name string
rule rule
c config.Constraints
existing []roachpb.ReplicaDescriptor
expected []roachpb.StoreID
}{
{
name: "no constraints or rules",
expected: []roachpb.StoreID{storeUSa15, storeUSa1, storeUSb, storeEurope},
},
{
name: "white list rule",
rule: func(state solveState) (float64, bool) {
switch state.store.StoreID {
case storeUSa15:
return 0, true
case storeUSb:
return 1, true
default:
return 0, false
}
},
expected: []roachpb.StoreID{storeUSb, storeUSa15},
},
{
name: "ruleReplicasUniqueNodes - 2 available nodes",
//.........这里部分代码省略.........
示例12: TestSendAndReceive
func TestSendAndReceive(t *testing.T) {
defer leaktest.AfterTest(t)()
rttc := newRaftTransportTestContext(t)
defer rttc.Stop()
// Create several servers, each of which has two stores (A raft
// node ID addresses a store). Node 1 has stores 1 and 2, node 2 has
// stores 3 and 4, etc.
//
// We suppose that range 1 is replicated across the odd-numbered
// stores in reverse order to ensure that the various IDs are not
// equal: replica 1 is store 5, replica 2 is store 3, and replica 3
// is store 1.
const numNodes = 3
const storesPerNode = 2
nextNodeID := roachpb.NodeID(2)
nextStoreID := roachpb.StoreID(2)
// Per-node state.
transports := map[roachpb.NodeID]*storage.RaftTransport{}
// Per-store state.
storeNodes := map[roachpb.StoreID]roachpb.NodeID{}
channels := map[roachpb.StoreID]channelServer{}
replicaIDs := map[roachpb.StoreID]roachpb.ReplicaID{
1: 3,
3: 2,
5: 1,
}
messageTypes := map[raftpb.MessageType]struct{}{
raftpb.MsgHeartbeat: {},
}
for nodeIndex := 0; nodeIndex < numNodes; nodeIndex++ {
nodeID := nextNodeID
nextNodeID++
transports[nodeID] = rttc.AddNode(nodeID)
for storeIndex := 0; storeIndex < storesPerNode; storeIndex++ {
storeID := nextStoreID
nextStoreID++
storeNodes[storeID] = nodeID
channels[storeID] = rttc.ListenStore(nodeID, storeID)
}
}
messageTypeCounts := make(map[roachpb.StoreID]map[raftpb.MessageType]int)
// Each store sends one snapshot and one heartbeat to each store, including
// itself.
for toStoreID, toNodeID := range storeNodes {
if _, ok := messageTypeCounts[toStoreID]; !ok {
messageTypeCounts[toStoreID] = make(map[raftpb.MessageType]int)
}
for fromStoreID, fromNodeID := range storeNodes {
baseReq := storage.RaftMessageRequest{
RangeID: 1,
Message: raftpb.Message{
From: uint64(fromStoreID),
To: uint64(toStoreID),
},
FromReplica: roachpb.ReplicaDescriptor{
NodeID: fromNodeID,
StoreID: fromStoreID,
},
ToReplica: roachpb.ReplicaDescriptor{
NodeID: toNodeID,
StoreID: toStoreID,
},
}
for messageType := range messageTypes {
req := baseReq
req.Message.Type = messageType
if !transports[fromNodeID].SendAsync(&req) {
t.Errorf("unable to send %s from %d to %d", req.Message.Type, fromNodeID, toNodeID)
}
messageTypeCounts[toStoreID][req.Message.Type]++
}
}
}
// Read all the messages from the channels. Note that the transport
// does not guarantee in-order delivery between independent
// transports, so we just verify that the right number of messages
// end up in each channel.
for toStoreID := range storeNodes {
for len(messageTypeCounts[toStoreID]) > 0 {
req := <-channels[toStoreID].ch
if req.Message.To != uint64(toStoreID) {
t.Errorf("got unexpected message %v on channel %d", req, toStoreID)
}
if typeCounts, ok := messageTypeCounts[toStoreID]; ok {
if _, ok := typeCounts[req.Message.Type]; ok {
//.........这里部分代码省略.........
示例13: TestBookieReserve
// TestBookieReserve ensures that you can never have more than one reservation
// for a specific rangeID at a time, and that both `Reserve` and `Fill` function
// correctly.
func TestBookieReserve(t *testing.T) {
defer leaktest.AfterTest(t)()
b := createTestBookie(5, defaultMaxReservedBytes)
testCases := []struct {
rangeID int
reserve bool // true for reserve, false for fill
expSuc bool // is the operation expected to succeed
expOut int // expected number of reserved replicas
expBytes int64 // expected number of bytes being reserved
deadReplicas []roachpb.ReplicaIdent // dead replicas that we should not reserve over
}{
{rangeID: 1, reserve: true, expSuc: true, expOut: 1, expBytes: 1},
{rangeID: 1, reserve: true, expSuc: false, expOut: 1, expBytes: 1},
{rangeID: 1, reserve: false, expSuc: true, expOut: 0, expBytes: 0},
{rangeID: 1, reserve: false, expSuc: false, expOut: 0, expBytes: 0},
{rangeID: 2, reserve: true, expSuc: true, expOut: 1, expBytes: 2},
{rangeID: 3, reserve: true, expSuc: true, expOut: 2, expBytes: 5},
{rangeID: 1, reserve: true, expSuc: true, expOut: 3, expBytes: 6},
{rangeID: 2, reserve: true, expSuc: false, expOut: 3, expBytes: 6},
{rangeID: 2, reserve: false, expSuc: true, expOut: 2, expBytes: 4},
{rangeID: 2, reserve: false, expSuc: false, expOut: 2, expBytes: 4},
{rangeID: 3, reserve: false, expSuc: true, expOut: 1, expBytes: 1},
{rangeID: 1, reserve: false, expSuc: true, expOut: 0, expBytes: 0},
{rangeID: 2, reserve: false, expSuc: false, expOut: 0, expBytes: 0},
{rangeID: 0, reserve: true, expSuc: false, expOut: 0, expBytes: 0, deadReplicas: []roachpb.ReplicaIdent{{RangeID: 0}}},
{rangeID: 0, reserve: true, expSuc: true, expOut: 1, expBytes: 0, deadReplicas: []roachpb.ReplicaIdent{{RangeID: 1}}},
{rangeID: 0, reserve: false, expSuc: true, expOut: 0, expBytes: 0},
}
ctx := context.Background()
for i, testCase := range testCases {
if testCase.reserve {
// Try to reserve the range.
req := reservationRequest{
StoreRequestHeader: StoreRequestHeader{
StoreID: roachpb.StoreID(i),
NodeID: roachpb.NodeID(i),
},
RangeID: roachpb.RangeID(testCase.rangeID),
RangeSize: int64(testCase.rangeID),
}
if resp := b.Reserve(ctx, req, testCase.deadReplicas); resp.Reserved != testCase.expSuc {
if testCase.expSuc {
t.Errorf("%d: expected a successful reservation, was rejected", i)
} else {
t.Errorf("%d: expected no reservation, but it was accepted", i)
}
}
} else {
// Fill the reservation.
if filled := b.Fill(ctx, roachpb.RangeID(testCase.rangeID)); filled != testCase.expSuc {
if testCase.expSuc {
t.Errorf("%d: expected a successful filled reservation, was rejected", i)
} else {
t.Errorf("%d: expected no reservation to be filled, but it was accepted", i)
}
}
}
verifyBookie(t, b, testCase.expOut, testCase.expBytes)
}
// Test that repeated requests with the same store and node number extend
// the timeout of the pre-existing reservation.
repeatReq := reservationRequest{
StoreRequestHeader: StoreRequestHeader{
StoreID: 100,
NodeID: 100,
},
RangeID: 100,
RangeSize: 100,
}
for i := 1; i < 10; i++ {
if !b.Reserve(context.Background(), repeatReq, nil).Reserved {
t.Errorf("%d: could not add repeated reservation", i)
}
verifyBookie(t, b, 1, 100)
}
// Test rejecting a reservation due to disk space constraints.
overfilledReq := reservationRequest{
StoreRequestHeader: StoreRequestHeader{
StoreID: 200,
NodeID: 200,
},
RangeID: 200,
RangeSize: 200,
}
b.mu.Lock()
// Set the bytes have 1 less byte free than needed by the reservation.
b.metrics.Available.Update(b.mu.size + (2 * overfilledReq.RangeSize) - 1)
b.mu.Unlock()
if b.Reserve(context.Background(), overfilledReq, nil).Reserved {
t.Errorf("expected reservation to fail due to disk space constraints, but it succeeded")
//.........这里部分代码省略.........
示例14: TestCandidateSelection
// TestCandidateSelection tests select{good,bad} and {best,worst}constraints.
func TestCandidateSelection(t *testing.T) {
defer leaktest.AfterTest(t)()
type scoreTuple struct {
constraint int
capacity int
}
genCandidates := func(scores []scoreTuple, idShift int) candidateList {
var cl candidateList
for i, score := range scores {
cl = append(cl, candidate{
store: roachpb.StoreDescriptor{
StoreID: roachpb.StoreID(i + idShift),
},
constraint: float64(score.constraint),
capacity: float64(score.capacity),
valid: true,
})
}
sort.Sort(sort.Reverse(byScore(cl)))
return cl
}
formatter := func(cl candidateList) string {
var buffer bytes.Buffer
for i, c := range cl {
if i != 0 {
buffer.WriteRune(',')
}
buffer.WriteString(fmt.Sprintf("%d:%d", int(c.constraint), int(c.capacity)))
}
return buffer.String()
}
testCases := []struct {
candidates []scoreTuple
best []scoreTuple
worst []scoreTuple
good scoreTuple
bad scoreTuple
}{
{
candidates: []scoreTuple{{0, 0}},
best: []scoreTuple{{0, 0}},
worst: []scoreTuple{{0, 0}},
good: scoreTuple{0, 0},
bad: scoreTuple{0, 0},
},
{
candidates: []scoreTuple{{0, 1}, {0, 0}},
best: []scoreTuple{{0, 1}, {0, 0}},
worst: []scoreTuple{{0, 1}, {0, 0}},
good: scoreTuple{0, 1},
bad: scoreTuple{0, 0},
},
{
candidates: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
best: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
worst: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
good: scoreTuple{0, 1},
bad: scoreTuple{0, 0},
},
{
candidates: []scoreTuple{{1, 0}, {0, 1}},
best: []scoreTuple{{1, 0}},
worst: []scoreTuple{{0, 1}},
good: scoreTuple{1, 0},
bad: scoreTuple{0, 1},
},
{
candidates: []scoreTuple{{1, 0}, {0, 2}, {0, 1}},
best: []scoreTuple{{1, 0}},
worst: []scoreTuple{{0, 2}, {0, 1}},
good: scoreTuple{1, 0},
bad: scoreTuple{0, 1},
},
{
candidates: []scoreTuple{{1, 1}, {1, 0}, {0, 2}},
best: []scoreTuple{{1, 1}, {1, 0}},
worst: []scoreTuple{{0, 2}},
good: scoreTuple{1, 1},
bad: scoreTuple{0, 2},
},
{
candidates: []scoreTuple{{1, 1}, {1, 0}, {0, 3}, {0, 2}},
best: []scoreTuple{{1, 1}, {1, 0}},
worst: []scoreTuple{{0, 3}, {0, 2}},
good: scoreTuple{1, 1},
bad: scoreTuple{0, 2},
},
}
pickResult := func(cl candidateList, storeID roachpb.StoreID) *candidate {
for _, c := range cl {
if c.store.StoreID == storeID {
return &c
}
}
return nil
//.........这里部分代码省略.........
示例15: testStoreCapacitySetup
return tiers
}
// testStoreCapacitySetup returns a store capacity in which the total capacity
// is always 100 and available and range count are passed in.
func testStoreCapacitySetup(available int64, rangeCount int32) roachpb.StoreCapacity {
return roachpb.StoreCapacity{
Capacity: 100,
Available: available,
RangeCount: rangeCount,
}
}
// This is a collection of test stores used by a suite of tests.
var (
testStoreUSa15 = roachpb.StoreID(1) // us-a-1-5
testStoreUSa1 = roachpb.StoreID(2) // us-a-1
testStoreUSb = roachpb.StoreID(3) // us-b
testStoreEurope = roachpb.StoreID(4) // eur-a-1-5
testStores = []roachpb.StoreDescriptor{
{
StoreID: testStoreUSa15,
Attrs: roachpb.Attributes{
Attrs: []string{"a"},
},
Node: roachpb.NodeDescriptor{
NodeID: roachpb.NodeID(testStoreUSa15),
Locality: roachpb.Locality{
Tiers: testStoreTierSetup("us", "a", "1", "5"),
},