|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <cinttypes>
|
|
|
|
#include "monitoring/perf_context_imp.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/string_util.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
|
|
|
// kValueTypeForSeek defines the ValueType that should be passed when
|
|
|
|
// constructing a ParsedInternalKey object for seeking to a particular
|
|
|
|
// sequence number (since we sort sequence numbers in decreasing order
|
|
|
|
// and the value type is embedded as the low 8 bits in the sequence
|
|
|
|
// number in internal keys, we need to use the highest-numbered
|
|
|
|
// ValueType, not the lowest).
|
|
|
|
const ValueType kValueTypeForSeek = kTypeDeletionWithTimestamp;
|
|
|
|
const ValueType kValueTypeForSeekForPrev = kTypeDeletion;
|
|
|
|
|
Added support for differential snapshots
Summary:
The motivation for this PR is to add to RocksDB support for differential (incremental) snapshots, as snapshot of the DB changes between two points in time (one can think of it as diff between to sequence numbers, or the diff D which can be thought of as an SST file or just set of KVs that can be applied to sequence number S1 to get the database to the state at sequence number S2).
This feature would be useful for various distributed storages layers built on top of RocksDB, as it should help reduce resources (time and network bandwidth) needed to recover and rebuilt DB instances as replicas in the context of distributed storages.
From the API standpoint that would like client app requesting iterator between (start seqnum) and current DB state, and reading the "diff".
This is a very draft PR for initial review in the discussion on the approach, i'm going to rework some parts and keep updating the PR.
For now, what's done here according to initial discussions:
Preserving deletes:
- We want to be able to optionally preserve recent deletes for some defined period of time, so that if a delete came in recently and might need to be included in the next incremental snapshot it would't get dropped by a compaction. This is done by adding new param to Options (preserve deletes flag) and new variable to DB Impl where we keep track of the sequence number after which we don't want to drop tombstones, even if they are otherwise eligible for deletion.
- I also added a new API call for clients to be able to advance this cutoff seqnum after which we drop deletes; i assume it's more flexible to let clients control this, since otherwise we'd need to keep some kind of timestamp < -- > seqnum mapping inside the DB, which sounds messy and painful to support. Clients could make use of it by periodically calling GetLatestSequenceNumber(), noting the timestamp, doing some calculation and figuring out by how much we need to advance the cutoff seqnum.
- Compaction codepath in compaction_iterator.cc has been modified to avoid dropping tombstones with seqnum > cutoff seqnum.
Iterator changes:
- couple params added to ReadOptions, to optionally allow client to request internal keys instead of user keys (so that client can get the latest value of a key, be it delete marker or a put), as well as min timestamp and min seqnum.
TableCache changes:
- I modified table_cache code to be able to quickly exclude SST files from iterators heep if creation_time on the file is less then iter_start_ts as passed in ReadOptions. That would help a lot in some DB settings (like reading very recent data only or using FIFO compactions), but not so much for universal compaction with more or less long iterator time span.
What's left:
- Still looking at how to best plug that inside DBIter codepath. So far it seems that FindNextUserKeyInternal only parses values as UserKeys, and iter->key() call generally returns user key. Can we add new API to DBIter as internal_key(), and modify this internal method to optionally set saved_key_ to point to the full internal key? I don't need to store actual seqnum there, but I do need to store type.
Closes https://github.com/facebook/rocksdb/pull/2999
Differential Revision: D6175602
Pulled By: mikhail-antonov
fbshipit-source-id: c779a6696ee2d574d86c69cec866a3ae095aa900
7 years ago
|
|
|
EntryType GetEntryType(ValueType value_type) {
|
|
|
|
switch (value_type) {
|
|
|
|
case kTypeValue:
|
|
|
|
return kEntryPut;
|
|
|
|
case kTypeDeletion:
|
|
|
|
return kEntryDelete;
|
|
|
|
case kTypeDeletionWithTimestamp:
|
|
|
|
return kEntryDeleteWithTimestamp;
|
Added support for differential snapshots
Summary:
The motivation for this PR is to add to RocksDB support for differential (incremental) snapshots, as snapshot of the DB changes between two points in time (one can think of it as diff between to sequence numbers, or the diff D which can be thought of as an SST file or just set of KVs that can be applied to sequence number S1 to get the database to the state at sequence number S2).
This feature would be useful for various distributed storages layers built on top of RocksDB, as it should help reduce resources (time and network bandwidth) needed to recover and rebuilt DB instances as replicas in the context of distributed storages.
From the API standpoint that would like client app requesting iterator between (start seqnum) and current DB state, and reading the "diff".
This is a very draft PR for initial review in the discussion on the approach, i'm going to rework some parts and keep updating the PR.
For now, what's done here according to initial discussions:
Preserving deletes:
- We want to be able to optionally preserve recent deletes for some defined period of time, so that if a delete came in recently and might need to be included in the next incremental snapshot it would't get dropped by a compaction. This is done by adding new param to Options (preserve deletes flag) and new variable to DB Impl where we keep track of the sequence number after which we don't want to drop tombstones, even if they are otherwise eligible for deletion.
- I also added a new API call for clients to be able to advance this cutoff seqnum after which we drop deletes; i assume it's more flexible to let clients control this, since otherwise we'd need to keep some kind of timestamp < -- > seqnum mapping inside the DB, which sounds messy and painful to support. Clients could make use of it by periodically calling GetLatestSequenceNumber(), noting the timestamp, doing some calculation and figuring out by how much we need to advance the cutoff seqnum.
- Compaction codepath in compaction_iterator.cc has been modified to avoid dropping tombstones with seqnum > cutoff seqnum.
Iterator changes:
- couple params added to ReadOptions, to optionally allow client to request internal keys instead of user keys (so that client can get the latest value of a key, be it delete marker or a put), as well as min timestamp and min seqnum.
TableCache changes:
- I modified table_cache code to be able to quickly exclude SST files from iterators heep if creation_time on the file is less then iter_start_ts as passed in ReadOptions. That would help a lot in some DB settings (like reading very recent data only or using FIFO compactions), but not so much for universal compaction with more or less long iterator time span.
What's left:
- Still looking at how to best plug that inside DBIter codepath. So far it seems that FindNextUserKeyInternal only parses values as UserKeys, and iter->key() call generally returns user key. Can we add new API to DBIter as internal_key(), and modify this internal method to optionally set saved_key_ to point to the full internal key? I don't need to store actual seqnum there, but I do need to store type.
Closes https://github.com/facebook/rocksdb/pull/2999
Differential Revision: D6175602
Pulled By: mikhail-antonov
fbshipit-source-id: c779a6696ee2d574d86c69cec866a3ae095aa900
7 years ago
|
|
|
case kTypeSingleDeletion:
|
|
|
|
return kEntrySingleDelete;
|
|
|
|
case kTypeMerge:
|
|
|
|
return kEntryMerge;
|
|
|
|
case kTypeRangeDeletion:
|
|
|
|
return kEntryRangeDeletion;
|
|
|
|
case kTypeBlobIndex:
|
|
|
|
return kEntryBlobIndex;
|
Added support for differential snapshots
Summary:
The motivation for this PR is to add to RocksDB support for differential (incremental) snapshots, as snapshot of the DB changes between two points in time (one can think of it as diff between to sequence numbers, or the diff D which can be thought of as an SST file or just set of KVs that can be applied to sequence number S1 to get the database to the state at sequence number S2).
This feature would be useful for various distributed storages layers built on top of RocksDB, as it should help reduce resources (time and network bandwidth) needed to recover and rebuilt DB instances as replicas in the context of distributed storages.
From the API standpoint that would like client app requesting iterator between (start seqnum) and current DB state, and reading the "diff".
This is a very draft PR for initial review in the discussion on the approach, i'm going to rework some parts and keep updating the PR.
For now, what's done here according to initial discussions:
Preserving deletes:
- We want to be able to optionally preserve recent deletes for some defined period of time, so that if a delete came in recently and might need to be included in the next incremental snapshot it would't get dropped by a compaction. This is done by adding new param to Options (preserve deletes flag) and new variable to DB Impl where we keep track of the sequence number after which we don't want to drop tombstones, even if they are otherwise eligible for deletion.
- I also added a new API call for clients to be able to advance this cutoff seqnum after which we drop deletes; i assume it's more flexible to let clients control this, since otherwise we'd need to keep some kind of timestamp < -- > seqnum mapping inside the DB, which sounds messy and painful to support. Clients could make use of it by periodically calling GetLatestSequenceNumber(), noting the timestamp, doing some calculation and figuring out by how much we need to advance the cutoff seqnum.
- Compaction codepath in compaction_iterator.cc has been modified to avoid dropping tombstones with seqnum > cutoff seqnum.
Iterator changes:
- couple params added to ReadOptions, to optionally allow client to request internal keys instead of user keys (so that client can get the latest value of a key, be it delete marker or a put), as well as min timestamp and min seqnum.
TableCache changes:
- I modified table_cache code to be able to quickly exclude SST files from iterators heep if creation_time on the file is less then iter_start_ts as passed in ReadOptions. That would help a lot in some DB settings (like reading very recent data only or using FIFO compactions), but not so much for universal compaction with more or less long iterator time span.
What's left:
- Still looking at how to best plug that inside DBIter codepath. So far it seems that FindNextUserKeyInternal only parses values as UserKeys, and iter->key() call generally returns user key. Can we add new API to DBIter as internal_key(), and modify this internal method to optionally set saved_key_ to point to the full internal key? I don't need to store actual seqnum there, but I do need to store type.
Closes https://github.com/facebook/rocksdb/pull/2999
Differential Revision: D6175602
Pulled By: mikhail-antonov
fbshipit-source-id: c779a6696ee2d574d86c69cec866a3ae095aa900
7 years ago
|
|
|
default:
|
|
|
|
return kEntryOther;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
|
|
|
|
result->append(key.user_key.data(), key.user_key.size());
|
|
|
|
PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendInternalKeyWithDifferentTimestamp(std::string* result,
|
|
|
|
const ParsedInternalKey& key,
|
|
|
|
const Slice& ts) {
|
|
|
|
assert(key.user_key.size() >= ts.size());
|
|
|
|
result->append(key.user_key.data(), key.user_key.size() - ts.size());
|
|
|
|
result->append(ts.data(), ts.size());
|
|
|
|
PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendInternalKeyFooter(std::string* result, SequenceNumber s,
|
|
|
|
ValueType t) {
|
|
|
|
PutFixed64(result, PackSequenceAndType(s, t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendKeyWithMinTimestamp(std::string* result, const Slice& key,
|
|
|
|
size_t ts_sz) {
|
|
|
|
assert(ts_sz > 0);
|
|
|
|
const std::string kTsMin(ts_sz, static_cast<unsigned char>(0));
|
|
|
|
result->append(key.data(), key.size());
|
|
|
|
result->append(kTsMin.data(), ts_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendKeyWithMaxTimestamp(std::string* result, const Slice& key,
|
|
|
|
size_t ts_sz) {
|
|
|
|
assert(ts_sz > 0);
|
|
|
|
const std::string kTsMax(ts_sz, static_cast<unsigned char>(0xff));
|
|
|
|
result->append(key.data(), key.size());
|
|
|
|
result->append(kTsMax.data(), ts_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ParsedInternalKey::DebugString(bool log_err_key, bool hex) const {
|
|
|
|
std::string result = "'";
|
|
|
|
if (log_err_key) {
|
|
|
|
result += user_key.ToString(hex);
|
|
|
|
} else {
|
|
|
|
result += "<redacted>";
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[50];
|
|
|
|
snprintf(buf, sizeof(buf), "' seq:%" PRIu64 ", type:%d", sequence,
|
|
|
|
static_cast<int>(type));
|
|
|
|
|
|
|
|
result += buf;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InternalKey::DebugString(bool hex) const {
|
|
|
|
std::string result;
|
|
|
|
ParsedInternalKey parsed;
|
|
|
|
if (ParseInternalKey(rep_, &parsed, false /* log_err_key */).ok()) {
|
|
|
|
result = parsed.DebugString(true /* log_err_key */, hex); // TODO
|
|
|
|
} else {
|
|
|
|
result = "(bad)";
|
|
|
|
result.append(EscapeString(rep_));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* InternalKeyComparator::Name() const {
|
|
|
|
if (name_.empty()) {
|
|
|
|
return "rocksdb.anonymous.InternalKeyComparator";
|
|
|
|
}
|
|
|
|
return name_.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
int InternalKeyComparator::Compare(const ParsedInternalKey& a,
|
|
|
|
const ParsedInternalKey& b) const {
|
|
|
|
// Order by:
|
|
|
|
// increasing user key (according to user-supplied comparator)
|
|
|
|
// decreasing sequence number
|
|
|
|
// decreasing type (though sequence# should be enough to disambiguate)
|
|
|
|
int r = user_comparator_.Compare(a.user_key, b.user_key);
|
|
|
|
if (r == 0) {
|
|
|
|
if (a.sequence > b.sequence) {
|
|
|
|
r = -1;
|
|
|
|
} else if (a.sequence < b.sequence) {
|
|
|
|
r = +1;
|
|
|
|
} else if (a.type > b.type) {
|
|
|
|
r = -1;
|
|
|
|
} else if (a.type < b.type) {
|
|
|
|
r = +1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalKeyComparator::FindShortestSeparator(std::string* start,
|
|
|
|
const Slice& limit) const {
|
|
|
|
// Attempt to shorten the user portion of the key
|
|
|
|
Slice user_start = ExtractUserKey(*start);
|
|
|
|
Slice user_limit = ExtractUserKey(limit);
|
|
|
|
std::string tmp(user_start.data(), user_start.size());
|
|
|
|
user_comparator_.FindShortestSeparator(&tmp, user_limit);
|
|
|
|
if (tmp.size() <= user_start.size() &&
|
|
|
|
user_comparator_.Compare(user_start, tmp) < 0) {
|
|
|
|
// User key has become shorter physically, but larger logically.
|
|
|
|
// Tack on the earliest possible number to the shortened user key.
|
|
|
|
PutFixed64(&tmp,
|
|
|
|
PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
|
|
|
|
assert(this->Compare(*start, tmp) < 0);
|
|
|
|
assert(this->Compare(tmp, limit) < 0);
|
|
|
|
start->swap(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
|
|
|
|
Slice user_key = ExtractUserKey(*key);
|
|
|
|
std::string tmp(user_key.data(), user_key.size());
|
|
|
|
user_comparator_.FindShortSuccessor(&tmp);
|
|
|
|
if (tmp.size() <= user_key.size() &&
|
|
|
|
user_comparator_.Compare(user_key, tmp) < 0) {
|
|
|
|
// User key has become shorter physically, but larger logically.
|
|
|
|
// Tack on the earliest possible number to the shortened user key.
|
|
|
|
PutFixed64(&tmp,
|
|
|
|
PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
|
|
|
|
assert(this->Compare(*key, tmp) < 0);
|
|
|
|
key->swap(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupKey::LookupKey(const Slice& _user_key, SequenceNumber s,
|
|
|
|
const Slice* ts) {
|
|
|
|
size_t usize = _user_key.size();
|
|
|
|
size_t ts_sz = (nullptr == ts) ? 0 : ts->size();
|
|
|
|
size_t needed = usize + ts_sz + 13; // A conservative estimate
|
|
|
|
char* dst;
|
|
|
|
if (needed <= sizeof(space_)) {
|
|
|
|
dst = space_;
|
|
|
|
} else {
|
|
|
|
dst = new char[needed];
|
|
|
|
}
|
|
|
|
start_ = dst;
|
|
|
|
// NOTE: We don't support users keys of more than 2GB :)
|
|
|
|
dst = EncodeVarint32(dst, static_cast<uint32_t>(usize + ts_sz + 8));
|
|
|
|
kstart_ = dst;
|
|
|
|
memcpy(dst, _user_key.data(), usize);
|
|
|
|
dst += usize;
|
|
|
|
if (nullptr != ts) {
|
|
|
|
memcpy(dst, ts->data(), ts_sz);
|
|
|
|
dst += ts_sz;
|
|
|
|
}
|
|
|
|
EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
|
|
|
|
dst += 8;
|
|
|
|
end_ = dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IterKey::EnlargeBuffer(size_t key_size) {
|
|
|
|
// If size is smaller than buffer size, continue using current buffer,
|
|
|
|
// or the static allocated one, as default
|
|
|
|
assert(key_size > buf_size_);
|
|
|
|
// Need to enlarge the buffer.
|
|
|
|
ResetBuffer();
|
|
|
|
buf_ = new char[key_size];
|
|
|
|
buf_size_ = key_size;
|
|
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|