本文整理汇总了Golang中github.com/cockroachdb/cockroach/internal/client.NewTxn函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTxn函数的具体用法?Golang NewTxn怎么用?Golang NewTxn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewTxn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTxnCoordSenderCleanupOnAborted
// TestTxnCoordSenderCleanupOnAborted verifies that if a txn receives a
// TransactionAbortedError, the coordinator cleans up the transaction.
func TestTxnCoordSenderCleanupOnAborted(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Create a transaction with intent at "a".
key := roachpb.Key("a")
txn1 := client.NewTxn(context.Background(), *s.DB)
txn1.InternalSetPriority(1)
if err := txn1.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
// Push the transaction (by writing key "a" with higher priority) to abort it.
txn2 := client.NewTxn(context.Background(), *s.DB)
txn2.InternalSetPriority(2)
if err := txn2.Put(key, []byte("value2")); err != nil {
t.Fatal(err)
}
// Now end the transaction and verify we've cleanup up, even though
// end transaction failed.
err := txn1.CommitOrCleanup()
assertTransactionAbortedError(t, err)
if err := txn2.CommitOrCleanup(); err != nil {
t.Fatal(err)
}
verifyCleanup(key, sender, s.Eng, t)
}
示例2: TestTxnCoordSenderBeginTransaction
// TestTxnCoordSenderBeginTransaction verifies that a command sent with a
// not-nil Txn with empty ID gets a new transaction initialized.
func TestTxnCoordSenderBeginTransaction(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Name = "test txn"
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if txn.Proto.Name != "test txn" {
t.Errorf("expected txn name to be %q; got %q", "test txn", txn.Proto.Name)
}
if txn.Proto.Priority != 10 {
t.Errorf("expected txn priority 10; got %d", txn.Proto.Priority)
}
if !bytes.Equal(txn.Proto.Key, key) {
t.Errorf("expected txn Key to match %q != %q", key, txn.Proto.Key)
}
if txn.Proto.Isolation != enginepb.SNAPSHOT {
t.Errorf("expected txn isolation to be SNAPSHOT; got %s", txn.Proto.Isolation)
}
}
示例3: TestTxnCoordSenderAddIntentOnError
// TestTxnCoordSenderAddIntentOnError verifies that intents are tracked if
// the transaction is, even on error.
func TestTxnCoordSenderAddIntentOnError(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Create a transaction with intent at "a".
key := roachpb.Key("x")
txn := client.NewTxn(context.Background(), *s.DB)
// Write so that the coordinator begins tracking this txn.
if err := txn.Put("x", "y"); err != nil {
t.Fatal(err)
}
err, ok := txn.CPut(key, []byte("x"), []byte("born to fail")).(*roachpb.ConditionFailedError)
if !ok {
t.Fatal(err)
}
sender.Lock()
txnID := *txn.Proto.ID
roachpb.MergeSpans(&sender.txns[txnID].keys)
intentSpans := sender.txns[txnID].keys
expSpans := []roachpb.Span{{Key: key, EndKey: []byte("")}}
equal := !reflect.DeepEqual(intentSpans, expSpans)
sender.Unlock()
if err := txn.Rollback(); err != nil {
t.Fatal(err)
}
if !equal {
t.Fatalf("expected stored intents %v, got %v", expSpans, intentSpans)
}
}
示例4: TestTxnCoordIdempotentCleanup
// TestTxnCoordIdempotentCleanup verifies that cleanupTxnLocked is idempotent.
func TestTxnCoordIdempotentCleanup(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
ba := txn.NewBatch()
ba.Put(roachpb.Key("a"), []byte("value"))
if err := txn.Run(ba); err != nil {
t.Fatal(err)
}
sender.Lock()
// Clean up twice successively.
sender.cleanupTxnLocked(context.Background(), txn.Proto)
sender.cleanupTxnLocked(context.Background(), txn.Proto)
sender.Unlock()
// For good measure, try to commit (which cleans up once more if it
// succeeds, which it may not if the previous cleanup has already
// terminated the heartbeat goroutine)
ba = txn.NewBatch()
ba.AddRawRequest(&roachpb.EndTransactionRequest{})
err := txn.Run(ba)
if err != nil && !testutils.IsError(err, errNoState.Error()) {
t.Fatal(err)
}
}
示例5: TestTxnInitialTimestamp
// TestTxnInitialTimestamp verifies that the timestamp requested
// before the Txn is created is honored.
func TestTxnInitialTimestamp(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Request a specific timestamp.
refTimestamp := hlc.Timestamp{WallTime: 42, Logical: 69}
txn.Proto.OrigTimestamp = refTimestamp
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Name = "test txn"
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if txn.Proto.OrigTimestamp != refTimestamp {
t.Errorf("expected txn orig ts to be %s; got %s", refTimestamp, txn.Proto.OrigTimestamp)
}
if txn.Proto.Timestamp != refTimestamp {
t.Errorf("expected txn ts to be %s; got %s", refTimestamp, txn.Proto.Timestamp)
}
}
示例6: TestTxnCoordSenderGCTimeout
// TestTxnCoordSenderGCTimeout verifies that the coordinator cleans up extant
// transactions and intents after the lastUpdateNanos exceeds the timeout.
func TestTxnCoordSenderGCTimeout(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
// Set heartbeat interval to 1ms for testing.
sender.heartbeatInterval = 1 * time.Millisecond
txn := client.NewTxn(context.Background(), *s.DB)
key := roachpb.Key("a")
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
// Now, advance clock past the default client timeout.
// Locking the TxnCoordSender to prevent a data race.
sender.Lock()
s.Manual.Set(defaultClientTimeout.Nanoseconds() + 1)
sender.Unlock()
txnID := *txn.Proto.ID
util.SucceedsSoon(t, func() error {
// Locking the TxnCoordSender to prevent a data race.
sender.Lock()
_, ok := sender.txns[txnID]
sender.Unlock()
if ok {
return errors.Errorf("expected garbage collection")
}
return nil
})
verifyCleanup(key, sender, s.Eng, t)
}
示例7: reset
// reset creates a new Txn and initializes it using the session defaults.
func (ts *txnState) reset(ctx context.Context, e *Executor, s *Session) {
*ts = txnState{}
ts.txn = client.NewTxn(ctx, *e.ctx.DB)
ts.txn.Proto.Isolation = s.DefaultIsolationLevel
ts.tr = s.Trace
// Discard the old schemaChangers, if any.
ts.schemaChangers = schemaChangerCollection{}
}
示例8: setupTxn
func (ds *ServerImpl) setupTxn(
ctx context.Context,
txnProto *roachpb.Transaction,
) *client.Txn {
txn := client.NewTxn(ctx, *ds.ctx.DB)
// TODO(radu): we should sanity check some of these fields
txn.Proto = *txnProto
return txn
}
示例9: Prepare
// Prepare returns the result types of the given statement. pinfo may
// contain partial type information for placeholders. Prepare will
// populate the missing types. The column result types are returned (or
// nil if there are no results).
func (e *Executor) Prepare(
query string,
session *Session,
pinfo parser.PlaceholderTypes,
) ([]ResultColumn, error) {
if log.V(2) {
log.Infof(session.Ctx(), "preparing: %s", query)
} else if traceSQL {
log.Tracef(session.Ctx(), "preparing: %s", query)
}
stmt, err := parser.ParseOne(query, parser.Syntax(session.Syntax))
if err != nil {
return nil, err
}
if err = pinfo.ProcessPlaceholderAnnotations(stmt); err != nil {
return nil, err
}
protoTS, err := isAsOf(&session.planner, stmt, e.ctx.Clock.Now())
if err != nil {
return nil, err
}
session.planner.resetForBatch(e)
session.planner.semaCtx.Placeholders.SetTypes(pinfo)
session.planner.evalCtx.PrepareOnly = true
// Prepare needs a transaction because it needs to retrieve db/table
// descriptors for type checking.
txn := client.NewTxn(session.Ctx(), *e.ctx.DB)
txn.Proto.Isolation = session.DefaultIsolationLevel
session.planner.setTxn(txn)
defer session.planner.setTxn(nil)
if protoTS != nil {
session.planner.asOf = true
defer func() {
session.planner.asOf = false
}()
setTxnTimestamps(txn, *protoTS)
}
plan, err := session.planner.prepare(stmt)
if err != nil {
return nil, err
}
if plan == nil {
return nil, nil
}
cols := plan.Columns()
for _, c := range cols {
if err := checkResultDatum(c.Typ); err != nil {
return nil, err
}
}
return cols, nil
}
示例10: TestTxnCoordSenderMultipleTxns
// TestTxnCoordSenderMultipleTxns verifies correct operation with
// multiple outstanding transactions.
func TestTxnCoordSenderMultipleTxns(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn1 := client.NewTxn(context.Background(), *s.DB)
txn2 := client.NewTxn(context.Background(), *s.DB)
if err := txn1.Put(roachpb.Key("a"), []byte("value")); err != nil {
t.Fatal(err)
}
if err := txn2.Put(roachpb.Key("b"), []byte("value")); err != nil {
t.Fatal(err)
}
if len(sender.txns) != 2 {
t.Errorf("expected length of transactions map to be 2; got %d", len(sender.txns))
}
}
示例11: Restore
// Restore imports a SQL table (or all user SQL tables) from a set of
// non-overlapping sstable files.
func Restore(ctx context.Context, db client.DB, base string, table string, overwrite bool) error {
sst, err := engine.MakeRocksDBSstFileReader()
if err != nil {
return err
}
defer sst.Close()
descBytes, err := ioutil.ReadFile(filepath.Join(base, backupDescriptorName))
if err != nil {
return err
}
var backupDesc sqlbase.BackupDescriptor
if err := backupDesc.Unmarshal(descBytes); err != nil {
return err
}
for _, r := range backupDesc.Ranges {
if len(r.Path) == 0 {
continue
}
if err := sst.AddFile(r.Path); err != nil {
return err
}
}
// TODO(dan): This uses one giant transaction for the entire restore, which
// works for small datasets, but not for big ones.
txn := client.NewTxn(ctx, db)
if len(table) > 0 {
found := false
for _, desc := range backupDesc.SQL {
if t := desc.GetTable(); t != nil && t.Name == table {
if err := restoreTable(ctx, sst, txn, t, overwrite); err != nil {
return err
}
found = true
break
}
}
if !found {
return errors.Errorf("table not found: %s", table)
}
} else {
for _, desc := range backupDesc.SQL {
if t := desc.GetTable(); t != nil && t.ParentID != keys.SystemDatabaseID {
if err := restoreTable(ctx, sst, txn, t, overwrite); err != nil {
return err
}
}
}
}
return txn.CommitOrCleanup()
}
示例12: TestTxnCoordSenderKeyRanges
// TestTxnCoordSenderKeyRanges verifies that multiple requests to same or
// overlapping key ranges causes the coordinator to keep track only of
// the minimum number of ranges.
func TestTxnCoordSenderKeyRanges(t *testing.T) {
defer leaktest.AfterTest(t)()
ranges := []struct {
start, end roachpb.Key
}{
{roachpb.Key("a"), roachpb.Key(nil)},
{roachpb.Key("a"), roachpb.Key(nil)},
{roachpb.Key("aa"), roachpb.Key(nil)},
{roachpb.Key("b"), roachpb.Key(nil)},
{roachpb.Key("aa"), roachpb.Key("c")},
{roachpb.Key("b"), roachpb.Key("c")},
}
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
for _, rng := range ranges {
if rng.end != nil {
if err := txn.DelRange(rng.start, rng.end); err != nil {
t.Fatal(err)
}
} else {
if err := txn.Put(rng.start, []byte("value")); err != nil {
t.Fatal(err)
}
}
}
txnID := *txn.Proto.ID
// Verify that the transaction metadata contains only two entries
// in its "keys" range group. "a" and range "aa"-"c".
txnMeta, ok := sender.txns[txnID]
if !ok {
t.Fatalf("expected a transaction to be created on coordinator")
}
roachpb.MergeSpans(&txnMeta.keys)
keys := txnMeta.keys
if len(keys) != 2 {
t.Errorf("expected 2 entries in keys range group; got %v", keys)
}
}
示例13: TestTxnCoordSenderReleaseTxnMeta
// TestTxnCoordSenderReleaseTxnMeta verifies that TxnCoordSender releases the
// txnMetadata after the txn has committed successfully.
func TestTxnCoordSenderReleaseTxnMeta(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
ba := txn.NewBatch()
ba.Put(roachpb.Key("a"), []byte("value"))
ba.Put(roachpb.Key("b"), []byte("value"))
if err := txn.CommitInBatch(ba); err != nil {
t.Fatal(err)
}
txnID := *txn.Proto.ID
if _, ok := sender.txns[txnID]; ok {
t.Fatal("expected TxnCoordSender has released the txn")
}
}
示例14: TestTxnCoordSenderBeginTransactionMinPriority
// TestTxnCoordSenderBeginTransactionMinPriority verifies that when starting
// a new transaction, a non-zero priority is treated as a minimum value.
func TestTxnCoordSenderBeginTransactionMinPriority(t *testing.T) {
defer leaktest.AfterTest(t)()
s, sender := createTestDB(t)
defer s.Stop()
defer teardownHeartbeats(sender)
txn := client.NewTxn(context.Background(), *s.DB)
// Put request will create a new transaction.
key := roachpb.Key("key")
txn.InternalSetPriority(10)
txn.Proto.Isolation = enginepb.SNAPSHOT
txn.Proto.Priority = 11
if err := txn.Put(key, []byte("value")); err != nil {
t.Fatal(err)
}
if prio := txn.Proto.Priority; prio != 11 {
t.Errorf("expected txn priority 11; got %d", prio)
}
}
示例15: reset
// reset creates a new Txn and initializes it using the session defaults.
func (ts *txnState) reset(ctx context.Context, e *Executor, s *Session) {
*ts = txnState{}
ts.txn = client.NewTxn(ctx, *e.ctx.DB)
ts.txn.Context = s.context
ts.txn.Proto.Isolation = s.DefaultIsolationLevel
ts.tr = s.Trace
// Discard the old schemaChangers, if any.
ts.schemaChangers = schemaChangerCollection{}
if traceSQL {
sp, err := tracing.JoinOrNewSnowball("coordinator", nil, func(sp basictracer.RawSpan) {
ts.txn.CollectedSpans = append(ts.txn.CollectedSpans, sp)
})
if err != nil {
log.Warningf(ctx, "unable to create snowball tracer: %s", err)
return
}
ts.txn.Context = opentracing.ContextWithSpan(ts.txn.Context, sp)
ts.sp = sp
}
}