Archive

Archive for the ‘hamsterdb’ Category

hamsterdb: release 1.1.10

June 25th, 2011

From the README:

This release fixes a bug in the cache, which caused the cache to grow and exceed the cache limits. A few other minor bugs were fixed, and the recovery process was improved: changes in the header page (i.e. when adding new Databases or when changing the address of a root page) were not correctly logged. The flag HAM_WRITE_THROUGH has a new meaning: instead of immediately flushing all pages it will now flush all file handles after a Transaction was committed or aborted. This has a performance impact, but improves the Durability of the Transactions. Sources, CHANGELOG and precompiled win32 libraries are available for download here!

This release was long overdue. There were some bugs reported that were hard to reproduce and to track down, family also required more time than usual, and in the same time i was also working on the first release of 2.0.0 (rc1), which will implement the new transaction handling. I hope i can release it in the next few weeks – i have to port a couple of patches from 1.1.10 to the new branch and also make sure that everything compiles and works on Windows.  The first release will be unstable – there were so many code changes and the test coverage for some parts of the new code is still not satisfying. In addition i have some ideas how to boost performance, but so far i had no time to implement them.

Stay tuned :)

chris Coding, Databases, hamsterdb, Libraries

A short status update

April 6th, 2011

The next release is due, but it will week another 2 weeks. The last release was about 6 weeks ago, and my release cycle is usually not more than 4-6 weeks. There were some bug reports for 1.1.9 – but nothing critical, therefore I’m not seeing a need to release a 1.1.10 immediately.

But since 1.1.9 is the last stable release, and the new transaction implementation (which will be 1.2.0) will be unstable for the next few months, there definitely WILL be a 1.1.10 coming soon.

In the meantime let me give you an update about the re-implementation of the Transaction implementation, which was already described here.

A quick reminder about the new functionality:

  • You can have as many parallel Transactions as you want
  • Transactions are stored in RAM in a tree structure
  • For recovery, they are also written to a journal (and right now also to a physical log, which will be removed in the release after)
  • When Transactions are committed, they’re flushed to the disk. When they’re aborted then they’re just removed from the in-memory structures.

At runtime, hamsterdb consolidates the two trees (the Transaction tree in RAM and the BTree on disk). This is a complex task, especially when traversing the Database with a cursor. And things get even more complex when the Database has duplicate keys, because duplicate keys are not necessary inserted in the same order as they’re stored in the Database.

Right now most of the duplicate functionality is implemented and working fine. A few things are still missing, and should be ready in about 2 weeks or so.

After the first (unstable) release, there are a couple of things that are in urgent need of attention.

  • The test coverage and automization has to be improved
  • The high-level APIs (Java, .NET etc) also need some love
  • Improve performance by getting rid of the physical log file
  • Flush committed Transactions to disk in the background
  • etc, etc, etc…

chris Coding, hamsterdb, Libraries

hamsterdb on iPhone and Android

April 6th, 2011

I received two mails regarding support of Android and iOS. And hamsterdb works on both of them.

I even discovered a github project about Android support:

https://github.com/glacefullife/Hamster-DB-for-Android

This reminds me that the Java API (just like Python and .NET) was terribly neglected. Shame on me. My plans for the high-level APIs are to fully automize their releases. With every new release, the high-level APIs will automatically be built and tested. I plan to create the necessary infrastructure shortly after the next big release with the new Transaction code. I’ll write another blogpost about the current status and the next steps later today.

And finally, a screenshot of an app based on hamsterdb running on iOS. Thanks for sending, SSB! If someone is interested in the xcode project then please drop me a mail.

hamsterdb on iOS

an iPhone/iOS app powered by hamsterdb

chris Coding, hamsterdb, Libraries

hamsterdb: release 1.1.9!

February 18th, 2011

This release fixes a bug in the Transaction handling, and an out-of-memory condition with long-running Transactions. It fixes several other issues detected by static code analysis tools. A .spec file was added for RPM generation. A 64bit incompatibility was fixed in the remote functions/hamserver. Sources, CHANGELOG and precompiled win32 libraries are available for download here:

http://hamsterdb.com/download

The new transaction implementation is still in development; right now the only missing big piece is the support for duplicate keys. Especially the implementation of the cursors will be tricky and take a few weeks. After that some cleanup is required and then the first (unstable) release will be available!

