OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UpdateDeleteInsertConcurrencyTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 import java.util.concurrent.CyclicBarrier;
24 
26  final static Logger logger =
27  LoggerFactory.getLogger(UpdateDeleteInsertConcurrencyTest.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 {
52  test.testUpdateDeleteInsertConcurrency();
53  }
54 
55  private void runTest(String db,
56  String dbaUser,
57  String dbaPassword,
58  String dbUser,
59  String dbPassword,
60  Boolean concurrentInserts) throws Exception {
61  int num_threads = 5;
62  final int runs = 25;
63  final int num_rows = 1000;
64  final int fragment_size = 10;
65  final String tableName = "test";
66  Exception exceptions[] = new Exception[num_threads];
67 
68  final CyclicBarrier barrier = new CyclicBarrier(num_threads, new Runnable() {
69  public void run() {
70  try {
71  MapdTestClient dba =
72  MapdTestClient.getClient("localhost", 6274, db, dbaUser, dbaPassword);
73  dba.runSql("CREATE TABLE " + tableName
74  + "(x BIGINT, y INTEGER, z SMALLINT, a TINYINT, f FLOAT, d DOUBLE, deci DECIMAL(18,6), str TEXT ENCODING NONE) WITH (FRAGMENT_SIZE = "
75  + fragment_size + ")");
76  if (!concurrentInserts) {
77  for (int i = 0; i < num_rows; i++) {
78  final String integer_val = Integer.toString(i);
79  final String small_val = Integer.toString(i % 128);
80  final String fp_val = Double.toString(i * 1.1);
81  final String deci_val = Double.toString(i + 0.01);
82  final String str_val = "'" + text_values[i % text_values.length] + "'";
83  final String values_string = String.join(" , ",
84  integer_val,
85  integer_val,
86  small_val,
87  small_val,
88  fp_val,
89  fp_val,
90  deci_val,
91  str_val);
92  dba.runSql("INSERT INTO " + tableName + " VALUES "
93  + "(" + values_string + ")");
94  }
95  }
96  } catch (Exception e) {
97  logger.error("[" + Thread.currentThread().getId() + "]"
98  + " Caught Exception: " + e.getMessage(),
99  e);
100  exceptions[0] = e;
101  }
102  }
103  });
104 
105  ArrayList<Thread> threads = new ArrayList<>();
106  for (int i = 0; i < num_threads; i++) {
107  logger.info("Starting " + i);
108  final int threadId = i;
109 
110  Thread t = new Thread(new Runnable() {
111  @Override
112  public void run() {
113  long tid = Thread.currentThread().getId();
114  String logPrefix = "[" + tid + "]";
115  String sql = "";
116 
117  try {
118  barrier.await();
119 
120  MapdTestClient user =
121  MapdTestClient.getClient("localhost", 6274, db, dbUser, dbPassword);
122 
123  if (concurrentInserts) {
124  for (int i = 0; i < num_rows / num_threads; i++) {
125  final String integer_val = Integer.toString(i);
126  final String small_val = Integer.toString(i % 128);
127  final String fp_val = Double.toString(i * 1.1);
128  final String deci_val = Double.toString(i + 0.01);
129  final String str_val = "'" + text_values[i % text_values.length] + "'";
130  final String values_string = String.join(" , ",
131  integer_val,
132  integer_val,
133  small_val,
134  small_val,
135  fp_val,
136  fp_val,
137  deci_val,
138  str_val);
139  user.runSql("INSERT INTO " + tableName + " VALUES "
140  + "(" + values_string + ")");
141  }
142  }
143 
144  Random rand = new Random(tid);
145 
146  sql = "DELETE FROM " + tableName + " WHERE x = " + (tid * 2) + ";";
147  logger.info(logPrefix + " " + sql);
148  user.runSql(sql);
149 
150  sql = "DELETE FROM " + tableName + " WHERE y = " + rand.nextInt(num_rows)
151  + ";";
152  logger.info(logPrefix + " " + sql);
153  user.runSql(sql);
154 
155  sql = "SELECT COUNT(*) FROM " + tableName + " WHERE x > " + (tid * 2) + ";";
156  logger.info(logPrefix + " " + sql);
157  user.runSql(sql);
158 
159  sql = "DELETE FROM " + tableName + " WHERE str = '"
160  + text_values[rand.nextInt(text_values.length)] + "';";
161  logger.info(logPrefix + " " + sql);
162  user.runSql(sql);
163 
164  sql = "SELECT * FROM " + tableName + " WHERE str = '"
165  + text_values[rand.nextInt(text_values.length)] + "';";
166  logger.info(logPrefix + " " + sql);
167  user.runSql(sql);
168 
169  sql = "DELETE FROM " + tableName + " WHERE d < " + rand.nextInt(num_rows / 4)
170  + ";";
171  logger.info(logPrefix + " " + sql);
172  user.runSql(sql);
173 
174  sql = "INSERT INTO " + tableName + " VALUES "
175  + "(" + tid + "," + tid + "," + tid + "," + tid + "," + tid + ","
176  + tid + "," + tid + "," + (tid % 2 == 0 ? "'mapd'" : "'omnisci'")
177  + ");";
178  logger.info(logPrefix + " " + sql);
179  user.runSql(sql);
180 
181  sql = "DELETE FROM " + tableName + " WHERE z = " + tid + ";";
182  logger.info(logPrefix + " " + sql);
183  user.runSql(sql);
184 
185  } catch (Exception e) {
186  logger.error(logPrefix + " Caught Exception: " + e.getMessage(), e);
187  exceptions[threadId] = e;
188  }
189  }
190  });
191  t.start();
192  threads.add(t);
193  }
194 
195  for (Thread t : threads) {
196  t.join();
197  }
198 
199  MapdTestClient dba =
200  MapdTestClient.getClient("localhost", 6274, db, dbaUser, dbaPassword);
201  dba.runSql("DROP TABLE " + tableName + ";");
202 
203  for (Exception e : exceptions) {
204  if (null != e) {
205  logger.error("Exception: " + e.getMessage(), e);
206  throw e;
207  }
208  }
209  }
210 
211  public void testUpdateDeleteInsertConcurrency() throws Exception {
212  logger.info("testUpdateDeleteInsertConcurrency()");
213 
214  MapdTestClient su = MapdTestClient.getClient(
215  "localhost", 6274, "omnisci", "admin", "HyperInteractive");
216  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
217  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
218 
219  su.runSql("GRANT CREATE on DATABASE omnisci TO bob;");
220 
221  su.runSql("CREATE DATABASE db1;");
222  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
223  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
224  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
225  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
226 
227  runTest("db1",
228  "admin",
229  "HyperInteractive",
230  "admin",
231  "HyperInteractive",
232  /* concurrentInserts= */ false);
233  runTest("db1",
234  "admin",
235  "HyperInteractive",
236  "admin",
237  "HyperInteractive",
238  /* concurrentInserts= */ true);
239  // TODO: run some tests as bob
240 
241  su.runSql("DROP DATABASE db1;");
242  su.runSql("DROP USER bob;");
243  su.runSql("DROP USER dba;");
244  }
245 }
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword, Boolean concurrentInserts)
const int8_t const int64_t * num_rows
static bool run