-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oracle_undo_log_manager.go
288 lines (239 loc) · 7.57 KB
/
oracle_undo_log_manager.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package oci8
import (
"database/sql"
"database/sql/driver"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
const (
DeleteUndoLogSql = "DELETE FROM undo_log WHERE xid = :1 and branch_id = :2"
DeleteUndoLogByCreateSql = "DELETE FROM undo_log WHERE log_created <= :1 LIMIT :2"
InsertUndoLogSql = "INSERT INTO UNDO_LOG (id, branch_id, xid, context, rollback_info, log_status, log_created, log_modified)" +
"VALUES (UNDO_LOG_SEQ.NEXTVAL, :1, :2, :3, :4, :5, sysdate, sysdate)"
SelectUndoLogSql = "SELECT branch_id, xid, context, rollback_info, log_status FROM undo_log " +
"WHERE xid = :1 AND branch_id = :2"
)
type State byte
const (
Normal State = iota
GlobalFinished
)
func (state State) String() string {
switch state {
case Normal:
return "Normal"
case GlobalFinished:
return "GlobalFinished"
default:
return fmt.Sprintf("%d", state)
}
}
type OracleUndoLogManager struct {
}
func GetUndoLogManager() OracleUndoLogManager {
return OracleUndoLogManager{}
}
func (manager OracleUndoLogManager) FlushUndoLogs(conn *Conn) error {
// defer func() {
// if err := recover(); err != nil {
// errLog.Print(err)
// }
// }()
ctx := conn.ctx
xid := ctx.xid
branchID := ctx.branchID
branchUndoLog := &branchUndoLog{
Xid: xid,
BranchID: branchID,
SqlUndoLogs: ctx.sqlUndoItemsBuffer,
}
parser := GetUndoLogParser()
undoLogContent := parser.Encode(branchUndoLog)
fmt.Printf("Flushing UNDO LOG: %s", string(undoLogContent))
println(undoLogContent)
return manager.insertUndoLogWithNormal(conn, xid, branchID, buildContext(parser.GetName()), undoLogContent)
}
func (manager OracleUndoLogManager) Undo(conn *Conn, xid string, branchID int64, resourceID string) error {
tx, err := conn.Begin()
if err != nil {
return err
}
args := []driver.Value{xid, branchID}
rows, err := conn.prepareQuery(SelectUndoLogSql, args)
if err != nil {
return err
}
exists := false
undoLogs := make([]*branchUndoLog, 0)
var context string
var rollbackInfo []byte
var state int64
vals := make([]driver.Value, 5)
dest := []interface{}{&branchID, &xid, &context, &rollbackInfo, &state}
for {
err := rows.Next(vals)
if err != nil {
break
}
for i, sv := range vals {
err := convertAssignRows(dest[i], sv)
if err != nil {
return fmt.Errorf(`sql: Scan error on column index %d, name %q: %v`, i, rows.Columns()[i], err)
}
}
exists = true
if State(state) != Normal {
fmt.Printf("xid %s branch %d, ignore %s undo_log", xid, branchID, State(state).String())
return nil
}
//serializer := getSerializer(context)
parser := GetUndoLogParser()
branchUndoLog := parser.Decode(rollbackInfo)
undoLogs = append(undoLogs, branchUndoLog)
}
rows.Close()
for _, branchUndoLog := range undoLogs {
sqlUndoLogs := branchUndoLog.SqlUndoLogs
for _, sqlUndoLog := range sqlUndoLogs {
tableMetaCache, err := GetTableMetaCache(conn.cfg.Username)
if err != nil {
return errors.WithStack(err)
}
tableMeta, err := tableMetaCache.GetTableMeta(conn, sqlUndoLog.TableName)
if err != nil {
tx.Rollback()
return errors.WithStack(err)
}
sqlUndoLog.SetTableMeta(tableMeta)
err1 := NewOracleUndoExecutor(*sqlUndoLog).Execute(conn)
if err1 != nil {
tx.Rollback()
return errors.WithStack(err1)
}
}
}
if exists {
_, err := conn.execAlways(DeleteUndoLogSql, args)
if err != nil {
tx.Rollback()
return errors.WithStack(err)
}
fmt.Printf("xid %s branch %d, undo_log deleted with %s", xid, branchID,
GlobalFinished.String())
err = tx.Commit()
if err != nil {
tx.Rollback()
return err
}
} else {
manager.insertUndoLogWithGlobalFinished(conn, xid, branchID,
buildContext(GetUndoLogParser().GetName()), GetUndoLogParser().GetDefaultContent())
tx.Commit()
}
return nil
}
func (manager OracleUndoLogManager) selectUndolog(conn *Conn, xid string, branchID int64) ([]*branchUndoLog, error) {
args := []driver.Value{xid, branchID}
rows, err := conn.prepareQuery(SelectUndoLogSql, args)
undoLogs := make([]*branchUndoLog, 0)
if err != nil {
return undoLogs, err
}
var branchID2 sql.NullInt64
var xid2, context sql.NullString
var rollbackInfo sql.RawBytes
var state sql.NullInt32
vals := make([]driver.Value, 5)
dest := []interface{}{&branchID, &xid, &context, &rollbackInfo, &state}
for {
err := rows.Next(vals)
if err != nil {
break
}
for i, sv := range vals {
err := convertAssignRows(dest[i], sv)
if err != nil {
return undoLogs, fmt.Errorf(`sql: Scan error on column index %d, name %q: %v`, i, rows.Columns()[i], err)
}
}
if State(state.Int32) != Normal {
xid2val, _ := xid2.Value()
branchID2val, _ := branchID2.Value()
fmt.Printf("xid %s branch %s, ignore %s undo_log", xid2val, branchID2val, State(state.Int32).String())
return undoLogs, nil
}
//serializer := getSerializer(context)
parser := GetUndoLogParser()
branchUndoLog := parser.Decode(rollbackInfo)
undoLogs = append(undoLogs, branchUndoLog)
}
rows.Close()
return undoLogs, err
}
func (manager OracleUndoLogManager) DeleteUndoLog(conn *Conn, xid string, branchID int64) error {
args := []driver.Value{xid, branchID}
result, err := conn.execAlways(DeleteUndoLogSql, args)
if err != nil {
return err
}
affectCount, _ := result.RowsAffected()
fmt.Printf("%d undo log deleted by xid:%s and branchID:%d", affectCount, xid, branchID)
return nil
}
func (manager OracleUndoLogManager) BatchDeleteUndoLog(conn *Conn, xids []string, branchIDs []int64) error {
if xids == nil || branchIDs == nil || len(xids) == 0 || len(branchIDs) == 0 {
return nil
}
xidSize := len(xids)
branchIDSize := len(branchIDs)
batchDeleteSql := toBatchDeleteUndoLogSql(xidSize, branchIDSize)
var args = make([]driver.Value, 0, xidSize+branchIDSize)
for _, xid := range xids {
args = append(args, xid)
}
for _, branchID := range branchIDs {
args = append(args, branchID)
}
result, err := conn.execAlways(batchDeleteSql, args)
if err != nil {
return err
}
affectCount, _ := result.RowsAffected()
fmt.Printf("%d undo log deleted by xids:%v and branchIDs:%v", affectCount, xids, branchIDs)
return nil
}
func (manager OracleUndoLogManager) DeleteUndoLogByLogCreated(conn *Conn, logCreated time.Time, limitRows int) (sql.Result, error) {
args := []driver.Value{logCreated, limitRows}
result, err := conn.execAlways(DeleteUndoLogByCreateSql, args)
return result, err
}
func toBatchDeleteUndoLogSql(xidSize int, branchIDSize int) string {
var sb strings.Builder
fmt.Fprint(&sb, "DELETE FROM undo_log WHERE xid in ")
fmt.Fprint(&sb, appendInParam(xidSize))
fmt.Fprint(&sb, " AND branch_id in ")
fmt.Fprint(&sb, appendInParam(branchIDSize))
return sb.String()
}
func (manager OracleUndoLogManager) insertUndoLogWithNormal(conn *Conn, xid string, branchID int64,
rollbackCtx string, undoLogContent []byte) error {
return manager.insertUndoLog(conn, xid, branchID, rollbackCtx, undoLogContent, Normal)
}
func (manager OracleUndoLogManager) insertUndoLogWithGlobalFinished(conn *Conn, xid string, branchID int64,
rollbackCtx string, undoLogContent []byte) error {
return manager.insertUndoLog(conn, xid, branchID, rollbackCtx, undoLogContent, GlobalFinished)
}
func (manager OracleUndoLogManager) insertUndoLog(conn *Conn, xid string, branchID int64,
rollbackCtx string, undoLogContent []byte, state State) error {
args := []driver.Value{branchID, xid, rollbackCtx, undoLogContent, int64(state)}
_, err := conn.execAlways(InsertUndoLogSql, args)
return err
}
func buildContext(serializer string) string {
return fmt.Sprintf("serializer=%s", serializer)
}
func getSerializer(context string) string {
return context[10:]
}