// 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.sendInternal(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.sendInternal(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)() const key = "key" // Set up a filter to so that the get operation at Step 3 will return an error. var numGets int32 stopper := stop.NewStopper() defer stopper.Stop() manual := hlc.NewManualClock(123) cfg := storage.TestStoreConfig(hlc.NewClock(manual.UnixNano, time.Nanosecond)) cfg.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 } eng := engine.NewInMem(roachpb.Attributes{}, 10<<20) stopper.AddCloser(eng) store := createTestStoreWithEngine(t, eng, true, cfg, stopper, ) // Put an initial value. initVal := []byte("initVal") err := store.DB().Put(context.TODO(), 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(context.TODO(), 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. manual.Increment(100) priority := roachpb.UserPriority(-math.MaxInt32) requestHeader := roachpb.Span{ Key: roachpb.Key(key), } if _, err := client.SendWrappedWith(context.Background(), rg1(store), roachpb.Header{ Timestamp: cfg.Clock.Now(), 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). manual.Increment(100) if _, err := client.SendWrappedWith(context.Background(), rg1(store), roachpb.Header{ Timestamp: cfg.Clock.Now(), 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.UserPriority = roachpb.UserPriority(-priority) }