2018-08-01 09:14:43 +02:00
|
|
|
// 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).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-11-27 23:24:24 +01:00
|
|
|
#include "rocksdb/options.h"
|
2021-01-26 07:07:26 +01:00
|
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
|
|
#include "rocksdb/status.h"
|
2018-08-01 09:14:43 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2018-08-01 09:14:43 +02:00
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// This file contains Tracer and Replayer classes that enable capturing and
|
|
|
|
// replaying RocksDB traces.
|
|
|
|
|
2018-08-01 09:14:43 +02:00
|
|
|
class ColumnFamilyHandle;
|
2018-08-11 02:56:11 +02:00
|
|
|
class ColumnFamilyData;
|
2018-08-01 09:14:43 +02:00
|
|
|
class DB;
|
|
|
|
class DBImpl;
|
2021-01-26 07:07:26 +01:00
|
|
|
class Env;
|
2018-08-01 09:14:43 +02:00
|
|
|
class Slice;
|
2021-01-26 07:07:26 +01:00
|
|
|
class SystemClock;
|
|
|
|
class TraceReader;
|
|
|
|
class TraceWriter;
|
2018-08-01 09:14:43 +02:00
|
|
|
class WriteBatch;
|
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
struct ReadOptions;
|
|
|
|
struct TraceOptions;
|
|
|
|
struct WriteOptions;
|
|
|
|
|
2018-11-27 06:30:12 +01:00
|
|
|
extern const std::string kTraceMagic;
|
2018-08-01 09:14:43 +02:00
|
|
|
const unsigned int kTraceTimestampSize = 8;
|
|
|
|
const unsigned int kTraceTypeSize = 1;
|
|
|
|
const unsigned int kTracePayloadLengthSize = 4;
|
|
|
|
const unsigned int kTraceMetadataSize =
|
|
|
|
kTraceTimestampSize + kTraceTypeSize + kTracePayloadLengthSize;
|
|
|
|
|
2021-02-19 08:03:49 +01:00
|
|
|
static const int kTraceFileMajorVersion = 0;
|
|
|
|
static const int kTraceFileMinorVersion = 2;
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Supported Trace types.
|
2018-08-01 09:14:43 +02:00
|
|
|
enum TraceType : char {
|
|
|
|
kTraceBegin = 1,
|
|
|
|
kTraceEnd = 2,
|
|
|
|
kTraceWrite = 3,
|
|
|
|
kTraceGet = 4,
|
2018-08-11 02:56:11 +02:00
|
|
|
kTraceIteratorSeek = 5,
|
|
|
|
kTraceIteratorSeekForPrev = 6,
|
2019-06-06 20:21:11 +02:00
|
|
|
// Block cache related types.
|
|
|
|
kBlockTraceIndexBlock = 7,
|
|
|
|
kBlockTraceFilterBlock = 8,
|
|
|
|
kBlockTraceDataBlock = 9,
|
|
|
|
kBlockTraceUncompressionDictBlock = 10,
|
|
|
|
kBlockTraceRangeDeletionBlock = 11,
|
2021-01-25 23:35:45 +01:00
|
|
|
// For IOTracing.
|
|
|
|
kIOTracer = 12,
|
2019-05-25 01:55:53 +02:00
|
|
|
// All trace types should be added before kTraceMax
|
2018-08-01 09:14:43 +02:00
|
|
|
kTraceMax,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: This should also be made part of public interface to help users build
|
|
|
|
// custom TracerReaders and TraceWriters.
|
2019-05-25 01:55:53 +02:00
|
|
|
//
|
|
|
|
// The data structure that defines a single trace.
|
2018-08-01 09:14:43 +02:00
|
|
|
struct Trace {
|
2019-05-25 01:55:53 +02:00
|
|
|
uint64_t ts; // timestamp
|
2018-08-01 09:14:43 +02:00
|
|
|
TraceType type;
|
2021-02-19 08:03:49 +01:00
|
|
|
// Each bit in payload_map stores which corresponding struct member added in
|
|
|
|
// the payload. Each TraceType has its corresponding payload struct. For
|
|
|
|
// example, if bit at position 0 is set in write payload, then the write batch
|
|
|
|
// will be addedd.
|
|
|
|
uint64_t payload_map = 0;
|
|
|
|
// Each trace type has its own payload_struct, which will be serilized in the
|
|
|
|
// payload.
|
2018-08-01 09:14:43 +02:00
|
|
|
std::string payload;
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
ts = 0;
|
|
|
|
type = kTraceMax;
|
2021-02-19 08:03:49 +01:00
|
|
|
payload_map = 0;
|
2018-08-01 09:14:43 +02:00
|
|
|
payload.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-19 08:03:49 +01:00
|
|
|
enum TracePayloadType : char {
|
|
|
|
// Each member of all query payload structs should have a corresponding flag
|
|
|
|
// here. Make sure to add them sequentially in the order of it is added.
|
|
|
|
kEmptyPayload = 0,
|
|
|
|
kWriteBatchData = 1,
|
|
|
|
kGetCFID = 2,
|
|
|
|
kGetKey = 3,
|
|
|
|
kIterCFID = 4,
|
|
|
|
kIterKey = 5,
|
|
|
|
kIterLowerBound = 6,
|
|
|
|
kIterUpperBound = 7,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct WritePayload {
|
|
|
|
Slice write_batch_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GetPayload {
|
2021-03-27 01:47:32 +01:00
|
|
|
uint32_t cf_id = 0;
|
2021-02-19 08:03:49 +01:00
|
|
|
Slice get_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IterPayload {
|
2021-03-27 01:47:32 +01:00
|
|
|
uint32_t cf_id = 0;
|
2021-02-19 08:03:49 +01:00
|
|
|
Slice iter_key;
|
|
|
|
Slice lower_bound;
|
|
|
|
Slice upper_bound;
|
|
|
|
};
|
|
|
|
|
2019-06-06 20:21:11 +02:00
|
|
|
class TracerHelper {
|
|
|
|
public:
|
2021-02-19 08:03:49 +01:00
|
|
|
// Parse the string with major and minor version only
|
|
|
|
static Status ParseVersionStr(std::string& v_string, int* v_num);
|
|
|
|
|
|
|
|
// Parse the trace file version and db version in trace header
|
|
|
|
static Status ParseTraceHeader(const Trace& header, int* trace_version,
|
|
|
|
int* db_version);
|
|
|
|
|
|
|
|
// Encode a version 0.1 trace object into the given string.
|
2019-06-06 20:21:11 +02:00
|
|
|
static void EncodeTrace(const Trace& trace, std::string* encoded_trace);
|
|
|
|
|
|
|
|
// Decode a string into the given trace object.
|
|
|
|
static Status DecodeTrace(const std::string& encoded_trace, Trace* trace);
|
2021-02-19 08:03:49 +01:00
|
|
|
|
|
|
|
// Set the payload map based on the payload type
|
|
|
|
static bool SetPayloadMap(uint64_t& payload_map,
|
|
|
|
const TracePayloadType payload_type);
|
|
|
|
|
|
|
|
// Decode the write payload and store in WrteiPayload
|
|
|
|
static void DecodeWritePayload(Trace* trace, WritePayload* write_payload);
|
|
|
|
|
|
|
|
// Decode the get payload and store in WrteiPayload
|
|
|
|
static void DecodeGetPayload(Trace* trace, GetPayload* get_payload);
|
|
|
|
|
|
|
|
// Decode the iter payload and store in WrteiPayload
|
|
|
|
static void DecodeIterPayload(Trace* trace, IterPayload* iter_payload);
|
2019-06-06 20:21:11 +02:00
|
|
|
};
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Tracer captures all RocksDB operations using a user-provided TraceWriter.
|
|
|
|
// Every RocksDB operation is written as a single trace. Each trace will have a
|
|
|
|
// timestamp and type, followed by the trace payload.
|
2018-08-01 09:14:43 +02:00
|
|
|
class Tracer {
|
|
|
|
public:
|
2021-03-15 12:32:24 +01:00
|
|
|
Tracer(SystemClock* clock, const TraceOptions& trace_options,
|
2018-11-27 23:24:24 +01:00
|
|
|
std::unique_ptr<TraceWriter>&& trace_writer);
|
2018-08-01 09:14:43 +02:00
|
|
|
~Tracer();
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Trace all write operations -- Put, Merge, Delete, SingleDelete, Write
|
2018-08-01 09:14:43 +02:00
|
|
|
Status Write(WriteBatch* write_batch);
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Trace Get operations.
|
2018-08-01 09:14:43 +02:00
|
|
|
Status Get(ColumnFamilyHandle* cfname, const Slice& key);
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Trace Iterators.
|
2021-02-19 08:03:49 +01:00
|
|
|
Status IteratorSeek(const uint32_t& cf_id, const Slice& key,
|
|
|
|
const Slice& lower_bound, const Slice upper_bound);
|
|
|
|
Status IteratorSeekForPrev(const uint32_t& cf_id, const Slice& key,
|
|
|
|
const Slice& lower_bound, const Slice upper_bound);
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Returns true if the trace is over the configured max trace file limit.
|
|
|
|
// False otherwise.
|
2018-11-27 23:24:24 +01:00
|
|
|
bool IsTraceFileOverMax();
|
2018-08-01 09:14:43 +02:00
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Writes a trace footer at the end of the tracing
|
2018-08-01 09:14:43 +02:00
|
|
|
Status Close();
|
|
|
|
|
|
|
|
private:
|
2019-05-25 01:55:53 +02:00
|
|
|
// Write a trace header at the beginning, typically on initiating a trace,
|
|
|
|
// with some metadata like a magic number, trace version, RocksDB version, and
|
|
|
|
// trace format.
|
2018-08-01 09:14:43 +02:00
|
|
|
Status WriteHeader();
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Write a trace footer, typically on ending a trace, with some metadata.
|
2018-08-01 09:14:43 +02:00
|
|
|
Status WriteFooter();
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Write a single trace using the provided TraceWriter to the underlying
|
|
|
|
// system, say, a filesystem or a streaming service.
|
2018-08-01 09:14:43 +02:00
|
|
|
Status WriteTrace(const Trace& trace);
|
2019-05-25 01:55:53 +02:00
|
|
|
|
|
|
|
// Helps in filtering and sampling of traces.
|
|
|
|
// Returns true if a trace should be skipped, false otherwise.
|
2019-03-19 22:19:01 +01:00
|
|
|
bool ShouldSkipTrace(const TraceType& type);
|
2018-08-01 09:14:43 +02:00
|
|
|
|
2021-03-15 12:32:24 +01:00
|
|
|
SystemClock* clock_;
|
2018-11-27 23:24:24 +01:00
|
|
|
TraceOptions trace_options_;
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<TraceWriter> trace_writer_;
|
2019-02-09 02:29:41 +01:00
|
|
|
uint64_t trace_request_count_;
|
2018-08-01 09:14:43 +02:00
|
|
|
};
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Replayer helps to replay the captured RocksDB operations, using a user
|
|
|
|
// provided TraceReader.
|
|
|
|
// The Replayer is instantiated via db_bench today, on using "replay" benchmark.
|
2018-08-01 09:14:43 +02:00
|
|
|
class Replayer {
|
|
|
|
public:
|
|
|
|
Replayer(DB* db, const std::vector<ColumnFamilyHandle*>& handles,
|
|
|
|
std::unique_ptr<TraceReader>&& reader);
|
|
|
|
~Replayer();
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Replay all the traces from the provided trace stream, taking the delay
|
|
|
|
// between the traces into consideration.
|
2018-08-01 09:14:43 +02:00
|
|
|
Status Replay();
|
2019-05-25 01:55:53 +02:00
|
|
|
|
2019-10-18 23:12:21 +02:00
|
|
|
// Replay the provide trace stream, which is the same as Replay(), with
|
|
|
|
// multi-threads. Queries are scheduled in the thread pool job queue.
|
|
|
|
// User can set the number of threads in the thread pool.
|
|
|
|
Status MultiThreadReplay(uint32_t threads_num);
|
|
|
|
|
2019-05-25 01:55:53 +02:00
|
|
|
// Enables fast forwarding a replay by reducing the delay between the ingested
|
|
|
|
// traces.
|
|
|
|
// fast_forward : Rate of replay speedup.
|
|
|
|
// If 1, replay the operations at the same rate as in the trace stream.
|
|
|
|
// If > 1, speed up the replay by this amount.
|
2019-05-17 05:18:33 +02:00
|
|
|
Status SetFastForward(uint32_t fast_forward);
|
2018-08-01 09:14:43 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Status ReadHeader(Trace* header);
|
|
|
|
Status ReadFooter(Trace* footer);
|
|
|
|
Status ReadTrace(Trace* trace);
|
|
|
|
|
2019-10-18 23:12:21 +02:00
|
|
|
// The background function for MultiThreadReplay to execute Get query
|
|
|
|
// based on the trace records.
|
|
|
|
static void BGWorkGet(void* arg);
|
|
|
|
|
|
|
|
// The background function for MultiThreadReplay to execute WriteBatch
|
|
|
|
// (Put, Delete, SingleDelete, DeleteRange) based on the trace records.
|
|
|
|
static void BGWorkWriteBatch(void* arg);
|
|
|
|
|
|
|
|
// The background function for MultiThreadReplay to execute Iterator (Seek)
|
|
|
|
// based on the trace records.
|
|
|
|
static void BGWorkIterSeek(void* arg);
|
|
|
|
|
|
|
|
// The background function for MultiThreadReplay to execute Iterator
|
|
|
|
// (SeekForPrev) based on the trace records.
|
|
|
|
static void BGWorkIterSeekForPrev(void* arg);
|
|
|
|
|
2018-08-01 09:14:43 +02:00
|
|
|
DBImpl* db_;
|
2019-10-18 23:12:21 +02:00
|
|
|
Env* env_;
|
2018-08-01 09:14:43 +02:00
|
|
|
std::unique_ptr<TraceReader> trace_reader_;
|
|
|
|
std::unordered_map<uint32_t, ColumnFamilyHandle*> cf_map_;
|
2019-05-17 05:18:33 +02:00
|
|
|
uint32_t fast_forward_;
|
2021-02-19 08:03:49 +01:00
|
|
|
// When reading the trace header, the trace file version can be parsed.
|
|
|
|
// Replayer will use different decode method to get the trace content based
|
|
|
|
// on different trace file version.
|
|
|
|
int trace_file_version_;
|
2018-08-01 09:14:43 +02:00
|
|
|
};
|
|
|
|
|
2019-10-18 23:12:21 +02:00
|
|
|
// The passin arg of MultiThreadRepkay for each trace record.
|
|
|
|
struct ReplayerWorkerArg {
|
|
|
|
DB* db;
|
|
|
|
Trace trace_entry;
|
|
|
|
std::unordered_map<uint32_t, ColumnFamilyHandle*>* cf_map;
|
|
|
|
WriteOptions woptions;
|
|
|
|
ReadOptions roptions;
|
2021-02-19 08:03:49 +01:00
|
|
|
int trace_file_version;
|
2019-10-18 23:12:21 +02:00
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|