本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/roachpb.NodeID函數的典型用法代碼示例。如果您正苦於以下問題:Golang NodeID函數的具體用法?Golang NodeID怎麽用?Golang NodeID使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NodeID函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestInfoStoreMostDistant
// TestInfoStoreMostDistant verifies selection of most distant node &
// associated hops.
func TestInfoStoreMostDistant(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes := []roachpb.NodeID{
roachpb.NodeID(1),
roachpb.NodeID(2),
roachpb.NodeID(3),
}
is, stopper := newTestInfoStore()
defer stopper.Stop()
// Add info from each address, with hop count equal to index+1.
for i := 0; i < len(nodes); i++ {
inf := is.newInfo(nil, time.Second)
inf.Hops = uint32(i + 1)
inf.NodeID = nodes[i]
if err := is.addInfo(fmt.Sprintf("b.%d", i), inf); err != nil {
t.Fatal(err)
}
nodeID, hops := is.mostDistant()
if nodeID != inf.NodeID {
t.Errorf("%d: expected node %d; got %d", i, inf.NodeID, nodeID)
}
if hops != inf.Hops {
t.Errorf("%d: expected node %d; got %d", i, inf.Hops, hops)
}
}
}
示例2: TestPreexistingReplicaCheck
func TestPreexistingReplicaCheck(t *testing.T) {
defer leaktest.AfterTest(t)()
var existing []roachpb.ReplicaDescriptor
for i := 2; i < 10; i += 2 {
existing = append(existing, roachpb.ReplicaDescriptor{NodeID: roachpb.NodeID(i)})
}
for i := 1; i < 10; i++ {
if e, a := i%2 != 0, preexistingReplicaCheck(roachpb.NodeID(i), existing); e != a {
t.Errorf("NodeID %d expected to be %t, got %t", i, e, a)
}
}
}
示例3: allocateNodeID
// allocateNodeID increments the node id generator key to allocate
// a new, unique node id.
func allocateNodeID(ctx context.Context, db *client.DB) (roachpb.NodeID, error) {
r, err := db.Inc(ctx, keys.NodeIDGenerator, 1)
if err != nil {
return 0, errors.Errorf("unable to allocate node ID: %s", err)
}
return roachpb.NodeID(r.ValueInt()), nil
}
示例4: TestNodeSetAsSlice
func TestNodeSetAsSlice(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes := makeNodeSet(2, metric.NewGauge(metric.Metadata{Name: ""}))
node0 := roachpb.NodeID(1)
node1 := roachpb.NodeID(2)
nodes.addNode(node0)
nodes.addNode(node1)
nodeArr := nodes.asSlice()
if len(nodeArr) != 2 {
t.Error("expected slice of length 2:", nodeArr)
}
if (nodeArr[0] != node0 && nodeArr[0] != node1) ||
(nodeArr[1] != node1 && nodeArr[1] != node0) {
t.Error("expected slice to contain both node0 and node1:", nodeArr)
}
}
示例5: TestGetNodeLocalities
func TestGetNodeLocalities(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, sp, _ := createTestStorePool(
TestTimeUntilStoreDead, false /* deterministic */, false /* defaultNodeLiveness */)
defer stopper.Stop()
sg := gossiputil.NewStoreGossiper(g)
// Creates a node with a locality with the number of tiers passed in. The
// NodeID is the same as the tier count.
createDescWithLocality := func(tierCount int) roachpb.NodeDescriptor {
nodeDescriptor := roachpb.NodeDescriptor{NodeID: roachpb.NodeID(tierCount)}
for i := 1; i <= tierCount; i++ {
value := fmt.Sprintf("%d", i)
nodeDescriptor.Locality.Tiers = append(nodeDescriptor.Locality.Tiers, roachpb.Tier{
Key: value,
Value: value,
})
}
return nodeDescriptor
}
stores := []*roachpb.StoreDescriptor{
{
StoreID: 1,
Node: createDescWithLocality(1),
},
{
StoreID: 2,
Node: createDescWithLocality(2),
},
{
StoreID: 3,
Node: createDescWithLocality(3),
},
{
StoreID: 4,
Node: createDescWithLocality(2),
},
}
sg.GossipStores(stores, t)
var existingReplicas []roachpb.ReplicaDescriptor
for _, store := range stores {
existingReplicas = append(existingReplicas, roachpb.ReplicaDescriptor{NodeID: store.Node.NodeID})
}
localities := sp.getNodeLocalities(existingReplicas)
for _, store := range stores {
locality, ok := localities[store.Node.NodeID]
if !ok {
t.Fatalf("could not find locality for node %d", store.Node.NodeID)
}
if e, a := int(store.Node.NodeID), len(locality.Tiers); e != a {
t.Fatalf("for node %d, expected %d tiers, only got %d", store.Node.NodeID, e, a)
}
}
}
示例6: addNewNodeWithStore
// addNewNodeWithStore adds new node with a single store.
func (c *Cluster) addNewNodeWithStore() {
nodeID := roachpb.NodeID(len(c.nodes))
c.nodes[nodeID] = newNode(nodeID, c.gossip)
// Only output if we're running the simulation.
if c.epoch >= 0 {
fmt.Fprintf(c.actionWriter, "%d:\tNode %d added\n", c.epoch, nodeID)
}
c.addStore(nodeID)
}
示例7: TestNodeSetFilter
func TestNodeSetFilter(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes1 := makeNodeSet(2, metric.NewGauge(metric.Metadata{Name: ""}))
node0 := roachpb.NodeID(1)
node1 := roachpb.NodeID(2)
nodes1.addNode(node0)
nodes1.addNode(node1)
nodes2 := makeNodeSet(1, metric.NewGauge(metric.Metadata{Name: ""}))
nodes2.addNode(node1)
filtered := nodes1.filter(func(a roachpb.NodeID) bool {
return !nodes2.hasNode(a)
})
if filtered.len() != 1 || filtered.hasNode(node1) || !filtered.hasNode(node0) {
t.Errorf("expected filter to leave node0: %+v", filtered)
}
}
示例8: TestNodeSetAddAndRemoveNode
func TestNodeSetAddAndRemoveNode(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes := makeNodeSet(2, metric.NewGauge(metric.Metadata{Name: ""}))
node0 := roachpb.NodeID(1)
node1 := roachpb.NodeID(2)
nodes.addNode(node0)
nodes.addNode(node1)
if !nodes.hasNode(node0) || !nodes.hasNode(node1) {
t.Error("failed to locate added nodes")
}
nodes.removeNode(node0)
if nodes.hasNode(node0) || !nodes.hasNode(node1) {
t.Error("failed to remove node0", nodes)
}
nodes.removeNode(node1)
if nodes.hasNode(node0) || nodes.hasNode(node1) {
t.Error("failed to remove node1", nodes)
}
}
示例9: TestNodeSetMaxSize
func TestNodeSetMaxSize(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes := makeNodeSet(1, metric.NewGauge(metric.Metadata{Name: ""}))
if !nodes.hasSpace() {
t.Error("set should have space")
}
nodes.addNode(roachpb.NodeID(1))
if nodes.hasSpace() {
t.Error("set should have no space")
}
}
示例10: NodeIDFromKey
// NodeIDFromKey attempts to extract a NodeID from the provided key.
// The key should have been constructed by MakeNodeIDKey.
// Returns an error if the key is not of the correct type or is not parsable.
func NodeIDFromKey(key string) (roachpb.NodeID, error) {
trimmedKey := strings.TrimPrefix(key, KeyNodeIDPrefix+separator)
if trimmedKey == key {
return 0, errors.Errorf("%q is not a NodeID Key", key)
}
nodeID, err := strconv.ParseInt(trimmedKey, 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "failed parsing NodeID from key %q", key)
}
return roachpb.NodeID(nodeID), nil
}
示例11: Start
// Start starts the test cluster by bootstrapping an in-memory store
// (defaults to maximum of 50M). The server is started, launching the
// node RPC server and all HTTP endpoints. Use the value of
// TestServer.Addr after Start() for client connections. Use Stop()
// to shutdown the server after the test completes.
func (ltc *LocalTestCluster) Start(t util.Tester, baseCtx *base.Config, initSender InitSenderFn) {
ambient := log.AmbientContext{Tracer: tracing.NewTracer()}
nc := &base.NodeIDContainer{}
ambient.AddLogTag("n", nc)
nodeID := roachpb.NodeID(1)
nodeDesc := &roachpb.NodeDescriptor{NodeID: nodeID}
ltc.tester = t
ltc.Manual = hlc.NewManualClock(0)
ltc.Clock = hlc.NewClock(ltc.Manual.UnixNano)
ltc.Stopper = stop.NewStopper()
rpcContext := rpc.NewContext(ambient, baseCtx, ltc.Clock, ltc.Stopper)
server := rpc.NewServer(rpcContext) // never started
ltc.Gossip = gossip.New(ambient, nc, rpcContext, server, nil, ltc.Stopper, metric.NewRegistry())
ltc.Eng = engine.NewInMem(roachpb.Attributes{}, 50<<20)
ltc.Stopper.AddCloser(ltc.Eng)
ltc.Stores = storage.NewStores(ambient, ltc.Clock)
ltc.Sender = initSender(nodeDesc, ambient.Tracer, ltc.Clock, ltc.Latency, ltc.Stores, ltc.Stopper,
ltc.Gossip)
if ltc.DBContext == nil {
dbCtx := client.DefaultDBContext()
ltc.DBContext = &dbCtx
}
ltc.DB = client.NewDBWithContext(ltc.Sender, *ltc.DBContext)
transport := storage.NewDummyRaftTransport()
cfg := storage.TestStoreConfig()
if ltc.RangeRetryOptions != nil {
cfg.RangeRetryOptions = *ltc.RangeRetryOptions
}
cfg.AmbientCtx = ambient
cfg.Clock = ltc.Clock
cfg.DB = ltc.DB
cfg.Gossip = ltc.Gossip
cfg.Transport = transport
cfg.MetricsSampleInterval = metric.TestSampleInterval
ltc.Store = storage.NewStore(cfg, ltc.Eng, nodeDesc)
if err := ltc.Store.Bootstrap(roachpb.StoreIdent{NodeID: nodeID, StoreID: 1}); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
ltc.Stores.AddStore(ltc.Store)
if err := ltc.Store.BootstrapRange(nil); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
if err := ltc.Store.Start(context.Background(), ltc.Stopper); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
nc.Set(context.TODO(), nodeDesc.NodeID)
if err := ltc.Gossip.SetNodeDescriptor(nodeDesc); err != nil {
t.Fatalf("unable to set node descriptor: %s", err)
}
}
示例12: parseNodeID
func (s *statusServer) parseNodeID(nodeIDParam string) (roachpb.NodeID, bool, error) {
// No parameter provided or set to local.
if len(nodeIDParam) == 0 || localRE.MatchString(nodeIDParam) {
return s.gossip.NodeID.Get(), true, nil
}
id, err := strconv.ParseInt(nodeIDParam, 10, 64)
if err != nil {
return 0, false, fmt.Errorf("node id could not be parsed: %s", err)
}
nodeID := roachpb.NodeID(id)
return nodeID, nodeID == s.gossip.NodeID.Get(), nil
}
示例13: TestNodeSetHasNode
func TestNodeSetHasNode(t *testing.T) {
defer leaktest.AfterTest(t)()
nodes := makeNodeSet(2, metric.NewGauge(metric.Metadata{Name: ""}))
node := roachpb.NodeID(1)
if nodes.hasNode(node) {
t.Error("node wasn't added and should not be valid")
}
// Add node and verify it's valid.
nodes.addNode(node)
if !nodes.hasNode(node) {
t.Error("empty node wasn't added and should not be valid")
}
}
示例14: 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))
}
示例15: node
func (t *leaseTest) node(nodeID uint32) *csql.LeaseManager {
mgr := t.nodes[nodeID]
if mgr == nil {
nc := &base.NodeIDContainer{}
nc.Set(context.TODO(), roachpb.NodeID(nodeID))
mgr = csql.NewLeaseManager(
nc, *t.kvDB,
t.server.Clock(),
t.leaseManagerTestingKnobs,
t.server.Stopper(),
&csql.MemoryMetrics{},
)
t.nodes[nodeID] = mgr
}
return mgr
}