当前位置: 首页>>代码示例>>Golang>>正文


Golang RangeDescriptor.GetReplicaDescriptor方法代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/roachpb.RangeDescriptor.GetReplicaDescriptor方法的典型用法代码示例。如果您正苦于以下问题:Golang RangeDescriptor.GetReplicaDescriptor方法的具体用法?Golang RangeDescriptor.GetReplicaDescriptor怎么用?Golang RangeDescriptor.GetReplicaDescriptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/cockroachdb/cockroach/roachpb.RangeDescriptor的用法示例。


在下文中一共展示了RangeDescriptor.GetReplicaDescriptor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TransferRangeLease

// TransferRangeLease transfers the lease for a range from whoever has it to
// a particular store. That store must already have a replica of the range. If
// that replica already has the (active) lease, this method is a no-op.
func (tc *TestCluster) TransferRangeLease(
	rangeDesc *roachpb.RangeDescriptor, dest ReplicationTarget,
) error {
	destReplicaDesc, ok := rangeDesc.GetReplicaDescriptor(dest.StoreID)
	if !ok {
		log.Fatalf("Couldn't find store %d in range %+v", dest.StoreID, rangeDesc)
	}

	leaseHolderDesc, err := tc.FindRangeLeaseHolder(rangeDesc,
		&ReplicationTarget{
			NodeID:  destReplicaDesc.NodeID,
			StoreID: destReplicaDesc.StoreID,
		})
	if err != nil {
		return err
	}
	if leaseHolderDesc.StoreID == dest.StoreID {
		// The intended replica already has the lease. Nothing to do.
		return nil
	}
	oldStore, err := tc.findMemberStore(leaseHolderDesc.StoreID)
	if err != nil {
		return err
	}
	oldReplica, err := oldStore.GetReplica(rangeDesc.RangeID)
	if err != nil {
		return err
	}
	// Ask the lease holder to transfer the lease.
	if err := oldReplica.AdminTransferLease(destReplicaDesc); err != nil {
		return err
	}
	return nil
}
开发者ID:YuleiXiao,项目名称:cockroach,代码行数:37,代码来源:testcluster.go

示例2: FindRangeLeaseHolder

// FindRangeLeaseHolder returns the current lease holder for the given range. If
// there is no lease at the time of the call, a replica is gets one as a
// side-effect of calling this; if hint is not nil, that replica will be the
// one.
//
// One of the Stores in the cluster is used as a Sender to send a dummy read
// command, which will either result in success (if a replica on that Node has
// the lease), in a NotLeaseHolderError pointing to the current lease holder (if
// there is an active lease), or in the replica on that store acquiring the
// lease (if there isn't an active lease).
// If an active lease existed for the range, it's extended as a side-effect.
func (tc *TestCluster) FindRangeLeaseHolder(
	rangeDesc *roachpb.RangeDescriptor,
	hint *ReplicationTarget,
) (ReplicationTarget, error) {
	var hintReplicaDesc roachpb.ReplicaDescriptor
	if hint != nil {
		var ok bool
		if hintReplicaDesc, ok = rangeDesc.GetReplicaDescriptor(hint.StoreID); !ok {
			return ReplicationTarget{}, errors.Errorf(
				"bad hint; store doesn't have a replica of the range")
		}
	} else {
		hint = &ReplicationTarget{
			NodeID:  rangeDesc.Replicas[0].NodeID,
			StoreID: rangeDesc.Replicas[0].StoreID}
		hintReplicaDesc = rangeDesc.Replicas[0]
	}
	// TODO(andrei): Using a dummy GetRequest for the purpose of figuring out the
	// lease holder is a hack. Instead, we should have a dedicate admin command.
	getReq := roachpb.GetRequest{
		Span: roachpb.Span{
			Key: rangeDesc.StartKey.AsRawKey(),
		},
	}

	store, err := tc.findMemberStore(hint.StoreID)
	if err != nil {
		return ReplicationTarget{}, err
	}
	_, pErr := client.SendWrappedWith(
		store, nil,
		roachpb.Header{RangeID: rangeDesc.RangeID, Replica: hintReplicaDesc},
		&getReq)
	if pErr != nil {
		if nle, ok := pErr.GetDetail().(*roachpb.NotLeaseHolderError); ok {
			if nle.LeaseHolder == nil {
				return ReplicationTarget{}, errors.Errorf(
					"unexpected NotLeaseHolderError with lease holder unknown")
			}
			return ReplicationTarget{
				NodeID: nle.LeaseHolder.NodeID, StoreID: nle.LeaseHolder.StoreID}, nil
		}
		return ReplicationTarget{}, pErr.GoError()
	}
	// The replica we sent the request to either was already or just became
	// the lease holder.
	return *hint, nil
}
开发者ID:yangxuanjia,项目名称:cockroach,代码行数:59,代码来源:testcluster.go


注:本文中的github.com/cockroachdb/cockroach/roachpb.RangeDescriptor.GetReplicaDescriptor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。