Skip to content

Fix possible data corruption after FileStorage is truncated to roll back a transaction - #52

Merged
jimfulton merged 2 commits into
masterfrom
fs-flush_after_truncate
Apr 28, 2016
Merged

Fix possible data corruption after FileStorage is truncated to roll back a transaction#52
jimfulton merged 2 commits into
masterfrom
fs-flush_after_truncate

Conversation

@jmuchemb

@jmuchemb jmuchemb commented Apr 4, 2016

Copy link
Copy Markdown
Member

See the commit message for more information.
I wanted to attach the stress test I wrote to reproduce and understand the issue, but GitHub does not want *.py files. It was already sent to the mailing-list so you can find it there:
http://thread.gmane.org/gmane.comp.web.zope.zodb/12685/focus=12751

I'd like to also apply it on 3.10 branch.

/cc @vpelletier

@tseaver

tseaver commented Apr 4, 2016

Copy link
Copy Markdown
Member

@jmuchemb Can you please open a separate PR against the 3.10 branch (maybe after responding to review below).

@jimfulton This patch looks reasonable to me, but doesn't contain tests that the pooled files have been flushed. Can you suggest a reasonable strategy for such a test?

@jimfulton

Copy link
Copy Markdown
Member

Well, it should be possible to set up a simple scenario that demonstrates the problem without a stress test:

  • Create a database with a small record for object 0.
  • Perform a transaction up to the vote call that writes object 1.
  • do a read of object 0.
  • abort the transaction
  • commit a record of the same size but with different data for object 1 in a new transaction.
  • read object 1. Presumably, you'll get the old.

If you can reproduce the problem without the patch, then you can try it with the patch, and if that fixes it, you have a test.

One thing that puzzles me is that when reading, file-storage seeks to the record position of the record read, and IIRC seek flushes before seeking, but perhaps I'm mistaken.

@jmuchemb
jmuchemb force-pushed the fs-flush_after_truncate branch from 50d0b74 to 5311b51 Compare April 6, 2016 12:10
@jmuchemb

jmuchemb commented Apr 6, 2016

Copy link
Copy Markdown
Member Author

I added a unit test, and Travis reports the fix only works for Python 2. I need a Python3 setup of ZODB...

@jimfulton

Copy link
Copy Markdown
Member

Thanks Julien.

@tseaver

tseaver commented Apr 6, 2016

Copy link
Copy Markdown
Member

@jmuchemb If you have tox set up, you can just run tox -e py33 inside your checkout.

@jmuchemb

jmuchemb commented Apr 6, 2016

Copy link
Copy Markdown
Member Author

There's no API in Python 3 to invalidate the read buffer of a file that is open in read-only mode. So we have the choice between:

  • reset the FilePool (do flush = empty for Python 3)
  • make FilePool.get open files with r+b (in order not to keep FileStorage(read_only=True) working with non-writable files, we'd also need a read_only parameter to FilePool.

@jmuchemb
jmuchemb force-pushed the fs-flush_after_truncate branch from 5311b51 to 8a3aa0a Compare April 6, 2016 15:41
@jmuchemb

jmuchemb commented Apr 6, 2016

Copy link
Copy Markdown
Member Author

I chose the first solution and all tests pass.
You can merge if you agree.

…ack a transaction

Multi-threaded IO support, which is new to ZODB 3.10, allows clients to read
data (load & loadBefore) even after tpc_vote has started to write a new
transaction to disk. This is done by using different 'file' objects.

Issues start when a transaction is rolled back after data has been appended
(using the writing file object). Truncating is not enough because the FilePool
may have been used concurrently to read the end of the last transaction:
file objects have their own read buffers which, in this case, may also contain
the beginning of the aborted transaction.

So a solution is to invalidate read buffers whenever they may contain wrong
data. This patch does it on truncation, which happens rarely enough to not
affect performance.

We discovered this bug in the following conditions:
- ZODB splitted in several FileStorage
- many conflicts in the first committed DB, but always resolved
- unresolved conflict in another DB
If the transaction is replayed with success (no more conflict in the other DB),
a subsequent load of the object that could be resolved in the first DB may, for
example, return a wrong serial (tid of the aborted transaction) if the layout
of the committed transaction matches that of the aborted one.

The bug usually manifests with POSKeyError & CorruptedDataError exceptions in
ZEO logs, for example while trying to resolve a conflict (and restarting the
transaction does not help, causing Site Errors in Zope). But theorically,
this could also cause silent corruption or unpickling errors at client side.
@jmuchemb
jmuchemb force-pushed the fs-flush_after_truncate branch from 8a3aa0a to 06df0eb Compare April 27, 2016 12:46
@jmuchemb

Copy link
Copy Markdown
Member Author

reset the FilePool (do flush = empty for Python 3)

Contrary to empty(), I think flush() also needs to use write_lock() for Python 3, so I amended with the following change:

--- a/src/ZODB/FileStorage/FileStorage.py
+++ b/src/ZODB/FileStorage/FileStorage.py
@@ -2108,7 +2108,9 @@ def flush(self):

     # Unfortunately, Python 3.x has no API to flush read buffers.
     if sys.version_info.major > 2:
-        flush = empty
+        def flush(self):
+            with self.write_lock():
+                self.empty()

     def close(self):
         with self._cond:

I also did another commit to update the changelog.

@jimfulton
jimfulton merged commit 2d12e59 into master Apr 28, 2016
@jmuchemb
jmuchemb deleted the fs-flush_after_truncate branch April 28, 2016 21:12
jmuchemb added a commit that referenced this pull request Apr 28, 2016
@jimfulton

jimfulton commented May 4, 2016

Copy link
Copy Markdown
Member

Sadly, flushing isn't good enough on Python 2 and mac. Making a new PR to always empty.

#57

@jmuchemb

jmuchemb commented May 5, 2016

Copy link
Copy Markdown
Member Author

Contrary to Python 3, Python 2 uses file functions from the C standard library (here fflush), so it's possible that the optimization only worked with the glibc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants