Discussion:
[leveldb] Get() returns NotFound even if Put() successfully with write_options.sync=true.
Jianjun Zheng
2018-04-19 08:58:41 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...