-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathFlatfileReadMultithreadIT.java
More file actions
285 lines (238 loc) · 8.7 KB
/
FlatfileReadMultithreadIT.java
File metadata and controls
285 lines (238 loc) · 8.7 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
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
package net.snowflake.client.internal.loader;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.api.loader.LoadResultListener;
import net.snowflake.client.api.loader.LoaderFactory;
import net.snowflake.client.api.loader.LoaderProperty;
import net.snowflake.client.api.loader.LoadingError;
import net.snowflake.client.api.loader.Operation;
import net.snowflake.client.category.TestTags;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag(TestTags.LOADER)
public class FlatfileReadMultithreadIT {
private final int NUM_RECORDS = 100000;
private static final String TARGET_STAGE = "STAGE_MULTITHREAD_LOADER";
private static String TARGET_SCHEMA;
private static String TARGET_DB;
@BeforeAll
public static void setUpClass() throws Throwable {
try (Connection testConnection = AbstractDriverIT.getConnection();
// NOTE: the stage object must be created right after the connection
// because the Loader API assumes the stage object exists in the default
// namespace of the connection.
Statement statement = testConnection.createStatement()) {
statement.execute(String.format("CREATE OR REPLACE STAGE %s", TARGET_STAGE));
TARGET_SCHEMA = testConnection.getSchema();
TARGET_DB = testConnection.getCatalog();
}
}
@AfterAll
public static void tearDownClass() throws Throwable {
try (Connection testConnection = AbstractDriverIT.getConnection();
Statement statement = testConnection.createStatement()) {
statement.execute(String.format("DROP STAGE IF EXISTS %s", TARGET_STAGE));
}
}
/**
* Run loadAPI concurrently to ensure no race condition occurs.
*
* @throws Throwable raises an exception if any error occurs.
*/
@Test
public void testIssueSimpleDateFormat() throws Throwable {
final String targetTable = "TABLE_ISSUE_SIMPLEDATEFORMAT";
try (Connection testConnection = AbstractDriverIT.getConnection();
Statement statement = testConnection.createStatement()) {
try {
statement.execute(
String.format(
"CREATE OR REPLACE TABLE %s.%s.%s (" + "ID int, " + "C1 timestamp)",
TARGET_DB, TARGET_SCHEMA, targetTable));
FlatfileRead r1 =
new FlatfileRead(NUM_RECORDS, TARGET_DB, TARGET_SCHEMA, TARGET_STAGE, targetTable);
FlatfileRead r2 =
new FlatfileRead(NUM_RECORDS, TARGET_DB, TARGET_SCHEMA, TARGET_STAGE, targetTable);
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
t1.join();
t2.join();
if (r1.getError() != null) {
throw new AssertionError("Thread 1 failed", r1.getError());
}
if (r2.getError() != null) {
throw new AssertionError("Thread 2 failed", r2.getError());
}
try (ResultSet rs =
statement.executeQuery(
String.format(
"select count(*) from %s.%s.%s", TARGET_DB, TARGET_SCHEMA, targetTable))) {
rs.next();
assertThat("total number of records", rs.getInt(1), equalTo(NUM_RECORDS * 2));
}
} finally {
statement.execute(
String.format("DROP TABLE IF EXISTS %s.%s.%s", TARGET_DB, TARGET_SCHEMA, targetTable));
}
}
}
class FlatfileRead implements Runnable {
private final int totalRows;
private final String dbName;
private final String schemaName;
private final String tableName;
private final String stageName;
private volatile Throwable error;
FlatfileRead(
int totalRows, String dbName, String schemaName, String stageName, String tableName) {
this.totalRows = totalRows;
this.dbName = dbName;
this.schemaName = schemaName;
this.stageName = stageName;
this.tableName = tableName;
}
Throwable getError() {
return error;
}
@Override
public void run() {
try (Connection testConnection = AbstractDriverIT.getConnection();
Connection putConnection = AbstractDriverIT.getConnection()) {
ResultListener _resultListener = new ResultListener();
Map<LoaderProperty, Object> prop = new HashMap<>();
prop.put(LoaderProperty.tableName, this.tableName);
prop.put(LoaderProperty.schemaName, this.schemaName);
prop.put(LoaderProperty.databaseName, this.dbName);
prop.put(LoaderProperty.remoteStage, this.stageName);
prop.put(LoaderProperty.operation, Operation.INSERT);
StreamLoader underTest =
(StreamLoader) LoaderFactory.createLoader(prop, putConnection, testConnection);
underTest.setProperty(LoaderProperty.startTransaction, true);
underTest.setProperty(LoaderProperty.truncateTable, false);
underTest.setProperty(LoaderProperty.columns, Arrays.asList("ID", "C1"));
underTest.setListener(_resultListener);
underTest.start();
Random rnd = new Random();
for (int i = 0; i < this.totalRows; ++i) {
Object[] row = new Object[2];
row[0] = i;
long ms = -946771200000L + (Math.abs(rnd.nextLong()) % (70L * 365 * 24 * 60 * 60 * 1000));
row[1] = new Date(ms);
underTest.submitRow(row);
}
underTest.finish();
underTest.close();
assertThat("must be no error", _resultListener.getErrorCount(), equalTo(0));
assertThat(
"total number of rows",
_resultListener.getSubmittedRowCount(),
equalTo(this.totalRows));
} catch (Throwable e) {
error = e;
}
}
class ResultListener implements LoadResultListener {
private final List<LoadingError> errors = new ArrayList<>();
private final AtomicInteger errorCount = new AtomicInteger(0);
private final AtomicInteger errorRecordCount = new AtomicInteger(0);
private final AtomicInteger counter = new AtomicInteger(0);
private final AtomicInteger processed = new AtomicInteger(0);
private final AtomicInteger deleted = new AtomicInteger(0);
private final AtomicInteger updated = new AtomicInteger(0);
private final AtomicInteger submittedRowCount = new AtomicInteger(0);
private Object[] lastRecord = null;
public boolean throwOnError = false; // should not trigger rollback
@Override
public boolean needErrors() {
return true;
}
@Override
public boolean needSuccessRecords() {
return true;
}
@Override
public void addError(LoadingError error) {
errors.add(error);
}
@Override
public boolean throwOnError() {
return throwOnError;
}
public List<LoadingError> getErrors() {
return errors;
}
@Override
public void recordProvided(Operation op, Object[] record) {
lastRecord = record;
}
@Override
public void addProcessedRecordCount(Operation op, int i) {
processed.addAndGet(i);
}
@Override
public void addOperationRecordCount(Operation op, int i) {
counter.addAndGet(i);
if (op == Operation.DELETE) {
deleted.addAndGet(i);
} else if (op == Operation.MODIFY || op == Operation.UPSERT) {
updated.addAndGet(i);
}
}
public Object[] getLastRecord() {
return lastRecord;
}
@Override
public int getErrorCount() {
return errorCount.get();
}
@Override
public int getErrorRecordCount() {
return errorRecordCount.get();
}
@Override
public void resetErrorCount() {
errorCount.set(0);
}
@Override
public void resetErrorRecordCount() {
errorRecordCount.set(0);
}
@Override
public void addErrorCount(int count) {
errorCount.addAndGet(count);
}
@Override
public void addErrorRecordCount(int count) {
errorRecordCount.addAndGet(count);
}
@Override
public void resetSubmittedRowCount() {
submittedRowCount.set(0);
}
@Override
public void addSubmittedRowCount(int count) {
submittedRowCount.addAndGet(count);
}
@Override
public int getSubmittedRowCount() {
return submittedRowCount.get();
}
}
}
}