Skip to content

Commit 0ae8c77

Browse files
authored
feat: transaction metric (#116)
* feat: transaction metric * fix: fix golint issue * chore: adjust mr comment * feat: add timeout metric
1 parent bf3b084 commit 0ae8c77

11 files changed

Lines changed: 146 additions & 12 deletions

File tree

cmd/cmd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ var (
126126
}
127127

128128
dbpack := server.NewServer()
129-
130129
for _, listenerConf := range conf.Listeners {
131130
switch listenerConf.ProtocolType {
132131
case config.Mysql:

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ require (
4949
github.com/gorilla/mux v1.8.0
5050
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
5151
github.com/jonboulle/clockwork v0.2.2 // indirect
52+
github.com/mattn/go-colorable v0.1.12 // indirect
53+
github.com/mattn/go-runewidth v0.0.13 // indirect
54+
github.com/mgechev/revive v1.2.1 // indirect
5255
github.com/soheilhy/cmux v0.1.5-0.20210205191134-5ec6847320e5 // indirect
5356
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
54-
golang.org/x/sys v0.0.0-20220519141025-dcacdad47464 // indirect
57+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
5558
golang.org/x/tools v0.1.10 // indirect
5659
google.golang.org/protobuf v1.27.1
5760
)

go.sum

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tj
119119
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
120120
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
121121
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
122+
github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4 h1:tFXjAxje9thrTF4h57Ckik+scJjTWdwAtZqZPtOT48M=
123+
github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4/go.mod h1:W8EnPSQ8Nv4fUjc/v1/8tHFqhuOJXnRub0dTfuAQktU=
122124
github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA=
123125
github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys=
124126
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@@ -225,7 +227,10 @@ github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod
225227
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
226228
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
227229
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
230+
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
231+
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
228232
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
233+
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
229234
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
230235
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
231236
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
@@ -560,17 +565,24 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
560565
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
561566
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
562567
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
568+
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
569+
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
570+
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
563571
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
564572
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
565573
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
566574
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
567575
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
568576
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
569577
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
578+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
579+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
570580
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
571581
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
572582
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
573583
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
584+
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
585+
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
574586
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
575587
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
576588
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
@@ -580,12 +592,17 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
580592
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg=
581593
github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ=
582594
github.com/mgechev/dots v0.0.0-20190921121421-c36f7dcfbb81/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
595+
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
596+
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
583597
github.com/mgechev/revive v1.0.2/go.mod h1:rb0dQy1LVAxW9SWy5R3LPUjevzUbUS316U5MFySA2lo=
598+
github.com/mgechev/revive v1.2.1 h1:GjFml7ZsoR0IrQ2E2YIvWFNS5GPDV7xNwvA5GM1HZC4=
599+
github.com/mgechev/revive v1.2.1/go.mod h1:+Ro3wqY4vakcYNtkBWdZC7dBg1xSB6sp054wWwmeFm0=
584600
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
585601
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
586602
github.com/minio/sio v0.3.0/go.mod h1:8b0yPp2avGThviy/+OCJBI6OMpvxoUuiLvE6F1lebhw=
587603
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
588604
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
605+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
589606
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
590607
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
591608
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
@@ -623,6 +640,8 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
623640
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60=
624641
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
625642
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
643+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
644+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
626645
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
627646
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
628647
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -748,6 +767,7 @@ github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qq
748767
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
749768
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
750769
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
770+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
751771
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
752772
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
753773
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
@@ -1171,12 +1191,15 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
11711191
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11721192
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11731193
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1194+
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11741195
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11751196
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11761197
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11771198
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11781199
golang.org/x/sys v0.0.0-20220519141025-dcacdad47464 h1:MpIuURY70f0iKp/oooEFtB2oENcHITo/z1b6u41pKCw=
11791200
golang.org/x/sys v0.0.0-20220519141025-dcacdad47464/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1201+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
1202+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11801203
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
11811204
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
11821205
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

pkg/dt/distributed_transaction_manger.go

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/cectc/dbpack/pkg/config"
3131
"github.com/cectc/dbpack/pkg/dt/api"
32+
"github.com/cectc/dbpack/pkg/dt/metrics"
3233
"github.com/cectc/dbpack/pkg/dt/storage"
3334
"github.com/cectc/dbpack/pkg/log"
3435
"github.com/cectc/dbpack/pkg/misc"
@@ -57,6 +58,7 @@ func InitDistributedTransactionManager(conf *config.DistributedTransaction, stor
5758
if conf.RetryDeadThreshold == 0 {
5859
conf.RetryDeadThreshold = DefaultRetryDeadThreshold
5960
}
61+
6062
manager = &DistributedTransactionManager{
6163
applicationID: conf.ApplicationID,
6264
storageDriver: storageDriver,
@@ -110,6 +112,7 @@ func (manager *DistributedTransactionManager) Begin(ctx context.Context, transac
110112
if err := manager.storageDriver.AddGlobalSession(ctx, gt); err != nil {
111113
return "", err
112114
}
115+
metrics.GlobalTransactionCounter.WithLabelValues(manager.applicationID, transactionName, metrics.TransactionStatusActive).Inc()
113116
manager.globalSessionQueue.AddAfter(gt, time.Duration(timeout)*time.Millisecond)
114117
log.Infof("successfully begin global transaction xid = {%s}", gt.XID)
115118
return xid, nil
@@ -144,6 +147,7 @@ func (manager *DistributedTransactionManager) BranchRegister(ctx context.Context
144147
if err := manager.storageDriver.AddBranchSession(ctx, bs); err != nil {
145148
return "", 0, err
146149
}
150+
metrics.BranchTransactionCounter.WithLabelValues(manager.applicationID, in.ResourceID, metrics.TransactionStatusActive).Inc()
147151
return branchID, branchSessionID, nil
148152
}
149153

@@ -220,14 +224,15 @@ func (manager *DistributedTransactionManager) _branchRollback(bs *api.BranchSess
220224
}
221225

222226
func (manager *DistributedTransactionManager) processGlobalSessions() error {
223-
globalSessions, err := manager.storageDriver.ListGlobalSession(context.Background(), manager.applicationID)
227+
ctx := context.Background()
228+
globalSessions, err := manager.storageDriver.ListGlobalSession(ctx, manager.applicationID)
224229
if err != nil {
225230
return err
226231
}
227232
for _, gs := range globalSessions {
228233
if gs.Status == api.Begin {
229234
if isGlobalSessionTimeout(gs) {
230-
if _, err := manager.Rollback(context.Background(), gs.XID); err != nil {
235+
if _, err := manager.Rollback(ctx, gs.XID); err != nil {
231236
return err
232237
}
233238
}
@@ -244,11 +249,21 @@ func (manager *DistributedTransactionManager) processGlobalSessions() error {
244249
if err != nil {
245250
return err
246251
}
252+
// branch session has been committed or rollbacked
247253
if len(bsKeys) == 0 {
248254
if err := manager.storageDriver.DeleteGlobalSession(context.Background(), gs.XID); err != nil {
249255
return err
250256
}
251257
log.Debugf("global session finished, key: %s", gs.XID)
258+
switch gs.Status {
259+
case api.Committing:
260+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusCommitted)
261+
case api.Rollbacking:
262+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusRollbacked)
263+
}
264+
} else {
265+
// global transaction timeout
266+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusTimeout)
252267
}
253268
}
254269
}
@@ -283,7 +298,7 @@ func (manager *DistributedTransactionManager) processNextGlobalSession(ctx conte
283298
}
284299
if newGlobalSession.Status == api.Begin {
285300
if isGlobalSessionTimeout(newGlobalSession) {
286-
_, err := manager.Rollback(context.Background(), newGlobalSession.XID)
301+
_, err = manager.Rollback(context.Background(), newGlobalSession.XID)
287302
if err != nil {
288303
log.Error(err)
289304
}
@@ -299,6 +314,15 @@ func (manager *DistributedTransactionManager) processNextGlobalSession(ctx conte
299314
log.Error(err)
300315
}
301316
log.Debugf("global session finished, key: %s", newGlobalSession.XID)
317+
switch newGlobalSession.Status {
318+
case api.Committing:
319+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusCommitted)
320+
case api.Rollbacking:
321+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusRollbacked)
322+
}
323+
} else {
324+
// global transaction timeout.
325+
manager.recordGlobalTransactionMetric(gs.TransactionName, metrics.TransactionStatusRollbacked)
302326
}
303327
}
304328
return true
@@ -320,6 +344,7 @@ func (manager *DistributedTransactionManager) processBranchSessions() error {
320344
manager.branchSessionQueue.Add(bs)
321345
case api.PhaseTwoRollbacking:
322346
if manager.IsRollingBackDead(bs) {
347+
metrics.BranchTransactionCounter.WithLabelValues(bs.ApplicationID, bs.ResourceID, metrics.TransactionStatusTimeout)
323348
log.Debugf("branch session rollback dead, key: %s, lock key: %s", bs.BranchID, bs.LockKey)
324349
if manager.rollbackRetryTimeoutUnlockEnable {
325350
log.Debugf("branch id: %d, lock key: %s released", bs.BranchID, bs.LockKey)
@@ -356,8 +381,14 @@ func (manager *DistributedTransactionManager) processNextBranchSession(ctx conte
356381
defer manager.branchSessionQueue.Done(obj)
357382

358383
bs := obj.(*api.BranchSession)
384+
var (
385+
status api.BranchSession_BranchStatus
386+
transactionStatus string
387+
err error
388+
)
359389
if bs.Status == api.PhaseTwoCommitting {
360-
status, err := manager.branchCommit(bs)
390+
transactionStatus = metrics.TransactionStatusCommitted
391+
status, err = manager.branchCommit(bs)
361392
if err != nil {
362393
log.Error(err)
363394
manager.branchSessionQueue.Add(obj)
@@ -367,14 +398,16 @@ func (manager *DistributedTransactionManager) processNextBranchSession(ctx conte
367398
}
368399
}
369400
if bs.Status == api.PhaseTwoRollbacking {
401+
transactionStatus = metrics.TransactionStatusRollbacked
370402
if manager.IsRollingBackDead(bs) {
403+
metrics.BranchTransactionCounter.WithLabelValues(bs.ApplicationID, bs.ResourceID, metrics.TransactionStatusTimeout)
371404
if manager.rollbackRetryTimeoutUnlockEnable {
372-
if _, err := manager.storageDriver.ReleaseLockKeys(context.Background(), bs.ResourceID, []string{bs.LockKey}); err != nil {
405+
if _, err := manager.storageDriver.ReleaseLockKeys(ctx, bs.ResourceID, []string{bs.LockKey}); err != nil {
373406
log.Error(err)
374407
}
375408
}
376409
} else {
377-
status, err := manager.branchRollback(bs)
410+
status, err = manager.branchRollback(bs)
378411
if err != nil {
379412
log.Error(err)
380413
manager.branchSessionQueue.Add(obj)
@@ -384,6 +417,14 @@ func (manager *DistributedTransactionManager) processNextBranchSession(ctx conte
384417
}
385418
}
386419
}
420+
421+
if status == api.Complete {
422+
metrics.BranchTransactionTimer.WithLabelValues(manager.applicationID, bs.ResourceID, transactionStatus).Observe(
423+
float64(int64(misc.CurrentTimeMillis()) - bs.BeginTime))
424+
metrics.BranchTransactionCounter.WithLabelValues(manager.applicationID, bs.ResourceID, metrics.TransactionStatusActive).Desc()
425+
metrics.BranchTransactionCounter.WithLabelValues(manager.applicationID, bs.ResourceID, transactionStatus).Inc()
426+
}
427+
387428
return true
388429
}
389430

@@ -395,6 +436,11 @@ func (manager *DistributedTransactionManager) watchBranchSession() {
395436
}
396437
}
397438

439+
func (manager *DistributedTransactionManager) recordGlobalTransactionMetric(transactionName string, transactionStatus string) {
440+
metrics.GlobalTransactionCounter.WithLabelValues(manager.applicationID, transactionName, metrics.TransactionStatusActive).Desc()
441+
metrics.GlobalTransactionCounter.WithLabelValues(manager.applicationID, transactionName, transactionStatus).Inc()
442+
}
443+
398444
func isGlobalSessionTimeout(gs *api.GlobalSession) bool {
399445
return (misc.CurrentTimeMillis() - uint64(gs.BeginTime)) > uint64(gs.Timeout)
400446
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 CECTC, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import "github.com/prometheus/client_golang/prometheus"
20+
21+
const (
22+
TransactionStatusActive = "active"
23+
TransactionStatusCommitted = "committed"
24+
TransactionStatusRollbacked = "rollbacked"
25+
TransactionStatusTimeout = "timeout"
26+
)
27+
28+
var (
29+
GlobalTransactionCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
30+
Namespace: "dbpack",
31+
Subsystem: "global_transaction",
32+
Name: "count",
33+
Help: "global transaction count",
34+
}, []string{"appid", "transactionname", "status"})
35+
36+
BranchTransactionCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
37+
Namespace: "dbpack",
38+
Subsystem: "branch_transaction",
39+
Name: "count",
40+
Help: "branch transaction count",
41+
}, []string{"appid", "resourceid", "status"})
42+
43+
BranchTransactionTimer = prometheus.NewHistogramVec(prometheus.HistogramOpts{
44+
Namespace: "dbpack",
45+
Subsystem: "branch_transaction",
46+
Name: "timer",
47+
Help: "global transaction timer",
48+
}, []string{"appid", "resourceid", "status"})
49+
)
50+
51+
func init() {
52+
prometheus.MustRegister(GlobalTransactionCounter)
53+
prometheus.MustRegister(BranchTransactionCounter)
54+
prometheus.MustRegister(BranchTransactionTimer)
55+
}

pkg/dt/storage/etcd/etcd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (s *store) AddBranchSession(ctx context.Context, branchSession *api.BranchS
111111
}
112112
txn = txn.If(cmpSlice...)
113113

114-
var ops []clientv3.Op
114+
ops := make([]clientv3.Op, 0, 2*len(rowKeys))
115115
for _, rowKey := range rowKeys {
116116
lockKey := fmt.Sprintf("lk/%s/%s", branchSession.XID, rowKey)
117117
ops = append(ops, clientv3.OpPut(lockKey, rowKey))

0 commit comments

Comments
 (0)