// maybeBeginTxn begins a new transaction if a txn has been specified // in the request but has a nil ID. The new transaction is initialized // using the name and isolation in the otherwise uninitialized txn. // The Priority, if non-zero is used as a minimum. func (tc *TxnCoordSender) maybeBeginTxn(header *proto.RequestHeader) { if header.Txn != nil { if len(header.Txn.ID) == 0 { newTxn := proto.NewTransaction(header.Txn.Name, keys.KeyAddress(header.Key), header.GetUserPriority(), header.Txn.Isolation, tc.clock.Now(), tc.clock.MaxOffset().Nanoseconds()) // Use existing priority as a minimum. This is used on transaction // aborts to ratchet priority when creating successor transaction. if newTxn.Priority < header.Txn.Priority { newTxn.Priority = header.Txn.Priority } header.Txn = newTxn } } }
// updateForBatch updates the first argument (the header of a request contained // in a batch) from the second one (the batch header), returning an error when // inconsistencies are found. // It is checked that the individual call does not have a User, UserPriority // or Txn set that differs from the batch's. func updateForBatch(aHeader *proto.RequestHeader, bHeader proto.RequestHeader) error { // Disallow transaction, user and priority on individual calls, unless // equal. if aHeader.User != "" && aHeader.User != bHeader.User { return util.Error("conflicting user on call in batch") } if aPrio := aHeader.GetUserPriority(); aPrio != proto.Default_RequestHeader_UserPriority && aPrio != bHeader.GetUserPriority() { return util.Error("conflicting user priority on call in batch") } if aHeader.Txn != nil && !aHeader.Txn.Equal(bHeader.Txn) { return util.Error("conflicting transaction in transactional batch") } aHeader.User = bHeader.User aHeader.UserPriority = bHeader.UserPriority aHeader.Txn = bHeader.Txn return nil }
// updateResponseTxn updates the response txn based on the response // timestamp and error. The timestamp may have changed upon // encountering a newer write or read. Both the timestamp and the // priority may change depending on error conditions. func (tc *TxnCoordSender) updateResponseTxn(argsHeader *proto.RequestHeader, replyHeader *proto.ResponseHeader) { // Move txn timestamp forward to response timestamp if applicable. if replyHeader.Txn.Timestamp.Less(replyHeader.Timestamp) { replyHeader.Txn.Timestamp = replyHeader.Timestamp } // Take action on various errors. switch t := replyHeader.GoError().(type) { case *proto.ReadWithinUncertaintyIntervalError: // Mark the host as certain. See the protobuf comment for // Transaction.CertainNodes for details. replyHeader.Txn.CertainNodes.Add(argsHeader.Replica.NodeID) // If the reader encountered a newer write within the uncertainty // interval, move the timestamp forward, just past that write or // up to MaxTimestamp, whichever comes first. var candidateTS proto.Timestamp if t.ExistingTimestamp.Less(replyHeader.Txn.MaxTimestamp) { candidateTS = t.ExistingTimestamp candidateTS.Logical++ } else { candidateTS = replyHeader.Txn.MaxTimestamp } // Only change the timestamp if we're moving it forward. if replyHeader.Txn.Timestamp.Less(candidateTS) { replyHeader.Txn.Timestamp = candidateTS } replyHeader.Txn.Restart(argsHeader.GetUserPriority(), replyHeader.Txn.Priority, replyHeader.Txn.Timestamp) case *proto.TransactionAbortedError: // Increase timestamp if applicable. if replyHeader.Txn.Timestamp.Less(t.Txn.Timestamp) { replyHeader.Txn.Timestamp = t.Txn.Timestamp } replyHeader.Txn.Priority = t.Txn.Priority case *proto.TransactionPushError: // Increase timestamp if applicable. if replyHeader.Txn.Timestamp.Less(t.PusheeTxn.Timestamp) { replyHeader.Txn.Timestamp = t.PusheeTxn.Timestamp replyHeader.Txn.Timestamp.Logical++ // ensure this txn's timestamp > other txn } replyHeader.Txn.Restart(argsHeader.GetUserPriority(), t.PusheeTxn.Priority-1, replyHeader.Txn.Timestamp) case *proto.TransactionRetryError: // Increase timestamp if applicable. if replyHeader.Txn.Timestamp.Less(t.Txn.Timestamp) { replyHeader.Txn.Timestamp = t.Txn.Timestamp } replyHeader.Txn.Restart(argsHeader.GetUserPriority(), t.Txn.Priority, replyHeader.Txn.Timestamp) } }
// updateForBatch updates the first argument (the header of a request contained // in a batch) from the second one (the batch header), returning an error when // inconsistencies are found. // It is checked that the individual call does not have a User, UserPriority // or Txn set that differs from the batch's. func updateForBatch(args proto.Request, bHeader proto.RequestHeader) error { // Disallow transaction, user and priority on individual calls, unless // equal. aHeader := args.Header() if aHeader.User != "" && aHeader.User != bHeader.User { return util.Error("conflicting user on call in batch") } if aPrio := aHeader.GetUserPriority(); aPrio != proto.Default_RequestHeader_UserPriority && aPrio != bHeader.GetUserPriority() { return util.Error("conflicting user priority on call in batch") } aHeader.User = bHeader.User aHeader.UserPriority = bHeader.UserPriority // Only allow individual transactions on the requests of a batch if // - the batch is non-transactional, // - the individual transaction does not write intents, and // - the individual transaction is initialized. // The main usage of this is to allow mass-resolution of intents, which // entails sending a non-txn batch of transactional InternalResolveIntent. if aHeader.Txn != nil && !aHeader.Txn.Equal(bHeader.Txn) { if len(aHeader.Txn.ID) == 0 || proto.IsTransactionWrite(args) || bHeader.Txn != nil { return util.Error("conflicting transaction in transactional batch") } } else { aHeader.Txn = bHeader.Txn } return nil }
// UpdateForBatch updates the first argument (the header of a request contained // in a batch) from the second one (the batch header), returning an error when // inconsistencies are found. // It is checked that the individual call does not have a UserPriority // or Txn set that differs from the batch's. // TODO(tschottdorf): will go with #2143. func updateForBatch(args proto.Request, bHeader proto.RequestHeader) error { // Disallow transaction, user and priority on individual calls, unless // equal. aHeader := args.Header() if aPrio := aHeader.GetUserPriority(); aPrio != proto.Default_RequestHeader_UserPriority && aPrio != bHeader.GetUserPriority() { return util.Errorf("conflicting user priority on call in batch") } aHeader.UserPriority = bHeader.UserPriority aHeader.Txn = bHeader.Txn // reqs always take Txn from batch return nil }