OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlterDropTruncateValidateConcurrencyTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.tests;
17 
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 
21 import java.util.ArrayList;
22 import java.util.Random;
23 import java.util.concurrent.CyclicBarrier;
24 
26  final static Logger logger =
27  LoggerFactory.getLogger(AlterDropTruncateValidateConcurrencyTest.class);
28 
29  final static String[] text_values = {"foo",
30  "bar",
31  "hello",
32  "world",
33  "a",
34  "b",
35  "c",
36  "d",
37  "e",
38  "f",
39  "g",
40  "h",
41  "i",
42  "j",
43  "k",
44  "l",
45  "m",
46  "n",
47  "o",
48  "p"};
49 
50  public static void main(String[] args) throws Exception {
53  test.testConcurrency();
54  }
55 
56  private void runTest(
57  String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
58  throws Exception {
59  int num_threads = 5;
60  final int runs = 25;
61  final int num_rows = 1000;
62  final int fragment_size = 10;
63  final String tableName = "test";
64  Exception exceptions[] = new Exception[num_threads];
65 
66  final CyclicBarrier barrier = new CyclicBarrier(num_threads, new Runnable() {
67  public void run() {
68  try {
69  HeavyDBTestClient dba = HeavyDBTestClient.getClient(
70  "localhost", 6274, db, dbaUser, dbaPassword);
71  dba.runSql("CREATE TABLE " + tableName
72  + "(x BIGINT, y INTEGER, z SMALLINT, a TINYINT, f FLOAT, d DOUBLE, deci DECIMAL(18,6), str TEXT ENCODING NONE) WITH (FRAGMENT_SIZE = "
73  + fragment_size + ")");
74 
75  for (int i = 0; i < num_rows; i++) {
76  final String integer_val = Integer.toString(i);
77  final String small_val = Integer.toString(i % 128);
78  final String fp_val = Double.toString(i * 1.1);
79  final String deci_val = Double.toString(i + 0.01);
80  final String str_val = "'" + text_values[i % text_values.length] + "'";
81  final String values_string = String.join(" , ",
82  integer_val,
83  integer_val,
84  small_val,
85  small_val,
86  fp_val,
87  fp_val,
88  deci_val,
89  str_val);
90  dba.runSql("INSERT INTO " + tableName + " VALUES "
91  + "(" + values_string + ")");
92  }
93  } catch (Exception e) {
94  logger.error("[" + Thread.currentThread().getId() + "]"
95  + " Caught Exception: " + e.getMessage(),
96  e);
97  exceptions[0] = e;
98  }
99  }
100  });
101 
102  ArrayList<Thread> threads = new ArrayList<>();
103  for (int i = 0; i < num_threads; i++) {
104  logger.info("Starting " + i);
105  final int threadId = i;
106 
107  Thread t = new Thread(new Runnable() {
108  @Override
109  public void run() {
110  long tid = Thread.currentThread().getId();
111  String logPrefix = "[" + tid + "]";
112  String sql = "";
113 
114  try {
115  barrier.await();
116 
117  HeavyDBTestClient user = HeavyDBTestClient.getClient(
118  "localhost", 6274, db, dbUser, dbPassword);
119 
120  Random rand = new Random(tid);
121 
122  if (threadId == 2) {
123  sql = "ALTER TABLE " + tableName + " ADD COLUMN zz TEXT ENCODING DICT(8);";
124  logger.info(logPrefix + " " + sql);
125  user.runSql(sql);
126  }
127 
128  // TODO(adb): add get_table_details once thread safe
129 
130  sql = "SELECT * FROM " + tableName + " LIMIT 2;";
131  logger.info(logPrefix + " " + sql);
132  user.runSql(sql);
133 
134  sql = "DELETE FROM " + tableName + " WHERE y = " + rand.nextInt(num_rows)
135  + ";";
136  logger.info(logPrefix + " " + sql);
137  user.runSql(sql);
138 
139  if (threadId == 0) {
140  sql = "ALTER TABLE " + tableName + " DROP COLUMN x;";
141  logger.info(logPrefix + " " + sql);
142  user.runSql(sql);
143  }
144 
145  sql = "SELECT * FROM " + tableName + " WHERE str = '"
146  + text_values[rand.nextInt(text_values.length)] + "';";
147  logger.info(logPrefix + " " + sql);
148  user.runSql(sql);
149 
150  sql = "SELECT COUNT(*) FROM " + tableName + ";";
151  logger.info(logPrefix + " VALIDATE " + sql);
152  user.sqlValidate(sql);
153 
154  sql = "TRUNCATE TABLE " + tableName + ";";
155  logger.info(logPrefix + " " + sql);
156  user.runSql(sql);
157 
158  } catch (Exception e) {
159  logger.error(logPrefix + " Caught Exception: " + e.getMessage(), e);
160  exceptions[threadId] = e;
161  }
162  }
163  });
164  t.start();
165  threads.add(t);
166  }
167 
168  for (Thread t : threads) {
169  t.join();
170  }
171 
172  HeavyDBTestClient dba =
173  HeavyDBTestClient.getClient("localhost", 6274, db, dbaUser, dbaPassword);
174  dba.runSql("DROP TABLE " + tableName + ";");
175 
176  for (Exception e : exceptions) {
177  if (null != e) {
178  logger.error("Exception: " + e.getMessage(), e);
179  throw e;
180  }
181  }
182  }
183 
184  public void testConcurrency() throws Exception {
185  logger.info("AlterDropTruncateValidateConcurrencyTest()");
186 
187  HeavyDBTestClient su = HeavyDBTestClient.getClient(
188  "localhost", 6274, "heavyai", "admin", "HyperInteractive");
189  try {
190  su.runSql("DROP USER dba");
191  } catch (Exception e) {
192  }
193  try {
194  su.runSql("DROP USER bob");
195  } catch (Exception e) {
196  }
197  su.runSql("DROP DATABASE IF EXISTS db1");
198 
199  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
200  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
201 
202  su.runSql("GRANT CREATE on DATABASE heavyai TO bob;");
203 
204  su.runSql("CREATE DATABASE db1;");
205  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
206  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
207  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
208  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
209 
210  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive");
211  // TODO: run some tests as bob
212 
213  su.runSql("DROP DATABASE db1;");
214  su.runSql("DROP USER bob;");
215  su.runSql("DROP USER dba;");
216 
217  logger.info("AlterDropTruncateValidateConcurrencyTest() done");
218  }
219 }
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
static bool run