]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/trace_replay.h
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / trace_replay.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #pragma once
7
8 #include <memory>
9 #include <unordered_map>
10 #include <utility>
11
12 #include "rocksdb/env.h"
13 #include "rocksdb/options.h"
14 #include "rocksdb/trace_reader_writer.h"
15
16 namespace rocksdb {
17
18 class ColumnFamilyHandle;
19 class ColumnFamilyData;
20 class DB;
21 class DBImpl;
22 class Slice;
23 class WriteBatch;
24
25 extern const std::string kTraceMagic;
26 const unsigned int kTraceTimestampSize = 8;
27 const unsigned int kTraceTypeSize = 1;
28 const unsigned int kTracePayloadLengthSize = 4;
29 const unsigned int kTraceMetadataSize =
30 kTraceTimestampSize + kTraceTypeSize + kTracePayloadLengthSize;
31
32 enum TraceType : char {
33 kTraceBegin = 1,
34 kTraceEnd = 2,
35 kTraceWrite = 3,
36 kTraceGet = 4,
37 kTraceIteratorSeek = 5,
38 kTraceIteratorSeekForPrev = 6,
39 kTraceMax,
40 };
41
42 // TODO: This should also be made part of public interface to help users build
43 // custom TracerReaders and TraceWriters.
44 struct Trace {
45 uint64_t ts;
46 TraceType type;
47 std::string payload;
48
49 void reset() {
50 ts = 0;
51 type = kTraceMax;
52 payload.clear();
53 }
54 };
55
56 // Trace RocksDB operations using a TraceWriter.
57 class Tracer {
58 public:
59 Tracer(Env* env, const TraceOptions& trace_options,
60 std::unique_ptr<TraceWriter>&& trace_writer);
61 ~Tracer();
62
63 Status Write(WriteBatch* write_batch);
64 Status Get(ColumnFamilyHandle* cfname, const Slice& key);
65 Status IteratorSeek(const uint32_t& cf_id, const Slice& key);
66 Status IteratorSeekForPrev(const uint32_t& cf_id, const Slice& key);
67 bool IsTraceFileOverMax();
68
69 Status Close();
70
71 private:
72 Status WriteHeader();
73 Status WriteFooter();
74 Status WriteTrace(const Trace& trace);
75 bool ShouldSkipTrace(const TraceType& type);
76
77 Env* env_;
78 TraceOptions trace_options_;
79 std::unique_ptr<TraceWriter> trace_writer_;
80 uint64_t trace_request_count_;
81 };
82
83 // Replay RocksDB operations from a trace.
84 class Replayer {
85 public:
86 Replayer(DB* db, const std::vector<ColumnFamilyHandle*>& handles,
87 std::unique_ptr<TraceReader>&& reader);
88 ~Replayer();
89
90 Status Replay();
91
92 private:
93 Status ReadHeader(Trace* header);
94 Status ReadFooter(Trace* footer);
95 Status ReadTrace(Trace* trace);
96
97 DBImpl* db_;
98 std::unique_ptr<TraceReader> trace_reader_;
99 std::unordered_map<uint32_t, ColumnFamilyHandle*> cf_map_;
100 };
101
102 } // namespace rocksdb