]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/trace_replay.h
update sources to ceph Nautilus 14.2.1
[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/trace_reader_writer.h"
14
15 namespace rocksdb {
16
17 class ColumnFamilyHandle;
18 class ColumnFamilyData;
19 class DB;
20 class DBImpl;
21 class Slice;
22 class WriteBatch;
23
24 const std::string kTraceMagic = "feedcafedeadbeef";
25 const unsigned int kTraceTimestampSize = 8;
26 const unsigned int kTraceTypeSize = 1;
27 const unsigned int kTracePayloadLengthSize = 4;
28 const unsigned int kTraceMetadataSize =
29 kTraceTimestampSize + kTraceTypeSize + kTracePayloadLengthSize;
30
31 enum TraceType : char {
32 kTraceBegin = 1,
33 kTraceEnd = 2,
34 kTraceWrite = 3,
35 kTraceGet = 4,
36 kTraceIteratorSeek = 5,
37 kTraceIteratorSeekForPrev = 6,
38 kTraceMax,
39 };
40
41 // TODO: This should also be made part of public interface to help users build
42 // custom TracerReaders and TraceWriters.
43 struct Trace {
44 uint64_t ts;
45 TraceType type;
46 std::string payload;
47
48 void reset() {
49 ts = 0;
50 type = kTraceMax;
51 payload.clear();
52 }
53 };
54
55 // Trace RocksDB operations using a TraceWriter.
56 class Tracer {
57 public:
58 Tracer(Env* env, std::unique_ptr<TraceWriter>&& trace_writer);
59 ~Tracer();
60
61 Status Write(WriteBatch* write_batch);
62 Status Get(ColumnFamilyHandle* cfname, const Slice& key);
63 Status IteratorSeek(const uint32_t& cf_id, const Slice& key);
64 Status IteratorSeekForPrev(const uint32_t& cf_id, const Slice& key);
65
66 Status Close();
67
68 private:
69 Status WriteHeader();
70 Status WriteFooter();
71 Status WriteTrace(const Trace& trace);
72
73 Env* env_;
74 unique_ptr<TraceWriter> trace_writer_;
75 };
76
77 // Replay RocksDB operations from a trace.
78 class Replayer {
79 public:
80 Replayer(DB* db, const std::vector<ColumnFamilyHandle*>& handles,
81 std::unique_ptr<TraceReader>&& reader);
82 ~Replayer();
83
84 Status Replay();
85
86 private:
87 Status ReadHeader(Trace* header);
88 Status ReadFooter(Trace* footer);
89 Status ReadTrace(Trace* trace);
90
91 DBImpl* db_;
92 std::unique_ptr<TraceReader> trace_reader_;
93 std::unordered_map<uint32_t, ColumnFamilyHandle*> cf_map_;
94 };
95
96 } // namespace rocksdb