Skip to content

Commit 0f037cf

Browse files
committed
add test case for group commit sys view
1 parent c879b38 commit 0f037cf

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*-
2+
* #%L
3+
* OBKV Table Client Framework
4+
* %%
5+
* Copyright (C) 2023 OceanBase
6+
* %%
7+
* OBKV Table Client Framework is licensed under Mulan PSL v2.
8+
* You can use this software according to the terms and conditions of the Mulan PSL v2.
9+
* You may obtain a copy of Mulan PSL v2 at:
10+
* http://license.coscl.org.cn/MulanPSL2
11+
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12+
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13+
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14+
* See the Mulan PSL v2 for more details.
15+
* #L%
16+
*/
17+
18+
package com.alipay.oceanbase.rpc;
19+
20+
import com.alipay.oceanbase.rpc.property.Property;
21+
import com.alipay.oceanbase.rpc.util.ObTableClientTestUtil;
22+
import org.junit.Assert;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import java.sql.Connection;
27+
import java.sql.ResultSet;
28+
import java.sql.SQLException;
29+
import java.sql.Statement;
30+
import java.util.ArrayList;
31+
32+
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.colVal;
33+
import static com.alipay.oceanbase.rpc.mutation.MutationFactory.row;
34+
import static org.junit.Assert.assertEquals;
35+
36+
public class ObTableGroupCommitTest {
37+
public ObTableClient client;
38+
39+
@Before
40+
public void setup() throws Exception {
41+
System.setProperty("ob_table_min_rslist_refresh_interval_millis", "1");
42+
43+
final ObTableClient obTableClient = ObTableClientTestUtil.newTestClient();
44+
obTableClient.init();
45+
46+
this.client = obTableClient;
47+
}
48+
49+
/**
50+
CREATE TABLE IF NOT EXISTS `test_varchar_table` (
51+
`c1` varchar(20) NOT NULL,
52+
`c2` varchar(20) DEFAULT NULL,
53+
PRIMARY KEY (`c1`)
54+
);
55+
**/
56+
@Test
57+
public void test_group_commit() throws Exception {
58+
String tableName = "test_varchar_table";
59+
long executeTime = 10000; // 10s
60+
int threadCnt = 100;
61+
ArrayList<Thread> workers = new ArrayList<>();
62+
deleteTable(tableName);
63+
switchGroupCommit(true);
64+
for (int i = 0; i < threadCnt; i++) {
65+
InsertWorker worker = new InsertWorker(i, tableName, this.client, executeTime);
66+
workers.add(worker);
67+
}
68+
69+
for (int i = 0; i < threadCnt; i++) {
70+
GetWorker worker = new GetWorker(i, tableName, this.client, executeTime);
71+
workers.add(worker);
72+
}
73+
74+
for (int i = 0; i < 2*threadCnt; i++) {
75+
workers.get(i).start();
76+
}
77+
78+
for (int i = 0; i < 2*threadCnt; i++) {
79+
workers.get(i).join();
80+
}
81+
checkSystemView();
82+
deleteTable(tableName);
83+
switchGroupCommit(false);
84+
this.client.close();
85+
}
86+
87+
private void checkSystemView() throws SQLException {
88+
// mysql tenant
89+
Connection mysql_conn = ObTableClientTestUtil.getConnection();
90+
Statement statement = mysql_conn.createStatement();
91+
statement.execute("select b.tenant_id as tenant_id, b.tenant_name as tenant_name, a.group_type as group_type, a.batch_size as batch_size " +
92+
" from oceanbase.GV$OB_KV_GROUP_COMMIT_STATUS a inner join " +
93+
"oceanbase.DBA_OB_TENANTS b on a.tenant_id = b.tenant_id group by a.group_type");
94+
ResultSet resultSet = statement.getResultSet();
95+
int resCount = 0;
96+
System.out.println("visit by mysql tenant:");
97+
while (resultSet.next()) {
98+
long tenant_id = resultSet.getLong("tenant_id");
99+
String tenant_name = resultSet.getString("tenant_name");
100+
String group_type = resultSet.getString("group_type");
101+
long batch_size = resultSet.getLong("batch_size");
102+
System.out.println("tenant_id:" + tenant_id+", tenant_name: "+ tenant_name +", group_type: "+group_type+", batch_size: "+batch_size);
103+
resCount++;
104+
}
105+
Assert.assertTrue(resCount >= 3);
106+
mysql_conn.close();
107+
108+
// sys tenant
109+
Connection sys_conn = ObTableClientTestUtil.getSysConnection();
110+
Statement statement2 = sys_conn.createStatement();
111+
statement2.execute("select b.tenant_id as tenant_id, b.tenant_name as tenant_name, a.group_type as group_type, a.batch_size as batch_size " +
112+
" from oceanbase.GV$OB_KV_GROUP_COMMIT_STATUS a inner join " +
113+
"oceanbase.__all_tenant b on a.tenant_id = b.tenant_id where b.tenant_name in ('sys', '"+ObTableClientTestUtil.getTenantName()+"') group by b.tenant_name, a.group_type;");
114+
resultSet = statement2.getResultSet();
115+
resCount = 0;
116+
System.out.println("visit by sys tenant:");
117+
while (resultSet.next()) {
118+
long tenant_id = resultSet.getLong("tenant_id");
119+
String tenant_name = resultSet.getString("tenant_name");
120+
String group_type = resultSet.getString("group_type");
121+
long batch_size = resultSet.getLong("batch_size");
122+
System.out.println("tenant_id:" + tenant_id+", tenant_name: "+ tenant_name +", group_type: "+group_type+", batch_size: "+batch_size);
123+
resCount++;
124+
}
125+
Assert.assertTrue(resCount >= 4);
126+
sys_conn.close();
127+
}
128+
129+
class InsertWorker extends Thread {
130+
private int id;
131+
private String tableName;
132+
private ObTableClient obTableClient;
133+
private long executeTime; // in millisecond
134+
135+
public InsertWorker(int id, String tableName, ObTableClient obTableClient, long executeTime) {
136+
this.id = id;
137+
this.tableName = tableName;
138+
this.obTableClient = obTableClient;
139+
this.executeTime = executeTime;
140+
}
141+
142+
public void run() {
143+
long start = System.currentTimeMillis();
144+
int counter = 0;
145+
while ((System.currentTimeMillis() - start) < executeTime) {
146+
try {
147+
String c1 = String.format("rk_%d_%d", id, counter);
148+
String c2 = String.format("col_%d_%d", id, counter);
149+
obTableClient.insert(tableName).setRowKey(row(colVal("c1", c1)))
150+
.addMutateRow(row(colVal("c2",c2))).execute();
151+
counter++;
152+
} catch (Exception e) {
153+
e.printStackTrace();
154+
System.out.println("thread " + id + " get occurs exception !");
155+
}
156+
}
157+
}
158+
}
159+
160+
class GetWorker extends Thread {
161+
private int id;
162+
private String tableName;
163+
private ObTableClient obTableClient;
164+
private long executeTime; // in millisecond
165+
166+
public GetWorker(int id, String tableName, ObTableClient obTableClient, long executeTime) {
167+
this.id = id;
168+
this.tableName = tableName;
169+
this.obTableClient = obTableClient;
170+
this.executeTime = executeTime;
171+
}
172+
173+
public void run() {
174+
long start = System.currentTimeMillis();
175+
int counter = 0;
176+
while ((System.currentTimeMillis() - start) < executeTime) {
177+
try {
178+
String c1 = String.format("rk_%d_%d", id, counter);
179+
obTableClient.get(tableName, new String[] { "c1" }, new String[] { c1 });
180+
counter++;
181+
} catch (Exception e) {
182+
e.printStackTrace();
183+
System.out.println("thread " + id + " get occurs exception !");
184+
}
185+
}
186+
}
187+
}
188+
189+
private void switchGroupCommit(boolean is_enable) throws SQLException {
190+
int batch_size = is_enable ? 10 : 1;
191+
Connection mysql_conn = ObTableClientTestUtil.getConnection();
192+
Statement statement = mysql_conn.createStatement();
193+
statement.execute("alter system set kv_group_commit_batch_size = "+ batch_size);
194+
}
195+
196+
private void deleteTable(String tableName) throws SQLException {
197+
Connection mysql_conn = ObTableClientTestUtil.getConnection();
198+
Statement statement = mysql_conn.createStatement();
199+
statement.execute("delete from "+ tableName);
200+
}
201+
}

0 commit comments

Comments
 (0)