1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28#
29# test_timestamp12.py
30#   Timestamps: Test the use_timestamp setting when closing the connection.
31#
32
33import shutil, os, wiredtiger, wttest
34from wtscenario import make_scenarios
35
36def timestamp_str(t):
37    return '%x' % t
38
39class test_timestamp12(wttest.WiredTigerTestCase):
40    conn_config = 'config_base=false,create,log=(enabled)'
41    coll_uri = 'table:collection12'
42    oplog_uri = 'table:oplog12'
43    closecfg = [
44        ('dfl', dict(close_cfg='', all_expected=False)),
45        ('use_stable', dict(close_cfg='use_timestamp=true', all_expected=False)),
46        ('all_dirty', dict(close_cfg='use_timestamp=false', all_expected=True)),
47   ]
48    scenarios = make_scenarios(closecfg)
49
50    def verify_expected(self, op_exp, coll_exp):
51        c_op = self.session.open_cursor(self.oplog_uri)
52        c_coll = self.session.open_cursor(self.coll_uri)
53        op_actual = dict((k, v) for k, v in c_op if v != 0)
54        coll_actual = dict((k, v) for k, v in c_coll if v != 0)
55        #print "CHECK: Op Expected"
56        #print op_exp
57        #print "CHECK: Op Actual"
58        #print op_actual
59        self.assertTrue(op_actual == op_exp)
60        #print "CHECK: Coll Expected"
61        #print coll_exp
62        #print "CHECK: Coll Actual"
63        #print coll_actual
64        self.assertTrue(coll_actual == coll_exp)
65
66    def test_timestamp_recovery(self):
67        if not wiredtiger.timestamp_build():
68            self.skipTest('requires a timestamp build')
69
70        #
71        # Create several collection-like tables that are checkpoint durability.
72        # Add data to each of them separately and checkpoint so that each one
73        # has a different stable timestamp.
74        #
75        self.session.create(self.oplog_uri, 'key_format=i,value_format=i')
76        self.session.create(self.coll_uri, 'key_format=i,value_format=i,log=(enabled=false)')
77        c_op = self.session.open_cursor(self.oplog_uri)
78        c_coll = self.session.open_cursor(self.coll_uri)
79
80        # Begin by adding some data.
81        nentries = 10
82        first_range = range(1, nentries)
83        second_range = range(nentries, nentries*2)
84        all_keys = range(1, nentries*2)
85        for i in first_range:
86            self.session.begin_transaction()
87            c_op[i] = 1
88            c_coll[i] = 1
89            self.session.commit_transaction(
90              'commit_timestamp=' + timestamp_str(i))
91        # Set the oldest and stable timestamp to the end.
92        self.conn.set_timestamp('oldest_timestamp=' + timestamp_str(nentries-1) +
93        ',stable_timestamp=' + timestamp_str(nentries-1))
94
95        # Add more data but don't advance the stable timestamp.
96        for i in second_range:
97            self.session.begin_transaction()
98            c_op[i] = 1
99            c_coll[i] = 1
100            self.pr("i: " + str(i))
101            self.session.commit_transaction(
102              'commit_timestamp=' + timestamp_str(i))
103
104        # Close and reopen the connection. We cannot use reopen_conn because
105        # we want to test the specific close configuration string.
106        self.close_conn(self.close_cfg)
107        self.open_conn()
108
109        # Set up our expected data and verify after the reopen.
110        op_exp = dict((k, 1) for k in all_keys)
111        if self.all_expected == True:
112            coll_exp = dict((k, 1) for k in all_keys)
113        else:
114            coll_exp = dict((k, 1) for k in first_range)
115
116        self.verify_expected(op_exp, coll_exp)
117
118if __name__ == '__main__':
119    wttest.run()
120