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 388 389 390 391
|
#!/usr/bin/env python
#
# Public Domain 2014-2019 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_assert06.py
# Timestamps: verify key consistent setting for durable timestamps
#
from suite_subprocess import suite_subprocess
import wiredtiger, wttest
def timestamp_str(t):
return '%x' % t
class test_assert06(wttest.WiredTigerTestCase, suite_subprocess):
session_config = 'isolation=snapshot'
def apply_timestamps(self, timestamp):
self.session.prepare_transaction(
'prepare_timestamp=' + timestamp_str(timestamp))
self.session.timestamp_transaction(
'commit_timestamp=' + timestamp_str(timestamp))
self.session.timestamp_transaction(
'durable_timestamp=' + timestamp_str(timestamp))
def test_timestamp_alter(self):
base = 'assert06'
uri = 'file:' + base
cfg_on = 'assert=(durable_timestamp=key_consistent)'
cfg_off = 'assert=(durable_timestamp=none)'
msg_ooo='/out of order/'
msg_usage='/used inconsistently/'
# Create the table without the key consistency checking turned on.
# Create a few items breaking the rules. Then alter the setting and
# verify the inconsistent usage is detected.
self.session.create(uri, 'key_format=S,value_format=S')
# Insert a data item at timestamp 2.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value2'
self.apply_timestamps(2)
self.session.commit_transaction()
c.close()
# Modify the data item at timestamp 1.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value1'
self.apply_timestamps(1)
self.session.commit_transaction()
c.close()
# Insert a non-timestamped item. Then modify with a timestamp. And
# again modify without a timestamp.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots'
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value2'
self.apply_timestamps(2)
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots2'
self.session.commit_transaction()
c.close()
# We must move the oldest timestamp forward in order to alter.
# Otherwise alter closing the file will fail with EBUSY.
self.conn.set_timestamp('oldest_timestamp=' + timestamp_str(2))
# Now alter the setting and make sure we detect incorrect usage.
self.session.alter(uri, cfg_on)
# Detect decreasing timestamp.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value5'
self.apply_timestamps(5)
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value4'
self.apply_timestamps(4)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_ooo)
c.close()
# Detect not using a timestamp.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value_nots3'
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
# Detect using a timestamp on the non-timestamp key.
# We must first use a non timestamped operation on the key
# in order to violate the key consistency condition in the
# following transaction.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots3'
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value3'
self.apply_timestamps(3)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
self.session.checkpoint()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts1'], 'value5')
self.assertEquals(c['key_nots'], 'value_nots3')
c.close()
# Test to make sure that key consistency can be turned off
# after turning it on.
self.conn.set_timestamp('oldest_timestamp=' + timestamp_str(5))
self.session.alter(uri, cfg_off)
# Detection is off we can successfully change the same key with and
# without a timestamp.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots4'
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value6'
self.apply_timestamps(6)
self.session.commit_transaction()
c.close()
def test_timestamp_usage(self):
base = 'assert06'
uri = 'file:' + base
msg_ooo='/out of order/'
msg_usage='/used inconsistently/'
# Create the table with the key consistency checking turned on.
# That checking will verify any individual key is always or never
# used with a timestamp. And if it is used with a timestamp that
# the timestamps are in increasing order for that key.
self.session.create(uri, 'key_format=S,value_format=S,assert=(durable_timestamp=key_consistent)')
# Insert a data item at timestamp 2.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value2'
self.apply_timestamps(2)
self.session.commit_transaction()
c.close()
# Modify the data item at timestamp 1. We should detect it is wrong.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts1'] = 'value1'
self.apply_timestamps(1)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_ooo)
c.close()
# Make sure we can successfully add a different key at timestamp 1.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts2'] = 'value1'
self.apply_timestamps(1)
self.session.commit_transaction()
c.close()
#
# Insert key_ts3 at timestamp 10 and key_ts4 at 15.
# Then modify both keys in one transaction at timestamp 13.
# We should not be allowed to modify the one from 15.
# So the whole transaction should fail.
#
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts3'] = 'value10'
self.apply_timestamps(10)
self.session.commit_transaction()
self.session.begin_transaction()
c['key_ts4'] = 'value15'
self.apply_timestamps(15)
self.session.commit_transaction()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts3'] = 'value13'
c['key_ts4'] = 'value13'
self.apply_timestamps(13)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_ooo)
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts3'], 'value10')
self.assertEquals(c['key_ts4'], 'value15')
c.close()
#
# Separately, we should be able to update key_ts3 at timestamp 10
# but not update key_ts4 inserted at timestamp 15.
#
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts3'] = 'value13'
self.apply_timestamps(13)
self.session.commit_transaction()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts4'] = 'value13'
self.apply_timestamps(13)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_ooo)
c.close()
# Make sure multiple update attempts still fail and eventually
# succeed with a later timestamp. This tests that aborted entries
# in the update chain are not considered for the timestamp check.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts4'] = 'value14'
self.apply_timestamps(14)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_ooo)
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts4'], 'value15')
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts4'] = 'value16'
self.apply_timestamps(16)
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts4'], 'value16')
c.close()
# Now try to modify a key previously used with timestamps without
# one. We should get the inconsistent usage message.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts4'] = 'value_nots'
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts4'] = 'value_nots'
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts4'], 'value16')
c.close()
# Now confirm the other way. Create a key without a timestamp and then
# attempt to modify it with a timestamp. The only error checking that
# makes sense here is the inconsistent usage.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots'
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value16'
self.apply_timestamps(16)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value_nots1'
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value17'
self.apply_timestamps(17)
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(), msg_usage)
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_nots'], 'value_nots1')
c.close()
# Confirm it is okay to set the timestamp in the middle or end of the
# transaction. That should set the timestamp for the whole thing.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts5'] = 'value_notsyet'
c['key_ts5'] = 'value20'
self.apply_timestamps(20)
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts5'], 'value20')
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts6'] = 'value_notsyet'
c['key_ts6'] = 'value21_after'
self.apply_timestamps(21)
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.assertEquals(c['key_ts6'], 'value21_after')
c.close()
# Confirm it is okay to set the durable timestamp on the commit call.
# That should set the timestamp for the whole thing.
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_ts6'] = 'value_committs1'
c['key_ts6'] = 'value22'
self.session.prepare_transaction(
'prepare_timestamp=' + timestamp_str(22))
self.session.timestamp_transaction(
'commit_timestamp=' + timestamp_str(22))
self.session.timestamp_transaction(
'durable_timestamp=' + timestamp_str(22))
self.session.commit_transaction()
c.close()
c = self.session.open_cursor(uri)
self.session.begin_transaction()
c['key_nots'] = 'value23'
self.session.prepare_transaction(
'prepare_timestamp=' + timestamp_str(23))
self.session.timestamp_transaction(
'commit_timestamp=' + timestamp_str(23))
self.assertRaisesWithMessage(wiredtiger.WiredTigerError,
lambda: self.session.commit_transaction(
'durable_timestamp=' + timestamp_str(23)), msg_usage)
c.close()
if __name__ == '__main__':
wttest.run()
|