-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathSessionsStoreTest.cpp
387 lines (351 loc) · 14.6 KB
/
SessionsStoreTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright 2022 HEAVY.AI, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file SessionsStoreTest.cpp
* @brief Test suite for distributed sessions store (single node environment)
*
*/
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#include <string>
#include "Calcite/Calcite.h"
#include "Catalog/Catalog.h"
#include "Catalog/SessionsStore.h"
#include "Catalog/SysCatalog.h"
#include "CudaMgr/CudaMgr.h"
#include "Shared/StringTransform.h"
#include "Tests/DBHandlerTestHelpers.h"
#include "Tests/TestHelpers.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
#ifndef CALCITEPORT
#define CALCITEPORT 3279
#endif
using namespace Catalog_Namespace;
namespace fs = boost::filesystem;
namespace {
auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
const std::string store_path_ = std::string(BASE_PATH) + "/sessions";
bool sessionsMatch(const SessionInfo& a, const SessionInfo& b) {
return a.get_public_session_id() == b.get_public_session_id() &&
a.get_session_id() == b.get_session_id() &&
a.getCatalog().getDatabaseId() == b.getCatalog().getDatabaseId() &&
a.get_currentUser().userId == b.get_currentUser().userId &&
a.get_connection_info() == b.get_connection_info() &&
a.get_executor_device_type() == b.get_executor_device_type();
}
void noopCallback(SessionInfoPtr&) {}
const std::string users[] = {"test_user_1", "test_user_2"};
const std::string dbs[] = {"test_db_1", "test_db_2"};
} // namespace
class SessionsStoreTest : public testing::Test {
void grantDBToUser(const std::string db_name, const std::string& user_name) {
DBObject mapd_object(db_name, DBObjectType::DatabaseDBObjectType);
mapd_object.setPrivileges(AccessPrivileges::ALL_DATABASE);
auto db_cat = sys_cat.getCatalog(db_name);
mapd_object.loadKey(*db_cat);
sys_cat.grantDBObjectPrivileges(user_name, mapd_object, *db_cat);
}
void createDB(const std::string& db_name) {
if (!sys_cat.getCatalog(db_name)) {
sys_cat.createDatabase(db_name, shared::kRootUserId);
}
}
void createUser(const std::string& user_name) {
sys_cat.createUser(
user_name,
Catalog_Namespace::UserAlterations{
"password", /*is_super=*/false, /*default_db=*/"", /*can_login=*/true},
false);
}
protected:
void SetUp() override {
createDB("test_db_1");
createDB("test_db_2");
createUser("test_user_1");
createUser("test_user_2");
grantDBToUser("test_db_1", "test_user_1");
grantDBToUser("test_db_1", "test_user_2");
grantDBToUser("test_db_2", "test_user_1");
grantDBToUser("test_db_2", "test_user_2");
}
void TearDown() override {
DBMetadata db;
sys_cat.getMetadataForDB("test_db_1", db);
sys_cat.dropDatabase(db);
sys_cat.getMetadataForDB("test_db_2", db);
sys_cat.dropDatabase(db);
sys_cat.dropUser("test_user_1");
sys_cat.dropUser("test_user_2");
}
public:
SessionInfoPtr createSession(const std::string& user, const std::string& db) const {
auto cat = sys_cat.getCatalog(db);
UserMetadata bob_md;
sys_cat.getMetadataForUser(user, bob_md);
return std::make_shared<SessionInfo>(
cat, bob_md, ExecutorDeviceType::CPU, generate_random_string(32));
}
UserMetadata get_user_md(int i) {
auto user = sys_cat.getUser(users[i]);
CHECK(user.has_value());
return user.value();
}
std::shared_ptr<Catalog> get_db_catalog(int i) {
auto cat = sys_cat.getCatalog(dbs[i]);
CHECK(cat);
return cat;
}
};
void check_session(const SessionInfoPtr& session, int id) {
ASSERT_TRUE(session != nullptr);
ASSERT_TRUE(session->get_currentUser().userName == users[id] &&
session->get_catalog_ptr() == sys_cat.getCatalog(dbs[id]) &&
session->get_executor_device_type() == ExecutorDeviceType::CPU);
}
TEST_F(SessionsStoreTest, AddGet) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
auto session_from_store = store->get(session1->get_session_id());
ASSERT_TRUE(sessionsMatch(*session1, *session_from_store));
session_from_store = store->get(session2->get_session_id());
ASSERT_TRUE(sessionsMatch(*session2, *session_from_store));
}
TEST_F(SessionsStoreTest, Erase) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
store->erase(session1->get_session_id());
ASSERT_EQ(store->get(session1->get_session_id()), nullptr);
auto session_from_store = store->get(session2->get_session_id());
ASSERT_TRUE(sessionsMatch(*session2, *session_from_store));
}
TEST_F(SessionsStoreTest, EraseByUser) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
store->eraseByUser(users[0]);
ASSERT_EQ(store->get(session1->get_session_id()), nullptr);
auto session_from_store = store->get(session2->get_session_id());
ASSERT_TRUE(sessionsMatch(*session2, *session_from_store));
}
TEST_F(SessionsStoreTest, EraseByDB) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
store->eraseByDB(dbs[0]);
ASSERT_EQ(store->get(session1->get_session_id()), nullptr);
auto session_from_store = store->get(session2->get_session_id());
ASSERT_TRUE(sessionsMatch(*session2, *session_from_store));
}
TEST_F(SessionsStoreTest, GetAll) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
auto all_sessions = store->getAllSessions();
ASSERT_EQ(all_sessions.size(), 2ul);
bool match = (sessionsMatch(*all_sessions[0], *session1) &&
sessionsMatch(*all_sessions[1], *session2)) ||
(sessionsMatch(*all_sessions[0], *session2) &&
sessionsMatch(*all_sessions[1], *session1));
ASSERT_TRUE(match);
}
TEST_F(SessionsStoreTest, GetUserSessions) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
auto user_sessions = store->getUserSessions(users[0]);
ASSERT_EQ(user_sessions.size(), 1ul);
ASSERT_TRUE(sessionsMatch(*session1, *user_sessions[0]));
}
TEST_F(SessionsStoreTest, GetByPublicID) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
ASSERT_TRUE(
sessionsMatch(*session1, *store->getByPublicID(session1->get_public_session_id())));
}
TEST_F(SessionsStoreTest, DisconnectTest) {
bool disconnected = false;
auto store = SessionsStore::create(
BASE_PATH, 1, 1, 1, -1, [&disconnected](SessionInfoPtr&) { disconnected = true; });
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
store->disconnect(session1->get_session_id());
ASSERT_TRUE(store->get(session1->get_session_id()) == nullptr);
ASSERT_TRUE(store->get(session2->get_session_id()) != nullptr);
ASSERT_TRUE(disconnected);
}
TEST_F(SessionsStoreTest, GetCopy) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
ASSERT_TRUE(
sessionsMatch(*session1, store->getSessionCopy(session1->get_session_id())));
ASSERT_TRUE(
sessionsMatch(*session2, store->getSessionCopy(session2->get_session_id())));
}
TEST_F(SessionsStoreTest, StoreOverflow) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, 2, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session1, 0);
check_session(session2, 1);
try {
store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
ASSERT_TRUE(false) << "No exception thrown";
} catch (const std::exception& e) {
ASSERT_EQ(std::string(e.what()), "Too many active sessions");
}
}
// For a session to be expired we need to nullify all shared_pointers pointing to it
TEST_F(SessionsStoreTest, GetWithTimeout) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
check_session(session1, 0);
sleep(2);
auto session_id = session1->get_session_id();
session1 = nullptr;
ASSERT_EQ(store->get(session_id), nullptr);
}
// This test case:
// 1) creates a store with limited capacity of two sessions
// 2) creates two sessions
// 3) expires the first one
// 4) creates another two sessions - one successfuly, another fails with store overflow
TEST_F(SessionsStoreTest, StoreOverflowExpired) {
auto store = SessionsStore::create(BASE_PATH, 1, 3, 3, 2, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
check_session(session1, 0);
auto session1_id = session1->get_session_id();
session1 = nullptr;
sleep(2);
auto session2 = store->add(get_user_md(1), get_db_catalog(1), ExecutorDeviceType::CPU);
check_session(session2, 1);
ASSERT_EQ(store->getAllSessions().size(), 2ul);
auto session2_id = session2->get_session_id();
session2 = nullptr;
sleep(2);
auto session3 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
auto session3_id = session3->get_session_id();
ASSERT_TRUE(session3 != nullptr);
session3 = nullptr;
try {
store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
ASSERT_TRUE(false) << "No exception thrown";
} catch (const std::exception& e) {
ASSERT_EQ(std::string(e.what()), "Too many active sessions");
}
ASSERT_EQ(store->getAllSessions().size(), 2ul);
ASSERT_TRUE(store->get(session1_id) == nullptr);
ASSERT_TRUE(store->get(session2_id) != nullptr);
ASSERT_TRUE(store->get(session3_id) != nullptr);
}
TEST_F(SessionsStoreTest, NoTimeoutWhenInUse) {
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session1 = store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
check_session(session1, 0);
// we sleep longer than max_idle_time, but we still have the shared_ptr to the session
// so it should not be expired, because it is in "active" use.
sleep(2);
ASSERT_TRUE(sessionsMatch(*store->get(session1->get_session_id()), *session1));
}
// Test for a very simple concurrent sessions lifecycle:
// 1) creates #n_workers threads
// 2) each thread runs multiple session lifeccle:
// - create session
// - read a few times
// - erase 50% of them
// 3) after all threads are done wait couple seconds for the rest of the sessions
// to time out
// 4) check if there are no sessions left in the store
TEST_F(SessionsStoreTest, ConcurrentSessionLifeCycle) {
const int n_sessions = 1024;
std::atomic<int> current_session = 0;
std::vector<std::string> session_ids(n_sessions);
auto store = SessionsStore::create(BASE_PATH, 1, 1, 1, -1, noopCallback);
auto session_work = [&](int i) {
while (true) {
auto session_id_pos = current_session++;
if (session_id_pos >= n_sessions) {
return;
}
auto session =
store->add(get_user_md(0), get_db_catalog(0), ExecutorDeviceType::CPU);
ASSERT_TRUE(session != nullptr);
const auto& session_id = session->get_session_id();
session_ids[session_id_pos] = session_id;
for (int j = 0; j < 10; ++j) {
ASSERT_TRUE(sessionsMatch(*session, *store->get(session_id)));
}
if (i % 2) {
store->erase(session_id);
ASSERT_EQ(store->get(session_id), nullptr);
}
}
};
int n_workers = 16;
std::vector<std::thread> threads;
threads.reserve(n_workers);
for (int i = 0; i < n_workers; ++i) {
threads.emplace_back(session_work, i);
}
for (auto& t : threads) {
t.join();
}
sleep(2);
for (const auto& session_id : session_ids) {
ASSERT_EQ(store->get(session_id), nullptr);
}
}
int main(int argc, char** argv) {
TestHelpers::init_logger_stderr_only(argc, argv);
testing::InitGoogleTest(&argc, argv);
int err{0};
SystemParameters sys_parms;
std::string data_path = std::string(BASE_PATH) + "/" + shared::kDataDirectoryName;
auto dummy =
std::make_shared<Data_Namespace::DataMgr>(data_path, sys_parms, nullptr, false, 0);
auto calcite = std::make_shared<Calcite>(
-1, CALCITEPORT, std::string(BASE_PATH), 1024, 5000, true, "");
sys_cat.init(BASE_PATH, dummy, {}, calcite, false, false, {});
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}