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