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