-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
DataBaseSessionManagerTest.java
600 lines (509 loc) · 23.9 KB
/
DataBaseSessionManagerTest.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* 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.server.session.db;
import io.seata.common.XID;
import io.seata.common.util.IOUtil;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.BranchStatus;
import io.seata.core.model.BranchType;
import io.seata.core.model.GlobalStatus;
import io.seata.server.storage.db.store.LogStoreDataBaseDAO;
import io.seata.server.UUIDGenerator;
import io.seata.server.session.BranchSession;
import io.seata.server.session.GlobalSession;
import io.seata.server.session.SessionCondition;
import io.seata.server.session.SessionManager;
import io.seata.server.storage.db.session.DataBaseSessionManager;
import io.seata.server.storage.db.store.DataBaseTransactionStoreManager;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
/**
* The type Data base session manager test.
*
* @author zhangsen
*/
class DataBaseSessionManagerTest {
static SessionManager sessionManager = null;
static LogStoreDataBaseDAO logStoreDataBaseDAO = null;
static BasicDataSource dataSource = null;
@BeforeAll
public static void start() throws Exception {
DataBaseSessionManager tempSessionManager = new DataBaseSessionManager();
DataBaseTransactionStoreManager transactionStoreManager = DataBaseTransactionStoreManager.getInstance();
dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:./db_store/db_session");
dataSource.setUsername("sa");
dataSource.setPassword("");
logStoreDataBaseDAO = new LogStoreDataBaseDAO(dataSource);
logStoreDataBaseDAO.setDbType("h2");
logStoreDataBaseDAO.setGlobalTable("global_table");
logStoreDataBaseDAO.setBranchTable("branch_table");
transactionStoreManager.setLogQueryLimit(100);
transactionStoreManager.setLogStore(logStoreDataBaseDAO);
tempSessionManager.setTransactionStoreManager(transactionStoreManager);
sessionManager = tempSessionManager;
prepareTable(dataSource);
logStoreDataBaseDAO.initTransactionNameSize();
}
private static void prepareTable(BasicDataSource dataSource) {
Connection conn = null;
try {
conn = dataSource.getConnection();
Statement s = conn.createStatement();
try {
s.execute("drop table global_table");
} catch (Exception e) {
}
s.execute("CREATE TABLE global_table ( xid varchar(96), transaction_id long , STATUS int, application_id varchar(32), transaction_service_group varchar(32) ,transaction_name varchar(128) ,timeout int, begin_time long, application_data varchar(500), gmt_create TIMESTAMP(6) ,gmt_modified TIMESTAMP(6) ) ");
System.out.println("create table global_table success.");
try {
s.execute("drop table branch_table");
} catch (Exception e) {
}
s.execute("CREATE TABLE branch_table ( xid varchar(96), transaction_id long , branch_id long, resource_group_id varchar(32), resource_id varchar(32) ,lock_key varchar(64) ,branch_type varchar(32) , status int , client_id varchar(128), application_data varchar(500), gmt_create TIMESTAMP(6) ,gmt_modified TIMESTAMP(6) ) ");
System.out.println("create table branch_table success.");
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtil.close(conn);
}
}
@Test
void test_addGlobalSession() throws TransactionException, SQLException {
GlobalSession session = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(session.getTransactionId());
session.setXid(xid);
session.setTransactionId(146757978);
session.setBeginTime(System.currentTimeMillis());
session.setApplicationData("abc=878s");
session.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(session);
String sql = "select * from global_table where xid= '"+xid+"'";
String delSql = "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(true);
}else{
Assertions.assertTrue(false);
}
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_updateGlobalSessionStatus() throws TransactionException, SQLException {
GlobalSession session = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(session.getTransactionId());
session.setXid(xid);
session.setTransactionId(146757978);
session.setBeginTime(System.currentTimeMillis());
session.setApplicationData("abc=878s");
session.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(session);
session.setStatus(GlobalStatus.Committing);
sessionManager.updateGlobalSessionStatus(session, GlobalStatus.Committing);
String sql = "select * from global_table where xid= '"+xid+"'";
String delSql = "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(true);
Assertions.assertEquals(rs.getInt("status"), GlobalStatus.Committing.getCode());
}else{
Assertions.assertTrue(false);
}
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_removeGlobalSession() throws Exception {
GlobalSession session = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(session.getTransactionId());
session.setXid(xid);
session.setTransactionId(146757978);
session.setBeginTime(System.currentTimeMillis());
session.setApplicationData("abc=878s");
session.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(session);
String sql = "select * from global_table where xid= '"+xid+"'";
String delSql = "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(true);
}else{
Assertions.assertTrue(false);
}
rs.close();
//delete
sessionManager.removeGlobalSession(session);
rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(false);
}else{
Assertions.assertTrue(true);
}
rs.close();
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_findGlobalSession() throws Exception {
GlobalSession session = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(session.getTransactionId());
session.setXid(xid);
session.setTransactionId(146757978);
session.setBeginTime(System.currentTimeMillis());
session.setApplicationData("abc=878s");
session.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(session);
GlobalSession globalSession_db = sessionManager.findGlobalSession(session.getXid());
Assertions.assertNotNull(globalSession_db);
Assertions.assertEquals(globalSession_db.getTransactionId(), session.getTransactionId());
Assertions.assertEquals(globalSession_db.getXid(), session.getXid());
Assertions.assertEquals(globalSession_db.getApplicationData(), session.getApplicationData());
Assertions.assertEquals(globalSession_db.getApplicationId(), session.getApplicationId());
Assertions.assertEquals(globalSession_db.getTransactionName(), session.getTransactionName());
Assertions.assertEquals(globalSession_db.getTransactionServiceGroup(), session.getTransactionServiceGroup());
Assertions.assertEquals(globalSession_db.getBeginTime(), session.getBeginTime());
Assertions.assertEquals(globalSession_db.getTimeout(), session.getTimeout());
Assertions.assertEquals(globalSession_db.getStatus(), session.getStatus());
String delSql = "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_addBranchSession() throws Exception {
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.Begin);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setApplicationData("{\"data\":\"test\"}");
sessionManager.addBranchSession(globalSession, branchSession);
String sql = "select * from branch_table where xid= '"+xid+"'";
String delSql = "delete from branch_table where xid= '"+xid+"'" + ";" + "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(true);
}else{
Assertions.assertTrue(false);
}
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_updateBranchSessionStatus() throws Exception {
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.Begin);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setApplicationData("{\"data\":\"test\"}");
branchSession.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession);
branchSession.setStatus(BranchStatus.PhaseOne_Timeout);
sessionManager.updateBranchSessionStatus(branchSession, BranchStatus.PhaseOne_Timeout);
String sql = "select * from branch_table where xid= '"+xid+"'";
String delSql = "delete from branch_table where xid= '"+xid+"'" + ";" + "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(true);
Assertions.assertEquals(rs.getInt("status"), BranchStatus.PhaseOne_Timeout.getCode());
}else{
Assertions.fail();
}
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_removeBranchSession() throws Exception {
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.Begin);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setApplicationData("{\"data\":\"test\"}");
branchSession.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession);
sessionManager.removeBranchSession(globalSession, branchSession);
String sql = "select * from branch_table where xid= '"+xid+"'";
String delSql = "delete from branch_table where xid= '"+xid+"'" + ";" + "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
ResultSet rs = conn.createStatement().executeQuery(sql);
if(rs.next()){
Assertions.assertTrue(false);
}else{
Assertions.assertTrue(true);
}
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_allSessions() throws Exception {
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(globalSession);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setClientId("abc-123");
branchSession.setApplicationData("{\"data\":\"test\"}");
branchSession.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession);
BranchSession branchSession2 = new BranchSession();
branchSession2.setBranchId(UUIDGenerator.generateUUID());
branchSession2.setXid(xid);
branchSession2.setTransactionId(globalSession.getTransactionId());
branchSession2.setBranchId(2L);
branchSession2.setResourceGroupId("my_test_tx_group");
branchSession2.setResourceId("tb_1");
branchSession2.setLockKey("t_1");
branchSession2.setBranchType(BranchType.TCC);
branchSession2.setClientId("abc-123");
branchSession2.setApplicationData("{\"data\":\"test\"}");
branchSession2.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession2);
Collection<GlobalSession> rets = sessionManager.allSessions();
Assertions.assertNotNull(rets);
Assertions.assertEquals(1, rets.size());
GlobalSession globalSession_db = (GlobalSession) new ArrayList(rets).get(0);
Assertions.assertNotNull(globalSession_db.getReverseSortedBranches());
Assertions.assertEquals(2, globalSession_db.getReverseSortedBranches().size());
Assertions.assertNotNull(globalSession_db.getBranch(1L));
Assertions.assertNotNull(globalSession_db.getBranch(2L));
String delSql = "delete from branch_table where xid= '"+xid+"'" + ";" + "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_findGlobalSessions() throws TransactionException, SQLException {
String xid = null;
{
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(globalSession);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setClientId("abc-123");
branchSession.setApplicationData("{\"data\":\"test\"}");
branchSession.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession);
}
String xid2 = null;
{
GlobalSession globalSession = GlobalSession.createGlobalSession("test",
"test", "test123", 100);
xid2 = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.setTransactionId(146757978);
globalSession.setBeginTime(System.currentTimeMillis());
globalSession.setApplicationData("abc=878s");
globalSession.setStatus(GlobalStatus.CommitRetrying);
sessionManager.addGlobalSession(globalSession);
BranchSession branchSession = new BranchSession();
branchSession.setBranchId(UUIDGenerator.generateUUID());
branchSession.setXid(xid2);
branchSession.setTransactionId(globalSession.getTransactionId());
branchSession.setBranchId(1L);
branchSession.setResourceGroupId("my_test_tx_group");
branchSession.setResourceId("tb_1");
branchSession.setLockKey("t_1");
branchSession.setBranchType(BranchType.AT);
branchSession.setClientId("abc-123");
branchSession.setApplicationData("{\"data\":\"test\"}");
branchSession.setStatus(BranchStatus.PhaseOne_Done);
sessionManager.addBranchSession(globalSession, branchSession);
}
Collection<GlobalSession> rets = sessionManager.findGlobalSessions(new SessionCondition( GlobalStatus.Begin));
Assertions.assertNotNull(rets);
Assertions.assertEquals(1, rets.size());
GlobalSession globalSession_db = (GlobalSession) new ArrayList(rets).get(0);
Assertions.assertNotNull(globalSession_db.getReverseSortedBranches());
Assertions.assertEquals(1, globalSession_db.getReverseSortedBranches().size());
Assertions.assertNotNull(globalSession_db.getBranch(1L));
String delSql = "delete from branch_table where xid= '"+xid+"'" + ";" + "delete from global_table where xid= '"+xid+"'";
String delSql2 = "delete from branch_table where xid= '"+xid2+"'" + ";" + "delete from global_table where xid= '"+xid2+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
conn.createStatement().execute(delSql);
conn.createStatement().execute(delSql2);
}finally {
if(conn != null){
conn.close();
}
}
}
@Test
void test_transactionNameGreaterDbSize() throws Exception {
int transactionNameColumnSize = logStoreDataBaseDAO.getTransactionNameColumnSize();
StringBuilder sb = new StringBuilder("test");
for (int i = 4; i < transactionNameColumnSize; i++) {
sb.append("0");
}
final String finalTxName = sb.toString();
sb.append("1321465454545436");
GlobalSession session = GlobalSession.createGlobalSession("test",
"test", sb.toString(), 100);
String xid = XID.generateXID(session.getTransactionId());
session.setXid(xid);
session.setTransactionId(146757978);
session.setBeginTime(System.currentTimeMillis());
session.setApplicationData("abc=878s");
session.setStatus(GlobalStatus.Begin);
sessionManager.addGlobalSession(session);
GlobalSession globalSession_db = sessionManager.findGlobalSession(session.getXid());
Assertions.assertNotNull(globalSession_db);
Assertions.assertEquals(globalSession_db.getTransactionName(), finalTxName);
String delSql = "delete from global_table where xid= '"+xid+"'";
Connection conn = null;
try{
conn = dataSource.getConnection();
conn.createStatement().execute(delSql);
}finally {
if(conn != null){
conn.close();
}
}
}
}