Storage Engine API
wiredtiger_record_store_oplog_stones.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <boost/optional.hpp>
33 
35 #include "mongo/platform/atomic_word.h"
36 #include "mongo/stdx/condition_variable.h"
37 #include "mongo/stdx/mutex.h"
38 
39 namespace mongo {
40 
41 class OperationContext;
42 class RecordId;
43 
44 // Keep "milestones" against the oplog to efficiently remove the old records when the collection
45 // grows beyond its desired maximum size.
47 public:
48  struct Stone {
49  int64_t records; // Approximate number of records in a chunk of the oplog.
50  int64_t bytes; // Approximate size of records in a chunk of the oplog.
51  RecordId lastRecord; // RecordId of the last record in a chunk of the oplog.
52  };
53 
54  OplogStones(OperationContext* opCtx, WiredTigerRecordStore* rs);
55 
56  bool isDead();
57 
58  void kill();
59 
60  bool hasExcessStones_inlock() const {
61  int64_t total_bytes = 0;
62  for (std::deque<OplogStones::Stone>::const_iterator it = _stones.begin();
63  it != _stones.end();
64  ++it) {
65  total_bytes += it->bytes;
66  }
67  return total_bytes > _rs->cappedMaxSize();
68  }
69 
71 
72  boost::optional<OplogStones::Stone> peekOldestStoneIfNeeded() const;
73 
74  void popOldestStone();
75 
76  void createNewStoneIfNeeded(RecordId lastRecord);
77 
78  void updateCurrentStoneAfterInsertOnCommit(OperationContext* opCtx,
79  int64_t bytesInserted,
80  RecordId highestInserted,
81  int64_t countInserted);
82 
83  void clearStonesOnCommit(OperationContext* opCtx);
84 
85  // Updates the metadata about the oplog stones after a rollback occurs.
86  void updateStonesAfterCappedTruncateAfter(int64_t recordsRemoved,
87  int64_t bytesRemoved,
88  RecordId firstRemovedId);
89 
90  // Resize oplog size
91  void adjust(int64_t maxSize);
92 
93  // The start point of where to truncate next. Used by the background reclaim thread to
94  // efficiently truncate records with WiredTiger by skipping over tombstones, etc.
95  RecordId firstRecord;
96 
97  //
98  // The following methods are public only for use in tests.
99  //
100 
101  size_t numStones() const {
102  stdx::lock_guard<stdx::mutex> lk(_mutex);
103  return _stones.size();
104  }
105 
106  int64_t currentBytes() const {
107  return _currentBytes.load();
108  }
109 
110  int64_t currentRecords() const {
111  return _currentRecords.load();
112  }
113 
114  void setMinBytesPerStone(int64_t size);
115 
116 private:
117  class InsertChange;
118  class TruncateChange;
119 
120  void _calculateStones(OperationContext* opCtx, size_t size);
121  void _calculateStonesByScanning(OperationContext* opCtx);
122  void _calculateStonesBySampling(OperationContext* opCtx,
123  int64_t estRecordsPerStone,
124  int64_t estBytesPerStone);
125 
127 
128  static const uint64_t kRandomSamplesPerStone = 10;
129 
131 
132  stdx::mutex _oplogReclaimMutex;
133  stdx::condition_variable _oplogReclaimCv;
134 
135  // True if '_rs' has been destroyed, e.g. due to repairDatabase being called on the "local"
136  // database, and false otherwise.
137  bool _isDead = false;
138 
139  // Minimum number of bytes the stone being filled should contain before it gets added to the
140  // deque of oplog stones.
142 
143  AtomicInt64 _currentRecords; // Number of records in the stone being filled.
144  AtomicInt64 _currentBytes; // Number of bytes in the stone being filled.
145 
146  mutable stdx::mutex _mutex; // Protects against concurrent access to the deque of oplog stones.
147  std::deque<OplogStones::Stone> _stones; // front = oldest, back = newest.
148 };
149 
150 } // namespace mongo
bool hasExcessStones_inlock() const
Definition: wiredtiger_record_store_oplog_stones.h:60
void _calculateStonesBySampling(OperationContext *opCtx, int64_t estRecordsPerStone, int64_t estBytesPerStone)
Definition: wiredtiger_record_store.cpp:356
void popOldestStone()
Definition: wiredtiger_record_store.cpp:217
void adjust(int64_t maxSize)
Definition: wiredtiger_record_store.cpp:445
stdx::mutex _oplogReclaimMutex
Definition: wiredtiger_record_store_oplog_stones.h:132
Definition: wiredtiger_record_store.h:73
void createNewStoneIfNeeded(RecordId lastRecord)
Definition: wiredtiger_record_store.cpp:222
boost::optional< OplogStones::Stone > peekOldestStoneIfNeeded() const
Definition: wiredtiger_record_store.cpp:207
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
AtomicInt64 _currentBytes
Definition: wiredtiger_record_store_oplog_stones.h:144
int64_t currentRecords() const
Definition: wiredtiger_record_store_oplog_stones.h:110
int64_t records
Definition: wiredtiger_record_store_oplog_stones.h:49
RecordId lastRecord
Definition: wiredtiger_record_store_oplog_stones.h:51
int64_t cappedMaxSize() const
Definition: wiredtiger_record_store.cpp:780
OplogStones(OperationContext *opCtx, WiredTigerRecordStore *rs)
Definition: wiredtiger_record_store.cpp:158
Definition: wiredtiger_record_store.cpp:140
int64_t bytes
Definition: wiredtiger_record_store_oplog_stones.h:50
stdx::mutex _mutex
Definition: wiredtiger_record_store_oplog_stones.h:146
std::unique_ptr< RecordStore > rs
Definition: kv_engine_test_timestamps.cpp:207
size_t numStones() const
Definition: wiredtiger_record_store_oplog_stones.h:101
void clearStonesOnCommit(OperationContext *opCtx)
Definition: wiredtiger_record_store.cpp:258
Definition: wiredtiger_record_store_oplog_stones.h:46
IndexSet::const_iterator it
Definition: ephemeral_for_test_btree_impl.cpp:458
void updateCurrentStoneAfterInsertOnCommit(OperationContext *opCtx, int64_t bytesInserted, RecordId highestInserted, int64_t countInserted)
Definition: wiredtiger_record_store.cpp:249
void _pokeReclaimThreadIfNeeded()
Definition: wiredtiger_record_store.cpp:439
bool isDead()
Definition: wiredtiger_record_store.cpp:178
stdx::condition_variable _oplogReclaimCv
Definition: wiredtiger_record_store_oplog_stones.h:133
void _calculateStones(OperationContext *opCtx, size_t size)
Definition: wiredtiger_record_store.cpp:301
void _calculateStonesByScanning(OperationContext *opCtx)
Definition: wiredtiger_record_store.cpp:331
static const uint64_t kRandomSamplesPerStone
Definition: wiredtiger_record_store_oplog_stones.h:128
std::deque< OplogStones::Stone > _stones
Definition: wiredtiger_record_store_oplog_stones.h:147
Definition: wiredtiger_record_store_oplog_stones.h:48
int64_t currentBytes() const
Definition: wiredtiger_record_store_oplog_stones.h:106
void updateStonesAfterCappedTruncateAfter(int64_t recordsRemoved, int64_t bytesRemoved, RecordId firstRemovedId)
Definition: wiredtiger_record_store.cpp:262
void awaitHasExcessStonesOrDead()
Definition: wiredtiger_record_store.cpp:191
void setMinBytesPerStone(int64_t size)
Definition: wiredtiger_record_store.cpp:291
Definition: wiredtiger_record_store.cpp:109
RecordId firstRecord
Definition: wiredtiger_record_store_oplog_stones.h:95
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
WiredTigerRecordStore * _rs
Definition: wiredtiger_record_store_oplog_stones.h:130
void kill()
Definition: wiredtiger_record_store.cpp:183
int64_t _minBytesPerStone
Definition: wiredtiger_record_store_oplog_stones.h:141
AtomicInt64 _currentRecords
Definition: wiredtiger_record_store_oplog_stones.h:143
bool _isDead
Definition: wiredtiger_record_store_oplog_stones.h:137