forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_txn_case.groovy
308 lines (249 loc) · 10.8 KB
/
test_txn_case.groovy
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
suite("test_mow_txn_case") {
// TODO: bugfix
def fullPriv = ["SELECT_PRIV"/*, "LOAD_PRIV"*/, "ALTER_PRIV", "CREATE_PRIV", "DROP_PRIV"]
def nowPriv = []
def recursionPriv = { fullPrivList, idx, nowPrivList, num, callback ->
for (; (num - nowPrivList.size() <= fullPrivList.size() - idx) && (nowPrivList.size()) < num; ++idx) {
nowPrivList.push(fullPrivList[idx])
call(fullPrivList, idx + 1, nowPrivList, num, callback)
nowPrivList.pop()
}
if (nowPrivList.size() == num) {
String privStr = ""
for (int i = 0; i < num; ++i) {
privStr += nowPrivList[i]
if (i < num - 1) {
privStr += ", "
}
}
callback.call(privStr)
}
}
def syncer = getSyncer()
if (!syncer.checkEnableFeatureBinlog()) {
logger.info("fe enable_feature_binlog is false, skip case test_mow_txn_case")
return
}
def txnTableName = "tbl_txn_case"
def test_num = 0
sql "DROP TABLE IF EXISTS ${txnTableName}"
sql """
CREATE TABLE if NOT EXISTS ${txnTableName}
(
`test` INT,
`id` INT
)
ENGINE=OLAP
UNIQUE KEY(`test`, `id`)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES (
"enable_unique_key_merge_on_write" = "true",
"replication_allocation" = "tag.location.default: 1"
)
"""
sql """ALTER TABLE ${txnTableName} set ("binlog.enable" = "true")"""
target_sql "DROP TABLE IF EXISTS ${txnTableName}"
target_sql """
CREATE TABLE if NOT EXISTS ${txnTableName}
(
`test` INT,
`id` INT
)
ENGINE=OLAP
UNIQUE KEY(`test`, `id`)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES (
"enable_unique_key_merge_on_write" = "true",
"replication_allocation" = "tag.location.default: 1"
)
"""
assertTrue(syncer.getTargetMeta("${txnTableName}"))
logger.info("=== Test 1: common txn case ===")
test_num = 1
sql """
INSERT INTO ${txnTableName} VALUES (${test_num}, 0)
"""
assertTrue(syncer.getBinlog("${txnTableName}"))
assertTrue(syncer.getBackendClients())
assertTrue(syncer.beginTxn("${txnTableName}"))
assertTrue(syncer.ingestBinlog())
assertTrue(syncer.commitTxn())
assertTrue(syncer.checkTargetVersion())
target_sql " sync "
def res = target_sql """SELECT * FROM ${txnTableName} WHERE test=${test_num}"""
assertEquals(res.size(), 1)
logger.info("=== Test 2: Wrong BeginTxnRequest context case ===")
logger.info("=== Test 2.1: Begin a txn with non-existent table case ===")
assertTrue(syncer.beginTxn("tbl_non_existent") == false)
logger.info("=== Test 2.2: Begin a txn with duplicate labels case ===")
assertTrue(syncer.beginTxn("${txnTableName}") == false)
// End Test 2
syncer.closeBackendClients()
logger.info("=== Test 3: Begin a txn with different priv user case ===")
test_num = 3
sql """
INSERT INTO ${txnTableName} VALUES (${test_num}, 0)
"""
assertTrue(syncer.getBinlog("${txnTableName}"))
logger.info("=== Test 3.1: Begin a txn with non-existent user set in syncer case ===")
syncer.context.user = "this_is_an_invalid_user"
syncer.context.passwd = "this_is_an_invalid_user"
assertTrue(syncer.beginTxn("${txnTableName}") == false)
logger.info("=== Test 3.2: Begin a txn with no priv user case ===")
def noPrivUser = "no_priv_user1"
def emptyTable = "tbl_empty_test"
target_sql "DROP TABLE IF EXISTS ${emptyTable}"
target_sql """
CREATE TABLE if NOT EXISTS ${emptyTable}
(
`test` INT,
`id` INT
)
ENGINE=OLAP
UNIQUE KEY(`test`, `id`)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES (
"enable_unique_key_merge_on_write" = "true",
"replication_allocation" = "tag.location.default: 1"
)
"""
target_sql """DROP USER IF EXISTS ${noPrivUser}"""
target_sql """CREATE USER ${noPrivUser} IDENTIFIED BY '123456'"""
target_sql """GRANT ALL ON ${context.config.defaultDb}.* TO ${noPrivUser}"""
target_sql """GRANT ALL ON TEST_${context.dbName}.${emptyTable} TO ${noPrivUser}"""
syncer.context.user = "${noPrivUser}"
syncer.context.passwd = "123456"
assertTrue(syncer.beginTxn("${txnTableName}") == false)
// TODO: bugfix
// Recursively selecting privileges,
// if not all privileges are obtained, txn should not be began
logger.info("=== Test 3.3: Begin a txn with low priv user case ===")
def lowPrivUser = "low_priv_user0"
target_sql """DROP USER IF EXISTS ${lowPrivUser}"""
target_sql """CREATE USER ${lowPrivUser} IDENTIFIED BY '123456'"""
target_sql """GRANT ALL ON ${context.config.defaultDb}.* TO ${lowPrivUser}"""
syncer.context.user = "${lowPrivUser}"
syncer.context.passwd = "123456"
def beginTxnCallback = { privStr ->
target_sql """GRANT ${privStr} ON TEST_${context.dbName}.${txnTableName} TO ${lowPrivUser}"""
assertTrue((syncer.beginTxn("${txnTableName}")) == false)
target_sql """REVOKE ${privStr} ON TEST_${context.dbName}.${txnTableName} FROM ${lowPrivUser}"""
}
for (int i = 1; i <= 4; ++i) {
recursionPriv.call(fullPriv, 0, nowPriv, i, beginTxnCallback)
}
logger.info("=== Test 3.4: Complete the txn with SHOW_PRIV user case ===")
def showPrivUser = "show_priv_user0"
target_sql """DROP USER IF EXISTS ${showPrivUser}"""
target_sql """CREATE USER ${showPrivUser} IDENTIFIED BY '123456'"""
target_sql """GRANT ALL ON ${context.config.defaultDb}.* TO ${showPrivUser}"""
target_sql """
GRANT
SELECT_PRIV, LOAD_PRIV, ALTER_PRIV, CREATE_PRIV, DROP_PRIV
ON TEST_${context.dbName}.${txnTableName}
TO ${showPrivUser}
"""
syncer.context.user = "${showPrivUser}"
syncer.context.passwd = "123456"
assertTrue(syncer.beginTxn("${txnTableName}"))
assertTrue(syncer.getBackendClients())
assertTrue(syncer.ingestBinlog())
assertTrue(syncer.commitTxn())
assertTrue(syncer.checkTargetVersion())
res = target_sql """SELECT * FROM ${txnTableName} WHERE test=${test_num}"""
assertEquals(res.size(), 1)
// End Test 3
syncer.context.user = context.config.feSyncerUser
syncer.context.passwd = context.config.feSyncerPassword
syncer.closeBackendClients()
logger.info("=== Test 4: Wrong CommitTxnRequest context case ===")
test_num = 4
sql """
INSERT INTO ${txnTableName} VALUES (${test_num}, 0)
"""
assertTrue(syncer.getBinlog("${txnTableName}"))
assertTrue(syncer.getBackendClients())
assertTrue(syncer.beginTxn("${txnTableName}"))
assertTrue(syncer.ingestBinlog())
logger.info("=== Test 4.1: Wrong txnId case ===")
def originTxnId = syncer.context.txnId
syncer.context.txnId = -1
assertTrue(syncer.commitTxn() == false)
syncer.context.txnId = originTxnId
logger.info("=== Test 4.2: Wrong commit info case ===")
// TODO: bugfix
// def originCommitInfos = syncer.resetCommitInfos()
// syncer.context.addCommitInfo(-1, -1)
// assertTrue(syncer.commitTxn()) == false)
logger.info("=== Test 4.3: Empty commit info case ===")
// TODO: bugfix
// assertTrue(syncer.commitTxn() == false)
logger.info("=== Test 4.4: duplicate txnId case ===")
// TODO: bugfix
// def lastCommitInfo = syncer.copyCommitInfos()
assertTrue(syncer.commitTxn())
assertTrue(syncer.checkTargetVersion())
target_sql " sync "
res = target_sql """SELECT * FROM ${txnTableName} WHERE test=${test_num}"""
assertEquals(res.size(), 1)
// syncer.context.commitInfos = lastCommitInfo
// assertTrue(syncer.commitTxn() == false)
// End Test 4
syncer.closeBackendClients()
logger.info("=== Test 5: User root beginTxn, Other user commitTxn case ===")
test_num = 5
sql """
INSERT INTO ${txnTableName} VALUES (${test_num}, 0)
"""
assertTrue(syncer.getBinlog("${txnTableName}"))
assertTrue(syncer.getBackendClients())
assertTrue(syncer.beginTxn("${txnTableName}"))
assertTrue(syncer.ingestBinlog())
logger.info("=== Test 5.1: Non-existent user commitTxn case ===")
syncer.context.user = "this_is_an_invalid_user"
syncer.context.passwd = "this_is_an_invalid_user"
assertTrue(syncer.commitTxn() == false)
logger.info("=== Test 5.2: No priv user commitTxn case ===")
syncer.context.user = "${noPrivUser}"
syncer.context.passwd = "123456"
assertTrue(syncer.commitTxn() == false)
logger.info("=== Test 5.3: Low priv user commitTxn case ===")
syncer.context.user = "${lowPrivUser}"
syncer.context.passwd = "123456"
def commitTxnCallback = { privStr ->
target_sql """GRANT ${privStr} ON TEST_${context.dbName}.${txnTableName} TO ${lowPrivUser}"""
assertTrue(syncer.commitTxn() == false)
target_sql """REVOKE ${privStr} ON TEST_${context.dbName}.${txnTableName} FROM ${lowPrivUser}"""
}
for (int i = 1; i <= 4; ++i) {
recursionPriv.call(fullPriv, 0, nowPriv, i, commitTxnCallback)
}
logger.info("=== Test 5.4: SHOW_PRIV user commitTxn case ===")
syncer.context.user = "${showPrivUser}"
syncer.context.passwd = "123456"
assertTrue(syncer.commitTxn())
assertTrue(syncer.checkTargetVersion())
target_sql " sync "
res = target_sql """SELECT * FROM ${txnTableName} WHERE test=${test_num}"""
assertEquals(res.size(), 1)
// End Test 5
syncer.context.user = context.config.feSyncerUser
syncer.context.passwd = context.config.feSyncerPassword
syncer.closeBackendClients()
}