YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/log_writer.cc
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#include "yb/rocksdb/db/log_writer.h"
25
26
#include <stdint.h>
27
#include "yb/rocksdb/env.h"
28
#include "yb/rocksdb/util/coding.h"
29
#include "yb/rocksdb/util/crc32c.h"
30
#include "yb/rocksdb/util/file_reader_writer.h"
31
32
namespace rocksdb {
33
namespace log {
34
35
Writer::Writer(unique_ptr<WritableFileWriter>&& dest,
36
               uint64_t log_number, bool recycle_log_files)
37
    : dest_(std::move(dest)),
38
      block_offset_(0),
39
      log_number_(log_number),
40
953k
      recycle_log_files_(recycle_log_files) {
41
9.52M
  for (int i = 0; i <= kMaxRecordType; i++) {
42
8.57M
    char t = static_cast<char>(i);
43
8.57M
    type_crc_[i] = crc32c::Value(&t, 1);
44
8.57M
  }
45
953k
}
46
47
932k
Writer::~Writer() {
48
932k
}
49
50
20.0M
Status Writer::AddRecord(const Slice& slice) {
51
20.0M
  const char* ptr = slice.cdata();
52
20.0M
  size_t left = slice.size();
53
54
  // Header size varies depending on whether we are recycling or not.
55
20.0M
  const int header_size =
56
19.9M
      recycle_log_files_ ? kRecyclableHeaderSize : kHeaderSize;
57
58
  // Fragment the record if necessary and emit it.  Note that if slice
59
  // is empty, we still want to iterate once to emit a single
60
  // zero-length record
61
20.0M
  Status s;
62
20.0M
  bool begin = true;
63
20.6M
  do {
64
20.6M
    const int64_t leftover = kBlockSize - block_offset_;
65
20.6M
    assert(leftover >= 0);
66
20.6M
    if (leftover < header_size) {
67
      // Switch to a new block
68
617k
      if (leftover > 0) {
69
        // Fill the trailer (literal below relies on kHeaderSize and
70
        // kRecyclableHeaderSize being <= 11)
71
1.52k
        assert(header_size <= 11);
72
1.52k
        RETURN_NOT_OK(dest_->Append(
73
1.52k
            Slice("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", leftover)));
74
1.52k
      }
75
617k
      block_offset_ = 0;
76
617k
    }
77
78
    // Invariant: we never leave < header_size bytes in a block.
79
20.6M
    assert(static_cast<int64_t>(kBlockSize - block_offset_) >= header_size);
80
81
20.6M
    const size_t avail = kBlockSize - block_offset_ - header_size;
82
20.0M
    const size_t fragment_length = (left < avail) ? left : avail;
83
84
20.6M
    RecordType type;
85
20.6M
    const bool end = (left == fragment_length);
86
20.6M
    if (begin && end) {
87
19.5M
      type = recycle_log_files_ ? kRecyclableFullType : kFullType;
88
1.01M
    } else if (begin) {
89
387k
      type = recycle_log_files_ ? kRecyclableFirstType : kFirstType;
90
612k
    } else if (end) {
91
387k
      type = recycle_log_files_ ? kRecyclableLastType : kLastType;
92
215k
    } else {
93
207k
      type = recycle_log_files_ ? kRecyclableMiddleType : kMiddleType;
94
215k
    }
95
96
20.6M
    s = EmitPhysicalRecord(type, ptr, fragment_length);
97
20.6M
    ptr += fragment_length;
98
20.6M
    left -= fragment_length;
99
20.6M
    begin = false;
100
20.6M
  } while (s.ok() && left > 0);
101
20.0M
  return s;
102
20.0M
}
103
104
20.6M
Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, size_t n) {
105
20.6M
  assert(n <= 0xffff);  // Must fit in two bytes
106
107
20.6M
  size_t header_size;
108
20.6M
  char buf[kRecyclableHeaderSize];
109
110
  // Format the header
111
20.6M
  buf[4] = static_cast<char>(n & 0xff);
112
20.6M
  buf[5] = static_cast<char>(n >> 8);
113
20.6M
  buf[6] = static_cast<char>(t);
114
115
20.6M
  uint32_t crc = type_crc_[t];
116
20.6M
  if (t < kRecyclableFullType) {
117
    // Legacy record format
118
20.5M
    assert(block_offset_ + kHeaderSize + n <= kBlockSize);
119
20.5M
    header_size = kHeaderSize;
120
144k
  } else {
121
    // Recyclable record format
122
144k
    assert(block_offset_ + kRecyclableHeaderSize + n <= kBlockSize);
123
144k
    header_size = kRecyclableHeaderSize;
124
125
    // Only encode low 32-bits of the 64-bit log number.  This means
126
    // we will fail to detect an old record if we recycled a log from
127
    // ~4 billion logs ago, but that is effectively impossible, and
128
    // even if it were we'd be far more likely to see a false positive
129
    // on the 32-bit CRC.
130
144k
    EncodeFixed32(buf + 7, static_cast<uint32_t>(log_number_));
131
144k
    crc = crc32c::Extend(crc, buf + 7, 4);
132
144k
  }
133
134
  // Compute the crc of the record type and the payload.
135
20.6M
  crc = crc32c::Extend(crc, ptr, n);
136
20.6M
  crc = crc32c::Mask(crc);  // Adjust for storage
137
20.6M
  EncodeFixed32(buf, crc);
138
139
  // Write the header and the payload
140
20.6M
  Status s = dest_->Append(Slice(buf, header_size));
141
20.6M
  if (s.ok()) {
142
20.6M
    s = dest_->Append(Slice(ptr, n));
143
20.6M
    if (s.ok()) {
144
20.6M
      s = dest_->Flush();
145
20.6M
    }
146
20.6M
  }
147
20.6M
  block_offset_ += header_size + n;
148
20.6M
  return s;
149
20.6M
}
150
151
}  // namespace log
152
}  // namespace rocksdb