// beginCmd waits for any overlapping, already-executing commands via // the command queue and adds itself to the queue to gate follow-on // commands which overlap its key range. This method will block if // there are any overlapping commands already in the queue. Returns // the command queue insertion key, to be supplied to subsequent // invocation of endCmd(). func (r *Range) beginCmd(header *proto.RequestHeader, readOnly bool) interface{} { r.Lock() var wg sync.WaitGroup r.cmdQ.GetWait(header.Key, header.EndKey, readOnly, &wg) cmdKey := r.cmdQ.Add(header.Key, header.EndKey, readOnly) r.Unlock() wg.Wait() // Update the incoming timestamp if unset. Wait until after any // preceding command(s) for key range are complete so that the node // clock has been updated to the high water mark of any commands // which might overlap this one in effect. if header.Timestamp.Equal(proto.ZeroTimestamp) { header.Timestamp = r.rm.Clock().Now() } return cmdKey }
// 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 storage.TestingCommandFilter = func(args proto.Request) error { if _, ok := args.(*proto.GetRequest); ok && args.Header().Key.Equal(proto.Key(key)) && args.Header().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 util.Errorf("Test") } } return nil } defer func() { storage.TestingCommandFilter = nil }() manualClock := hlc.NewManualClock(0) clock := hlc.NewClock(manualClock.UnixNano) store, stopper := createTestStoreWithEngine(t, engine.NewInMem(proto.Attributes{}, 10<<20), clock, true, nil) defer stopper.Stop() // 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 := &client.Batch{} err = txn.Commit(b) return err }); 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 := int32(math.MaxInt32) requestHeader := proto.RequestHeader{ Key: proto.Key(key), RaftID: 1, Replica: proto.Replica{StoreID: store.StoreID()}, UserPriority: &priority, Timestamp: clock.Now(), } getCall := proto.Call{ Args: &proto.GetRequest{ RequestHeader: requestHeader, }, Reply: &proto.GetResponse{}, } err = store.ExecuteCmd(context.Background(), getCall) if 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) requestHeader.Timestamp = clock.Now() getCall = proto.Call{ Args: &proto.GetRequest{ RequestHeader: requestHeader, }, Reply: &proto.GetResponse{}, } err = store.ExecuteCmd(context.Background(), getCall) if err == nil { t.Fatal("unexpected success of get") } close(waitSecondGet) <-waitTxnComplete }