Skip to content

Commit 1880268

Browse files
committed
Make an API to get database identity from the IDENTITY file
Summary: This would enable rocksdb users to get the db identity without depending on implementation details(storing that in IDENTITY file) Test Plan: db/db_test (has identity checks) Reviewers: dhruba, haobo, igor, kailiu Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D14463
1 parent fa88cbc commit 1880268

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

db/db_impl.cc

+27
Original file line numberDiff line numberDiff line change
@@ -3497,6 +3497,33 @@ void DBImpl::GetLiveFilesMetaData(std::vector<LiveFileMetaData> *metadata) {
34973497
return versions_->GetLiveFilesMetaData(metadata);
34983498
}
34993499

3500+
Status DBImpl::GetDbIdentity(std::string& identity) {
3501+
std::string idfilename = IdentityFileName(dbname_);
3502+
unique_ptr<SequentialFile> idfile;
3503+
const EnvOptions soptions;
3504+
Status s = env_->NewSequentialFile(idfilename, &idfile, soptions);
3505+
if (!s.ok()) {
3506+
return s;
3507+
}
3508+
uint64_t file_size;
3509+
s = env_->GetFileSize(idfilename, &file_size);
3510+
if (!s.ok()) {
3511+
return s;
3512+
}
3513+
char buffer[file_size];
3514+
Slice id;
3515+
s = idfile->Read(file_size, &id, buffer);
3516+
if (!s.ok()) {
3517+
return s;
3518+
}
3519+
identity.assign(id.ToString());
3520+
// If last character is '\n' remove it from identity
3521+
if (identity.size() > 0 && identity.back() == '\n') {
3522+
identity.pop_back();
3523+
}
3524+
return s;
3525+
}
3526+
35003527
// Default implementations of convenience methods that subclasses of DB
35013528
// can call if they wish
35023529
Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {

db/db_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class DBImpl : public DB {
8585
virtual void GetLiveFilesMetaData(
8686
std::vector<LiveFileMetaData> *metadata);
8787

88+
virtual Status GetDbIdentity(std::string& identity);
89+
8890
// Extra methods (for testing) that are not in the public DB interface
8991

9092
// Compact any files in the named level that overlap [*begin, *end]

db/db_test.cc

+10-18
Original file line numberDiff line numberDiff line change
@@ -1731,31 +1731,23 @@ TEST(DBTest, ManifestRollOver) {
17311731

17321732
TEST(DBTest, IdentityAcrossRestarts) {
17331733
do {
1734-
std::string idfilename = IdentityFileName(dbname_);
1735-
unique_ptr<SequentialFile> idfile;
1736-
const EnvOptions soptions;
1737-
ASSERT_OK(env_->NewSequentialFile(idfilename, &idfile, soptions));
1738-
char buffer1[100];
1739-
Slice id1;
1740-
ASSERT_OK(idfile->Read(100, &id1, buffer1));
1734+
std::string id1;
1735+
ASSERT_OK(db_->GetDbIdentity(id1));
17411736

17421737
Options options = CurrentOptions();
17431738
Reopen(&options);
1744-
char buffer2[100];
1745-
Slice id2;
1746-
ASSERT_OK(env_->NewSequentialFile(idfilename, &idfile, soptions));
1747-
ASSERT_OK(idfile->Read(100, &id2, buffer2));
1739+
std::string id2;
1740+
ASSERT_OK(db_->GetDbIdentity(id2));
17481741
// id1 should match id2 because identity was not regenerated
1749-
ASSERT_EQ(id1.ToString(), id2.ToString());
1742+
ASSERT_EQ(id1.compare(id2), 0);
17501743

1744+
std::string idfilename = IdentityFileName(dbname_);
17511745
ASSERT_OK(env_->DeleteFile(idfilename));
17521746
Reopen(&options);
1753-
char buffer3[100];
1754-
Slice id3;
1755-
ASSERT_OK(env_->NewSequentialFile(idfilename, &idfile, soptions));
1756-
ASSERT_OK(idfile->Read(100, &id3, buffer3));
1757-
// id1 should NOT match id2 because identity was regenerated
1758-
ASSERT_NE(id1.ToString(0), id3.ToString());
1747+
std::string id3;
1748+
ASSERT_OK(db_->GetDbIdentity(id3));
1749+
// id1 should NOT match id3 because identity was regenerated
1750+
ASSERT_NE(id1.compare(id3), 0);
17591751
} while (ChangeCompactOptions());
17601752
}
17611753

include/rocksdb/db.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class DB {
273273
// Sets iter to an iterator that is positioned at a write-batch containing
274274
// seq_number. If the sequence number is non existent, it returns an iterator
275275
// at the first available seq_no after the requested seq_no
276-
// Returns Status::Ok if iterator is valid
276+
// Returns Status::OK if iterator is valid
277277
// Must set WAL_ttl_seconds or WAL_size_limit_MB to large values to
278278
// use this api, else the WAL files will get
279279
// cleared aggressively and the iterator might keep getting invalid before
@@ -292,6 +292,14 @@ class DB {
292292
std::vector<LiveFileMetaData> *metadata) {
293293
}
294294

295+
// Sets the globally unique ID created at database creation time by invoking
296+
// Env::GenerateUniqueId(), in identity. Returns Status::OK if identity could
297+
// be set properly
298+
virtual Status GetDbIdentity(std::string& identity) {
299+
identity.clear();
300+
return Status::OK();
301+
}
302+
295303
private:
296304
// No copying allowed
297305
DB(const DB&);

0 commit comments

Comments
 (0)