- 17 Aug, 2015 3 commits
-
-
Kirill Smelkov authored
ZODB 3.10.4 was released almost 4 years ago, and contains significant change how ghost objects coming from DB are initially setup.
-
Kirill Smelkov authored
Continuing theme from the previous patch, here is propagation of invalidation messages from ZODB to BigFileH memory. The use-case here is that e.g. one fileh mapping was created in one connection, another in another, and after doing changes in second connection and committing there, the first fileh has to invalidate appropriate already-loaded pages, so its next transaction won't work with stale data. To do it, we hook into ZBlk._p_invalidate() and propagate the invalidation message to ZBigFile which then notifies all opened-through-it ZBigFileH to invalidate a page. ZBlk -> ZBigFile lookup is done without storing backpointer in ZODB - instead, every time ZBigFile touches ZBlk object (and thus potentially does GHOST -> Live transition to it), we (re-)bind it back to ZBigFile. Since ZBigFile is the only class that works with ZBlk objects it is safe to do so. For ZBigFile to notify "all-opened-through-it" ZBigFileH, a weakset is introduced to track them. Otherwise the real page invalidation work is done by virtmem (see previous patch).
-
Kirill Smelkov authored
FileH is a handle representing snapshot of a file. If, for a pgoffset, fileh already has loaded page, but we know the content of the file has changed externally after loading has been done, we need to propagate to fileh that such-and-such page should be invalidated (and reloaded on next access). This patch introduces fileh_invalidate_page(fileh, pgoffset) to do just that. In the next patch we'll use this facility to propagate invalidations of ZBlk ZODB objects to virtmem subsystem. NOTE Since invalidation removes "dirtiness" from a page state, several subsequent invalidations can make a fileh completely non-dirty (invalidating all dirty page). Previously fileh->dirty was just a one bit, so we needed to improve how we track dirtiness. One way would be to have a dirty list for fileh pages and operate on that. This has advantage to even optimize dirty pages processing like fileh_dirty_writeout() where we currently scan through all fileh pages just to write only PAGE_DIRTY ones. Another simpler way is to make fileh->dirty a counter and maintain that. Since we are going to move virtmem subsystem back into the kernel, here, a simpler less-intrusive approach is used.
-
- 12 Aug, 2015 3 commits
-
-
Kirill Smelkov authored
Intro ----- ZODB maintains pool of opened-to-DB connections. For each request Zope opens 1 connection and, after request handling is done, returns the connection back to ZODB pool (via Connection.close()). The same connection will be opened again for handling some future next request at some future time. This next open can happen in different-from-first request worker thread. TransactionManager (as accessed by transaction.{get,commit,abort,...}) is thread-local, that is e.g. transaction.get() returns different transaction for threads T1 and T2. When _ZBigFileH hooks into txn_manager to get a chance to run its .beforeCompletion() when transaction.commit() is run, it hooks into _current_ _thread_ transaction manager. Without unhooking on connection close, and circumstances where connection migrates to different thread this can lead to dissynchronization between ZBigFileH managing fileh pages and Connection with ZODB objects. And even to data corruption, e.g. T1 T2 open zarray[0] = 11 commit close open # opens connection as closed in T1 open zarray[0] = 21 commit abort close close Here zarray[0]=21 _will_ be committed by T1 as part of T1 transaction - because when T1 does commit .beforeCompletion() for zarray is invoked, sees there is dirty data and propagate changes to zodb objects in connection for T2, joins connection for T2 into txn for T1, and then txn for t1 when doing two-phase-commit stores modified objects to DB -> oops. ---------------------------------------- To prevent such dissynchronization _ZBigFileH needs to be a DataManager which works in sync with the connection it was initially created under - on connection close, unregister from transaction_manager, and on connection open, register to transaction manager in current, possibly different, thread context. Then there won't be incorrect beforeCompletion() notification and corruption. This issue, besides possible data corruption, was probably also exposing itself via following ways we've seen in production (everywhere connection was migrated from T1 to T2): 1. Exception ZODB.POSException.ConnectionStateError: ConnectionStateError('Cannot close a connection joined to a transaction',) in <bound method Cleanup.__del__ of <App.ZApplication.Cleanup instance at 0x7f10f4bab050>> ignored T1 T2 modify zarray commit/abort # does not join zarray to T2.txn, # because .beforeCompletion() is # registered in T1.txn_manager commit # T1 invokes .beforeCompletion() ... # beforeCompletion() joins ZBigFileH and zarray._p_jar (=T2.conn) to T1.txn ... # commit is going on in progress ... ... close # T2 thinks request handling is done and ... # and closes connection. But T2.conn is ... # still joined to T1.txn 2. Traceback (most recent call last): File ".../wendelin/bigfile/file_zodb.py", line 121, in storeblk def storeblk(self, blk, buf): return self.zself.storeblk(blk, buf) File ".../wendelin/bigfile/file_zodb.py", line 220, in storeblk zblk._v_blkdata = bytes(buf) # FIXME does memcpy File ".../ZODB/Connection.py", line 857, in setstate raise ConnectionStateError(msg) ZODB.POSException.ConnectionStateError: Shouldn't load state for 0x1f23a5 when the connection is closed Similar to "1", but close in T2 happens sooner, so that when T1 does the commit and tries to store object to database, Connection refuses to do the store: T1 T2 modify zarray commit/abort commit ... close ... ... . obj.store() ... ... 3. Traceback (most recent call last): File ".../wendelin/bigfile/file_zodb.py", line 121, in storeblk def storeblk(self, blk, buf): return self.zself.storeblk(blk, buf) File ".../wendelin/bigfile/file_zodb.py", line 221, in storeblk zblk._p_changed = True # if zblk was already in DB: _p_state -> CHANGED File ".../ZODB/Connection.py", line 979, in register self._register(obj) File ".../ZODB/Connection.py", line 989, in _register self.transaction_manager.get().join(self) File ".../transaction/_transaction.py", line 220, in join Status.ACTIVE, Status.DOOMED, self.status)) ValueError: expected txn status 'Active' or 'Doomed', but it's 'Committing' ( storeblk() does zblk._p_changed -> Connection.register(zblk) -> txn.join() but txn is already committing IOW storeblk() was invoked with txn.state being already 'Committing' ) T1 T2 modify obj # this way T2.conn joins T2.txn modify zarray commit # T1 invokes .beforeCompletion() ... # beforeCompletion() joins only _ZBigFileH to T1.txn ... # (because T2.conn is already marked as joined) ... ... commit/abort # T2 does commit/abort - this touches only T2.conn, not ZBigFileH ... # in particular T2.conn is now reset to be not joined ... . tpc_begin # actual active commit phase of T1 was somehow delayed a bit . tpc_commit # when changes from RAM propagate to ZODB objects associated . storeblk # connection (= T2.conn !) is notified again, . zblk = ... # wants to join txn for it thinks its transaction_manager, # which when called from under T1 returns *T1* transaction manager for # which T1.txn is already in state='Committing' 4. Empty transaction committed to NEO ( different from doing just transaction.commit() without changing any data - a connection was joined to txn, but set of modified object turned out to be empty ) This is probably a race in Connection._register when both T1 and T2 go to it at the same time: https://github.com/zopefoundation/ZODB/blob/3.10/src/ZODB/Connection.py#L988 def _register(self, obj=None): if self._needs_to_join: self.transaction_manager.get().join(self) self._needs_to_join = False T1 T2 modify zarray commit ... .beforeCompletion modify obj . if T2.conn.needs_join if T2.conn.needs_join # race here . T2.conn.join(T1.txn) T2.conn.join(T2.txn) # as a result T2.conn joins both T1.txn and T2.txn . commit finishes # T2.conn registered-for-commit object list is now empty commit tpc_begin storage.tpc_begin tpc_commit # no object stored, because for-commit-list is empty /cc @jm, @klaus, @Tyagov, @vpelletier
-
Kirill Smelkov authored
ZODB.Connection has support for calling callbacks on .close() but not on .open() . We'll need to hook into both Connection open/close process in the next patch (for _ZBigFileH to stay in sync with Connection state). NOTE on-open callbacks are setup once and fire many times on every open, on-close callbacks are setup once and fire only once on next close. The reason for this is that on-close callbacks are useful for scheduling current connection cleanup, after its processing is done. But on-open callback is for future connection usage, which is generally not related to current connection. /cc @jm, @vpelletier
-
Kirill Smelkov authored
-
- 09 Aug, 2015 1 commit
-
-
Kirill Smelkov authored
Previously we were limited to printing traceback starting down from just storeblk() via explicit PyErr_PrintEx() - because pybuf was attached to memory which could go away right after return from C function - so we had to destroy that object for sure, not letting any traceback to hold a reference to it. This turned out to be too limiting and not showing full context where errors happen. So do the following trick: before returning, reattach pybuf to empty region at NULL, and this way we don't need to worry about pybuf pointing to memory which can go away -> thus instead of printing exception locally - just return it the usual way it is done with C api in Python. NOTE In contrast to PyMemoryViewObject, PyBufferObject definition is not public, so to support Python2 - had to copy its definition to PY2 compat header. NOTE2 loadblk() is not touched - the loading is done from sighandler context, which simulates as if it work in separate python thread, so it is leaved as is for now.
-
- 06 Aug, 2015 4 commits
-
-
Kirill Smelkov authored
At present several threads running can corrupt internal virtmem datastructures (e.g. ram->lru_list, fileh->pagemap, etc). This can happen even if we have zope instances only with 1 worker thread - because there are other "system" thread, and python garbage collection can trigger at any thread, so if a virtmem object, e.g. VMA or FileH was there sitting at GC queue to be collected, their collection, and thus e.g. vma_unmap() and fileh_close() will be called from different-from-worker thread. Because of that virtmem just has to be aware of threads not to allow internal datastructure corruption. On the other hand, the idea of introducing userspace virtual memory manager turned out to be not so good from performance and complexity point of view, and thus the plan is to try to move it back into the kernel. This way it does not make sense to do a well-optimised locking implementation for userspace version. So we do just a simple single "protect-all" big lock for virtmem. Of a particular note is interaction with Python's GIL - any long-lived lock has to be taken with GIL released, because else it can deadlock: t1 t2 G V G !G V G so we introduce helpers to make sure the GIL is not taken, and to retake it back if we were holding it initially. Those helpers (py_gil_ensure_unlocked / py_gil_retake_if_waslocked) are symmetrical opposites to what Python provides to make sure the GIL is locked (via PyGILState_Ensure / PyGILState_Release). Otherwise, the patch is more-or-less straightforward application for one-big-lock to protect everything idea.
-
Kirill Smelkov authored
And specifically that GC'ed object __del__ calls into virtmem (vma_dealloc and fileh_dealloc) again. NOTE not sure it is a good idea to do GC from under sighandle, but currently it happens in practice, because we did not cared to protect against it.
-
Kirill Smelkov authored
We factored out SIGSEGV block/restore from fileh_dirty_writeout() to all functions in cb7a7055 (bigfile/virtmem: Block/restore SIGSEGV in non-pagefault-handling function). The restoration however just sets whole thread sigmask. It could be possible that between block/restore calls procmask for other signals could be changed, and this way - setting procmask directly - we will overwrite them. So be careful, and when restoring SIGSEGV mask, touch mask bit for only that signal. ( we need xsigismember helper to get this done, which is also introduced in this patch )
-
Kirill Smelkov authored
Non on-pagefault code should not access any not-mmapped memory. Here we just refactor the code we already had to block/restore SIGSEGV from fileh_dirty_writeout() and use it in all functions called from non-pagefaulting context, as promised. This way, if there is an error in virtmem implementation which incorrectly accesses prepared for BigFile maps memory, we'll just die with coredump instead of trying to incorrectly handle the pagefault.
-
- 26 Jun, 2015 1 commit
-
-
Kirill Smelkov authored
Previously we were always testing with DBs backed up by FileStorage. Now we provide a way to run the testsuite with user selected storage backend: $ WENDELIN_CORE_TEST_DB="<fs>" make test.py # test with temporary db with FileStorage $ WENDELIN_CORE_TEST_DB="<zeo>" make test.py # ----------//---------- with ZEO $ WENDELIN_CORE_TEST_DB="<neo>" make test.py # ----------//---------- with NEO $ WENDELIN_CORE_TEST_DB=neo://db@master make test.py # test with externally provided DB Default is still to run tests with FileStorage. /cc @jm
-
- 25 Jun, 2015 2 commits
-
-
Kirill Smelkov authored
Factor out those routines to open a ZODB database to common place. The reason for doing so is that we'll soon teach dbopen to automatically recognize several protocols, e.g. neo:// and zeo:// and this way, clients who use dbopen() could automatically access storages besides FileStorage.
-
Kirill Smelkov authored
-
- 02 Jun, 2015 3 commits
-
-
Kirill Smelkov authored
Because numpy.ndarray does not accept it as buffer= argument https://github.com/numpy/numpy/issues/5935 and our memcpy crashes. NOTE if we'll need to use memoryview, we can adapt our memcpy to use array() directly which works with memoryview, as outlined in the above numpy issue.
-
Kirill Smelkov authored
Consider e.g. this for pyvma: 1. in pyfileh_mmap() pyvma is created 2. next fileh_mmap(pyvma, pyfileh, ...) fails 3. we need to deallocate pyvma which was not mapped 4. in pyvma_dealloc() we unmap pyvma unconditionally -> boom. The same story goes for pyfileh dealloc vs not fully constructing it in pyfileh_open().
-
Kirill Smelkov authored
Currently we always raise RuntimeError for problems, which is more-or-less ok for humans, but soon we'll need to distinguish "no memory" errors from other error conditions in upper layers in code. Introduce helper function for choosing appropriate exception type for an error - MemoryError for when errno=ENOMEM and RuntimeError otherwise, and use it where appropriate. ( Unfortunately Python does not provide such a helper... )
-
- 29 Apr, 2015 1 commit
-
-
Kirill Smelkov authored
For example on Debian 7 Wheezy the kernel (linux 3.2) supports holepunching, because holepunching was added in linux 2.6.38: http://git.kernel.org/linus/79124f18b335172e1916075c633745e12dae1dac but glibc does not provide fallocate mode constants, and compilation fails: gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -D_GNU_SOURCE -I./include -I./3rdparty/ccan -I./3rdparty/include -I/opt/slapgrid/f2a5f59d0d2b521681b9333ee76a2859/parts/python2.7/include/python2.7 -c bigfile/ram_shmfs.c -o build/temp.linux-x86_64-2.7/bigfile/ram_shmfs.o -std=gnu99 -fplan9-extensions -fvisibility=hidden -Wno-declaration-after-statement -Wno-error=declaration-after-statement bigfile/ram_shmfs.c: In function ‘shmfs_drop_memory’: bigfile/ram_shmfs.c:135:31: error: ‘FALLOC_FL_PUNCH_HOLE’ undeclared (first use in this function) bigfile/ram_shmfs.c:135:31: note: each undeclared identifier is reported only once for each function it appears in bigfile/ram_shmfs.c:135:54: error: ‘FALLOC_FL_KEEP_SIZE’ undeclared (first use in this function) error: command 'gcc' failed with exit status 1 Try to fix it via fallbacking to provide the needed defines on older systems ourselves. Cc: Klaus Wölfel <klaus@nexedi.com> Cc: Ivan Tyagov <ivan@tyagov.com>
-
- 03 Apr, 2015 11 commits
-
-
Kirill Smelkov authored
- for virtual memory subsytem - for ZBigFiles They are not currently great, e.g. for virtmem we have in-kernel overhead of page clearing - in perf profiles, for bigfile_mmap compared to file_read kernel's clear_page_c raises significantly. That is the worker for clearing page memory and we currently cannot avoid that - any memory obtained from kernel (MAP_ANONYMOUS, mmap(file) with hole, etc...) comes pre-initialized to zeros to userspace. This can be seen in the benchmarks as well: file_readbig differs from file_read in only that the latter uses 1 small buffer and the first allocates large memory (cleared by kernel + python does the memset). bigfile/tests/bench_virtmem.py@125::bench_file_mmap_adler32 0.47 (0.86 0.49 0.47) bigfile/tests/bench_virtmem.py@126::bench_file_read_adler32 0.69 (1.11 0.71 0.69) bigfile/tests/bench_virtmem.py@127::bench_file_readbig_adler32 1.41 (1.70 1.42 1.41) bigfile/tests/bench_virtmem.py@128::bench_bigfile_mmap_adler32 1.42 (1.45 1.42 1.51) bigfile/tests/bench_virtmem.py@130::bench_file_mmap_md5 1.52 (1.91 1.54 1.52) bigfile/tests/bench_virtmem.py@131::bench_file_read_md5 1.73 (2.10 1.75 1.73) bigfile/tests/bench_virtmem.py@132::bench_file_readbig_md5 2.44 (2.73 2.46 2.44) bigfile/tests/bench_virtmem.py@133::bench_bigfile_mmap_md5 2.40 (2.48 2.40 2.53) There is MAP_UNINITIALIZED which works only for non-mmu targets and only if explicitly allowed when configuring kernel (off by default). There were patches to disable that pages zeroing, as it gives significant speedup for people's workloads, e.g. [1,2] but all of them did not got merged for security reasons. [1] http://marc.info/?t=132691315900001&r=1&w=2 [2] http://thread.gmane.org/gmane.linux.kernel/548926 ~~~~ For ZBigFile - it is the storage who is dominating in profiles.
-
Kirill Smelkov authored
This adds transactionality and with e.g. NEO[1] allows to distribute objects to nodes into cluster. We hook into ZODB two-phase commit process as a separate data manager, and synchronize changes to memory, to changes to object only at that time. Alternative would be to get notified on every page change, and mark appropriate object as dirty right at that moment. But I wanted to stay close to filesystem design (we don't get notification for every file change from kernel) - that's why it is done the first way. [1] http://www.neoppod.org/
-
Kirill Smelkov authored
In Python. To demonstrate how backends could be implemented and test system on simple things.
-
Kirill Smelkov authored
Exposes BigFile - this way users can define BigFile backend in Python. Also exposed are BigFile handles, and VMA objects which are results of mmaping.
-
Kirill Smelkov authored
Does similar things to what kernel does - users can mmap file parts into address space and access them read/write. The manager will be getting invoked by hardware/OS kernel for cases when there is no page loaded for read, or when a previousle read-only page is being written to. Additionally to features provided in kernel, it support to be used to store back changes in transactional way (see fileh_dirty_writeout()) and potentially use huge pages for mappings (though this is currently TODO)
-
Kirill Smelkov authored
This thing allows to get aliasable RAM from OS kernel and to manage it. Currently we get memory from a tmpfs mount, and hugetlbfs should also work, but is TODO because hugetlbfs in the kernel needs to be improved. We need aliasing because we'll need to be able to memory map the same page into several places in address space, e.g. for taking two slices overlapping slice of the same array at different times. Comes with test programs that show we aliasing does not work for anonymous memory.
-
Kirill Smelkov authored
This will be the core of virtual memory subsystem. For now we just define a structure to describe pages of memory and add utility to allocate address space from OS.
-
Kirill Smelkov authored
We hook into SIGSEGV and handle read/write pagefaults this way. In this patch there goes stub code that only detects faults and determines (in arch specific way) whether fault was for read or write and there is a TODO to pass that information to higher level. It also comes with tests to detect we still crash if we access something incorrectly, so people could have coredumps and investigate them.
-
Kirill Smelkov authored
For BigFiles we'll needs to maintain `{} offset-in-file -> void *` mapping. A hash or a binary tree could be used there, but since we know files are most of the time accessed sequentially and locally in pages-batches, we can also organize the mapping in batches of keys. Specifically offset bits are so divided into parts, that every part addresses 1 entry in a table of hardware-page in size. To get to the actual value, the system lookups first table by first part of offset, then from first table and next part from address - second table, etc. To clients this looks like a dictionary with get/set/del & clear methods, but lookups are O(1) time always, and in contrast to hashes values are stored with locality (= adjacent lookups almost always access the same tables).
-
Kirill Smelkov authored
It is an early project decision to use gnu99 & Plan9 C extensions, to simplify C code. So far we only build stub wendelin/bigfile/_bigfile.so . Makefile is introduced because there will be targets which are easier to handle at make level. For end users no make knowledge is required - usual `python setup.py build|install|...` work, redirecting to make where necessary. As was promised (e870781d "Top-level in-tree import redirector") setup.py contains install-time hooks to handle in-tree wendelin.py and install it as a module namespace. For sdist, we just use `git ls-files` info if we are in a checkout.
-
Kirill Smelkov authored
This will be a module to allow users to program custom file-like backends and latter memory-map content of files from the backend. For now just start the module structure.
-