Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/db/transaction_log_impl.h
Line
Count
Source (jump to first uncovered line)
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
#pragma once
6
7
#include <vector>
8
9
#include "db/log_reader.h"
10
#include "db/version_set.h"
11
#include "file/filename.h"
12
#include "logging/logging.h"
13
#include "options/db_options.h"
14
#include "port/port.h"
15
#include "rocksdb/env.h"
16
#include "rocksdb/options.h"
17
#include "rocksdb/transaction_log.h"
18
#include "rocksdb/types.h"
19
20
namespace ROCKSDB_NAMESPACE {
21
22
class WalFileImpl : public WalFile {
23
 public:
24
  WalFileImpl(uint64_t logNum, WalFileType logType, SequenceNumber startSeq,
25
              uint64_t sizeBytes)
26
      : logNumber_(logNum),
27
        type_(logType),
28
        startSequence_(startSeq),
29
0
        sizeFileBytes_(sizeBytes) {}
30
31
0
  std::string PathName() const override {
32
0
    if (type_ == kArchivedLogFile) {
33
0
      return ArchivedLogFileName("", logNumber_);
34
0
    }
35
0
    return LogFileName("", logNumber_);
36
0
  }
37
38
0
  uint64_t LogNumber() const override { return logNumber_; }
39
40
0
  WalFileType Type() const override { return type_; }
41
42
0
  SequenceNumber StartSequence() const override { return startSequence_; }
43
44
0
  uint64_t SizeFileBytes() const override { return sizeFileBytes_; }
45
46
0
  bool operator<(const WalFile& that) const {
47
0
    return LogNumber() < that.LogNumber();
48
0
  }
49
50
 private:
51
  uint64_t logNumber_;
52
  WalFileType type_;
53
  SequenceNumber startSequence_;
54
  uint64_t sizeFileBytes_;
55
};
56
57
class TransactionLogIteratorImpl : public TransactionLogIterator {
58
 public:
59
  TransactionLogIteratorImpl(
60
      const std::string& dir, const ImmutableDBOptions* options,
61
      const TransactionLogIterator::ReadOptions& read_options,
62
      const EnvOptions& soptions, const SequenceNumber seqNum,
63
      std::unique_ptr<VectorWalPtr> files, VersionSet const* const versions,
64
      const bool seq_per_batch, const std::shared_ptr<IOTracer>& io_tracer);
65
66
  bool Valid() override;
67
68
  void Next() override;
69
70
  Status status() override;
71
72
  BatchResult GetBatch() override;
73
74
 private:
75
  const std::string& dir_;
76
  const ImmutableDBOptions* options_;
77
  const TransactionLogIterator::ReadOptions read_options_;
78
  const EnvOptions& soptions_;
79
  SequenceNumber starting_sequence_number_;
80
  std::unique_ptr<VectorWalPtr> files_;
81
  // Used only to get latest seq. num
82
  // TODO(icanadi) can this be just a callback?
83
  VersionSet const* const versions_;
84
  const bool seq_per_batch_;
85
  std::shared_ptr<IOTracer> io_tracer_;
86
87
  // State variables
88
  bool started_;
89
  bool is_valid_;  // not valid when it starts of.
90
  Status current_status_;
91
  size_t current_file_index_;
92
  std::unique_ptr<WriteBatch> current_batch_;
93
  std::unique_ptr<log::Reader> current_log_reader_;
94
  std::string scratch_;
95
  Status OpenLogFile(const WalFile* log_file,
96
                     std::unique_ptr<SequentialFileReader>* file);
97
98
  struct LogReporter : public log::Reader::Reporter {
99
    Env* env;
100
    Logger* info_log;
101
0
    void Corruption(size_t bytes, const Status& s) override {
102
0
      ROCKS_LOG_ERROR(info_log, "dropping %" ROCKSDB_PRIszt " bytes; %s", bytes,
103
0
                      s.ToString().c_str());
104
0
    }
105
0
    virtual void Info(const char* s) { ROCKS_LOG_INFO(info_log, "%s", s); }
106
  } reporter_;
107
108
  SequenceNumber
109
      current_batch_seq_;  // sequence number at start of current batch
110
  SequenceNumber current_last_seq_;  // last sequence in the current batch
111
  // Reads from transaction log only if the writebatch record has been written
112
  bool RestrictedRead(Slice* record);
113
  // Seeks to starting_sequence_number_ reading from start_file_index in files_.
114
  // If strict is set, then must get a batch starting with
115
  // starting_sequence_number_.
116
  void SeekToStartSequence(uint64_t start_file_index = 0, bool strict = false);
117
  // Implementation of Next. SeekToStartSequence calls it internally with
118
  // internal=true to let it find next entry even if it has to jump gaps because
119
  // the iterator may start off from the first available entry but promises to
120
  // be continuous after that
121
  void NextImpl(bool internal = false);
122
  // Check if batch is expected, else return false
123
  bool IsBatchExpected(const WriteBatch* batch, SequenceNumber expected_seq);
124
  // Update current batch if a continuous batch is found.
125
  void UpdateCurrentWriteBatch(const Slice& record);
126
  Status OpenLogReader(const WalFile* file);
127
};
128
}  // namespace ROCKSDB_NAMESPACE