Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type LockCtx struct {
PessimisticLockWaited *int32
LockKeysDuration *int64
LockKeysCount *int32
SharedLockKeysCount *int32
ReturnValues bool
CheckExistence bool
LockOnlyIfExists bool
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const (
LblBatchGet = "batch_get"
LblGet = "get"
LblLockKeys = "lock_keys"
LblSharedLockKeys = "shared_lock_keys"
LabelBatchRecvLoop = "batch-recv-loop"
LabelBatchSendLoop = "batch-send-loop"
LblAddress = "address"
Expand Down
24 changes: 14 additions & 10 deletions metrics/shortcuts.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ import "github.com/prometheus/client_golang/prometheus"

// Shortcuts for performance improvement.
var (
TxnCmdHistogramWithCommitInternal prometheus.Observer
TxnCmdHistogramWithCommitGeneral prometheus.Observer
TxnCmdHistogramWithRollbackInternal prometheus.Observer
TxnCmdHistogramWithRollbackGeneral prometheus.Observer
TxnCmdHistogramWithBatchGetInternal prometheus.Observer
TxnCmdHistogramWithBatchGetGeneral prometheus.Observer
TxnCmdHistogramWithGetInternal prometheus.Observer
TxnCmdHistogramWithGetGeneral prometheus.Observer
TxnCmdHistogramWithLockKeysInternal prometheus.Observer
TxnCmdHistogramWithLockKeysGeneral prometheus.Observer
TxnCmdHistogramWithCommitInternal prometheus.Observer
TxnCmdHistogramWithCommitGeneral prometheus.Observer
TxnCmdHistogramWithRollbackInternal prometheus.Observer
TxnCmdHistogramWithRollbackGeneral prometheus.Observer
TxnCmdHistogramWithBatchGetInternal prometheus.Observer
TxnCmdHistogramWithBatchGetGeneral prometheus.Observer
TxnCmdHistogramWithGetInternal prometheus.Observer
TxnCmdHistogramWithGetGeneral prometheus.Observer
TxnCmdHistogramWithLockKeysInternal prometheus.Observer
TxnCmdHistogramWithLockKeysGeneral prometheus.Observer
TxnCmdHistogramWithSharedLockKeysInternal prometheus.Observer
TxnCmdHistogramWithSharedLockKeysGeneral prometheus.Observer

RawkvCmdHistogramWithGet prometheus.Observer
RawkvCmdHistogramWithBatchGet prometheus.Observer
Expand Down Expand Up @@ -195,6 +197,8 @@ func initShortcuts() {
TxnCmdHistogramWithGetGeneral = TiKVTxnCmdHistogram.WithLabelValues(LblGet, LblGeneral)
TxnCmdHistogramWithLockKeysInternal = TiKVTxnCmdHistogram.WithLabelValues(LblLockKeys, LblInternal)
TxnCmdHistogramWithLockKeysGeneral = TiKVTxnCmdHistogram.WithLabelValues(LblLockKeys, LblGeneral)
TxnCmdHistogramWithSharedLockKeysInternal = TiKVTxnCmdHistogram.WithLabelValues(LblSharedLockKeys, LblInternal)
TxnCmdHistogramWithSharedLockKeysGeneral = TiKVTxnCmdHistogram.WithLabelValues(LblSharedLockKeys, LblGeneral)

RawkvCmdHistogramWithGet = TiKVRawkvCmdHistogram.WithLabelValues("get")
RawkvCmdHistogramWithBatchGet = TiKVRawkvCmdHistogram.WithLabelValues("batch_get")
Expand Down
38 changes: 33 additions & 5 deletions txnkv/transaction/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,18 @@ func (txn *KVTxn) lockKeys(ctx context.Context, lockCtx *tikv.LockCtx, fn func()
}

defer func() {
if txn.isInternal() {
metrics.TxnCmdHistogramWithLockKeysInternal.Observe(time.Since(startTime).Seconds())
if lockCtx.InShareMode {
if txn.isInternal() {
metrics.TxnCmdHistogramWithSharedLockKeysInternal.Observe(time.Since(startTime).Seconds())
} else {
metrics.TxnCmdHistogramWithSharedLockKeysGeneral.Observe(time.Since(startTime).Seconds())
}
} else {
metrics.TxnCmdHistogramWithLockKeysGeneral.Observe(time.Since(startTime).Seconds())
if txn.isInternal() {
metrics.TxnCmdHistogramWithLockKeysInternal.Observe(time.Since(startTime).Seconds())
} else {
metrics.TxnCmdHistogramWithLockKeysGeneral.Observe(time.Since(startTime).Seconds())
}
}
if err == nil {
if lockCtx.PessimisticLockWaited != nil {
Expand All @@ -1277,8 +1285,14 @@ func (txn *KVTxn) lockKeys(ctx context.Context, lockCtx *tikv.LockCtx, fn func()
}
}
}
if lockCtx.LockKeysCount != nil {
*lockCtx.LockKeysCount += int32(len(keys))
if lockCtx.InShareMode {
if lockCtx.SharedLockKeysCount != nil {
*lockCtx.SharedLockKeysCount += int32(len(keys))
}
} else {
if lockCtx.LockKeysCount != nil {
*lockCtx.LockKeysCount += int32(len(keys))
}
}
if lockCtx.Stats != nil {
lockCtx.Stats.TotalTime = time.Since(startTime)
Expand All @@ -1297,6 +1311,20 @@ func (txn *KVTxn) lockKeys(ctx context.Context, lockCtx *tikv.LockCtx, fn func()
return errors.New("trying to perform aggressive locking in optimistic transaction")
}

if lockCtx.InShareMode {
// create shared lock span in tracing.
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("Shared Lock", opentracing.ChildOf(span.Context()))
defer span1.Finish()
ctx = opentracing.ContextWithSpan(ctx, span1)
}

// Shared lock in aggressive locking mode is not supported.
if txn.IsInAggressiveLockingMode() {
txn.DoneAggressiveLocking(ctx)
}
}

memBuf := txn.us.GetMemBuffer()
// Avoid data race with concurrent updates to the memBuf
memBuf.RLock()
Expand Down