OmniSciDB  2e3a973ef4
DistributedConcurrencyTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 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  MapdTestClient user =
79  MapdTestClient.getClient("localhost", 6274, db, dbUser, dbPassword);
80 
81  user.runSql("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 
85  for (int i = 0; i < num_rows; i++) {
86  final String integer_val = Integer.toString(i);
87  final String small_val = Integer.toString(i % 128);
88  final String fp_val = Double.toString(i * 1.1);
89  final String deci_val = Double.toString(i + 0.01);
90  final String str_val = "'" + text_values[i % text_values.length] + "'";
91  final String values_string = String.join(" , ",
92  integer_val,
93  integer_val,
94  small_val,
95  small_val,
96  fp_val,
97  fp_val,
98  deci_val,
99  str_val);
100  user.runSql("INSERT INTO " + tableName + " VALUES "
101  + "(" + values_string + ")");
102  }
103 
104  Random rand = new Random(tid);
105 
106  sql = "ALTER TABLE " + tableName + " ADD COLUMN zz TEXT ENCODING DICT(8);";
107  logger.info(logPrefix + " " + sql);
108  user.runSql(sql);
109 
110  // TODO(adb): add get_table_details once thread safe
111 
112  sql = "SELECT * FROM " + tableName + " LIMIT 2;";
113  logger.info(logPrefix + " " + sql);
114  user.runSql(sql);
115 
116  sql = "VALIDATE CLUSTER;";
117  logger.info(logPrefix + " " + sql);
118  user.runSql(sql);
119 
120  user.get_status();
121  user.get_hardware_info();
122  user.get_memory("cpu");
123 
124  sql = "DELETE FROM " + tableName + " WHERE y = " + rand.nextInt(num_rows)
125  + ";";
126  logger.info(logPrefix + " " + sql);
127  user.runSql(sql);
128 
129  sql = "ALTER TABLE " + tableName + " DROP COLUMN x;";
130  logger.info(logPrefix + " " + sql);
131  user.runSql(sql);
132 
133  sql = "SELECT * FROM " + tableName + " WHERE str = '"
134  + text_values[rand.nextInt(text_values.length)] + "';";
135  logger.info(logPrefix + " " + sql);
136  user.runSql(sql);
137 
138  sql = "SELECT COUNT(*) FROM " + tableName + ";";
139  logger.info(logPrefix + " VALIDATE " + sql);
140  user.sqlValidate(sql);
141 
142  sql = "TRUNCATE TABLE " + tableName + ";";
143  logger.info(logPrefix + " " + sql);
144  user.runSql(sql);
145 
146  sql = "DROP TABLE " + tableName + ";";
147  logger.info(logPrefix + " " + sql);
148  user.runSql(sql);
149 
150  } catch (Exception e) {
151  logger.error(logPrefix + " Caught Exception: " + e.getMessage(), e);
152  exceptions[threadId] = e;
153  }
154  }
155  }
156  });
157  t.start();
158  threads.add(t);
159  }
160 
161  for (Thread t : threads) {
162  t.join();
163  }
164 
165  for (Exception e : exceptions) {
166  if (null != e) {
167  logger.error("Exception: " + e.getMessage(), e);
168  throw e;
169  }
170  }
171  }
172 
173  public void testConcurrency() throws Exception {
174  logger.info("DistributedConcurrencyTest()");
175 
177  "localhost", 6274, "omnisci", "admin", "HyperInteractive");
178  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
179  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
180 
181  su.runSql("GRANT CREATE on DATABASE omnisci TO bob;");
182 
183  su.runSql("CREATE DATABASE db1;");
184  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
185  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
186  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
187  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
188 
189  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive");
190  // TODO: run some tests as bob
191 
192  su.runSql("DROP DATABASE db1;");
193  su.runSql("DROP USER bob;");
194  su.runSql("DROP USER dba;");
195 
196  logger.info("DistributedConcurrencyTest() done");
197  }
198 }
List< TNodeMemoryInfo > get_memory(String memory_level)
const int8_t const int64_t * num_rows
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
List< TServerStatus > get_status()
static MapdTestClient getClient(String host, int port, String db, String user, String password)
List< TColumnType > sqlValidate(String sql)
TQueryResult runSql(String sql)
static bool run
TClusterHardwareInfo get_hardware_info()