summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/suite/test_txn15.py
blob: 809dce4ebfa9e972f45aa69a68117f7c2435ade2 (plain)
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
#!/usr/bin/env python
#
# Public Domain 2014-2016 MongoDB, Inc.
# Public Domain 2008-2014 WiredTiger, Inc.
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# test_txn15.py
#   Transactions: different sync modes
#

import fnmatch, os, shutil, time
from suite_subprocess import suite_subprocess
from wiredtiger import stat
from wtscenario import multiply_scenarios, number_scenarios, prune_scenarios
import wttest

class test_txn15(wttest.WiredTigerTestCase, suite_subprocess):
    uri = 'table:test_txn15_1'
    create_params = 'key_format=i,value_format=i'
    entries = 100
    # Turn on logging for this test.
    def conn_config(self, dir):
        return 'statistics=(fast),' + \
            'log=(archive=false,enabled,file_max=100K),' + \
            'use_environment=false,' + \
            'transaction_sync=(enabled=%s),' % self.conn_enable + \
            'transaction_sync=(method=%s),' % self.conn_method

    conn_sync_enabled = [
        ('en_off', dict(conn_enable='false')),
        ('en_on', dict(conn_enable='true')),
    ]
    conn_sync_method = [
        ('m_dsync', dict(conn_method='dsync')),
        ('m_fsync', dict(conn_method='fsync')),
        ('m_none', dict(conn_method='none')),
    ]
    begin_sync = [
        ('b_method', dict(begin_sync='sync=true')),
        ('b_none', dict(begin_sync=None)),
        ('b_off', dict(begin_sync='sync=false')),
    ]
    # We don't test 'background' in this test.  It isn't applicable for the
    # particulars here and reduces the number of scenarios.  It is an error for
    # both begin_transaction and commit_transaction to have explicit settings.
    # We will avoid that in the code below.
    commit_sync = [
        ('c_method', dict(commit_sync='sync=on')),
        ('c_none', dict(commit_sync=None)),
        ('c_off', dict(commit_sync='sync=off')),
    ]
    scenarios = multiply_scenarios('.', conn_sync_enabled, conn_sync_method,
        begin_sync, commit_sync)

    # Given the different configuration settings determine if this group
    # of settings would result in either a wait for write or sync.
    # Returns None, "write" or "sync".  None means no waiting for either.
    # "write" means wait for write, but not sync.  "sync" means both
    # write and sync.
    def syncLevel(self):
        # We know that we skip illegal settings where both begin and commit
        # have explicit settings.  So we don't have to check that here.

        # If the transaction doesn't override it, then it is whatever is
        # set on the connection.
        if self.begin_sync == None and self.commit_sync == None:
            if self.conn_enable == 'false':
                return None
            # Only fsync implies sync.  Both dsync and none (meaning
            # write-no-sync) imply write.
            if self.conn_method == 'fsync':
                return 'sync'
            else:
                return 'write'
        # If the user explicitly turns it off for the transaction, it is None.
        if self.begin_sync == 'sync=false' or self.commit_sync == 'sync=off':
            return None

        # If we get here, the transaction turns sync on in some way so we
        # only need to check the method type.
        if self.conn_method == 'fsync':
            return 'sync'
        else:
            return 'write'

    def test_sync_ops(self):
        # It is illegal to set a sync option on both begin and commit.
        # If the scenario multiplication sets both, just return.
        if self.begin_sync != None and self.commit_sync != None:
            return

        self.session.create(self.uri, self.create_params)

        stat_cursor = self.session.open_cursor('statistics:', None, None)
        #
        # !!! Implementation detail here.  We record the stats that
        # indicate the code went through the release code explicitly
        # (instead of the worker thread).  They indicate we came
        # through the special case path for handling a flush or sync.
        #
        write1 = stat_cursor[stat.conn.log_release_write_lsn][2]
        sync1 = stat_cursor[stat.conn.log_sync][2]
        stat_cursor.close()

        c = self.session.open_cursor(self.uri, None, None)
        self.session.begin_transaction(self.begin_sync)
        for i in range(self.entries):
            c[i] = i + 1
        self.session.commit_transaction(self.commit_sync)
        c.close()

        stat_cursor = self.session.open_cursor('statistics:', None, None)
        write2 = stat_cursor[stat.conn.log_release_write_lsn][2]
        sync2 = stat_cursor[stat.conn.log_sync][2]
        stat_cursor.close()

        checks = self.syncLevel()
        # Checking sync implies the write.
        # If we did special processing the stats should not be the
        # same.  Otherwise the number of writes should be equal.
        # We do not check for syncs being equal because worker threads
        # also increment that stat when they sync and close a log file.
        if checks != None:
            self.assertNotEqual(write1, write2)
            if checks == 'sync':
                self.assertNotEqual(sync1, sync2)
        else:
            self.assertEqual(write1, write2)

if __name__ == '__main__':
    wttest.run()