chris Coding, hamsterdb, Libraries

hamsterdb: release 1.1.8!

December 6th, 2010

1.1.8 is a bugfix release. The following issues were fixed:

  1. bugfix with HAM_DIRECT_ACCESS and cursors which insert duplicates at the beginning of the duplicate list; if record size is <= 8 bytes then invalid data was returned. Parts of this bug were fixed in 1.1.7.
  2. bugfix in transaction handling – in certain cases with long running transactions, a circular reference in a linked list causes an endless loop (thanks, Mark)
  3. configure.in will no longer overwrite -Ox flags; the default compilation option is now -O2

Files are here: http://hamsterdb.com/download!

chris hamsterdb, Libraries

hammy is a hamsterdb Erlang wrapper

November 22nd, 2010

… which i found by pure coincidence. It’s written by Kevin Smith, and sources are here: https://github.com/kevsmith/hammy.

I have absolutely no experience with Erlang and functional languages of any kind, but i finally decided to give it a try in the near future.

chris hamsterdb

hamsterdb: release 1.1.7!

November 4th, 2010

This release fixes a small memory leak when using remote transactions. It fixes HAM_DIRECT_ACCESS with records <= 8 bytes. And it implements some significant performance improvements.

- the shutdown time improved a lot when using many extended keys

- when appending (or prepending) keys to a database then most inserts are now extremely fast in constant time

As always, you can get everything from here: http://hamsterdb.com/download.

The new transactional code is not yet included.

chris Coding, hamsterdb, Libraries

A new implementation for Transactions

October 14th, 2010

Over the last few weeks i started merging the hamsterdb2 Transaction handling into hamsterdb. (Some time ago i already announced that hamsterdb2 will be discontinued due to lack of resources [= time]).

