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