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 // This test fails if the multi_operation_lock prefers readers.  (See #4347).
40 // But works well if the multi_operation_lock prefers writers (which, since there is typically only one writer, makes it fair).
41 // What this test does:
42 //  Starts a bunch of threads (100 seems to work):  Each executes many transactions (and thus obtains the multi_operation_lock during the txn->commit, and until #4346 is changed, holds it through the fsync.  If we fix #4346 then
43 //   this test may not be sensitive to the bug.)
44 //  Meanwhile another thread tries to do W checkpoints.  (W=10 seems to work).
45 //  The checkpoint thread waits until all the transaction threads have gotten going (waits until each transaction thread has done 10 transactions).
46 //  The transaction threads get upset if they manage to run for 1000 transactions without the W checkpoints being finished.
47 //  The theory is that the transaction threads can starve the checkpoint thread by obtaining the multi_operation_lock.
48 //  But making the multi_operation_lock prefer writers means that the checkpoint gets a chance to run.
49 
50 #include "test.h"
51 #include "toku_pthread.h"
52 #include <portability/toku_atomic.h>
53 
54 DB_ENV *env;
55 DB     *db;
56 const char   *env_dir = TOKU_TEST_FILENAME;
57 
58 const int n_threads = 100;
59 volatile int reader_start_count = 0;
60 
61 const int W = 10;
62 volatile int writer_done_count = 0;
63 
start_txns(void * e)64 static void *start_txns (void *e) {
65     int *CAST_FROM_VOIDP(idp, e);
66     int id = *idp;
67     int j;
68     DBT k;
69     dbt_init(&k, &id, sizeof id);
70     for (j=0; writer_done_count<W; j++) { // terminate the loop when the checkpoint thread has done it's W items.
71 	DB_TXN *txn;
72 	{ int chk_r = env->txn_begin(env, NULL, &txn, 0); CKERR(chk_r); }
73 	{ int chk_r = db->put(db, txn, &k, &k, 0); CKERR(chk_r); }
74 	{ int chk_r = txn->commit(txn, 0); CKERR(chk_r); }
75 	if (j==10) (void)toku_sync_fetch_and_add(&reader_start_count, 1);
76 	if (j%1000==999) { printf("."); fflush(stdout); }
77 	assert(j<1000); // Get upset if we manage to run this many transactions without the checkpoint thread
78     }
79     if (verbose) printf("rdone j=%d\n", j);
80     return NULL;
81 }
start_checkpoints(void)82 static void start_checkpoints (void) {
83     while (reader_start_count < n_threads) { sched_yield(); }
84     for (int i=0; i<W; i++) {
85 	if (verbose) printf("cks\n");
86 	{ int chk_r = env->txn_checkpoint(env, 0, 0, 0); CKERR(chk_r); }
87 	if (verbose) printf("ck\n");
88 	sched_yield();
89 	(void)toku_sync_fetch_and_add(&writer_done_count, 1);
90     }
91 }
92 
test_main(int argc,char * const argv[])93 int test_main(int argc, char * const argv[]) {
94     parse_args(argc, argv);
95 
96     // try to starve the checkpoint
97     { int chk_r = db_env_create(&env, 0); CKERR(chk_r); }
98     { int chk_r = env->set_redzone(env, 0); CKERR(chk_r); }
99     {
100 	const int size = 10+strlen(env_dir);
101 	char cmd[size];
102 	snprintf(cmd, size, "rm -rf %s", env_dir);
103 	int r = system(cmd);
104         CKERR(r);
105     }
106     { int chk_r = toku_os_mkdir(env_dir, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
107 
108     const int envflags = DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE | DB_RECOVER;
109     { int chk_r = env->open(env, env_dir, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
110 
111     { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
112 
113     { int chk_r = db->open(db, NULL, "db", NULL, DB_BTREE, DB_CREATE|DB_AUTO_COMMIT, 0666); CKERR(chk_r); }
114 
115     pthread_t thds[n_threads];
116     int ids[n_threads];
117     for (int i = 0; i < n_threads; i++) {
118         ids[i] = i;
119         {
120             int chk_r = toku_pthread_create(
121                 toku_uninstrumented, &thds[i], nullptr, start_txns, &ids[i]);
122             CKERR(chk_r);
123         }
124     }
125     start_checkpoints();
126 
127     for (int i=0; i<n_threads; i++) {
128 	void *retval;
129 	{ int chk_r = toku_pthread_join(thds[i], &retval); CKERR(chk_r); }
130 	assert(retval==NULL);
131     }
132     { int chk_r = db->close(db, 0); CKERR(chk_r); }
133 
134     { int chk_r = env->close(env, 0); CKERR(chk_r); }
135 
136     return 0;
137 }
138