Get() returns NotFound even if Put() successfully with write_options.sync=true.

46 views
Skip to first unread message

Jianjun Zheng

unread,
Apr 19, 2018, 4:58:41 AM4/19/18
to leveldb

Here is an example shows that:
Get() returns NotFound even if Put() successfully with write_options.sync=true.


This is an interesting case.

Pay attention that I release disk space(not the data in the db) when io error(No space left on device) occurs, then the error disappears.


The test code is as follow:


#include <string>
#include <unistd.h>
#include "leveldb/db.h"

int main() {
    const char* dbname = "./trash/ldb_data";
    leveldb::DB* db = NULL;
    leveldb::Options opts;
    opts.create_if_missing = true;
    leveldb::WriteOptions wopts;
    leveldb::Status s;
    long long count = 0;

    s = leveldb::DB::Open(opts, dbname, &db);
    assert(db != NULL);

    printf("Put until io error(No space left on device) occurs.\n");

    while (true) {
        std::string key = std::to_string(count++);
        s = db->Put(wopts, key, "hello world");
        if (!s.ok()) {
            printf("Put status %s.\n", s.ToString().c_str());
            break;
        }
    }

    printf("Release disk space(not the data in the db).\n");
    sleep(20);

    {
        wopts.sync = true;
        std::string key = std::to_string(count);
        s = db->Put(wopts, key, std::string(32 * 1024, 'x'));
        printf("Put key %s status %s.\n", key.c_str(), s.ToString().c_str());
        assert(s.ok());
    }

    printf("Re-open the leveldb.\n");
    {
        delete db;
        db = NULL;
        s = leveldb::DB::Open(opts, dbname, &db);
        assert(db != NULL);
    }

    printf("Read the key put successfully with wopts.sync=true.\n");
    {
        std::string key = std::to_string(count);
        std::string value;
        s = db->Get(leveldb::ReadOptions(), key, &value);
        printf("Get key %s status %s.\n", key.c_str(), s.ToString().c_str());
        assert(!s.IsNotFound()); // assert happen !!!
    }

    return 0;
}


Run the code and the output:


:~/open/leveldb-master>./a.out
Put until io error(No space left on device) occurs.
Put status IO error: ./trash/ldb_data/000715.log: No space left on device.
Release disk space(not the data in the db).
Put key 14411510 status OK.
Re-open the leveldb.
Read the key put successfully with wopts.sync=true.
Get key 14411510 status NotFound: .
a.out: test.cc:53: int main(): Assertion `!s.IsNotFound()' failed.
Aborted




Reply all
Reply to author
Forward
0 new messages