OmniSciDB  a5dc49c757
 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.*;
22 
23 import ai.heavy.thrift.server.*;
24 
25 public class CatalogConcurrencyTest {
26  final static String defPwd = "HyperInteractive", local = "localhost", defDb = "heavyai",
27  admin = "admin";
28  final static int port = 6274;
29  final static Logger logger = LoggerFactory.getLogger(CatalogConcurrencyTest.class);
30 
31  public static void main(String[] args) throws Exception {
33  test.testCatalogConcurrency();
34  }
35 
36  private void run_test(HeavyDBTestClient dba,
37  HeavyDBTestClient user,
38  String prefix,
39  int max,
40  List<Integer> dashboardIds) throws Exception {
41  final String sharedTableName = "table_shared";
42  for (int i = 0; i < max; i++) {
43  final long tid = Thread.currentThread().getId();
44  final String threadPrefix = "[" + tid + "] ",
45  tableName = "table_" + prefix + "_" + i,
46  viewName = "view_" + prefix + "_" + i,
47  dashName = "dash_" + prefix + "_" + i;
48 
49  // Modify the fixed id dashboards in parallel.
50  for (int id : dashboardIds) {
51  TDashboard board = dba.get_dashboard(id);
52  logger.info("REPLACE DASHBOARD id (" + id + ") " + board.dashboard_name);
53  dba.replace_dashboard(board.dashboard_id, board.dashboard_name + "_", admin);
54  }
55 
56  logger.info(threadPrefix + "CREATE TABLE " + tableName);
57  user.runSql("CREATE TABLE " + tableName + " (id text);");
58  HeavyDBAsserts.assertEqual(true, null != dba.get_table_details(tableName));
59  logger.info(threadPrefix + "INSERT INTO " + tableName);
60  user.runSql("INSERT INTO " + tableName + " VALUES(1);");
61  dba.runSql("GRANT SELECT ON TABLE " + tableName + " TO bob;");
62 
63  logger.info(threadPrefix + "CREATE VIEW " + viewName);
64  user.runSql("CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName + ";");
65  HeavyDBAsserts.assertEqual(true, null != dba.get_table_details(viewName));
66  dba.runSql("GRANT SELECT ON VIEW " + viewName + " TO bob;");
67 
68  logger.info(threadPrefix + "CREATE DASHBOARD " + dashName);
69  int dash_id = user.create_dashboard(dashName);
70  HeavyDBAsserts.assertEqual(true, null != dba.get_dashboard(dash_id));
71  dba.runSql("GRANT VIEW ON DASHBOARD " + dash_id + " TO bob;");
72 
73  dba.runSql("REVOKE VIEW ON DASHBOARD " + dash_id + " FROM bob;");
74  dba.runSql("REVOKE SELECT ON VIEW " + viewName + " FROM bob;");
75  dba.runSql("REVOKE SELECT ON TABLE " + tableName + " FROM bob;");
76 
77  logger.info(threadPrefix + "DELETE DASHBOARD " + dashName);
78  dba.delete_dashboard(dash_id);
79  logger.info(threadPrefix + "DROP VIEW " + viewName);
80  dba.runSql("DROP VIEW " + viewName + ";");
81  logger.info(threadPrefix + "DROP TABLE " + tableName);
82  dba.runSql("DROP TABLE " + tableName + ";");
83 
84  logger.info(threadPrefix + "CREATE IF NOT EXISTS " + sharedTableName);
85  dba.runSql("CREATE TABLE IF NOT EXISTS " + sharedTableName + " (id INTEGER);");
86 
87  logger.info(threadPrefix + "DROP IF EXISTS " + sharedTableName);
88  dba.runSql("DROP TABLE IF EXISTS " + sharedTableName + ";");
89  }
90  }
91 
92  private void runTest(String db,
93  String dbaUser,
94  String dbaPassword,
95  String dbUser,
96  String dbPassword,
97  List<Integer> dashboardIds) throws Exception {
98  final int num_threads = 5, runs = 25;
99  Exception exceptions[] = new Exception[num_threads];
100 
101  ArrayList<Thread> threads = new ArrayList<>();
102  for (int i = 0; i < num_threads; i++) {
103  logger.info("Starting " + i);
104  final String prefix = "for_bob_" + i + "_";
105  final int threadId = i;
106  Thread t = new Thread(new Runnable() {
107  @Override
108  public void run() {
109  try {
110  HeavyDBTestClient dba =
111  HeavyDBTestClient.getClient(local, port, db, dbaUser, dbaPassword);
112  HeavyDBTestClient user =
113  HeavyDBTestClient.getClient(local, port, db, dbUser, dbPassword);
114  run_test(dba, user, prefix, runs, dashboardIds);
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(local, port, defDb, admin, defPwd);
143 
144  su.runSql("DROP USER IF EXISTS bob;");
145  su.runSql("DROP USER IF EXISTS dba;");
146  su.runSql("DROP DATABASE IF EXISTS db1;");
147 
148  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
149  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
150 
151  su.runSql("GRANT CREATE on DATABASE heavyai TO bob;");
152  su.runSql("GRANT CREATE VIEW on DATABASE heavyai TO bob;");
153  su.runSql("GRANT CREATE DASHBOARD on DATABASE heavyai TO bob;");
154 
155  su.runSql("GRANT DROP on DATABASE heavyai TO bob;");
156  su.runSql("GRANT DROP VIEW on DATABASE heavyai TO bob;");
157  su.runSql("GRANT DELETE DASHBOARD on DATABASE heavyai TO bob;");
158 
159  su.runSql("CREATE DATABASE db1;");
160 
161  su.runSql("GRANT CREATE on DATABASE db1 TO bob;");
162  su.runSql("GRANT CREATE VIEW on DATABASE db1 TO bob;");
163  su.runSql("GRANT CREATE DASHBOARD on DATABASE db1 TO bob;");
164 
165  su.runSql("GRANT DROP on DATABASE db1 TO bob;");
166  su.runSql("GRANT DROP VIEW on DATABASE db1 TO bob;");
167  su.runSql("GRANT DELETE DASHBOARD on DATABASE db1 TO bob;");
168 
169  su.runSql("GRANT ACCESS on database heavyai TO dba;");
170  su.runSql("GRANT ACCESS on database heavyai TO bob;");
171  su.runSql("GRANT ACCESS on database db1 TO dba;");
172  su.runSql("GRANT ACCESS on database db1 TO bob;");
173 
174  // We create a series of dashboards with fixed ids to be modified in parallel.
175  HeavyDBTestClient dba =
176  HeavyDBTestClient.getClient(local, port, "db1", admin, defPwd);
177  for (TDashboard board : dba.get_dashboards()) {
178  logger.info("DROP DASHBOARD " + board.dashboard_name);
179  dba.delete_dashboard(board.dashboard_id);
180  }
181  ArrayList<Integer> dashboardIds = new ArrayList<>();
182  for (int i = 0; i < 5; ++i) {
183  String dashName = "dash_" + i;
184  logger.info("CREATE DASHBOARD " + dashName);
185  dashboardIds.add(dba.create_dashboard(dashName));
186  }
187  HeavyDBAsserts.assertEqual(5, dba.get_dashboards().size());
188 
189  runTest("db1", admin, defPwd, admin, defPwd, dashboardIds);
190  runTest("db1", admin, defPwd, "dba", "password", dashboardIds);
191  runTest("db1", admin, defPwd, "bob", "password", dashboardIds);
192  runTest("db1", "dba", "password", admin, defPwd, dashboardIds);
193  runTest("db1", "dba", "password", "bob", "password", dashboardIds);
194 
195  runTest(defDb, admin, defPwd, admin, defPwd, dashboardIds);
196  runTest(defDb, admin, defPwd, "dba", "password", dashboardIds);
197  runTest(defDb, admin, defPwd, "bob", "password", dashboardIds);
198  runTest(defDb, "dba", "password", admin, defPwd, dashboardIds);
199  runTest(defDb, "dba", "password", "bob", "password", dashboardIds);
200 
201  for (TDashboard board : dba.get_dashboards()) {
202  logger.info("DROP DASHBOARD " + board.dashboard_name);
203  dba.delete_dashboard(board.dashboard_id);
204  }
205  su.runSql("DROP DATABASE db1;");
206  su.runSql("DROP USER bob;");
207  su.runSql("DROP USER dba;");
208 
209  logger.info("testCatalogConcurrency() done");
210  }
211 }
void run_test(HeavyDBTestClient dba, HeavyDBTestClient user, String prefix, int max, List< Integer > dashboardIds)
void runTest(String db, String dbaUser, String dbaPassword, String dbUser, String dbPassword, List< Integer > dashboardIds)
static bool run