OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SelectUpdateDeleteDifferentTables.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 
25  final static Logger logger =
26  LoggerFactory.getLogger(SelectUpdateDeleteDifferentTables.class);
27 
28  final static String[] text_values = {"foo",
29  "bar",
30  "hello",
31  "world",
32  "a",
33  "b",
34  "c",
35  "d",
36  "e",
37  "f",
38  "g",
39  "h",
40  "i",
41  "j",
42  "k",
43  "l",
44  "m",
45  "n",
46  "o",
47  "p"};
48 
49  public static void main(String[] args) throws Exception {
51  test.testConcurrency();
52  }
53 
54  private void runTest(
55  String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
56  throws Exception {
57  int num_threads = 5;
58  final int runs = 5;
59  final int num_rows = 400;
60  final int fragment_size = 25;
61  Exception exceptions[] = new Exception[num_threads];
62 
63  ArrayList<Thread> threads = new ArrayList<>();
64  for (int i = 0; i < num_threads; i++) {
65  logger.info("Starting " + i);
66  final int threadId = i;
67 
68  Thread t = new Thread(new Runnable() {
69  @Override
70  public void run() {
71  long tid = Thread.currentThread().getId();
72  String logPrefix = "[" + tid + "]";
73  String sql = "";
74 
75  for (int runId = 0; runId < runs; runId++) {
76  final String tableName = dbaUser + "_" + threadId + "_" + runId;
77 
78  try {
79  HeavyDBTestClient user = HeavyDBTestClient.getClient(
80  "localhost", 6274, db, dbUser, dbPassword);
81 
82  user.runSql("CREATE TABLE " + tableName
83  + "(x BIGINT, y INTEGER, z SMALLINT, a TINYINT, f FLOAT, d DOUBLE, deci DECIMAL(18,6), str TEXT ENCODING NONE) WITH (FRAGMENT_SIZE = "
84  + fragment_size + ")");
85 
86  for (int i = 0; i < num_rows; i++) {
87  final String integer_val = Integer.toString(i);
88  final String small_val = Integer.toString(i % 128);
89  final String fp_val = Double.toString(i * 1.1);
90  final String deci_val = Double.toString(i + 0.01);
91  final String str_val = "'" + text_values[i % text_values.length] + "'";
92  final String values_string = String.join(" , ",
93  integer_val,
94  integer_val,
95  small_val,
96  small_val,
97  fp_val,
98  fp_val,
99  deci_val,
100  str_val);
101  user.runSql("INSERT INTO " + tableName + " VALUES "
102  + "(" + values_string + ")");
103  }
104 
105  Random rand = new Random(tid);
106 
107  sql = "ALTER TABLE " + tableName + " ADD COLUMN zz TEXT ENCODING DICT(8);";
108  logger.info(logPrefix + " " + sql);
109  user.runSql(sql);
110 
111  // TODO(adb): add get_table_details once thread safe
112 
113  sql = "SELECT * FROM " + tableName + " LIMIT 2;";
114  logger.info(logPrefix + " " + sql);
115  user.runSql(sql);
116 
117  sql = "DELETE FROM " + tableName + " WHERE y = " + rand.nextInt(num_rows)
118  + ";";
119  logger.info(logPrefix + " " + sql);
120  user.runSql(sql);
121 
122  sql = "ALTER TABLE " + tableName + " DROP COLUMN x;";
123  logger.info(logPrefix + " " + sql);
124  user.runSql(sql);
125 
126  sql = "SELECT * FROM " + tableName + " WHERE str = '"
127  + text_values[rand.nextInt(text_values.length)] + "';";
128  logger.info(logPrefix + " " + sql);
129  user.runSql(sql);
130 
131  sql = "SELECT COUNT(*) FROM " + tableName + ";";
132  logger.info(logPrefix + " VALIDATE " + sql);
133  user.sqlValidate(sql);
134 
135  final String tableRename = tableName + "_rename";
136  sql = "ALTER TABLE " + tableName + " RENAME TO " + tableRename;
137  user.runSql(sql);
138 
139  sql = "TRUNCATE TABLE " + tableRename + ";";
140  logger.info(logPrefix + " " + sql);
141  user.runSql(sql);
142 
143  sql = "INSERT INTO " + tableRename + " VALUES "
144  + "(" + tid + "," + tid + "," + tid + "," + tid + "," + tid + ","
145  + tid + "," + tid + "," + (tid % 2 == 0 ? "'value_1'" : "'value_2'")
146  + ");";
147  logger.info(logPrefix + " " + sql);
148  user.runSql(sql);
149 
150  sql = "DROP TABLE " + tableRename + ";";
151  logger.info(logPrefix + " " + sql);
152  user.runSql(sql);
153 
154  } catch (Exception e) {
155  logger.error(logPrefix + " Caught Exception: " + e.getMessage(), e);
156  exceptions[threadId] = e;
157  }
158  }
159  }
160  });
161  t.start();
162  threads.add(t);
163  }
164 
165  for (Thread t : threads) {
166  t.join();
167  }
168 
169  for (Exception e : exceptions) {
170  if (null != e) {
171  logger.error("Exception: " + e.getMessage(), e);
172  throw e;
173  }
174  }
175  }
176 
177  public void testConcurrency() throws Exception {
178  logger.info("SelectUpdateDeleteDifferentTables()");
179 
180  HeavyDBTestClient su = HeavyDBTestClient.getClient(
181  "localhost", 6274, "heavyai", "admin", "HyperInteractive");
182  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
183  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
184 
185  su.runSql("GRANT CREATE on DATABASE heavyai TO bob;");
186 
187  su.runSql("CREATE DATABASE db1;");
188  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
189  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
190  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
191  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
192 
193  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive");
194  // TODO: run some tests as bob
195 
196  su.runSql("DROP DATABASE db1;");
197  su.runSql("DROP USER bob;");
198  su.runSql("DROP USER dba;");
199 
200  logger.info("SelectUpdateDeleteDifferentTables() done");
201  }
202 }
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
static bool run