本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/roachpb.NewError函数的典型用法代码示例。如果您正苦于以下问题:Golang NewError函数的具体用法?Golang NewError怎么用?Golang NewError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewError函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: requestLeaseLocked
// requestLeaseLocked executes a request to obtain or extend a lease
// asynchronously and returns a channel on which the result will be posted. If
// there's already a request in progress, we join in waiting for the results of
// that request. Unless an error is returned, the obtained lease will be valid
// for a time interval containing the requested timestamp.
// If a transfer is in progress, a NotLeaseHolderError directing to the recipient is
// sent on the returned chan.
func (r *Replica) requestLeaseLocked(timestamp hlc.Timestamp) <-chan *roachpb.Error {
if r.store.TestingKnobs().LeaseRequestEvent != nil {
r.store.TestingKnobs().LeaseRequestEvent(timestamp)
}
// Propose a Raft command to get a lease for this replica.
repDesc, err := r.getReplicaDescriptorLocked()
if err != nil {
llChan := make(chan *roachpb.Error, 1)
llChan <- roachpb.NewError(err)
return llChan
}
if transferLease, ok := r.mu.pendingLeaseRequest.TransferInProgress(
repDesc.ReplicaID); ok {
llChan := make(chan *roachpb.Error, 1)
llChan <- roachpb.NewError(
newNotLeaseHolderError(&transferLease, r.store.StoreID(), r.mu.state.Desc))
return llChan
}
if r.store.IsDrainingLeases() {
// We've retired from active duty.
llChan := make(chan *roachpb.Error, 1)
llChan <- roachpb.NewError(newNotLeaseHolderError(nil, r.store.StoreID(), r.mu.state.Desc))
return llChan
}
return r.mu.pendingLeaseRequest.InitOrJoinRequest(
r, repDesc, timestamp, r.mu.state.Desc.StartKey.AsRawKey(), false /* transfer */)
}
开发者ID:veteranlu,项目名称:cockroach,代码行数:34,代码来源:replica_range_lease.go
示例2: deduceRetryEarlyExitError
func (ds *DistSender) deduceRetryEarlyExitError(ctx context.Context) *roachpb.Error {
select {
case <-ds.rpcRetryOptions.Closer:
// Typically happens during shutdown.
return roachpb.NewError(&roachpb.NodeUnavailableError{})
case <-ctx.Done():
// Happens when the client request is cancelled.
return roachpb.NewError(ctx.Err())
default:
}
return nil
}
开发者ID:knz,项目名称:cockroach,代码行数:12,代码来源:dist_sender.go
示例3: Send
// Send implements the client.Sender interface. The store is looked up from the
// store map if specified by the request; otherwise, the command is being
// executed locally, and the replica is determined via lookup through each
// store's LookupRange method. The latter path is taken only by unit tests.
func (ls *Stores) Send(
ctx context.Context, ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, *roachpb.Error) {
// If we aren't given a Replica, then a little bending over
// backwards here. This case applies exclusively to unittests.
if ba.RangeID == 0 || ba.Replica.StoreID == 0 {
rs, err := keys.Range(ba)
if err != nil {
return nil, roachpb.NewError(err)
}
rangeID, repDesc, err := ls.LookupReplica(rs.Key, rs.EndKey)
if err != nil {
return nil, roachpb.NewError(err)
}
ba.RangeID = rangeID
ba.Replica = repDesc
}
store, err := ls.GetStore(ba.Replica.StoreID)
if err != nil {
return nil, roachpb.NewError(err)
}
if ba.Txn != nil {
// For calls that read data within a txn, we keep track of timestamps
// observed from the various participating nodes' HLC clocks. If we have
// a timestamp on file for this Node which is smaller than MaxTimestamp,
// we can lower MaxTimestamp accordingly. If MaxTimestamp drops below
// OrigTimestamp, we effectively can't see uncertainty restarts any
// more.
// Note that it's not an issue if MaxTimestamp propagates back out to
// the client via a returned Transaction update - when updating a Txn
// from another, the larger MaxTimestamp wins.
if maxTS, ok := ba.Txn.GetObservedTimestamp(ba.Replica.NodeID); ok && maxTS.Less(ba.Txn.MaxTimestamp) {
// Copy-on-write to protect others we might be sharing the Txn with.
shallowTxn := *ba.Txn
// The uncertainty window is [OrigTimestamp, maxTS), so if that window
// is empty, there won't be any uncertainty restarts.
if !ba.Txn.OrigTimestamp.Less(maxTS) {
log.Event(ctx, "read has no clock uncertainty")
}
shallowTxn.MaxTimestamp.Backward(maxTS)
ba.Txn = &shallowTxn
}
}
br, pErr := store.Send(ctx, ba)
if br != nil && br.Error != nil {
panic(roachpb.ErrorUnexpectedlySet(store, br))
}
return br, pErr
}
开发者ID:jmptrader,项目名称:cockroach,代码行数:55,代码来源:stores.go
示例4: resendWithTxn
// TODO(tschottdorf): this method is somewhat awkward but unless we want to
// give this error back to the client, our options are limited. We'll have to
// run the whole thing for them, or any restart will still end up at the client
// which will not be prepared to be handed a Txn.
func (tc *TxnCoordSender) resendWithTxn(
ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, *roachpb.Error) {
ctx := tc.AnnotateCtx(context.TODO())
// Run a one-off transaction with that single command.
if log.V(1) {
log.Infof(ctx, "%s: auto-wrapping in txn and re-executing: ", ba)
}
// TODO(bdarnell): need to be able to pass other parts of DBContext
// through here.
dbCtx := client.DefaultDBContext()
dbCtx.UserPriority = ba.UserPriority
tmpDB := client.NewDBWithContext(tc, dbCtx)
var br *roachpb.BatchResponse
err := tmpDB.Txn(ctx, func(txn *client.Txn) error {
txn.SetDebugName("auto-wrap", 0)
b := txn.NewBatch()
b.Header = ba.Header
for _, arg := range ba.Requests {
req := arg.GetInner()
b.AddRawRequest(req)
}
err := txn.CommitInBatch(b)
br = b.RawResponse()
return err
})
if err != nil {
return nil, roachpb.NewError(err)
}
br.Txn = nil // hide the evidence
return br, nil
}
开发者ID:hvaara,项目名称:cockroach,代码行数:36,代码来源:txn_coord_sender.go
示例5: maybeRejectClientLocked
// maybeRejectClientLocked checks whether the (transactional) request is in a
// state that prevents it from continuing, such as the coordinator having
// considered the client abandoned, or a heartbeat having reported an error.
func (tc *TxnCoordSender) maybeRejectClientLocked(
ctx context.Context, txn roachpb.Transaction,
) *roachpb.Error {
if !txn.Writing {
return nil
}
txnMeta, ok := tc.txns[*txn.ID]
// Check whether the transaction is still tracked and has a chance of
// completing. It's possible that the coordinator learns about the
// transaction having terminated from a heartbeat, and GC queue correctness
// (along with common sense) mandates that we don't let the client
// continue.
switch {
case !ok:
log.VEventf(ctx, 2, "rejecting unknown txn: %s", txn.ID)
// TODO(spencerkimball): Could add coordinator node ID to the
// transaction session so that we can definitively return the right
// error between these possible errors. Or update the code to make an
// educated guess based on the incoming transaction timestamp.
return roachpb.NewError(errNoState)
case txnMeta.txn.Status == roachpb.ABORTED:
txn := txnMeta.txn.Clone()
tc.cleanupTxnLocked(ctx, txn)
return roachpb.NewErrorWithTxn(roachpb.NewTransactionAbortedError(),
&txn)
case txnMeta.txn.Status == roachpb.COMMITTED:
txn := txnMeta.txn.Clone()
tc.cleanupTxnLocked(ctx, txn)
return roachpb.NewErrorWithTxn(roachpb.NewTransactionStatusError(
"transaction is already committed"), &txn)
default:
return nil
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:38,代码来源:txn_coord_sender.go
示例6: Batch
// Batch implements the roachpb.InternalServer interface.
func (n *Node) Batch(
ctx context.Context, args *roachpb.BatchRequest,
) (*roachpb.BatchResponse, error) {
growStack()
ctx = n.AnnotateCtx(ctx)
br, err := n.batchInternal(ctx, args)
// We always return errors via BatchResponse.Error so structure is
// preserved; plain errors are presumed to be from the RPC
// framework and not from cockroach.
if err != nil {
if br == nil {
br = &roachpb.BatchResponse{}
}
if br.Error != nil {
log.Fatalf(
ctx, "attempting to return both a plain error (%s) and roachpb.Error (%s)", err, br.Error,
)
}
br.Error = roachpb.NewError(err)
}
return br, nil
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:26,代码来源:node.go
示例7: RangeLookup
// RangeLookup implements the RangeDescriptorDB interface.
// RangeLookup dispatches a RangeLookup request for the given metadata
// key to the replicas of the given range. Note that we allow
// inconsistent reads when doing range lookups for efficiency. Getting
// stale data is not a correctness problem but instead may
// infrequently result in additional latency as additional range
// lookups may be required. Note also that rangeLookup bypasses the
// DistSender's Send() method, so there is no error inspection and
// retry logic here; this is not an issue since the lookup performs a
// single inconsistent read only.
func (ds *DistSender) RangeLookup(
ctx context.Context, key roachpb.RKey, desc *roachpb.RangeDescriptor, useReverseScan bool,
) ([]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, *roachpb.Error) {
ba := roachpb.BatchRequest{}
ba.ReadConsistency = roachpb.INCONSISTENT
ba.Add(&roachpb.RangeLookupRequest{
Span: roachpb.Span{
// We can interpret the RKey as a Key here since it's a metadata
// lookup; those are never local.
Key: key.AsRawKey(),
},
MaxRanges: ds.rangeLookupMaxRanges,
Reverse: useReverseScan,
})
replicas := newReplicaSlice(ds.gossip, desc)
replicas.Shuffle()
br, err := ds.sendRPC(ctx, desc.RangeID, replicas, ba)
if err != nil {
return nil, nil, roachpb.NewError(err)
}
if br.Error != nil {
return nil, nil, br.Error
}
resp := br.Responses[0].GetInner().(*roachpb.RangeLookupResponse)
return resp.Ranges, resp.PrefetchedRanges, nil
}
开发者ID:knz,项目名称:cockroach,代码行数:36,代码来源:dist_sender.go
示例8: TestSendNext_NonRetryableApplicationError
func TestSendNext_NonRetryableApplicationError(t *testing.T) {
defer leaktest.AfterTest(t)()
doneChans, sendChan, stopper := setupSendNextTest(t)
defer stopper.Stop()
// One replica finishes with a non-retryable error.
doneChans[1] <- BatchCall{
Reply: &roachpb.BatchResponse{
BatchResponse_Header: roachpb.BatchResponse_Header{
Error: roachpb.NewError(roachpb.NewTransactionReplayError()),
},
},
}
// The client completes with that error, without waiting for the
// others to finish.
bc := <-sendChan
if bc.Err != nil {
t.Fatalf("expected error in payload, not rpc error %s", bc.Err)
}
if _, ok := bc.Reply.Error.GetDetail().(*roachpb.TransactionReplayError); !ok {
t.Errorf("expected TransactionReplayError, got %v", bc.Reply.Error)
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:25,代码来源:send_test.go
示例9: TestSendNext_AllRetryableApplicationErrors
func TestSendNext_AllRetryableApplicationErrors(t *testing.T) {
defer leaktest.AfterTest(t)()
doneChans, sendChan, stopper := setupSendNextTest(t)
defer stopper.Stop()
// All replicas finish with a retryable error.
for _, ch := range doneChans {
ch <- BatchCall{
Reply: &roachpb.BatchResponse{
BatchResponse_Header: roachpb.BatchResponse_Header{
Error: roachpb.NewError(roachpb.NewRangeNotFoundError(1)),
},
},
}
}
// The client send finishes with one of the errors, wrapped in a SendError.
bc := <-sendChan
if bc.Err == nil {
t.Fatalf("expected SendError, got err=nil and reply=%s", bc.Reply)
} else if _, ok := bc.Err.(*roachpb.SendError); !ok {
t.Fatalf("expected SendError, got err=%s", bc.Err)
} else if exp := "range 1 was not found"; !testutils.IsError(bc.Err, exp) {
t.Errorf("expected SendError to contain %q, but got %v", exp, bc.Err)
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:27,代码来源:send_test.go
示例10: TestSendNext_RetryableApplicationErrorThenSuccess
func TestSendNext_RetryableApplicationErrorThenSuccess(t *testing.T) {
defer leaktest.AfterTest(t)()
doneChans, sendChan, stopper := setupSendNextTest(t)
defer stopper.Stop()
// One replica finishes with a retryable error.
doneChans[1] <- BatchCall{
Reply: &roachpb.BatchResponse{
BatchResponse_Header: roachpb.BatchResponse_Header{
Error: roachpb.NewError(roachpb.NewRangeNotFoundError(1)),
},
},
}
// A second replica finishes successfully.
doneChans[2] <- BatchCall{
Reply: &roachpb.BatchResponse{},
}
// The client send finishes with the second response.
bc := <-sendChan
if bc.Err != nil {
t.Fatalf("unexpected RPC error: %s", bc.Err)
}
if bc.Reply.Error != nil {
t.Errorf("expected successful reply, got %s", bc.Reply.Error)
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:29,代码来源:send_test.go
示例11: sendAndFill
// sendAndFill is a helper which sends the given batch and fills its results,
// returning the appropriate error which is either from the first failing call,
// or an "internal" error.
func sendAndFill(
send func(roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error), b *Batch,
) error {
// Errors here will be attached to the results, so we will get them from
// the call to fillResults in the regular case in which an individual call
// fails. But send() also returns its own errors, so there's some dancing
// here to do because we want to run fillResults() so that the individual
// result gets initialized with an error from the corresponding call.
var ba roachpb.BatchRequest
// TODO(tschottdorf): this nonsensical copy is required since (at least at
// the time of writing, the chunking and masking in DistSender operates on
// the original data (as attested to by a whole bunch of test failures).
ba.Requests = append([]roachpb.RequestUnion(nil), b.reqs...)
ba.Header = b.Header
b.response, b.pErr = send(ba)
if b.pErr != nil {
// Discard errors from fillResults.
_ = b.fillResults()
return b.pErr.GoError()
}
if err := b.fillResults(); err != nil {
b.pErr = roachpb.NewError(err)
return err
}
return nil
}
开发者ID:veteranlu,项目名称:cockroach,代码行数:29,代码来源:db.go
示例12: Batch
// Batch implements the roachpb.KVServer interface.
func (s *DBServer) Batch(
ctx context.Context, args *roachpb.BatchRequest,
) (br *roachpb.BatchResponse, err error) {
// TODO(marc,bdarnell): this code is duplicated in server/node.go,
// which should be fixed.
defer func() {
// We always return errors via BatchResponse.Error so structure is
// preserved; plain errors are presumed to be from the RPC
// framework and not from cockroach.
if err != nil {
if br == nil {
br = &roachpb.BatchResponse{}
}
if br.Error != nil {
panic(fmt.Sprintf(
"attempting to return both a plain error (%s) and roachpb.Error (%s)", err, br.Error))
}
br.Error = roachpb.NewError(err)
err = nil
}
}()
// TODO(marc): grpc's authentication model (which gives credential access in
// the request handler) doesn't really fit with the current design of the
// security package (which assumes that TLS state is only given at connection
// time) - that should be fixed.
if peer, ok := peer.FromContext(ctx); ok {
if tlsInfo, ok := peer.AuthInfo.(credentials.TLSInfo); ok {
certUser, err := security.GetCertificateUser(&tlsInfo.State)
if err != nil {
return nil, err
}
if certUser != security.NodeUser {
return nil, errors.Errorf("user %s is not allowed", certUser)
}
}
}
if err = verifyRequest(args); err != nil {
return br, err
}
err = s.stopper.RunTask(func() {
var pErr *roachpb.Error
// TODO(wiz): This is required to be a different context from the one
// provided by grpc since it has to last for the entire transaction and not
// just this one RPC call. See comment for (*TxnCoordSender).hearbeatLoop.
br, pErr = s.sender.Send(context.TODO(), *args)
if pErr != nil {
br = &roachpb.BatchResponse{}
}
if br.Error != nil {
panic(roachpb.ErrorUnexpectedlySet(s.sender, br))
}
br.Error = pErr
})
return br, err
}
开发者ID:knz,项目名称:cockroach,代码行数:58,代码来源:db.go
示例13: checkEndTransactionTrigger
// checkEndTransactionTrigger verifies that an EndTransactionRequest
// that includes intents for the SystemDB keys sets the proper trigger.
func checkEndTransactionTrigger(args storagebase.FilterArgs) *roachpb.Error {
req, ok := args.Req.(*roachpb.EndTransactionRequest)
if !ok {
return nil
}
if !req.Commit {
// This is a rollback: skip trigger verification.
return nil
}
modifiedSpanTrigger := req.InternalCommitTrigger.GetModifiedSpanTrigger()
modifiedSystemConfigSpan := modifiedSpanTrigger != nil && modifiedSpanTrigger.SystemConfigSpan
var hasSystemKey bool
for _, span := range req.IntentSpans {
keyAddr, err := keys.Addr(span.Key)
if err != nil {
return roachpb.NewError(err)
}
if bytes.Compare(keyAddr, keys.SystemConfigSpan.Key) >= 0 &&
bytes.Compare(keyAddr, keys.SystemConfigSpan.EndKey) < 0 {
hasSystemKey = true
break
}
}
// If the transaction in question has intents in the system span, then
// modifiedSystemConfigSpan should always be true. However, it is possible
// for modifiedSystemConfigSpan to be set, even though no system keys are
// present. This can occur with certain conditional DDL statements (e.g.
// "CREATE TABLE IF NOT EXISTS"), which set the SystemConfigTrigger
// aggressively but may not actually end up changing the system DB depending
// on the current state.
// For more information, see the related comment at the beginning of
// planner.makePlan().
if hasSystemKey && !modifiedSystemConfigSpan {
return roachpb.NewError(errors.Errorf("EndTransaction hasSystemKey=%t, but hasSystemConfigTrigger=%t",
hasSystemKey, modifiedSystemConfigSpan))
}
return nil
}
开发者ID:knz,项目名称:cockroach,代码行数:44,代码来源:main_test.go
示例14: Send
// Send implements the Sender interface.
func (s sender) Send(
ctx context.Context, ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, *roachpb.Error) {
br, err := s.Batch(ctx, &ba)
if err != nil {
return nil, roachpb.NewError(roachpb.NewSendError(err.Error()))
}
pErr := br.Error
br.Error = nil
return br, pErr
}
开发者ID:knz,项目名称:cockroach,代码行数:12,代码来源:rpc_sender.go
示例15: Seek
// Seek positions the iterator at the specified key.
func (ri *RangeIterator) Seek(ctx context.Context, key roachpb.RKey, scanDir ScanDirection) {
log.Eventf(ctx, "querying next range at %s", key)
ri.scanDir = scanDir
ri.init = true // the iterator is now initialized
ri.pErr = nil // clear any prior error
ri.key = key // set the key
// Retry loop for looking up next range in the span. The retry loop
// deals with retryable range descriptor lookups.
for r := retry.StartWithCtx(ctx, ri.ds.rpcRetryOptions); r.Next(); {
log.Event(ctx, "meta descriptor lookup")
var err error
ri.desc, ri.token, err = ri.ds.getDescriptor(
ctx, ri.key, ri.token, ri.scanDir == Descending)
// getDescriptor may fail retryably if, for example, the first
// range isn't available via Gossip. Assume that all errors at
// this level are retryable. Non-retryable errors would be for
// things like malformed requests which we should have checked
// for before reaching this point.
if err != nil {
log.VEventf(ctx, 1, "range descriptor lookup failed: %s", err)
continue
}
// It's possible that the returned descriptor misses parts of the
// keys it's supposed to include after it's truncated to match the
// descriptor. Example revscan [a,g), first desc lookup for "g"
// returns descriptor [c,d) -> [d,g) is never scanned.
// We evict and retry in such a case.
// TODO: this code is subject to removal. See
// https://groups.google.com/d/msg/cockroach-db/DebjQEgU9r4/_OhMe7atFQAJ
reverse := ri.scanDir == Descending
if (reverse && !ri.desc.ContainsExclusiveEndKey(ri.key)) ||
(!reverse && !ri.desc.ContainsKey(ri.key)) {
log.Eventf(ctx, "addressing error: %s does not include key %s", ri.desc, ri.key)
if err := ri.token.Evict(ctx); err != nil {
ri.pErr = roachpb.NewError(err)
return
}
// On addressing errors, don't backoff; retry immediately.
r.Reset()
continue
}
return
}
// Check for an early exit from the retry loop.
if pErr := ri.ds.deduceRetryEarlyExitError(ctx); pErr != nil {
ri.pErr = pErr
} else {
ri.pErr = roachpb.NewErrorf("RangeIterator failed to seek to %s", key)
}
}
开发者ID:veteranlu,项目名称:cockroach,代码行数:55,代码来源:range_iter.go
示例16: TestAbortTransactionOnCommitErrors
// TestAbortTransactionOnCommitErrors verifies that transactions are
// aborted on the correct errors.
func TestAbortTransactionOnCommitErrors(t *testing.T) {
defer leaktest.AfterTest(t)()
testCases := []struct {
err error
abort bool
}{
{roachpb.NewReadWithinUncertaintyIntervalError(hlc.ZeroTimestamp, hlc.ZeroTimestamp), true},
{&roachpb.TransactionAbortedError{}, false},
{&roachpb.TransactionPushError{}, true},
{&roachpb.TransactionRetryError{}, true},
{&roachpb.RangeNotFoundError{}, true},
{&roachpb.RangeKeyMismatchError{}, true},
{&roachpb.TransactionStatusError{}, true},
}
for _, test := range testCases {
var commit, abort bool
db := NewDB(newTestSender(func(ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
switch t := ba.Requests[0].GetInner().(type) {
case *roachpb.EndTransactionRequest:
if t.Commit {
commit = true
return nil, roachpb.NewError(test.err)
}
abort = true
}
return ba.CreateReply(), nil
}, nil))
txn := NewTxn(context.Background(), *db)
if pErr := txn.Put("a", "b"); pErr != nil {
t.Fatalf("put failed: %s", pErr)
}
if pErr := txn.CommitOrCleanup(); pErr == nil {
t.Fatalf("unexpected commit success")
}
if !commit {
t.Errorf("%T: failed to find commit", test.err)
}
if test.abort && !abort {
t.Errorf("%T: failed to find abort", test.err)
} else if !test.abort && abort {
t.Errorf("%T: found unexpected abort", test.err)
}
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:51,代码来源:txn_test.go
示例17: handleRaftRequest
// handleRaftRequest proxies a request to the listening server interface.
func (t *RaftTransport) handleRaftRequest(
ctx context.Context, req *RaftMessageRequest, respStream RaftMessageResponseStream,
) *roachpb.Error {
t.recvMu.Lock()
handler, ok := t.recvMu.handlers[req.ToReplica.StoreID]
t.recvMu.Unlock()
if !ok {
log.Warningf(ctx, "unable to accept Raft message from %+v: no handler registered for %+v",
req.FromReplica, req.ToReplica)
return roachpb.NewError(roachpb.NewStoreNotFoundError(req.ToReplica.StoreID))
}
return handler.HandleRaftRequest(ctx, req, respStream)
}
开发者ID:hvaara,项目名称:cockroach,代码行数:16,代码来源:raft_transport.go
示例18: TestAbortedRetryRenewsTimestamp
// Test that the a txn gets a fresh OrigTimestamp with every retry.
func TestAbortedRetryRenewsTimestamp(t *testing.T) {
defer leaktest.AfterTest(t)()
// Create a TestSender that aborts a transaction 2 times before succeeding.
mc := hlc.NewManualClock(123)
clock := hlc.NewClock(mc.UnixNano, time.Nanosecond)
count := 0
db := NewDB(newTestSender(func(ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
if _, ok := ba.GetArg(roachpb.Put); ok {
mc.Increment(1)
count++
if count < 3 {
return nil, roachpb.NewError(&roachpb.TransactionAbortedError{})
}
}
return ba.CreateReply(), nil
}, nil))
txnClosure := func(txn *Txn, opt *TxnExecOptions) error {
// Ensure the KV transaction is created.
return txn.Put("a", "b")
}
txn := NewTxn(context.Background(), *db)
// Request a client-defined timestamp.
refTimestamp := clock.Now()
execOpt := TxnExecOptions{
AutoRetry: true,
AutoCommit: true,
Clock: clock,
}
// Perform the transaction.
if err := txn.Exec(execOpt, txnClosure); err != nil {
t.Fatal(err)
}
// Check the timestamp was preserved.
if txn.Proto.OrigTimestamp.WallTime == refTimestamp.WallTime {
t.Errorf("expected txn orig ts to be different than %s", refTimestamp)
}
}
开发者ID:hvaara,项目名称:cockroach,代码行数:44,代码来源:txn_test.go
示例19: sendSingleRange
// sendSingleRange gathers and rearranges the replicas, and makes an RPC call.
func (ds *DistSender) sendSingleRange(
ctx context.Context, ba roachpb.BatchRequest, desc *roachpb.RangeDescriptor,
) (*roachpb.BatchResponse, *roachpb.Error) {
// Try to send the call.
replicas := newReplicaSlice(ds.gossip, desc)
// Rearrange the replicas so that those replicas with long common
// prefix of attributes end up first. If there's no prefix, this is a
// no-op.
ds.optimizeReplicaOrder(replicas)
// If this request needs to go to a lease holder and we know who that is, move
// it to the front.
if !(ba.IsReadOnly() && ba.ReadConsistency == roachpb.INCONSISTENT) {
if leaseHolder, ok := ds.leaseHolderCache.Lookup(ctx, desc.RangeID); ok {
if i := replicas.FindReplica(leaseHolder.StoreID); i >= 0 {
replicas.MoveToFront(i)
}
}
}
// TODO(tschottdorf): should serialize the trace here, not higher up.
br, err := ds.sendRPC(ctx, desc.RangeID, replicas, ba)
if err != nil {
return nil, roachpb.NewError(err)
}
// If the reply contains a timestamp, update the local HLC with it.
if br.Error != nil && br.Error.Now != hlc.ZeroTimestamp {
ds.clock.Update(br.Error.Now)
} else if br.Now != hlc.ZeroTimestamp {
ds.clock.Update(br.Now)
}
// Untangle the error from the received response.
pErr := br.Error
br.Error = nil // scrub the response error
return br, pErr
}
开发者ID:knz,项目名称:cockroach,代码行数:40,代码来源:dist_sender.go
示例20: TestBeginTransactionErrorIndex
// TestBeginTransactionErrorIndex verifies that the error index is cleared
// when a BeginTransaction command causes an error.
func TestBeginTransactionErrorIndex(t *testing.T) {
defer leaktest.AfterTest(t)()
db := NewDB(newTestSender(func(ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
pErr := roachpb.NewError(&roachpb.WriteIntentError{})
pErr.SetErrorIndex(0)
return nil, pErr
}, nil))
_ = db.Txn(context.TODO(), func(txn *Txn) error {
b := txn.NewBatch()
b.Put("a", "b")
err := getOneErr(txn.Run(b), b)
pErr := b.MustPErr()
// Verify that the original error type is preserved, but the error index is unset.
if _, ok := pErr.GetDetail().(*roachpb.WriteIntentError); !ok {
t.Fatalf("unexpected error %s", pErr)
}
if pErr.Index != nil {
t.Errorf("error index must not be set, but got %s", pErr.Index)
}
return err
})
}
开发者ID:hvaara,项目名称:cockroach,代码行数:24,代码来源:txn_test.go
注:本文中的github.com/cockroachdb/cockroach/pkg/roachpb.NewError函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论