forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCCFenceHandler.java
404 lines (364 loc) · 16.1 KB
/
TCCFenceHandler.java
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.rm.tcc;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import io.seata.common.exception.FrameworkErrorCode;
import io.seata.common.exception.SkipCallbackWrapperException;
import io.seata.common.executor.Callback;
import io.seata.common.thread.NamedThreadFactory;
import io.seata.rm.tcc.constant.TCCFenceConstant;
import io.seata.rm.tcc.exception.TCCFenceException;
import io.seata.rm.tcc.store.TCCFenceDO;
import io.seata.rm.tcc.store.TCCFenceStore;
import io.seata.rm.tcc.store.db.TCCFenceStoreDataBaseDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionTemplate;
/**
* TCC Fence Handler(idempotent, non_rollback, suspend)
*
* @author kaka2code
*/
public class TCCFenceHandler {
private TCCFenceHandler() {
throw new IllegalStateException("Utility class");
}
private static final Logger LOGGER = LoggerFactory.getLogger(TCCFenceHandler.class);
private static final TCCFenceStore TCC_FENCE_DAO = TCCFenceStoreDataBaseDAO.getInstance();
private static DataSource dataSource;
private static TransactionTemplate transactionTemplate;
private static final int MAX_THREAD_CLEAN = 1;
private static final int MAX_QUEUE_SIZE = 500;
/**
* limit of delete record by date (per sql)
*/
private static final int LIMIT_DELETE = 1000;
private static final LinkedBlockingQueue<FenceLogIdentity> LOG_QUEUE = new LinkedBlockingQueue<>(MAX_QUEUE_SIZE);
private static FenceLogCleanRunnable fenceLogCleanRunnable;
private static ExecutorService logCleanExecutor;
static {
try {
initLogCleanExecutor();
} catch (Exception e) {
LOGGER.error("init fence log clean executor error", e);
}
}
public static DataSource getDataSource() {
return TCCFenceHandler.dataSource;
}
public static void setDataSource(DataSource dataSource) {
TCCFenceHandler.dataSource = dataSource;
}
public static void setTransactionTemplate(TransactionTemplate transactionTemplate) {
TCCFenceHandler.transactionTemplate = transactionTemplate;
}
/**
* tcc prepare method enhanced
*
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param actionName the action name
* @param targetCallback the target callback
* @return the boolean
*/
public static Object prepareFence(String xid, Long branchId, String actionName, Callback<Object> targetCallback) {
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_TRIED);
LOGGER.info("TCC fence prepare result: {}. xid: {}, branchId: {}", result, xid, branchId);
if (result) {
return targetCallback.execute();
} else {
throw new TCCFenceException(String.format("Insert tcc fence record error, prepare fence failed. xid= %s, branchId= %s", xid, branchId),
FrameworkErrorCode.InsertRecordError);
}
} catch (TCCFenceException e) {
if (e.getErrcode() == FrameworkErrorCode.DuplicateKeyException) {
LOGGER.error("Branch transaction has already rollbacked before,prepare fence failed. xid= {},branchId = {}", xid, branchId);
addToLogCleanQueue(xid, branchId);
}
status.setRollbackOnly();
throw new SkipCallbackWrapperException(e);
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
}
});
}
/**
* tcc commit method enhanced
*
* @param commitMethod commit method
* @param targetTCCBean target tcc bean
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param args commit method's parameters
* @return the boolean
*/
public static boolean commitFence(Method commitMethod, Object targetTCCBean,
String xid, Long branchId, Object[] args) {
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);
if (tccFenceDO == null) {
throw new TCCFenceException(String.format("TCC fence record not exists, commit fence method failed. xid= %s, branchId= %s", xid, branchId),
FrameworkErrorCode.RecordNotExists);
}
if (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {
LOGGER.info("Branch transaction has already committed before. idempotency rejected. xid: {}, branchId: {}, status: {}", xid, branchId, tccFenceDO.getStatus());
return true;
}
if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Branch transaction status is unexpected. xid: {}, branchId: {}, status: {}", xid, branchId, tccFenceDO.getStatus());
}
return false;
}
return updateStatusAndInvokeTargetMethod(conn, commitMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_COMMITTED, status, args);
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
}
});
}
/**
* tcc rollback method enhanced
*
* @param rollbackMethod rollback method
* @param targetTCCBean target tcc bean
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param args rollback method's parameters
* @param actionName the action name
* @return the boolean
*/
public static boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
String xid, Long branchId, Object[] args, String actionName) {
return transactionTemplate.execute(status -> {
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
TCCFenceDO tccFenceDO = TCC_FENCE_DAO.queryTCCFenceDO(conn, xid, branchId);
// non_rollback
if (tccFenceDO == null) {
boolean result = insertTCCFenceLog(conn, xid, branchId, actionName, TCCFenceConstant.STATUS_SUSPENDED);
LOGGER.info("Insert tcc fence record result: {}. xid: {}, branchId: {}", result, xid, branchId);
if (!result) {
throw new TCCFenceException(String.format("Insert tcc fence record error, rollback fence method failed. xid= %s, branchId= %s", xid, branchId),
FrameworkErrorCode.InsertRecordError);
}
return true;
} else {
if (TCCFenceConstant.STATUS_ROLLBACKED == tccFenceDO.getStatus() || TCCFenceConstant.STATUS_SUSPENDED == tccFenceDO.getStatus()) {
LOGGER.info("Branch transaction had already rollbacked before, idempotency rejected. xid: {}, branchId: {}, status: {}", xid, branchId, tccFenceDO.getStatus());
return true;
}
if (TCCFenceConstant.STATUS_COMMITTED == tccFenceDO.getStatus()) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Branch transaction status is unexpected. xid: {}, branchId: {}, status: {}", xid, branchId, tccFenceDO.getStatus());
}
return false;
}
}
return updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, TCCFenceConstant.STATUS_ROLLBACKED, status, args);
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
}
});
}
/**
* Insert TCC fence log
*
* @param conn the db connection
* @param xid the xid
* @param branchId the branchId
* @param status the status
* @return the boolean
*/
private static boolean insertTCCFenceLog(Connection conn, String xid, Long branchId, String actionName, Integer status) {
TCCFenceDO tccFenceDO = new TCCFenceDO();
tccFenceDO.setXid(xid);
tccFenceDO.setBranchId(branchId);
tccFenceDO.setActionName(actionName);
tccFenceDO.setStatus(status);
return TCC_FENCE_DAO.insertTCCFenceDO(conn, tccFenceDO);
}
/**
* Update TCC Fence status and invoke target method
*
* @param method target method
* @param targetTCCBean target bean
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param status the tcc fence status
* @return the boolean
*/
private static boolean updateStatusAndInvokeTargetMethod(Connection conn, Method method, Object targetTCCBean,
String xid, Long branchId, int status,
TransactionStatus transactionStatus,
Object[] args) throws Exception {
boolean result = TCC_FENCE_DAO.updateTCCFenceDO(conn, xid, branchId, status, TCCFenceConstant.STATUS_TRIED);
if (result) {
// invoke two phase method
Object ret = method.invoke(targetTCCBean, args);
if (null != ret) {
if (ret instanceof TwoPhaseResult) {
result = ((TwoPhaseResult) ret).isSuccess();
} else {
result = (boolean) ret;
}
// If the business execution result is false, the transaction will be rolled back
if (!result) {
transactionStatus.setRollbackOnly();
}
}
}
return result;
}
/**
* Delete TCC Fence
*
* @param xid the global transaction id
* @param branchId the branch transaction id
* @return the boolean
*/
public static boolean deleteFence(String xid, Long branchId) {
return transactionTemplate.execute(status -> {
boolean ret = false;
try {
Connection conn = DataSourceUtils.getConnection(dataSource);
ret = TCC_FENCE_DAO.deleteTCCFenceDO(conn, xid, branchId);
} catch (RuntimeException e) {
status.setRollbackOnly();
LOGGER.error("delete fence log failed, xid: {}, branchId: {}", xid, branchId, e);
}
return ret;
});
}
public static int deleteFenceByDate(Date datetime) {
DataSource dataSource = TCCFenceHandler.getDataSource();
Connection connection = null;
int total = 0;
try {
connection = DataSourceUtils.getConnection(dataSource);
if(isOracle(connection)){
//delete by date if DB is oracle
return TCC_FENCE_DAO.deleteTCCFenceDOByDate(connection, datetime);
}
//delete by id if DB is not oracle
while (true) {
Set<String> xidSet = TCC_FENCE_DAO.queryEndStatusXidsByDate(connection, datetime, LIMIT_DELETE);
if (xidSet.isEmpty()) {
break;
}
total += TCC_FENCE_DAO.deleteTCCFenceDO(connection, new ArrayList<>(xidSet));
}
} catch (RuntimeException e) {
LOGGER.error("delete fence log failed ", e);
} finally {
if (connection != null) {
DataSourceUtils.releaseConnection(connection, dataSource);
}
}
return total;
}
private static boolean isOracle(Connection connection) {
try {
String driverName = connection.getMetaData().getDriverName();
return driverName.toUpperCase().contains("ORACLE");
} catch (SQLException e) {
LOGGER.error("get db type fail",e);
}
return false;
}
private static void initLogCleanExecutor() {
logCleanExecutor = new ThreadPoolExecutor(MAX_THREAD_CLEAN, MAX_THREAD_CLEAN, Integer.MAX_VALUE,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
new NamedThreadFactory("fenceLogCleanThread", MAX_THREAD_CLEAN, true)
);
fenceLogCleanRunnable = new FenceLogCleanRunnable();
logCleanExecutor.submit(fenceLogCleanRunnable);
}
private static void addToLogCleanQueue(final String xid, final long branchId) {
FenceLogIdentity logIdentity = new FenceLogIdentity();
logIdentity.setXid(xid);
logIdentity.setBranchId(branchId);
try {
LOG_QUEUE.add(logIdentity);
} catch (Exception e) {
LOGGER.warn("Insert tcc fence record into queue for async delete error,xid:{},branchId:{}", xid, branchId, e);
}
}
/**
* clean fence log that has the final status runnable.
*
* @see TCCFenceConstant
*/
private static class FenceLogCleanRunnable implements Runnable {
@Override
public void run() {
while (true) {
try {
FenceLogIdentity logIdentity = LOG_QUEUE.take();
boolean ret = TCCFenceHandler.deleteFence(logIdentity.getXid(), logIdentity.getBranchId());
if (!ret) {
LOGGER.error("delete fence log failed, xid: {}, branchId: {}", logIdentity.getXid(), logIdentity.getBranchId());
}
} catch (InterruptedException e) {
LOGGER.error("take fence log from queue for clean be interrupted", e);
} catch (Exception e) {
LOGGER.error("exception occur when clean fence log", e);
}
}
}
}
private static class FenceLogIdentity {
/**
* the global transaction id
*/
private String xid;
/**
* the branch transaction id
*/
private Long branchId;
public String getXid() {
return xid;
}
public Long getBranchId() {
return branchId;
}
public void setXid(String xid) {
this.xid = xid;
}
public void setBranchId(Long branchId) {
this.branchId = branchId;
}
}
}