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