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