-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDemoJdbcAutoRandom.java
More file actions
215 lines (174 loc) · 8.98 KB
/
DemoJdbcAutoRandom.java
File metadata and controls
215 lines (174 loc) · 8.98 KB
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
import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
// import java.util.concurrent.atomic.AtomicInteger;
public class DemoJdbcAutoRandom {
private static Connection getConnection() throws SQLException {
Connection connection = null;
String tidbHost = System.getenv().getOrDefault("TIDB_HOST", "localhost");
int tidbPort = Integer.parseInt(System.getenv().getOrDefault("TIDB_PORT", "4000"));
String tidbUser = System.getenv().getOrDefault("TIDB_USER", "root");
String tidbPassword = System.getenv().getOrDefault("TIDB_PASSWORD", "");
String tidbDatabase = System.getenv().getOrDefault("TIDB_DATABASE", "test");
String connectionUrl = "jdbc:mysql://" + tidbHost + ":" + tidbPort + "/" + tidbDatabase +
"?sslMode=VERIFY_IDENTITY&enabledTLSProtocols=TLSv1.2,TLSv1.3";
connection = DriverManager.getConnection(connectionUrl, tidbUser, tidbPassword);
return connection;
}
private static void setupTables(Connection connection) throws SQLException {
try (Statement stmt = connection.createStatement()) {
// Drop tables if they exist
stmt.execute("DROP TABLE IF EXISTS auto_random_demo");
stmt.execute("DROP TABLE IF EXISTS auto_increment_demo");
// Create a table with AUTO_RANDOM
stmt.execute("""
CREATE TABLE auto_random_demo (
id BIGINT PRIMARY KEY AUTO_RANDOM,
name VARCHAR(255)
)
""");
// Create a similar table with AUTO_INCREMENT for comparison
stmt.execute("""
CREATE TABLE auto_increment_demo (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
)
""");
System.out.println("Tables created successfully.");
}
}
private static void showTableDefinition(Connection connection, String tableName) throws SQLException {
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + tableName)) {
System.out.println("\nTable definition for " + tableName + ":");
if (rs.next()) {
System.out.println(rs.getString(2)); // The second column contains the CREATE TABLE statement
}
}
}
private static class ConcurrentInsertWorker implements Runnable {
private final String tableName;
private final List<String> values;
private final String threadId;
public ConcurrentInsertWorker(String tableName, List<String> values, String threadId) {
this.tableName = tableName;
this.values = values;
this.threadId = threadId;
}
@Override
public void run() {
try (Connection connection = getConnection()) {
String insertStmt = "INSERT INTO " + tableName + " (name) VALUES (?)";
try (PreparedStatement pstmt = connection.prepareStatement(insertStmt)) {
for (int i = 0; i < values.size(); i++) {
pstmt.setString(1, values.get(i));
pstmt.executeUpdate();
if (i % 5 == 0) { // Print progress every 5 inserts
System.out.println("Thread " + threadId + ": Inserted '" + values.get(i) + "' into " + tableName);
}
}
}
System.out.println("Thread " + threadId + ": Completed all inserts for " + tableName);
} catch (SQLException e) {
System.out.println("Thread " + threadId + ": Error inserting into " + tableName + ": " + e.getMessage());
}
}
}
private static void insertDataConcurrent(int count, int numThreads) throws InterruptedException {
// Generate values for each table
List<String> autoRandomValues = new ArrayList<>();
List<String> autoIncrementValues = new ArrayList<>();
for (int i = 0; i < count; i++) {
autoRandomValues.add("Auto-random value " + i);
autoIncrementValues.add("Auto-increment value " + i);
}
// Divide work among threads
int itemsPerThread = count / numThreads;
List<Thread> threads = new ArrayList<>();
System.out.println("\nStarting " + numThreads + " threads to insert " + count + " rows into each table...");
// Create and start threads for AUTO_RANDOM table
for (int i = 0; i < numThreads; i++) {
int startIdx = i * itemsPerThread;
int endIdx = (i < numThreads - 1) ? startIdx + itemsPerThread : count;
List<String> threadValues = autoRandomValues.subList(startIdx, endIdx);
Thread thread = new Thread(new ConcurrentInsertWorker("auto_random_demo", threadValues, "AR-" + (i + 1)));
threads.add(thread);
thread.start();
}
// Create and start threads for AUTO_INCREMENT table
for (int i = 0; i < numThreads; i++) {
int startIdx = i * itemsPerThread;
int endIdx = (i < numThreads - 1) ? startIdx + itemsPerThread : count;
List<String> threadValues = autoIncrementValues.subList(startIdx, endIdx);
Thread thread = new Thread(new ConcurrentInsertWorker("auto_increment_demo", threadValues, "AI-" + (i + 1)));
threads.add(thread);
thread.start();
}
// Wait for all threads to complete
for (Thread thread : threads) {
thread.join();
}
System.out.println("Concurrent insert completed: " + count + " rows inserted into each table using " + numThreads + " threads");
}
private static void compareIds(Connection connection) throws SQLException {
// Get AUTO_RANDOM IDs
List<Long> autoRandomIds = new ArrayList<>();
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM auto_random_demo ORDER BY id")) {
while (rs.next()) {
autoRandomIds.add(rs.getLong("id"));
}
}
// Get AUTO_INCREMENT IDs
List<Long> autoIncrementIds = new ArrayList<>();
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM auto_increment_demo ORDER BY id")) {
while (rs.next()) {
autoIncrementIds.add(rs.getLong("id"));
}
}
System.out.println("\n=== ID Comparison: AUTO_RANDOM vs AUTO_INCREMENT ===");
System.out.println("\nAUTO_RANDOM IDs (distributed to avoid hotspots):");
for (Long id : autoRandomIds) {
System.out.println(id);
}
System.out.println("\nAUTO_INCREMENT IDs (sequential, can cause hotspots):");
for (Long id : autoIncrementIds) {
System.out.println(id);
}
}
private static void cleanup(Connection connection) throws SQLException {
try (Statement stmt = connection.createStatement()) {
stmt.execute("DROP TABLE IF EXISTS auto_random_demo");
stmt.execute("DROP TABLE IF EXISTS auto_increment_demo");
System.out.println("\nCleanup complete. Test tables dropped.");
}
}
public static void main(String[] args) {
try (Connection connection = getConnection()) {
System.out.println("Connected to TiDB: " + connection.getMetaData().getUserName() +
"@" + connection.getMetaData().getURL());
// Set autocommit to false for transactional operations
connection.setAutoCommit(false);
// Create tables
setupTables(connection);
connection.commit();
// Show table definitions
showTableDefinition(connection, "auto_increment_demo");
showTableDefinition(connection, "auto_random_demo");
// Insert data using multiple concurrent threads
// Adjust these parameters to control the concurrency level and total insert count
int numThreads = 5; // Number of concurrent threads per table
int totalInserts = 10; // Total number of rows to insert into each table
insertDataConcurrent(totalInserts, numThreads);
// Compare the IDs
compareIds(connection);
// Cleanup
cleanup(connection);
connection.commit();
} catch (SQLException | InterruptedException e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}