fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
4.2 KiB
144 lines
4.2 KiB
6 years ago
|
// 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>
|
||
|
|
||
|
#include "rocksdb/env.h"
|
||
6 years ago
|
#include "rocksdb/options.h"
|
||
6 years ago
|
#include "rocksdb/trace_reader_writer.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
|
||
6 years ago
|
// This file contains Tracer and Replayer classes that enable capturing and
|
||
|
// replaying RocksDB traces.
|
||
|
|
||
6 years ago
|
class ColumnFamilyHandle;
|
||
6 years ago
|
class ColumnFamilyData;
|
||
6 years ago
|
class DB;
|
||
|
class DBImpl;
|
||
|
class Slice;
|
||
|
class WriteBatch;
|
||
|
|
||
6 years ago
|
extern const std::string kTraceMagic;
|
||
6 years ago
|
const unsigned int kTraceTimestampSize = 8;
|
||
|
const unsigned int kTraceTypeSize = 1;
|
||
|
const unsigned int kTracePayloadLengthSize = 4;
|
||
|
const unsigned int kTraceMetadataSize =
|
||
|
kTraceTimestampSize + kTraceTypeSize + kTracePayloadLengthSize;
|
||
|
|
||
6 years ago
|
// Supported Trace types.
|
||
6 years ago
|
enum TraceType : char {
|
||
|
kTraceBegin = 1,
|
||
|
kTraceEnd = 2,
|
||
|
kTraceWrite = 3,
|
||
|
kTraceGet = 4,
|
||
6 years ago
|
kTraceIteratorSeek = 5,
|
||
|
kTraceIteratorSeekForPrev = 6,
|
||
6 years ago
|
// All trace types should be added before kTraceMax
|
||
6 years ago
|
kTraceMax,
|
||
|
};
|
||
|
|
||
|
// TODO: This should also be made part of public interface to help users build
|
||
|
// custom TracerReaders and TraceWriters.
|
||
6 years ago
|
//
|
||
|
// The data structure that defines a single trace.
|
||
6 years ago
|
struct Trace {
|
||
6 years ago
|
uint64_t ts; // timestamp
|
||
6 years ago
|
TraceType type;
|
||
|
std::string payload;
|
||
|
|
||
|
void reset() {
|
||
|
ts = 0;
|
||
|
type = kTraceMax;
|
||
|
payload.clear();
|
||
|
}
|
||
|
};
|
||
|
|
||
6 years ago
|
// 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.
|
||
6 years ago
|
class Tracer {
|
||
|
public:
|
||
6 years ago
|
Tracer(Env* env, const TraceOptions& trace_options,
|
||
|
std::unique_ptr<TraceWriter>&& trace_writer);
|
||
6 years ago
|
~Tracer();
|
||
|
|
||
6 years ago
|
// Trace all write operations -- Put, Merge, Delete, SingleDelete, Write
|
||
6 years ago
|
Status Write(WriteBatch* write_batch);
|
||
6 years ago
|
|
||
|
// Trace Get operations.
|
||
6 years ago
|
Status Get(ColumnFamilyHandle* cfname, const Slice& key);
|
||
6 years ago
|
|
||
|
// Trace Iterators.
|
||
6 years ago
|
Status IteratorSeek(const uint32_t& cf_id, const Slice& key);
|
||
|
Status IteratorSeekForPrev(const uint32_t& cf_id, const Slice& key);
|
||
6 years ago
|
|
||
|
// Returns true if the trace is over the configured max trace file limit.
|
||
|
// False otherwise.
|
||
6 years ago
|
bool IsTraceFileOverMax();
|
||
6 years ago
|
|
||
6 years ago
|
// Writes a trace footer at the end of the tracing
|
||
6 years ago
|
Status Close();
|
||
|
|
||
|
private:
|
||
6 years ago
|
// 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.
|
||
6 years ago
|
Status WriteHeader();
|
||
6 years ago
|
|
||
|
// Write a trace footer, typically on ending a trace, with some metadata.
|
||
6 years ago
|
Status WriteFooter();
|
||
6 years ago
|
|
||
|
// Write a single trace using the provided TraceWriter to the underlying
|
||
|
// system, say, a filesystem or a streaming service.
|
||
6 years ago
|
Status WriteTrace(const Trace& trace);
|
||
6 years ago
|
|
||
|
// Helps in filtering and sampling of traces.
|
||
|
// Returns true if a trace should be skipped, false otherwise.
|
||
6 years ago
|
bool ShouldSkipTrace(const TraceType& type);
|
||
6 years ago
|
|
||
|
Env* env_;
|
||
6 years ago
|
TraceOptions trace_options_;
|
||
6 years ago
|
std::unique_ptr<TraceWriter> trace_writer_;
|
||
6 years ago
|
uint64_t trace_request_count_;
|
||
6 years ago
|
};
|
||
|
|
||
6 years ago
|
// 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.
|
||
6 years ago
|
class Replayer {
|
||
|
public:
|
||
|
Replayer(DB* db, const std::vector<ColumnFamilyHandle*>& handles,
|
||
|
std::unique_ptr<TraceReader>&& reader);
|
||
|
~Replayer();
|
||
|
|
||
6 years ago
|
// Replay all the traces from the provided trace stream, taking the delay
|
||
|
// between the traces into consideration.
|
||
6 years ago
|
Status Replay();
|
||
6 years ago
|
|
||
|
// 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.
|
||
6 years ago
|
Status SetFastForward(uint32_t fast_forward);
|
||
6 years ago
|
|
||
|
private:
|
||
|
Status ReadHeader(Trace* header);
|
||
|
Status ReadFooter(Trace* footer);
|
||
|
Status ReadTrace(Trace* trace);
|
||
|
|
||
|
DBImpl* db_;
|
||
|
std::unique_ptr<TraceReader> trace_reader_;
|
||
|
std::unordered_map<uint32_t, ColumnFamilyHandle*> cf_map_;
|
||
6 years ago
|
uint32_t fast_forward_;
|
||
6 years ago
|
};
|
||
|
|
||
|
} // namespace rocksdb
|