The code is merged on a new branch “bleeding_edge” (http://github.com/cruppstahl/hamsterdb/tree/bleeding_edge).

What’s so special about the Transaction stuff I’m now writing?

Traditional implementations of Transaction

Traditional implementations of Transactions are often based on one of the following methods:

  1. Modified database pages are locked when they’re modified; the lock is held till the Transaction is committed or aborted
  2. Modified database pages are copied to a “shadow page”; upon commit, the original page and the shadow page are exchanged, usually by swapping pointers
  3. Only one Transaction is allowed (currently this is hamsterdb’s way of dealing with Transactions)
  4. Transactions are not allowed at all

While the last two options obviously have some limitations, the first two are a little bit better. Locking pages can block your Transactions when they modify keys on the same database page:

begin T1
begin T2
insert T1, key1, record1
insert T2, key2, record2 –> this will block till T1 is committed or aborted

Depending on the implementation, “block” means that the calling thread will only continue when T1 is committed or aborted. This will always lead to deadlocks if T1 and T2 are used in the same thread.

Shadow pages are costly – a new page has to be allocated, the memory has to be copied etc.

How will hamsterdb deal with all this?

I have found a different way of dealing with Transactions in hamsterdb. They are based on the assumption that Transaction support is mainly interesting for desktop and enterprise environments, not so much for embedded ones. And a conclusion from that assumption is that there’s sufficient RAM to store additional data. (Another conclusion is that multi-core CPUs are available and some costly operations will be processed in the background; but this is not part of my early milestones.)

Whenever a key/value pair is inserted in a transactional environment, hamsterdb will create an in-memory tree structure for the modified database. The key/value pair is then inserted in this tree. As soon as this Transaction is committed, the tree is flushed to disk. Since RAM is volatile and the tree is lost if the application crashes, a logical journal is used to log the operations. (Lookup and erase operations are similar.)

What maybe sounds a bit complex will be much simpler when reading the pseudo code.

def insert(db, txn, key, value):
    // get or create a new Tree structure for this Database; each
    // Database has just one such tree
    tree=db_get_or_create_tree(db)

    // get or create a new node in this tree which manages all operations of this key
    // each Tree has just one node *per key+
    node=tree_get_or_create_node(tree, key)

    // check if another active transaction (neither committed nor aborted)
    // also modifies this key. If yes then we have two conflicting transactions and
    // we return an error. The function is listed below.
    check_for_conflicts(db, txn, node, key)

    // otherwise we append another operation to this node
    node_append_operation(node, INSERT, record, current_lsn, flags)

    // and write the operation to the journal
    log_append_operation(INSERT, key, record, flags)

    // that's it!
    return

As promised here’s the function to check for conflicting Transactions. It will also check for other errors, i.e. if we’re inserting a key which was already inserted in another committed Transaction (this would create a duplicate key which is not allowed unless explicitly requested by the user).

def check_for_conflicts(db, txn, node, key):
    // iterate over all database operations (insert/erase) which modify this key.
    foreach Operation op in node_get_operations(node):
        // get a reference to the Transaction of this operation. We need to see if it's
        // already committed, aborted or still active.
        optxn=operation_get_txn(op)

        // if the txn was aborted then it's not interesting for us
        if txn_is_aborted(optxn):
            continue

        // if the txn was committed (or if it's the current txn) then we have to have a
        // closer look; if the operation was INSERT then we have a duplicate. If it's ERASE
        // then we can return successfully, because the key no longer exists and we can
        // insert it.
        if txn_is_committed(optxn) or txn==optxn:
            if op_type==INSERT:
                return DUPLICATE_KEY
            if op_type==ERASE:
                return SUCCESS

        // if the txn is neither committed or aborted then it's still running.
        // if two running txn's modify the same key then we return an error
        if txn_is_active(optxn):
            return CONFLICT

    // we're nearly through - we now checked all Transaction trees that were not
    // yet committed. Now we have to check the committed transactions that were
    // already flushed to disk. This basically is a simple btree lookup.
    // if the key does NOT yet exist, we're fine. otherwise we insert a duplicate.
    switch status=btree_find(db, key, flags):
    case SUCCESS:
        return DUPLICATE_KEY
    case KEY_NOT_FOUND:
        return SUCCESS
    default:
        return status

That’s it. hamsterdb allows inserting duplicate keys and it can also overwrite existing keys. To simplify the code I have not included these cases here.

Challenges

While the algorithm above works similar for lookup/erase operations, there are still a couple of challenges and problems that i have not yet solved.

Replacing the physical logfile with a logical journal will bring vast performance benefits, and the file size of the journal will be much smaller than the size of the logfile. However, it also requires that all operations in the journal are idempotent, which means that they can be re-applied even if they were already applied partially or completely. For insert I already know how to solve this problem. For erase the situation is more complex because the SMOs (structure modification operations – the btree merging/shifting/collapsing) are maybe impossible to become idempotent. This will require a simplification of the erase algorithm, i.e. in combination with “lazy splits”, or maybe I will have to fallback to physical logging in such cases.

The Cursor operations will be tricky to implement. Internally, two Cursors have to be created; one for the Transaction tree, another for the Btree. At runtime, both Cursors then have to be consolidated.

Similar problems appear when reading partial keys or when doing approximate matching. In both scenarios the Transaction tree and the Btree have to be consolidated.

An outlook

On the other hand, benefits of the new approach are obvious. By keeping all Transactions in RAM, performance will be high and concurrency (as in having several Transactions in parallel) will be high, too. No pages have to be locked. If the amount of consumed RAM is too high, large keys or records can be swapped to disk immediately.

In addition, as mentioned earlier, committed Transactions can be flushed to disk asynchronously. On today’s multi-core CPUs this offer another performance benefit for many use cases.

I hope I can come up with a first release this year, although most likely with limited functionality. If you’re interested in the progress then drop me a mail, follow the blog or the code – the github link is above. If you’re just curious about the new Transactional abilities, you can browse a few of the unittests. The file is here:

http://github.com/cruppstahl/hamsterdb/blob/bleeding_edge/unittests/txn.cpp

Look at the tests called txnInsertConflict*, txnInsertFind*, txnInsertFindErase*.

chris hamsterdb

c’t 18/10, page 179

October 4th, 2010

hamsterdb: release 1.1.6!

September 17th, 2010

This release stabilized the remote functionality. New samples for client/server scenarios are provided. The remote server can now write access logfiles. A unix daemon and a win32 Service were added. The dependency to Google Protocol Buffers for C was removed; instead, Google Protocol Buffers (C++) are now used. Compilation on ArchLinux (gcc 4.5.1) was fixed. Several functions are now deprecated. The source repository was moved to github. Sources, CHANGELOG and precompiled win32 libraries are available for download here!

chris Coding, hamsterdb, Libraries