// Open creates a new database handle to the cockroach cluster specified by // addr. The cluster is identified by a URL with the format: // // [<sender>:]//[<user>@]<host>:<port>[?certs=<dir>,priority=<val>] // // The URL scheme (<sender>) specifies which transport to use for talking to // the cockroach cluster. Currently allowable values are: http, https, rpc, // rpcs. The rpc and rpcs senders use a variant of Go's builtin rpc library for // communication with the cluster. This protocol is lower overhead and more // efficient than http. The decision between the encrypted (https, rpcs) and // unencrypted senders (http, rpc) depends on the settings of the cluster. A // given cluster supports either encrypted or unencrypted traffic, but not // both. // // If not specified, the <user> field defaults to "root". // // The certs parameter can be used to override the default directory to use for // client certificates. In tests, the directory "test_certs" uses the embedded // test certificates. // // The priority parameter can be used to override the default priority for // operations. func Open(stopper *stop.Stopper, addr string) (*DB, error) { u, err := url.Parse(addr) if err != nil { return nil, err } ctx := &base.Context{} ctx.InitDefaults() if u.User != nil { ctx.User = u.User.Username() } q := u.Query() if dir := q["certs"]; len(dir) > 0 { ctx.Certs = dir[0] } sender, err := newSender(u, ctx, stopper) if err != nil { return nil, err } if sender == nil { return nil, fmt.Errorf("\"%s\" no sender specified", addr) } db := &DB{ sender: sender, userPriority: roachpb.NormalUserPriority, txnRetryOptions: DefaultTxnRetryOptions, } if priority := q["priority"]; len(priority) > 0 { p, err := strconv.ParseFloat(priority[0], 64) if err != nil { return nil, err } db.userPriority = roachpb.UserPriority(p) } return db, nil }
// TestSetPriority verifies that the batch UserPriority is correctly set // depending on the transaction priority. func TestSetPriority(t *testing.T) { defer leaktest.AfterTest(t)() var expected roachpb.UserPriority db := NewDB(newTestSender( func(ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) { if ba.UserPriority != expected { pErr := roachpb.NewErrorf("Priority not set correctly in the batch! "+ "(expected: %s, value: %s)", expected, ba.UserPriority) return nil, pErr } br := &roachpb.BatchResponse{} br.Txn = &roachpb.Transaction{} br.Txn.Update(ba.Txn) // copy return br, nil }, nil)) // Verify the normal priority setting path. expected = roachpb.HighUserPriority txn := NewTxn(context.Background(), *db) if err := txn.SetUserPriority(expected); err != nil { t.Fatal(err) } if _, pErr := txn.db.sender.Send(context.Background(), roachpb.BatchRequest{}); pErr != nil { t.Fatal(pErr) } // Verify the internal (fixed value) priority setting path. expected = roachpb.UserPriority(-13) txn = NewTxn(context.Background(), *db) txn.InternalSetPriority(13) if _, pErr := txn.db.sender.Send(context.Background(), roachpb.BatchRequest{}); pErr != nil { t.Fatal(pErr) } }
// TestTxnPutOutOfOrder tests a case where a put operation of an older // timestamp comes after a put operation of a newer timestamp in a // txn. The test ensures such an out-of-order put succeeds and // overrides an old value. The test uses a "Writer" and a "Reader" // to reproduce an out-of-order put. // // 1) The Writer executes a put operation and writes a write intent with // time T in a txn. // 2) Before the Writer's txn is committed, the Reader sends a high priority // get operation with time T+100. This pushes the Writer txn timestamp to // T+100 and triggers the restart of the Writer's txn. The original // write intent timestamp is also updated to T+100. // 3) The Writer starts a new epoch of the txn, but before it writes, the // Reader sends another high priority get operation with time T+200. This // pushes the Writer txn timestamp to T+200 to trigger a restart of the // Writer txn. The Writer will not actually restart until it tries to commit // the current epoch of the transaction. The Reader updates the timestamp of // the write intent to T+200. The test deliberately fails the Reader get // operation, and cockroach doesn't update its read timestamp cache. // 4) The Writer executes the put operation again. This put operation comes // out-of-order since its timestamp is T+100, while the intent timestamp // updated at Step 3 is T+200. // 5) The put operation overrides the old value using timestamp T+100. // 6) When the Writer attempts to commit its txn, the txn will be restarted // again at a new epoch timestamp T+200, which will finally succeed. func TestTxnPutOutOfOrder(t *testing.T) { defer leaktest.AfterTest(t)() key := "key" // Set up a filter to so that the get operation at Step 3 will return an error. var numGets int32 manualClock := hlc.NewManualClock(0) clock := hlc.NewClock(manualClock.UnixNano) stopper := stop.NewStopper() defer stopper.Stop() ctx := storage.TestStoreContext() ctx.TestingKnobs.TestingCommandFilter = func(filterArgs storagebase.FilterArgs) *roachpb.Error { if _, ok := filterArgs.Req.(*roachpb.GetRequest); ok && filterArgs.Req.Header().Key.Equal(roachpb.Key(key)) && filterArgs.Hdr.Txn == nil { // The Reader executes two get operations, each of which triggers two get requests // (the first request fails and triggers txn push, and then the second request // succeeds). Returns an error for the fourth get request to avoid timestamp cache // update after the third get operation pushes the txn timestamp. if atomic.AddInt32(&numGets, 1) == 4 { return roachpb.NewErrorWithTxn(errors.Errorf("Test"), filterArgs.Hdr.Txn) } } return nil } store := createTestStoreWithEngine(t, engine.NewInMem(roachpb.Attributes{}, 10<<20, stopper), clock, true, ctx, stopper) // Put an initial value. initVal := []byte("initVal") err := store.DB().Put(key, initVal) if err != nil { t.Fatalf("failed to put: %s", err) } waitPut := make(chan struct{}) waitFirstGet := make(chan struct{}) waitTxnRestart := make(chan struct{}) waitSecondGet := make(chan struct{}) waitTxnComplete := make(chan struct{}) // Start the Writer. go func() { epoch := -1 // Start a txn that does read-after-write. // The txn will be restarted twice, and the out-of-order put // will happen in the second epoch. if err := store.DB().Txn(func(txn *client.Txn) error { epoch++ if epoch == 1 { // Wait until the second get operation is issued. close(waitTxnRestart) <-waitSecondGet } updatedVal := []byte("updatedVal") if err := txn.Put(key, updatedVal); err != nil { return err } // Make sure a get will return the value that was just written. actual, err := txn.Get(key) if err != nil { return err } if !bytes.Equal(actual.ValueBytes(), updatedVal) { t.Fatalf("unexpected get result: %s", actual) } if epoch == 0 { // Wait until the first get operation will push the txn timestamp. close(waitPut) <-waitFirstGet } b := txn.NewBatch() return txn.CommitInBatch(b) }); err != nil { t.Fatal(err) } if epoch != 2 { t.Fatalf("unexpected number of txn retries: %d", epoch) } close(waitTxnComplete) }() <-waitPut // Start the Reader. // Advance the clock and send a get operation with higher // priority to trigger the txn restart. manualClock.Increment(100) priority := roachpb.UserPriority(-math.MaxInt32) requestHeader := roachpb.Span{ Key: roachpb.Key(key), } ts := clock.Now() if _, err := client.SendWrappedWith(rg1(store), nil, roachpb.Header{ Timestamp: ts, UserPriority: priority, }, &roachpb.GetRequest{Span: requestHeader}); err != nil { t.Fatalf("failed to get: %s", err) } // Wait until the writer restarts the txn. close(waitFirstGet) <-waitTxnRestart // Advance the clock and send a get operation again. This time // we use TestingCommandFilter so that a get operation is not // processed after the write intent is resolved (to prevent the // timestamp cache from being updated). manualClock.Increment(100) ts = clock.Now() if _, err := client.SendWrappedWith(rg1(store), nil, roachpb.Header{ Timestamp: ts, UserPriority: priority, }, &roachpb.GetRequest{Span: requestHeader}); err == nil { t.Fatal("unexpected success of get") } close(waitSecondGet) <-waitTxnComplete }
// InternalSetPriority sets the transaction priority. It is intended for // internal (testing) use only. func (txn *Txn) InternalSetPriority(priority int32) { // The negative user priority is translated on the server into a positive, // non-randomized, priority for the transaction. txn.db.userPriority = roachpb.UserPriority(-priority) }
func (m *Session_Transaction) Unmarshal(data []byte) error { l := len(data) iNdEx := 0 for iNdEx < l { preIndex := iNdEx var wire uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowSession } if iNdEx >= l { return io.ErrUnexpectedEOF } b := data[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { break } } fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { return fmt.Errorf("proto: Transaction: wiretype end group for non-group") } if fieldNum <= 0 { return fmt.Errorf("proto: Transaction: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Txn", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowSession } if iNdEx >= l { return io.ErrUnexpectedEOF } b := data[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthSession } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Txn.Unmarshal(data[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Timestamp", wireType) } var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowSession } if iNdEx >= l { return io.ErrUnexpectedEOF } b := data[iNdEx] iNdEx++ msglen |= (int(b) & 0x7F) << shift if b < 0x80 { break } } if msglen < 0 { return ErrInvalidLengthSession } postIndex := iNdEx + msglen if postIndex > l { return io.ErrUnexpectedEOF } if err := m.Timestamp.Unmarshal(data[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: if wireType != 1 { return fmt.Errorf("proto: wrong wireType = %d for field UserPriority", wireType) } var v uint64 if (iNdEx + 8) > l { return io.ErrUnexpectedEOF } iNdEx += 8 v = uint64(data[iNdEx-8]) v |= uint64(data[iNdEx-7]) << 8 v |= uint64(data[iNdEx-6]) << 16 v |= uint64(data[iNdEx-5]) << 24 v |= uint64(data[iNdEx-4]) << 32 v |= uint64(data[iNdEx-3]) << 40 v |= uint64(data[iNdEx-2]) << 48 v |= uint64(data[iNdEx-1]) << 56 m.UserPriority = github_com_cockroachdb_cockroach_roachpb.UserPriority(math.Float64frombits(v)) default: iNdEx = preIndex skippy, err := skipSession(data[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthSession } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } iNdEx += skippy } } if iNdEx > l { return io.ErrUnexpectedEOF } return nil }