16 package com.mapd.tests;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.util.ArrayList;
22 import java.util.Random;
23 import java.util.concurrent.CyclicBarrier;
25 import ai.heavy.thrift.server.TDBException;
29 LoggerFactory.getLogger(CtasItasSelectUpdelConcurrencyTest.class);
52 public static void main(String[]
args)
throws Exception {
54 test.testConcurrency();
58 String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
62 final int num_rows = 1000;
63 final int fragment_size = 10;
64 final String tableName =
"test";
65 Exception exceptions[] =
new Exception[num_threads];
67 final CyclicBarrier barrier =
new CyclicBarrier(num_threads,
new Runnable() {
71 "localhost", 6274, db, dbaUser, dbaPassword);
72 dba.runSql(
"CREATE TABLE " + tableName
73 +
"(x BIGINT, y INTEGER, z SMALLINT, a TINYINT, f FLOAT, d DOUBLE, deci DECIMAL(18,6), str TEXT ENCODING NONE) WITH (FRAGMENT_SIZE = "
74 + fragment_size +
")");
76 for (
int i = 0; i < num_rows; i++) {
77 final String integer_val = Integer.toString(i);
78 final String small_val = Integer.toString(i % 128);
79 final String fp_val = Double.toString(i * 1.1);
80 final String deci_val = Double.toString(i + 0.01);
81 final String str_val =
"'" +
text_values[i % text_values.length] +
"'";
82 final String values_string = String.join(
" , ",
91 dba.runSql(
"INSERT INTO " + tableName +
" VALUES "
92 +
"(" + values_string +
")");
95 }
catch (Exception e) {
96 logger.error(
"[" + Thread.currentThread().getId() +
"]"
97 +
" Caught Exception: " + e.getMessage(),
104 ArrayList<Thread> threads =
new ArrayList<>();
105 for (
int i = 0; i < num_threads; i++) {
106 logger.info(
"Starting " + i);
107 final int threadId = i;
109 Thread t =
new Thread(
new Runnable() {
112 long tid = Thread.currentThread().getId();
113 String logPrefix =
"[" + tid +
"]";
116 final String ctasTableName =
"ctas_test";
122 "localhost", 6274, db, dbUser, dbPassword);
124 Random rand =
new Random(tid);
127 sql =
"CREATE TABLE " + ctasTableName +
" AS (SELECT x, y, deci, str FROM "
129 logger.info(logPrefix +
" " + sql);
133 sql =
"SELECT * FROM " + tableName +
" LIMIT 2;";
134 logger.info(logPrefix +
" " + sql);
137 boolean ctas_table_created =
false;
138 while (!ctas_table_created) {
139 ctas_table_created =
true;
141 sql =
"SELECT COUNT(*) FROM " + ctasTableName +
";";
142 logger.info(logPrefix +
" VALIDATE " + sql);
143 user.sqlValidate(sql);
144 }
catch (TDBException e) {
145 if (e.getError_msg().indexOf(
"not found") != -1) {
147 ctas_table_created =
false;
154 sql =
"DELETE FROM " + ctasTableName +
" WHERE y = " + rand.nextInt(num_rows)
156 logger.info(logPrefix +
" " + sql);
160 sql =
"ALTER TABLE " + tableName +
" ADD COLUMN n TEXT ENCODING DICT(8);";
161 logger.info(logPrefix +
" VALIDATE " + sql);
164 sql =
"INSERT INTO " + tableName +
" VALUES "
165 +
"(" + tid +
"," + tid +
"," + tid +
"," + tid +
"," + tid +
","
166 + tid +
"," + tid +
"," + (tid % 2 == 0 ?
"'value_1'" :
"'value_2'")
168 logger.info(logPrefix +
" " + sql);
172 sql =
"INSERT INTO " + ctasTableName +
" (SELECT x, y, deci, str FROM "
173 + tableName +
" WHERE str = '"
174 +
text_values[rand.nextInt(text_values.length)] +
"');";
175 logger.info(logPrefix +
" " + sql);
178 sql =
"UPDATE " + tableName +
" SET str = (SELECT count(*) > 0 FROM "
180 logger.info(logPrefix +
" " + sql);
183 sql =
"TRUNCATE TABLE " + tableName +
";";
184 logger.info(logPrefix +
" " + sql);
187 }
catch (Exception e) {
188 logger.error(logPrefix +
" Caught Exception: " + e.getMessage(), e);
189 exceptions[threadId] = e;
197 for (Thread t : threads) {
202 HeavyDBTestClient.getClient(
"localhost", 6274, db, dbaUser, dbaPassword);
203 dba.runSql(
"DROP TABLE " + tableName +
";");
205 for (Exception e : exceptions) {
207 logger.error(
"Exception: " + e.getMessage(), e);
214 logger.info(
"CtasItasSelectUpdelConcurrencyTest()");
217 "localhost", 6274,
"heavyai",
"admin",
"HyperInteractive");
218 su.runSql(
"CREATE USER dba (password = 'password', is_super = 'true');");
219 su.runSql(
"CREATE USER bob (password = 'password', is_super = 'false');");
221 su.runSql(
"GRANT CREATE on DATABASE heavyai TO bob;");
223 su.runSql(
"CREATE DATABASE db1;");
224 su.runSql(
"GRANT CREATE on DATABASE db1 TO bob;");
225 su.runSql(
"GRANT CREATE VIEW on DATABASE db1 TO bob;");
226 su.runSql(
"GRANT DROP on DATABASE db1 TO bob;");
227 su.runSql(
"GRANT DROP VIEW on DATABASE db1 TO bob;");
229 runTest(
"db1",
"admin",
"HyperInteractive",
"admin",
"HyperInteractive");
232 su.runSql(
"DROP DATABASE db1;");
233 su.runSql(
"DROP USER bob;");
234 su.runSql(
"DROP USER dba;");
236 logger.info(
"CtasItasSelectUpdelConcurrencyTest() done");
static final String[] text_values
static final Logger logger
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
static void main(String[] args)