1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 #include <sys/resource.h>
41 
42 // create 200 databases and close them.  set the open file limit to 100 and try to open all of them.
43 // eventually, the locktree can not clone fractal tree, and the db open fails.
44 
test_main(int argc,char * const argv[])45 int test_main (int argc __attribute__((__unused__)), char *const argv[] __attribute__((__unused__))) {
46     int r;
47 
48     const int N = 200;
49 
50     toku_os_recursive_delete(TOKU_TEST_FILENAME);
51     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
52     assert(r == 0);
53 
54     DB_ENV *env;
55     r = db_env_create(&env, 0);
56     assert(r == 0);
57 
58     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO);
59     assert(r == 0);
60 
61     DB **dbs = new DB *[N];
62     for (int i = 0; i < N; i++) {
63         dbs[i] = NULL;
64     }
65     for (int i = 0; i < N; i++) {
66         r = db_create(&dbs[i], env, 0);
67         assert(r == 0);
68 
69         char dbname[32]; sprintf(dbname, "%d.test", i);
70         r = dbs[i]->open(dbs[i], NULL, dbname, NULL, DB_BTREE, DB_AUTO_COMMIT+DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
71         assert(r == 0);
72     }
73 
74     for (int i = 0; i < N; i++) {
75         if (dbs[i]) {
76             r = dbs[i]->close(dbs[i], 0);
77             assert(r == 0);
78         }
79     }
80 
81     struct rlimit nofile_limit = { N/2, N/2 };
82     r = setrlimit(RLIMIT_NOFILE, &nofile_limit);
83     // assert(r == 0); // valgrind does not like this
84     if (r != 0) {
85         printf("warning: set nofile limit to %d failed %d %s\n", N, errno, strerror(errno));
86     }
87 
88     for (int i = 0; i < N; i++) {
89         dbs[i] = NULL;
90     }
91     bool emfile_happened = false; // should happen since there are less than N unused file descriptors
92     for (int i = 0; i < N; i++) {
93         r = db_create(&dbs[i], env, 0);
94         assert(r == 0);
95 
96         char dbname[32]; sprintf(dbname, "%d.test", i);
97         r = dbs[i]->open(dbs[i], NULL, dbname, NULL, DB_BTREE, DB_AUTO_COMMIT, S_IRWXU+S_IRWXG+S_IRWXO);
98         if (r == EMFILE) {
99             emfile_happened = true;
100             break;
101         }
102     }
103     assert(emfile_happened);
104     for (int i = 0; i < N; i++) {
105         if (dbs[i]) {
106             r = dbs[i]->close(dbs[i], 0);
107             assert(r == 0);
108         }
109     }
110 
111     r = env->close(env, 0);
112     assert(r == 0);
113 
114     delete [] dbs;
115 
116     return 0;
117 }
118