2018-08-01 07:14:43 +00: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
|
|
|
|
|
2021-08-12 02:31:44 +00:00
|
|
|
#include <atomic>
|
2018-08-01 07:14:43 +00:00
|
|
|
#include <memory>
|
2021-08-12 02:31:44 +00:00
|
|
|
#include <mutex>
|
2018-08-01 07:14:43 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-11-27 22:24:24 +00:00
|
|
|
#include "rocksdb/options.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
|
|
#include "rocksdb/status.h"
|
2021-08-12 02:31:44 +00:00
|
|
|
#include "rocksdb/trace_record.h"
|
|
|
|
#include "rocksdb/utilities/replayer.h"
|
2018-08-01 07:14:43 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2018-08-01 07:14:43 +00:00
|
|
|
|
2019-05-24 23:55:53 +00:00
|
|
|
// This file contains Tracer and Replayer classes that enable capturing and
|
|
|
|
// replaying RocksDB traces.
|
|
|
|
|
2018-08-01 07:14:43 +00:00
|
|
|
class ColumnFamilyHandle;
|
2018-08-11 00:56:11 +00:00
|
|
|
class ColumnFamilyData;
|
2018-08-01 07:14:43 +00:00
|
|
|
class DB;
|
|
|
|
class DBImpl;
|
2021-01-26 06:07:26 +00:00
|
|
|
class Env;
|
2018-08-01 07:14:43 +00:00
|
|
|
class Slice;
|
2021-01-26 06:07:26 +00:00
|
|
|
class SystemClock;
|
|
|
|
class TraceReader;
|
|
|
|
class TraceWriter;
|
2018-08-01 07:14:43 +00:00
|
|
|
class WriteBatch;
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
struct ReadOptions;
|
|
|
|
struct TraceOptions;
|
|
|
|
struct WriteOptions;
|
|
|
|
|
2018-11-27 05:30:12 +00:00
|
|
|
extern const std::string kTraceMagic;
|
2018-08-01 07:14:43 +00: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 07:03:49 +00:00
|
|
|
static const int kTraceFileMajorVersion = 0;
|
|
|
|
static const int kTraceFileMinorVersion = 2;
|
|
|
|
|
2019-05-24 23:55:53 +00:00
|
|
|
// The data structure that defines a single trace.
|
2018-08-01 07:14:43 +00:00
|
|
|
struct Trace {
|
2019-05-24 23:55:53 +00:00
|
|
|
uint64_t ts; // timestamp
|
2018-08-01 07:14:43 +00:00
|
|
|
TraceType type;
|
2021-02-19 07:03:49 +00: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 07:14:43 +00:00
|
|
|
std::string payload;
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
ts = 0;
|
|
|
|
type = kTraceMax;
|
2021-02-19 07:03:49 +00:00
|
|
|
payload_map = 0;
|
2018-08-01 07:14:43 +00:00
|
|
|
payload.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-19 07:03:49 +00: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,
|
2021-06-18 22:02:59 +00:00
|
|
|
kMultiGetSize = 8,
|
|
|
|
kMultiGetCFIDs = 9,
|
|
|
|
kMultiGetKeys = 10,
|
2021-02-19 07:03:49 +00:00
|
|
|
};
|
|
|
|
|
2019-06-06 18:21:11 +00:00
|
|
|
class TracerHelper {
|
|
|
|
public:
|
2021-02-19 07:03:49 +00: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 18:21:11 +00: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 07:03:49 +00:00
|
|
|
|
2021-08-12 02:31:44 +00:00
|
|
|
// Decode a string into the given trace header.
|
|
|
|
static Status DecodeHeader(const std::string& encoded_trace, Trace* header);
|
|
|
|
|
2021-02-19 07:03:49 +00:00
|
|
|
// Set the payload map based on the payload type
|
|
|
|
static bool SetPayloadMap(uint64_t& payload_map,
|
|
|
|
const TracePayloadType payload_type);
|
|
|
|
|
2021-08-24 00:17:13 +00:00
|
|
|
// Decode a Trace object into the corresponding TraceRecord.
|
|
|
|
// Return Status::OK() if nothing is wrong, record will be set accordingly.
|
|
|
|
// Return Status::NotSupported() if the trace type is not support, or the
|
|
|
|
// corresponding error status, record will be set to nullptr.
|
|
|
|
static Status DecodeTraceRecord(Trace* trace, int trace_file_version,
|
2021-08-12 02:31:44 +00:00
|
|
|
std::unique_ptr<TraceRecord>* record);
|
2019-06-06 18:21:11 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 23:55:53 +00: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 07:14:43 +00:00
|
|
|
class Tracer {
|
|
|
|
public:
|
2021-03-15 11:32:24 +00:00
|
|
|
Tracer(SystemClock* clock, const TraceOptions& trace_options,
|
2018-11-27 22:24:24 +00:00
|
|
|
std::unique_ptr<TraceWriter>&& trace_writer);
|
2018-08-01 07:14:43 +00:00
|
|
|
~Tracer();
|
|
|
|
|
2019-05-24 23:55:53 +00:00
|
|
|
// Trace all write operations -- Put, Merge, Delete, SingleDelete, Write
|
2018-08-01 07:14:43 +00:00
|
|
|
Status Write(WriteBatch* write_batch);
|
2019-05-24 23:55:53 +00:00
|
|
|
|
|
|
|
// Trace Get operations.
|
2018-08-01 07:14:43 +00:00
|
|
|
Status Get(ColumnFamilyHandle* cfname, const Slice& key);
|
2019-05-24 23:55:53 +00:00
|
|
|
|
|
|
|
// Trace Iterators.
|
2021-02-19 07:03:49 +00: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-24 23:55:53 +00:00
|
|
|
|
2021-06-18 22:02:59 +00:00
|
|
|
// Trace MultiGet
|
|
|
|
|
|
|
|
Status MultiGet(const size_t num_keys, ColumnFamilyHandle** column_families,
|
|
|
|
const Slice* keys);
|
|
|
|
|
|
|
|
Status MultiGet(const size_t num_keys, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice* keys);
|
|
|
|
|
|
|
|
Status MultiGet(const std::vector<ColumnFamilyHandle*>& column_family,
|
|
|
|
const std::vector<Slice>& keys);
|
|
|
|
|
2019-05-24 23:55:53 +00:00
|
|
|
// Returns true if the trace is over the configured max trace file limit.
|
|
|
|
// False otherwise.
|
2018-11-27 22:24:24 +00:00
|
|
|
bool IsTraceFileOverMax();
|
2018-08-01 07:14:43 +00:00
|
|
|
|
2021-12-28 23:03:03 +00:00
|
|
|
// Returns true if the order of write trace records must match the order of
|
|
|
|
// the corresponding records logged to WAL and applied to the DB.
|
|
|
|
bool IsWriteOrderPreserved() { return trace_options_.preserve_write_order; }
|
|
|
|
|
2019-05-24 23:55:53 +00:00
|
|
|
// Writes a trace footer at the end of the tracing
|
2018-08-01 07:14:43 +00:00
|
|
|
Status Close();
|
|
|
|
|
|
|
|
private:
|
2019-05-24 23:55:53 +00: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 07:14:43 +00:00
|
|
|
Status WriteHeader();
|
2019-05-24 23:55:53 +00:00
|
|
|
|
|
|
|
// Write a trace footer, typically on ending a trace, with some metadata.
|
2018-08-01 07:14:43 +00:00
|
|
|
Status WriteFooter();
|
2019-05-24 23:55:53 +00:00
|
|
|
|
|
|
|
// Write a single trace using the provided TraceWriter to the underlying
|
|
|
|
// system, say, a filesystem or a streaming service.
|
2018-08-01 07:14:43 +00:00
|
|
|
Status WriteTrace(const Trace& trace);
|
2019-05-24 23:55:53 +00:00
|
|
|
|
|
|
|
// Helps in filtering and sampling of traces.
|
|
|
|
// Returns true if a trace should be skipped, false otherwise.
|
2019-03-19 21:19:01 +00:00
|
|
|
bool ShouldSkipTrace(const TraceType& type);
|
2018-08-01 07:14:43 +00:00
|
|
|
|
2021-03-15 11:32:24 +00:00
|
|
|
SystemClock* clock_;
|
2018-11-27 22:24:24 +00:00
|
|
|
TraceOptions trace_options_;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<TraceWriter> trace_writer_;
|
2019-02-09 01:29:41 +00:00
|
|
|
uint64_t trace_request_count_;
|
2018-08-01 07:14:43 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|