-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
contention.go
116 lines (100 loc) · 3.86 KB
/
contention.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package contentionpb
import (
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
const singleIndentation = " "
const doubleIndentation = singleIndentation + singleIndentation
const tripleIndentation = doubleIndentation + singleIndentation
const contentionEventsStr = "num contention events:"
const cumulativeContentionTimeStr = "cumulative contention time:"
const contendingTxnsStr = "contending txns:"
func (ice IndexContentionEvents) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("tableID=%d indexID=%d\n", ice.TableID, ice.IndexID))
b.WriteString(fmt.Sprintf("%s%s %d\n", singleIndentation, contentionEventsStr, ice.NumContentionEvents))
b.WriteString(fmt.Sprintf("%s%s %s\n", singleIndentation, cumulativeContentionTimeStr, ice.CumulativeContentionTime))
b.WriteString(fmt.Sprintf("%skeys:\n", singleIndentation))
for i := range ice.Events {
b.WriteString(ice.Events[i].String())
}
return b.String()
}
func (skc SingleKeyContention) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("%s%s %s\n", doubleIndentation, skc.Key, contendingTxnsStr))
for i := range skc.Txns {
b.WriteString(skc.Txns[i].String())
}
return b.String()
}
func toString(stx SingleTxnContention, indentation string) string {
return fmt.Sprintf("%sid=%s count=%d\n", indentation, stx.TxnID, stx.Count)
}
func (stx SingleTxnContention) String() string {
return toString(stx, tripleIndentation)
}
func (skc SingleNonSQLKeyContention) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf("non-SQL key %s %s\n", skc.Key, contendingTxnsStr))
b.WriteString(fmt.Sprintf("%s%s %d\n", singleIndentation, contentionEventsStr, skc.NumContentionEvents))
b.WriteString(fmt.Sprintf("%s%s %s\n", singleIndentation, cumulativeContentionTimeStr, skc.CumulativeContentionTime))
for i := range skc.Txns {
b.WriteString(toString(skc.Txns[i], doubleIndentation))
}
return b.String()
}
// Valid returns if the ResolvedTxnID is valid.
func (r *ResolvedTxnID) Valid() bool {
return !uuid.Nil.Equal(r.TxnID)
}
// Valid returns if the ExtendedContentionEvent is valid.
func (e *ExtendedContentionEvent) Valid() bool {
return !uuid.Nil.Equal(e.BlockingEvent.TxnMeta.ID)
}
// Hash returns a hash that's unique to ExtendedContentionEvent using
// blocking txn's txnID, waiting txn's txnID and the event waiting stmt id.
func (e *ExtendedContentionEvent) Hash() uint64 {
hash := util.MakeFNV64()
hashUUID(e.BlockingEvent.TxnMeta.ID, &hash)
hashUUID(e.WaitingTxnID, &hash)
hashClusterUniqueID(e.WaitingStmtID, &hash)
return hash.Sum()
}
// hashClusterUniqueID adds the hash of the clusterunique.ID into the fnv.
// A clusterunique.ID is an uint128. To hash we treat it as two uint64 integers,
// since uint128 is has a lo and hi uint64.
func hashClusterUniqueID(id clusterunique.ID, hash *util.FNV64) {
hash.Add(id.Lo)
hash.Add(id.Hi)
}
// hashUUID adds the hash of the uuid into the fnv.
// An uuid is a 16 byte array. To hash UUID, we treat it as two uint64 integers,
// since uint64 is 8-byte. This is why we decode the byte array twice and add
// the resulting uint64 into the fnv each time.
func hashUUID(u uuid.UUID, fnv *util.FNV64) {
b := u.GetBytes()
b, val, err := encoding.DecodeUint64Descending(b)
if err != nil {
panic(err)
}
fnv.Add(val)
_, val, err = encoding.DecodeUint64Descending(b)
if err != nil {
panic(err)
}
fnv.Add(val)
}