-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bal
228 lines (195 loc) · 8.03 KB
/
main.bal
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
import ballerina/http;
import ballerina/io;
import ballerina/jballerina.java;
import ballerina/sql;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
configurable string hostname = ?;
configurable string username = ?;
configurable string password1 = ?;
configurable string password2 = ?;
configurable int port1 = 3306;
configurable int port2 = 3308;
configurable string database1 = "xaLocalDB";
configurable string database2 = "xaDockerDB";
sql:ConnectionPool pool = {
maxOpenConnections: 5,
maxConnectionLifeTime: 300,
minIdleConnections: 0
};
boolean crashBeforeCommit = false;
boolean stopSqlServer = false;
int cellId = 1;
service / on new http:Listener(9090) {
final mysql:Client localDB;
final mysql:Client dockerDB;
function init() returns error? {
_ = check startServ();
self.localDB = check new (host = hostname,
user = username, password = password1,
port = port1, database = database1,
connectionPool = pool,
options = {useXADatasource: true,
ssl: {
allowPublicKeyRetrieval: true
}
}
);
io:println("> Local database initialized.");
self.dockerDB = check new (host = "localhost",
user = username, password = password2,
port = port2, database = database2,
connectionPool = pool,
options = {useXADatasource: true,
ssl: {
allowPublicKeyRetrieval: true
}
}
);
io:println("> Docker database initialized.");
}
// rollIt - forcefully rollback the transaction
// crashIt - crash the transaction manager before notifyCommit
// stopSql - stop the sql server before notifyCommit
resource function get updateToChipiChipi(boolean crashIt = false, boolean stopSql = false, boolean rollIt = false) returns string|error {
stopSqlServer = stopSql;
if (!stopSqlServer) {
_ = check startServ();
}
crashBeforeCommit = crashIt;
io:println(" + Crash before commit: ", crashBeforeCommit);
io:println(" + Stop SQL server: ", stopSqlServer);
sql:ParameterizedQuery updateQuery1 = `UPDATE xaTesting SET hello = 'chipichipi' WHERE id = ${cellId}`;
sql:ParameterizedQuery updateQuery2 = `UPDATE xaTesting SET hello = 'chipichipi' WHERE id = ${cellId}`;
io:println("~ Start of transaction block");
retry transaction {
transaction:onCommit(commitHanlder);
transaction:onRollback(rollbackHandler);
sql:ExecutionResult execResult1 = check self.localDB->execute(updateQuery1);
io:println("Affected row count: ", execResult1.affectedRowCount);
sql:ExecutionResult execResult2 = check self.dockerDB->execute(updateQuery2);
io:println("Affected row count: ", execResult2.affectedRowCount);
// call transaldb from here
http:Client httpClient = check new ("http://localhost:9896");
http:Response response = check httpClient->get("/hola?name=chipi");
if (response.statusCode != 200 && response.statusCode != 202) {
io:println("Error: ", response.statusCode, " - ", response.reasonPhrase);
io:println("> Rolling back...");
rollback;
return "transaction chipi failed";
} else {
io:println("Response: ", response.statusCode, " - ", response.reasonPhrase);
}
if (rollIt == true || execResult1.affectedRowCount == 0 || execResult2.affectedRowCount == 0) {
io:println("> Rolling back...");
rollback;
} else {
io:println("> Committing...");
check commit;
}
}
io:println("~ End of transaction block");
_ = check startServ();
io:println("Done.");
return "transaction chipi completed";
}
resource function get updateToDubiDubi(boolean crashIt = false, boolean stopSql = false, boolean rollIt = false) returns string|error {
stopSqlServer = stopSql;
if (!stopSqlServer) {
_ = check startServ();
}
crashBeforeCommit = crashIt;
io:println(" + Crash before commit: ", crashBeforeCommit);
io:println(" + Stop SQL server: ", stopSqlServer);
sql:ParameterizedQuery updateQuery1 = `UPDATE xaTesting SET hello = 'dubidubi' WHERE id = ${cellId}`;
sql:ParameterizedQuery updateQuery2 = `UPDATE xaTesting SET hello = 'dubidubi' WHERE id = ${cellId}`;
io:println("~ Start of transaction block");
retry transaction {
transaction:onCommit(commitHanlder);
transaction:onRollback(rollbackHandler);
sql:ExecutionResult execResult1 = check self.localDB->execute(updateQuery1);
io:println("Affected row count: ", execResult1.affectedRowCount);
sql:ExecutionResult execResult2 = check self.dockerDB->execute(updateQuery2);
io:println("Affected row count: ", execResult2.affectedRowCount);
if (rollIt == true || execResult1.affectedRowCount == 0 || execResult2.affectedRowCount == 0) {
io:println("> Rolling back...");
rollback;
} else {
io:println("> Committing...");
check commit;
}
}
io:println("~ End of transaction block");
_ = check startServ();
io:println("> Done.");
return "transaction dubi completed";
}
resource function get getCrash() returns boolean|error {
return crashBeforeCommit;
}
resource function get stopSql() returns boolean|error {
return stopSqlServer;
}
}
isolated function commitHanlder('transaction:Info info) {
http:Client crasherClient = checkpanic new ("http://localhost:9090");
boolean crasher = checkpanic crasherClient->get("/getCrash");
io:println(" + Crasher value: ", crasher);
http:Client stopperClient = checkpanic new ("http://localhost:9090");
boolean stopper = checkpanic stopperClient->get("/stopSql");
io:println(" + Stopper value: ", stopper);
if (crasher) {
io:println("[Crash. No Stop.]");
divideByZero(); // crashes here
}
if (stopper) {
io:println("[No Crash. Stop Sql.]");
_ = checkpanic stopServ();
}
io:println("[No Crash. No Stop.]");
io:println("> TM committed.");
}
isolated function rollbackHandler(transaction:Info info, error? cause, boolean willRetry = false) {
http:Client crasherClient = checkpanic new ("http://localhost:9090");
boolean crasher = checkpanic crasherClient->get("/getCrash");
io:println(" + Crasher value: ", crasher);
http:Client stopperClient = checkpanic new ("http://localhost:9090");
boolean stopper = checkpanic stopperClient->get("/stopSql");
io:println(" + Stopper value: ", stopper);
if (crasher) {
io:println("[Crash. No Stop.]");
divideByZero(); // crashes here
}
if (stopper) {
io:println("[No crash. Stop Sql.]");
_ = checkpanic stopServ();
}
io:println("[No crash. No Stop.]");
io:println("> TM rollbacked.");
}
isolated function stopServ() returns boolean|error {
io:println("> Stopping SQL...");
http:Client stopClient = check new ("http://localhost:9292");
boolean stopped = check stopClient->get("/stopSql");
if (stopped) {
io:println("> Stopped SQL server.");
} else {
io:println("> Failed to stop SQL server.");
}
return stopped;
}
isolated function startServ() returns boolean|error {
io:println("> Starting SQL...");
http:Client startClient = check new ("http://localhost:9292");
boolean started = check startClient->get("/startSql");
if (started) {
io:println("> Started SQL server.");
} else {
io:println("> Failed to start SQL server.");
}
return started;
}
isolated function divideByZero() = @java:Method {
name: "divideByZero",
'class: "a.b.c.Foo"
} external;