OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EagainConcurrencyTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2015 The Apache Software Foundation.
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 
23 public class EagainConcurrencyTest {
24  final static Logger logger = LoggerFactory.getLogger(EagainConcurrencyTest.class);
25 
26  public static void main(String[] args) throws Exception {
28  test.testCatalogConcurrency();
29  }
30 
31  private void run_test(HeavyDBTestClient dba,
32  String db,
33  String dbUser,
34  String dbPassword,
35  String prefix,
36  int max) throws Exception {
37  String tableName = "table_" + prefix + "_";
38  String viewName = "view_" + prefix + "_";
39  long tid = Thread.currentThread().getId();
40 
41  logger.info("[" + tid + "]"
42  + "FIRST USER CONNECT");
43  HeavyDBTestClient first_user =
44  HeavyDBTestClient.getClient("localhost", 6274, db, dbUser, dbPassword);
45 
46  logger.info("[" + tid + "]"
47  + "CREATE " + tableName);
48  first_user.runSql("CREATE TABLE " + tableName + " (id integer);");
49 
50  logger.info("[" + tid + "]"
51  + "CREATE VIEW " + viewName);
52  first_user.runSql(
53  "CREATE VIEW " + viewName + " AS (SELECT id * 2 FROM " + tableName + ");");
54 
55  logger.info("[" + tid + "]"
56  + "FIRST USER DISCONNECT");
57  first_user.disconnect();
58 
59  for (int i = 0; i < max; i++) {
60  logger.info("[" + tid + "]"
61  + "USER CONNECT");
62  HeavyDBTestClient user =
63  HeavyDBTestClient.getClient("localhost", 6274, db, dbUser, dbPassword);
64 
65  logger.info("[" + tid + "]"
66  + "INSERT INTO " + tableName);
67  user.runSql("INSERT INTO " + tableName + " VALUES (" + i + ");");
68 
69  user.get_status();
70  user.get_server_status();
71  user.get_hardware_info();
72 
73  logger.info("[" + tid + "]"
74  + "SELECT FROM " + tableName);
75  user.runSql("SELECT * FROM " + tableName + ";");
76 
77  user.get_memory("cpu");
78  user.get_dashboards();
79  // user.get_tables_meta(); // TODO(adb): re-enable after fixing up catalog/db
80  // locking
81 
82  logger.info("[" + tid + "]"
83  + "USER DISCONNECT");
84  user.disconnect();
85  }
86 
87  logger.info("[" + tid + "]"
88  + "DROP " + viewName);
89  dba.runSql("DROP VIEW " + viewName + ";");
90 
91  logger.info("[" + tid + "]"
92  + "DROP " + tableName);
93  dba.runSql("DROP TABLE " + tableName + ";");
94  }
95 
96  private void runTest(
97  String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
98  throws Exception {
99  int num_threads = 5;
100  final int runs = 25;
101  Exception exceptions[] = new Exception[num_threads];
102 
103  ArrayList<Thread> threads = new ArrayList<>();
104  for (int i = 0; i < num_threads; i++) {
105  logger.info("Starting " + i);
106  final String prefix = "for_bob_" + i + "_";
107  final int threadId = i;
108  Thread t = new Thread(new Runnable() {
109  @Override
110  public void run() {
111  try {
112  HeavyDBTestClient dba = HeavyDBTestClient.getClient(
113  "localhost", 6274, db, dbaUser, dbaPassword);
114  run_test(dba, db, dbUser, dbPassword, prefix, runs);
115  } catch (Exception e) {
116  logger.error("[" + Thread.currentThread().getId() + "]"
117  + "Caught Exception: " + e.getMessage(),
118  e);
119  exceptions[threadId] = e;
120  }
121  }
122  });
123  t.start();
124  threads.add(t);
125  }
126 
127  for (Thread t : threads) {
128  t.join();
129  }
130 
131  for (Exception e : exceptions) {
132  if (null != e) {
133  logger.error("Exception: " + e.getMessage(), e);
134  throw new Exception(e.getMessage(), e);
135  }
136  }
137  }
138 
139  public void testCatalogConcurrency() throws Exception {
140  logger.info("testCatalogConcurrency()");
141 
142  HeavyDBTestClient su = HeavyDBTestClient.getClient(
143  "localhost", 6274, "heavyai", "admin", "HyperInteractive");
144  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
145  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
146 
147  su.runSql("GRANT CREATE on DATABASE heavyai TO bob;");
148  su.runSql("GRANT CREATE VIEW on DATABASE heavyai TO bob;");
149  su.runSql("GRANT CREATE DASHBOARD on DATABASE heavyai TO bob;");
150 
151  su.runSql("GRANT DROP on DATABASE heavyai TO bob;");
152  su.runSql("GRANT DROP VIEW on DATABASE heavyai TO bob;");
153  su.runSql("GRANT DELETE DASHBOARD on DATABASE heavyai TO bob;");
154 
155  su.runSql("CREATE DATABASE db1;");
156 
157  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
158  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
159  su.runSql("GRANT CREATE DASHBOARD on DATABASE db1 TO bob;");
160 
161  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
162  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
163  su.runSql("GRANT DELETE DASHBOARD on DATABASE db1 TO bob;");
164 
165  su.runSql("GRANT ACCESS on database heavyai TO dba;");
166  su.runSql("GRANT ACCESS on database heavyai TO bob;");
167  su.runSql("GRANT ACCESS on database db1 TO dba;");
168  su.runSql("GRANT ACCESS on database db1 TO bob;");
169 
170  runTest("db1", "admin", "HyperInteractive", "admin", "HyperInteractive");
171  runTest("db1", "admin", "HyperInteractive", "dba", "password");
172  runTest("db1", "admin", "HyperInteractive", "bob", "password");
173  runTest("db1", "dba", "password", "admin", "HyperInteractive");
174  runTest("db1", "dba", "password", "bob", "password");
175 
176  runTest("heavyai", "admin", "HyperInteractive", "admin", "HyperInteractive");
177  runTest("heavyai", "admin", "HyperInteractive", "dba", "password");
178  runTest("heavyai", "admin", "HyperInteractive", "bob", "password");
179  runTest("heavyai", "dba", "password", "admin", "HyperInteractive");
180  runTest("heavyai", "dba", "password", "bob", "password");
181 
182  su.runSql("DROP DATABASE db1;");
183  su.runSql("DROP USER bob;");
184  su.runSql("DROP USER dba;");
185 
186  logger.info("testCatalogConcurrency() done");
187  }
188 }
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword)
void run_test(HeavyDBTestClient dba, String db, String dbUser, String dbPassword, String prefix, int max)
static bool run