-
Notifications
You must be signed in to change notification settings - Fork 10
/
root_context.go
167 lines (141 loc) · 4.51 KB
/
root_context.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* This file is part of the hptx distribution (https://github.com/cectc/htpx).
* Copyright 2022 CECTC, Inc.
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/>.
*/
package context
import (
"context"
"fmt"
"strings"
"github.com/cectc/dbpack/pkg/dt/api"
"github.com/cectc/dbpack/pkg/log"
)
const (
KeyXID = "TX_XID"
KeyXIDInterceptorType = "tx-xid-interceptor-type"
KeyGlobalLockFlag = "TX_LOCK"
)
// RootContext store the global transaction context
type RootContext struct {
context.Context
// like thread local map
localMap map[string]interface{}
}
// NewRootContext return a pointer to RootContext
func NewRootContext(ctx context.Context) *RootContext {
rootCtx := &RootContext{
Context: ctx,
localMap: make(map[string]interface{}),
}
xID := ctx.Value(KeyXID)
if xID != nil {
xid := xID.(string)
rootCtx.Bind(xid)
}
return rootCtx
}
// Set store key value to RootContext
func (c *RootContext) Set(key string, value interface{}) {
if c.localMap == nil {
c.localMap = make(map[string]interface{})
}
c.localMap[key] = value
}
// Get get a value by given key from RootContext
func (c *RootContext) Get(key string) (value interface{}, exists bool) {
value, exists = c.localMap[key]
return
}
// GetXID from RootContext get xid
func (c *RootContext) GetXID() string {
xID := c.localMap[KeyXID]
xid, ok := xID.(string)
if ok && xid != "" {
return xid
}
xIDType := c.localMap[KeyXIDInterceptorType]
xidType, success := xIDType.(string)
if success && xidType != "" && strings.Contains(xidType, "_") {
return strings.Split(xidType, "_")[0]
}
return ""
}
// GetXIDInterceptorType from RootContext get xid interceptor type
func (c *RootContext) GetXIDInterceptorType() string {
xIDType := c.localMap[KeyXIDInterceptorType]
xidType, _ := xIDType.(string)
return xidType
}
// Bind bind xid with RootContext
func (c *RootContext) Bind(xid string) {
log.Debugf("bind %s", xid)
c.Set(KeyXID, xid)
}
// BindInterceptorType bind interceptor type with RootContext
func (c *RootContext) BindInterceptorType(xidType string) {
if xidType != "" {
xidTypes := strings.Split(xidType, "_")
if len(xidTypes) == 2 {
c.BindInterceptorTypeWithBranchType(xidTypes[0],
api.BranchSession_BranchType(api.BranchSession_BranchType_value[xidTypes[1]]))
}
}
}
// BindInterceptorTypeWithBranchType bind interceptor type and branch type with RootContext
func (c *RootContext) BindInterceptorTypeWithBranchType(xid string, branchType api.BranchSession_BranchType) {
xidType := fmt.Sprintf("%s_%s", xid, branchType.String())
log.Debugf("bind interceptor type xid=%s branchType=%s", xid, branchType.String())
c.Set(KeyXIDInterceptorType, xidType)
}
// BindGlobalLockFlag bind global lock flag with RootContext
func (c *RootContext) BindGlobalLockFlag() {
log.Debug("local transaction global lock support enabled")
c.Set(KeyGlobalLockFlag, KeyGlobalLockFlag)
}
// Unbind unbind xid with RootContext
func (c *RootContext) Unbind() string {
xID := c.localMap[KeyXID]
xid, ok := xID.(string)
if ok && xid != "" {
log.Debugf("unbind %s", xid)
delete(c.localMap, KeyXID)
return xid
}
return ""
}
// UnbindInterceptorType unbind interceptor type with RootContext
func (c *RootContext) UnbindInterceptorType() string {
xidType := c.localMap[KeyXIDInterceptorType]
xt, ok := xidType.(string)
if ok && xt != "" {
log.Debugf("unbind inteceptor type %s", xidType)
delete(c.localMap, KeyXIDInterceptorType)
return xt
}
return ""
}
// UnbindGlobalLockFlag unbind global lock flag with RootContext
func (c *RootContext) UnbindGlobalLockFlag() {
log.Debug("unbind global lock flag")
delete(c.localMap, KeyGlobalLockFlag)
}
// InGlobalTransaction determine whether the context is in global transaction
func (c *RootContext) InGlobalTransaction() bool {
return c.localMap[KeyXID] != nil
}
// RequireGlobalLock return global lock flag
func (c *RootContext) RequireGlobalLock() bool {
_, exists := c.localMap[KeyGlobalLockFlag]
return exists
}