From: Adam Kupczyk Date: Fri, 23 Aug 2019 10:17:00 +0000 (+0200) Subject: kv/KeyValueDB: fix estimate_prefix_size() X-Git-Tag: v15.1.0~1659^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=f806539c282893aedc242a21e7a904fcff636fde;p=ceph.git kv/KeyValueDB: fix estimate_prefix_size() Signed-off-by: Adam Kupczyk --- diff --git a/src/kv/RocksDBStore.cc b/src/kv/RocksDBStore.cc index 146c36cea3b6..1ee0b9dc5272 100644 --- a/src/kv/RocksDBStore.cc +++ b/src/kv/RocksDBStore.cc @@ -705,8 +705,8 @@ int64_t RocksDBStore::estimate_prefix_size(const string& prefix, rocksdb::Range r(start, limit); db->GetApproximateSizes(cf, &r, 1, &size, flags); } else { - string start = prefix + key_prefix; - string limit = prefix + key_prefix + "\xff\xff\xff\xff"; + string start = combine_strings(prefix , key_prefix); + string limit = combine_strings(prefix , key_prefix + "\xff\xff\xff\xff"); rocksdb::Range r(start, limit); db->GetApproximateSizes(default_cf, &r, 1, &size, flags); } diff --git a/src/test/objectstore/test_kv.cc b/src/test/objectstore/test_kv.cc index 6633b67a0fd6..3318b38d4107 100644 --- a/src/test/objectstore/test_kv.cc +++ b/src/test/objectstore/test_kv.cc @@ -464,6 +464,72 @@ TEST_P(KVTest, RocksDBCFMerge) { fini(); } +TEST_P(KVTest, RocksDB_estimate_size) { + if(string(GetParam()) != "rocksdb") + GTEST_SKIP(); + + std::vector cfs; + cfs.push_back(KeyValueDB::ColumnFamily("cf1", "")); + ASSERT_EQ(0, db->init(g_conf()->bluestore_rocksdb_options)); + cout << "creating one column family and opening it" << std::endl; + ASSERT_EQ(0, db->create_and_open(cout)); + + for(int test = 0; test < 20; test++) + { + KeyValueDB::Transaction t = db->get_transaction(); + bufferlist v1; + v1.append(string(1000, '1')); + for (int i = 0; i < 100; i++) + t->set("A", to_string(rand()%100000), v1); + db->submit_transaction_sync(t); + db->compact(); + + int64_t size_a = db->estimate_prefix_size("A",""); + ASSERT_GT(size_a, (test + 1) * 1000 * 100 * 0.5); + ASSERT_LT(size_a, (test + 1) * 1000 * 100 * 1.5); + int64_t size_a1 = db->estimate_prefix_size("A","1"); + ASSERT_GT(size_a1, (test + 1) * 1000 * 100 * 0.1 * 0.5); + ASSERT_LT(size_a1, (test + 1) * 1000 * 100 * 0.1 * 1.5); + int64_t size_b = db->estimate_prefix_size("B",""); + ASSERT_EQ(size_b, 0); + } + + fini(); +} + +TEST_P(KVTest, RocksDB_estimate_size_column_family) { + if(string(GetParam()) != "rocksdb") + GTEST_SKIP(); + + std::vector cfs; + cfs.push_back(KeyValueDB::ColumnFamily("cf1", "")); + ASSERT_EQ(0, db->init(g_conf()->bluestore_rocksdb_options)); + cout << "creating one column family and opening it" << std::endl; + ASSERT_EQ(0, db->create_and_open(cout, cfs)); + + for(int test = 0; test < 20; test++) + { + KeyValueDB::Transaction t = db->get_transaction(); + bufferlist v1; + v1.append(string(1000, '1')); + for (int i = 0; i < 100; i++) + t->set("cf1", to_string(rand()%100000), v1); + db->submit_transaction_sync(t); + db->compact(); + + int64_t size_a = db->estimate_prefix_size("cf1",""); + ASSERT_GT(size_a, (test + 1) * 1000 * 100 * 0.5); + ASSERT_LT(size_a, (test + 1) * 1000 * 100 * 1.5); + int64_t size_a1 = db->estimate_prefix_size("cf1","1"); + ASSERT_GT(size_a1, (test + 1) * 1000 * 100 * 0.1 * 0.5); + ASSERT_LT(size_a1, (test + 1) * 1000 * 100 * 0.1 * 1.5); + int64_t size_b = db->estimate_prefix_size("B",""); + ASSERT_EQ(size_b, 0); + } + + fini(); +} + INSTANTIATE_TEST_SUITE_P( KeyValueDB, KVTest,