OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReductionConcurrencyTest.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(ReductionConcurrencyTest.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(String db,
54  String dbaUser,
55  String dbaPassword,
56  String dbUser,
57  String dbPassword,
58  int numRows,
59  int fragmentSize) throws Exception {
60  int num_threads = 5;
61  final int runs = 5;
62  Exception exceptions[] = new Exception[num_threads];
63 
64  ArrayList<Thread> threads = new ArrayList<>();
65  for (int i = 0; i < num_threads; i++) {
66  logger.info("Starting " + i);
67  final int threadId = i;
68 
69  Thread t = new Thread(new Runnable() {
70  @Override
71  public void run() {
72  long tid = Thread.currentThread().getId();
73  String logPrefix = "[" + tid + "]";
74  String sql = "";
75 
76  for (int runId = 0; runId < runs; runId++) {
77  final String tableName = dbaUser + "_" + threadId + "_" + runId;
78 
79  try {
80  HeavyDBTestClient user = HeavyDBTestClient.getClient(
81  "localhost", 6274, db, dbUser, dbPassword);
82  user.runSql("DROP TABLE IF EXISTS " + tableName + ";");
83  user.runSql("CREATE TABLE " + tableName
84  + "(x BIGINT, y INTEGER, z SMALLINT, a TINYINT, f FLOAT, d DOUBLE, deci DECIMAL(18,6), str TEXT ENCODING NONE) WITH (FRAGMENT_SIZE = "
85  + fragmentSize + ")");
86 
87  for (int i = 0; i < numRows; i++) {
88  final String integer_val = Integer.toString(i);
89  final String small_val = Integer.toString(i % 128);
90  final String fp_val = Double.toString(i * 1.1);
91  final String deci_val = Double.toString(i + 0.01);
92  final String str_val = "'" + text_values[i % text_values.length] + "'";
93  final String values_string = String.join(" , ",
94  integer_val,
95  integer_val,
96  small_val,
97  small_val,
98  fp_val,
99  fp_val,
100  deci_val,
101  str_val);
102  user.runSql("INSERT INTO " + tableName + " VALUES "
103  + "(" + values_string + ")");
104  }
105 
106  Random rand = new Random(tid);
107 
108  sql = "SELECT * FROM " + tableName + " LIMIT 2;";
109  logger.info(logPrefix + " " + sql);
110  user.runSql(sql);
111 
112  sql = "SELECT x, sum(1) FROM " + tableName + " GROUP BY 1;";
113  logger.info(logPrefix + " " + sql);
114  user.runSql(sql);
115 
116  sql = "SELECT x, y, sum(1) FROM " + tableName + " GROUP BY 1, 2;";
117  logger.info(logPrefix + " " + sql);
118  user.runSql(sql);
119 
120  sql = "SELECT x, y, z, sum(1) FROM " + tableName + " GROUP BY 1, 2, 3;";
121  logger.info(logPrefix + " " + sql);
122  user.runSql(sql);
123 
124  sql = "SELECT x, y, avg(z), sum(1) FROM " + tableName + " GROUP BY 1, 2;";
125  logger.info(logPrefix + " " + sql);
126  user.runSql(sql);
127 
128  sql = "SELECT x, y, max(z), sum(1) FROM " + tableName + " GROUP BY 1, 2;";
129  logger.info(logPrefix + " " + sql);
130  user.runSql(sql);
131 
132  sql = "SELECT x, y, min(z), sum(1) FROM " + tableName + " GROUP BY 1, 2;";
133  logger.info(logPrefix + " " + sql);
134  user.runSql(sql);
135 
136  sql = "SELECT * FROM " + tableName + " WHERE str = '"
137  + text_values[rand.nextInt(text_values.length)] + "';";
138  logger.info(logPrefix + " " + sql);
139  user.runSql(sql);
140 
141  } catch (Exception e) {
142  logger.error(logPrefix + " Caught Exception: " + e.getMessage(), e);
143  exceptions[threadId] = e;
144  }
145  }
146  }
147  });
148  t.start();
149  threads.add(t);
150  }
151 
152  for (Thread t : threads) {
153  t.join();
154  }
155 
156  for (Exception e : exceptions) {
157  if (null != e) {
158  logger.error("Exception: " + e.getMessage(), e);
159  throw e;
160  }
161  }
162  }
163 
164  public void testConcurrency() throws Exception {
165  logger.info("ReductionConcurrencyTest()");
166 
167  HeavyDBTestClient su = HeavyDBTestClient.getClient(
168  "localhost", 6274, "heavyai", "admin", "HyperInteractive");
169  su.runSql("DROP USER IF EXISTS dba;");
170  su.runSql("DROP USER IF EXISTS bob;");
171  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
172  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
173 
174  su.runSql("GRANT CREATE on DATABASE heavyai TO bob;");
175 
176  su.runSql("DROP DATABASE IF EXISTS db1;");
177  su.runSql("CREATE DATABASE db1;");
178 
179  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
180  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
181  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
182  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
183 
184  // run the test multiple times to make sure about the test results
185  for (int i = 0; i < 3; ++i) {
186  // test 1. reduction by interpreter
187  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive", 10, 3);
188  // test 2. reduction by codegen
189  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive", 30, 3);
190  }
191 
192  su.runSql("DROP DATABASE db1;");
193  su.runSql("DROP USER bob;");
194  su.runSql("DROP USER dba;");
195 
196  logger.info("ReductionConcurrencyTest() done");
197  }
198 }
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword, int numRows, int fragmentSize)
static bool run