You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by ad...@apache.org on 2019/12/04 22:57:49 UTC

[kudu] 02/02: log: remove SegmentAllocator functors

This is an automated email from the ASF dual-hosted git repository.

adar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit c29d144547df20ee626bed16a4a2aa0a6085cded
Author: Adar Dembo <ad...@cloudera.com>
AuthorDate: Mon Dec 2 16:42:34 2019 -0800

    log: remove SegmentAllocator functors
    
    When we roll the log, we close the active segment before switching to the
    next preallocated segment. What happens when we close the active segment?
    1. We sync it.
    2. We write out the (now complete) footer.
    3. We close the segment file. Under the hood, this will truncate any unused
       preallocated space out of the file and sync it one last time.
    4. We open the closed segment file for reading. Note that this is the second
       time we've opened it; we also opened it once when we last rolled the log.
    5. We create a new in-memory segment reader. To avoid needless IO, we
       initialize it using the in-memory header and footer, but we do end up
       reloading the segment's file size from disk.
    6. We replace the last segment reader in the log with the reader constructed
       in step #5. Side effect: the original segment reader is now closed.
    
    My original intent with this patch was to replace steps 4-6 with a simpler
    "inject the footer into the existing in-memory segment reader". This proved
    to be impossible without adding a new synchronization primitive to protect
    the footer in the segment reader, which is something I wanted to avoid. And
    the win would have been minimal: with proper file cache usage, step 4 is
    likely to be a cache hit.
    
    So now this patch just cleans up SegmentAllocator by removing its functors,
    which I found difficult to follow. Instead, Log now manipulates LogReader
    directly, using information passed back from SegmentAllocator functions.
    
    Change-Id: Id2f5dd47e3be930288dcfeb1f77409fda45daac4
    Reviewed-on: http://gerrit.cloudera.org:8080/14819
    Reviewed-by: Andrew Wong <aw...@cloudera.com>
    Tested-by: Kudu Jenkins
---
 src/kudu/consensus/consensus_queue-test.cc         |  10 +-
 src/kudu/consensus/log-test-base.h                 |   4 +-
 src/kudu/consensus/log-test.cc                     |   5 +-
 src/kudu/consensus/log.cc                          | 128 ++++++++++++---------
 src/kudu/consensus/log.h                           |  94 +++++++--------
 src/kudu/consensus/log_reader.cc                   |  36 +++---
 src/kudu/consensus/log_reader.h                    |  26 +++--
 src/kudu/consensus/log_util.cc                     |  34 +++---
 src/kudu/consensus/log_util.h                      |  38 +++---
 .../timestamp_advancement-itest.cc                 |   7 +-
 src/kudu/tablet/tablet_replica-test.cc             |   7 +-
 src/kudu/tserver/tablet_copy-test-base.h           |   2 +-
 12 files changed, 206 insertions(+), 185 deletions(-)

diff --git a/src/kudu/consensus/consensus_queue-test.cc b/src/kudu/consensus/consensus_queue-test.cc
index 0f8d866..7bb7a22 100644
--- a/src/kudu/consensus/consensus_queue-test.cc
+++ b/src/kudu/consensus/consensus_queue-test.cc
@@ -15,6 +15,8 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include "kudu/consensus/consensus_queue.h"
+
 #include <cstddef>
 #include <cstdint>
 #include <memory>
@@ -23,7 +25,6 @@
 #include <vector>
 
 #include <gflags/gflags.h>
-#include <gflags/gflags_declare.h>
 #include <glog/logging.h>
 #include <gtest/gtest.h>
 
@@ -36,7 +37,6 @@
 #include "kudu/common/wire_protocol.h"
 #include "kudu/consensus/consensus-test-util.h"
 #include "kudu/consensus/consensus.pb.h"
-#include "kudu/consensus/consensus_queue.h"
 #include "kudu/consensus/log-test-base.h"
 #include "kudu/consensus/log.h"
 #include "kudu/consensus/log_anchor_registry.h"
@@ -563,7 +563,7 @@ TEST_F(ConsensusQueueTest, TestQueueLoadsOperationsForPeer) {
     ASSERT_OK(log::AppendNoOpToLogSync(clock_, log_.get(), &opid));
     // Roll the log every 10 ops
     if (i % 10 == 0) {
-      ASSERT_OK(log_->AllocateSegmentAndRollOver());
+      ASSERT_OK(log_->AllocateSegmentAndRollOverForTests());
     }
   }
   ASSERT_OK(log_->WaitUntilAllFlushed());
@@ -626,7 +626,7 @@ TEST_F(ConsensusQueueTest, TestQueueHandlesOperationOverwriting) {
     ASSERT_OK(log::AppendNoOpToLogSync(clock_, log_.get(), &opid));
     // Roll the log every 3 ops
     if (i % 3 == 0) {
-      ASSERT_OK(log_->AllocateSegmentAndRollOver());
+      ASSERT_OK(log_->AllocateSegmentAndRollOverForTests());
     }
   }
 
@@ -636,7 +636,7 @@ TEST_F(ConsensusQueueTest, TestQueueHandlesOperationOverwriting) {
     ASSERT_OK(log::AppendNoOpToLogSync(clock_, log_.get(), &opid));
     // Roll the log every 3 ops
     if (i % 3 == 0) {
-      ASSERT_OK(log_->AllocateSegmentAndRollOver());
+      ASSERT_OK(log_->AllocateSegmentAndRollOverForTests());
     }
   }
 
diff --git a/src/kudu/consensus/log-test-base.h b/src/kudu/consensus/log-test-base.h
index f23b696..1fcbad0 100644
--- a/src/kudu/consensus/log-test-base.h
+++ b/src/kudu/consensus/log-test-base.h
@@ -347,10 +347,10 @@ class LogTestBase : public KuduTest {
   }
 
   Status RollLog() {
-    return log_->AllocateSegmentAndRollOver();
+    return log_->AllocateSegmentAndRollOverForTests();
   }
 
-  std::string DumpSegmentsToString(const SegmentSequence& segments) {
+  static std::string DumpSegmentsToString(const SegmentSequence& segments) {
     std::string dump;
     for (const scoped_refptr<ReadableLogSegment>& segment : segments) {
       dump.append("------------\n");
diff --git a/src/kudu/consensus/log-test.cc b/src/kudu/consensus/log-test.cc
index 67c07de..2bf9ad9 100644
--- a/src/kudu/consensus/log-test.cc
+++ b/src/kudu/consensus/log-test.cc
@@ -28,7 +28,6 @@
 #include <vector>
 
 #include <gflags/gflags.h>
-#include <gflags/gflags_declare.h>
 #include <glog/logging.h>
 #include <glog/stl_logging.h>
 #include <gtest/gtest.h>
@@ -193,7 +192,7 @@ TEST_P(LogTestOptionalCompression, TestMultipleEntriesInABatch) {
   AppendNoOpsToLogSync(clock_, log_.get(), &opid, 2);
 
   // RollOver() the batch so that we have a properly formed footer.
-  ASSERT_OK(log_->AllocateSegmentAndRollOver());
+  ASSERT_OK(log_->AllocateSegmentAndRollOverForTests());
 
   SegmentSequence segments;
   log_->reader()->GetSegmentsSnapshot(&segments);
@@ -488,7 +487,7 @@ TEST_F(LogTest, TestWriteAndReadToAndFromInProgressSegment) {
   ASSERT_EQ(written_entries_size, log_->segment_allocator_.active_segment_->written_offset());
 
   // When we roll it should go back to the header size.
-  ASSERT_OK(log_->AllocateSegmentAndRollOver());
+  ASSERT_OK(log_->AllocateSegmentAndRollOverForTests());
   ASSERT_EQ(header_size, log_->segment_allocator_.active_segment_->written_offset());
   written_entries_size = header_size;
 
diff --git a/src/kudu/consensus/log.cc b/src/kudu/consensus/log.cc
index 49cea4e..8640b3b 100644
--- a/src/kudu/consensus/log.cc
+++ b/src/kudu/consensus/log.cc
@@ -19,7 +19,6 @@
 
 #include <cerrno>
 #include <cstdint>
-#include <functional>
 #include <memory>
 #include <mutex>
 #include <ostream>
@@ -177,8 +176,6 @@ namespace log {
 using consensus::CommitMsg;
 using consensus::ReplicateRefPtr;
 using env_util::OpenFileForRandom;
-using std::bind;
-using std::function;
 using std::shared_ptr;
 using std::string;
 using std::vector;
@@ -440,21 +437,17 @@ bool Log::append_thread_active_for_tests() const {
 SegmentAllocator::SegmentAllocator(const LogOptions* opts,
                                    const LogContext* ctx,
                                    Schema schema,
-                                   uint32_t schema_version,
-                                   function<Status(scoped_refptr<ReadableLogSegment>)>
-                                       reader_replace_last_segment,
-                                   function<Status(scoped_refptr<ReadableLogSegment>)>
-                                       reader_add_segment)
-    : reader_replace_last_segment_(std::move(reader_replace_last_segment)),
-      reader_add_segment_(std::move(reader_add_segment)),
-      opts_(opts),
+                                   uint32_t schema_version)
+    : opts_(opts),
       ctx_(ctx),
       max_segment_size_(opts_->segment_size_mb * 1024 * 1024),
       schema_(std::move(schema)),
       schema_version_(schema_version),
       sync_disabled_(false) {}
 
-Status SegmentAllocator::Init(uint64_t sequence_number) {
+Status SegmentAllocator::Init(
+    uint64_t sequence_number,
+    scoped_refptr<ReadableLogSegment>* new_readable_segment) {
   // Init the compression codec.
   if (!FLAGS_log_compression_codec.empty()) {
     auto codec_type = GetCompressionCodecType(FLAGS_log_compression_codec);
@@ -467,10 +460,17 @@ Status SegmentAllocator::Init(uint64_t sequence_number) {
   RETURN_NOT_OK(ThreadPoolBuilder("log-alloc")
       .set_max_threads(1)
       .Build(&allocation_pool_));
-  return AllocateSegmentAndRollOver();
+
+  scoped_refptr<ReadableLogSegment> closed_segment;
+  RETURN_NOT_OK(AllocateSegmentAndRollOver(&closed_segment, new_readable_segment));
+  DCHECK(!closed_segment); // There was no previously active segment.
+  return Status::OK();
 }
 
-Status SegmentAllocator::AllocateOrRollOverIfNecessary(uint32_t write_size_bytes) {
+Status SegmentAllocator::AllocateOrRollOverIfNecessary(
+    uint32_t write_size_bytes,
+    scoped_refptr<ReadableLogSegment>* closed_segment,
+    scoped_refptr<ReadableLogSegment>* new_readable_segment) {
   bool should_rollover = false;
   // if the size of this entry overflows the current segment, get a new one
   {
@@ -493,7 +493,7 @@ Status SegmentAllocator::AllocateOrRollOverIfNecessary(uint32_t write_size_bytes
   if (should_rollover) {
     TRACE_COUNTER_SCOPE_LATENCY_US("log_roll");
     LOG_SLOW_EXECUTION(WARNING, 50, Substitute("$0Log roll took a long time", LogPrefix())) {
-      RETURN_NOT_OK(RollOver());
+      RETURN_NOT_OK(RollOver(closed_segment, new_readable_segment));
     }
   }
   return Status::OK();
@@ -531,7 +531,8 @@ Status SegmentAllocator::Sync() {
   return Status::OK();
 }
 
-Status SegmentAllocator::CloseCurrentSegment(CloseMode mode) {
+Status SegmentAllocator::CloseCurrentSegment(
+    scoped_refptr<ReadableLogSegment>* closed_segment) {
   if (hooks_) {
     RETURN_NOT_OK_PREPEND(hooks_->PreClose(), "PreClose hook failed");
   }
@@ -549,10 +550,8 @@ Status SegmentAllocator::CloseCurrentSegment(CloseMode mode) {
     RETURN_NOT_OK_PREPEND(hooks_->PostClose(), "PostClose hook failed");
   }
 
-  if (mode == CLOSE_AND_REPLACE_LAST_SEGMENT) {
-    scoped_refptr<ReadableLogSegment> last_segment;
-    RETURN_NOT_OK(GetClosedSegment(&last_segment));
-    return reader_replace_last_segment_(std::move(last_segment));
+  if (closed_segment) {
+    RETURN_NOT_OK(GetClosedSegment(closed_segment));
   }
 
   return Status::OK();
@@ -578,12 +577,14 @@ void SegmentAllocator::StopAllocationThread() {
   allocation_pool_->Shutdown();
 }
 
-Status SegmentAllocator::AllocateSegmentAndRollOver() {
+Status SegmentAllocator::AllocateSegmentAndRollOver(
+    scoped_refptr<ReadableLogSegment>* closed_segment,
+    scoped_refptr<ReadableLogSegment>* new_readable_segment) {
   {
     std::lock_guard<RWMutex> l(allocation_lock_);
     RETURN_NOT_OK(AsyncAllocateSegmentUnlocked());
   }
-  return RollOver();
+  return RollOver(closed_segment, new_readable_segment);
 }
 
 Status SegmentAllocator::GetClosedSegment(scoped_refptr<ReadableLogSegment>* readable_segment) {
@@ -663,7 +664,8 @@ Status SegmentAllocator::AllocateNewSegment() {
   return Status::OK();
 }
 
-Status SegmentAllocator::SwitchToAllocatedSegment() {
+Status SegmentAllocator::SwitchToAllocatedSegment(
+    scoped_refptr<ReadableLogSegment>* new_readable_segment) {
   // Increment "next" log segment seqno.
   active_segment_sequence_number_++;
   const auto& tablet_id = ctx_->tablet_id;
@@ -700,18 +702,22 @@ Status SegmentAllocator::SwitchToAllocatedSegment() {
   }
   RETURN_NOT_OK_PREPEND(new_segment->WriteHeaderAndOpen(header), "Failed to write header");
 
-  // Open the segment we just created in readable form and add it to the reader.
+  // Open the segment we just created in readable form; it is the caller's
+  // responsibility to add it to the reader.
+  //
   // TODO(todd): consider using a global FileCache here? With short log segments and
   // lots of tablets, this file descriptor usage may add up.
-  unique_ptr<RandomAccessFile> readable_file;
-
-  RandomAccessFileOptions opts;
-  RETURN_NOT_OK(env->NewRandomAccessFile(opts, new_segment_path, &readable_file));
-  scoped_refptr<ReadableLogSegment> readable_segment(
-    new ReadableLogSegment(new_segment_path,
-                           shared_ptr<RandomAccessFile>(readable_file.release())));
-  RETURN_NOT_OK(readable_segment->Init(header, new_segment->first_entry_offset()));
-  RETURN_NOT_OK(reader_add_segment_(std::move(readable_segment)));
+  {
+    unique_ptr<RandomAccessFile> readable_file;
+
+    RandomAccessFileOptions opts;
+    RETURN_NOT_OK(env->NewRandomAccessFile(opts, new_segment_path, &readable_file));
+    scoped_refptr<ReadableLogSegment> readable_segment(
+        new ReadableLogSegment(new_segment_path,
+                               shared_ptr<RandomAccessFile>(readable_file.release())));
+    RETURN_NOT_OK(readable_segment->Init(header, new_segment->first_entry_offset()));
+    *new_readable_segment = std::move(readable_segment);
+  }
 
   // Now set 'active_segment_' to the new segment.
   active_segment_ = std::move(new_segment);
@@ -721,19 +727,21 @@ Status SegmentAllocator::SwitchToAllocatedSegment() {
   return Status::OK();
 }
 
-Status SegmentAllocator::RollOver() {
+Status SegmentAllocator::RollOver(
+    scoped_refptr<ReadableLogSegment>* closed_segment,
+    scoped_refptr<ReadableLogSegment>* new_readable_segment) {
   SCOPED_LATENCY_METRIC(ctx_->metrics, roll_latency);
 
   // Wait for any on-going allocations to finish.
   RETURN_NOT_OK(allocation_status_.Get());
   DCHECK_EQ(kAllocationFinished, allocation_state());
 
-  // If this isn't the first active segment, close the segment and make it
-  // available to the log reader.
+  // If this isn't the first active segment, close it and return a reopened
+  // segment reader so that the caller can update its log reader.
   if (active_segment_) {
-    RETURN_NOT_OK(CloseCurrentSegment(CLOSE_AND_REPLACE_LAST_SEGMENT));
+    RETURN_NOT_OK(CloseCurrentSegment(closed_segment));
   }
-  RETURN_NOT_OK(SwitchToAllocatedSegment());
+  RETURN_NOT_OK(SwitchToAllocatedSegment(new_readable_segment));
 
   VLOG_WITH_PREFIX(1) << "Rolled over to a new log segment at "
                       << active_segment_->path();
@@ -773,9 +781,7 @@ Log::Log(LogOptions options, LogContext ctx, Schema schema, uint32_t schema_vers
       log_state_(kLogInitialized),
       entry_batch_queue_(FLAGS_group_commit_queue_size_bytes),
       append_thread_(new AppendThread(this)),
-      segment_allocator_(&options_, &ctx_, std::move(schema), schema_version,
-          bind(&Log::ReplaceSegmentInReader, this, std::placeholders::_1),
-          bind(&Log::AddEmptySegmentInReader, this, std::placeholders::_1)),
+      segment_allocator_(&options_, &ctx_, std::move(schema), schema_version),
       on_disk_size_(0) {
 }
 
@@ -813,7 +819,9 @@ Status Log::Init() {
   }
 
   // We always create a new segment when the log starts.
-  RETURN_NOT_OK(segment_allocator_.Init(active_seg_seq_num));
+  scoped_refptr<ReadableLogSegment> new_readable_segment;
+  RETURN_NOT_OK(segment_allocator_.Init(active_seg_seq_num, &new_readable_segment));
+  reader_->AppendEmptySegment(std::move(new_readable_segment));
   RETURN_NOT_OK(append_thread_->Init());
   log_state_ = kLogWriting;
   return Status::OK();
@@ -885,7 +893,17 @@ Status Log::WriteBatch(LogEntryBatch* entry_batch) {
     return Status::OK();
   }
 
-  RETURN_NOT_OK(segment_allocator_.AllocateOrRollOverIfNecessary(entry_batch_bytes));
+  scoped_refptr<ReadableLogSegment> closed_segment;
+  scoped_refptr<ReadableLogSegment> new_readable_segment;
+  RETURN_NOT_OK(segment_allocator_.AllocateOrRollOverIfNecessary(
+      entry_batch_bytes, &closed_segment, &new_readable_segment));
+  if (closed_segment) {
+    // Must be done before a new segment is appended.
+    reader_->ReplaceLastSegment(std::move(closed_segment));
+  }
+  if (new_readable_segment) {
+    reader_->AppendEmptySegment(std::move(new_readable_segment));
+  }
   auto* active_segment = segment_allocator_.active_segment_.get();
   int64_t start_offset = active_segment->written_offset();
 
@@ -930,9 +948,17 @@ Status Log::UpdateIndexForBatch(const LogEntryBatch& batch,
   return Status::OK();
 }
 
-Status Log::AllocateSegmentAndRollOver() {
+Status Log::AllocateSegmentAndRollOverForTests() {
   std::lock_guard<rw_spinlock> l(segment_idle_lock_);
-  return segment_allocator_.AllocateSegmentAndRollOver();
+  scoped_refptr<ReadableLogSegment> closed_segment;
+  scoped_refptr<ReadableLogSegment> new_readable_segment;
+  RETURN_NOT_OK(segment_allocator_.AllocateSegmentAndRollOver(
+      &closed_segment, &new_readable_segment));
+  if (closed_segment) {
+    reader_->ReplaceLastSegment(std::move(closed_segment));
+  }
+  reader_->AppendEmptySegment(std::move(new_readable_segment));
+  return Status::OK();
 }
 
 Status Log::Sync() {
@@ -1127,7 +1153,7 @@ Status Log::Close() {
     }
   }
 
-  RETURN_NOT_OK(segment_allocator_.CloseCurrentSegment(SegmentAllocator::CLOSE));
+  RETURN_NOT_OK(segment_allocator_.CloseCurrentSegment(/*closed_segment=*/ nullptr));
   VLOG_WITH_PREFIX(1) << "Log closed";
 
   // Release FDs held by these objects.
@@ -1184,16 +1210,6 @@ Status Log::RemoveRecoveryDirIfExists(FsManager* fs_manager, const string& table
   return Status::OK();
 }
 
-Status Log::AddEmptySegmentInReader(scoped_refptr<ReadableLogSegment> segment) {
-  std::lock_guard<percpu_rwlock> l(state_lock_);
-  return reader_->AppendEmptySegment(std::move(segment));
-}
-
-Status Log::ReplaceSegmentInReader(scoped_refptr<ReadableLogSegment> segment) {
-  std::lock_guard<percpu_rwlock> l(state_lock_);
-  return reader_->ReplaceLastSegment(std::move(segment));
-}
-
 std::string Log::LogPrefix() const { return ctx_.LogPrefix(); }
 
 Log::~Log() {
diff --git a/src/kudu/consensus/log.h b/src/kudu/consensus/log.h
index b5e6e3d..a0c7540 100644
--- a/src/kudu/consensus/log.h
+++ b/src/kudu/consensus/log.h
@@ -19,8 +19,8 @@
 #include <atomic>
 #include <cstddef>
 #include <cstdint>
-#include <functional>
 #include <limits>
+#include <list>
 #include <map>
 #include <memory>
 #include <string>
@@ -95,25 +95,23 @@ enum SegmentAllocationState {
 class SegmentAllocator {
  public:
   // Creates a new SegmentAllocator. The allocator isn't usable until Init() is
-  // called. 'opts' and 'ctx' are options and various context variables that
-  // are relevant for the Log for which we are allocating segments. 'schema'
-  // and 'schema_version' define the initial schema for the Log.
-  // 'reader_replace_last_segment' is a functor that should be called upon
-  // closing a segment to make the segment readable by the Log's reader.
-  // 'reader_add_segment' is a functor that should be called upon initializing
-  // a new segment to make the segment readable by the Log's reader.
+  // called.
+  //
+  // 'opts' and 'ctx' are options and various context variables that
+  // are relevant for the Log for which we are allocating segments.
+  //
+  // 'schema' and 'schema_version' define the initial schema for the Log.
   SegmentAllocator(const LogOptions* opts,
                    const LogContext* ctx,
                    Schema schema,
-                   uint32_t schema_version,
-                   std::function<Status(scoped_refptr<ReadableLogSegment>)>
-                       reader_replace_last_segment,
-                   std::function<Status(scoped_refptr<ReadableLogSegment>)>
-                       reader_add_segment);
+                   uint32_t schema_version);
 
-  // Sets the currently active segment number, starts the threadpool, and
-  // synchronously allocates an active segment.
-  Status Init(uint64_t sequence_number);
+  // Initializes the SegmentAllocator using 'sequence_number' as the active
+  // segment's sequence number.
+  //
+  // 'new_readable_segment' contains the newly active segment, reopened for reading.
+  Status Init(uint64_t sequence_number,
+              scoped_refptr<ReadableLogSegment>* new_readable_segment);
 
   // Checks whether it's time to allocate (e.g. the current segment is full)
   // and/or roll over (e.g. a previous pre-allocation has finished), and does
@@ -121,21 +119,24 @@ class SegmentAllocator {
   //
   // 'write_size_bytes' is the expected size of the next write; if the active
   // segment would go above the max segment size, a new segment is allocated.
-  Status AllocateOrRollOverIfNecessary(uint32_t write_size_bytes);
+  //
+  // In the event of a roll over, 'closed_segment' contains a new segment reader
+  // for the just-closed segment (if there was one), and 'new_readable_segment'
+  // contains the newly active segment, reopened for reading.
+  Status AllocateOrRollOverIfNecessary(
+      uint32_t write_size_bytes,
+      scoped_refptr<ReadableLogSegment>* closed_segment,
+      scoped_refptr<ReadableLogSegment>* new_readable_segment);
+
 
   // Fsyncs the currently active segment to disk.
   Status Sync();
 
   // Syncs the current segment and writes out the footer.
-  enum CloseMode {
-    // Just close the current semgent.
-    CLOSE,
-
-    // Close the current segment and call reader_replace_last_segment_ to
-    // replace the last log segment in the log reader.
-    CLOSE_AND_REPLACE_LAST_SEGMENT,
-  };
-  Status CloseCurrentSegment(CloseMode mode);
+  //
+  // If 'closed_segment' is not null, it will contain a new ReadableLogSegment
+  // corresponding to the segment that was just closed.
+  Status CloseCurrentSegment(scoped_refptr<ReadableLogSegment>* closed_segment);
 
   // Update the footer based on the written 'batch', e.g. to track the
   // last-written OpId.
@@ -163,7 +164,14 @@ class SegmentAllocator {
 
   // This is not thread-safe. It is up to the caller to ensure this does not
   // interfere with the append thread's attempts to switch log segments.
-  Status AllocateSegmentAndRollOver();
+  //
+  // If there was a previous active segment, 'closed_segment' contains a new
+  // segment reader built for that segment.
+  //
+  // 'new_readable_segment' contains the newly active segment, reopened for reading.
+  Status AllocateSegmentAndRollOver(
+      scoped_refptr<ReadableLogSegment>* closed_segment,
+      scoped_refptr<ReadableLogSegment>* new_readable_segment);
 
   // Returns a readable segment pointing at the most recently closed segment.
   Status GetClosedSegment(scoped_refptr<ReadableLogSegment>* readable_segment);
@@ -184,19 +192,20 @@ class SegmentAllocator {
   Status AllocateNewSegment();
 
   // Swaps in the next segment file as the new active segment.
-  Status SwitchToAllocatedSegment();
+  //
+  // 'new_readable_segment' contains the newly active segment, reopened for reading.
+  Status SwitchToAllocatedSegment(
+      scoped_refptr<ReadableLogSegment>* new_readable_segment);
 
   // Waits for any on-going allocation to complete and rolls over onto the
-  // allocated segment, swapping out the previous active segment (if one
-  // existed).
-  Status RollOver();
-
-  // Function call that replaces the last segment in the Log's log reader with
-  // the input readable log segment.
-  const std::function<Status(scoped_refptr<ReadableLogSegment>)> reader_replace_last_segment_;
-
-  // Function call that adds the input segment to the Log's log reader.
-  const std::function<Status(scoped_refptr<ReadableLogSegment>)> reader_add_segment_;
+  // allocated segment, swapping out the previous active segment if it existed.
+  //
+  // If there was a previous active segment, 'closed_segment' contains a new
+  // segment reader built for that segment.
+  //
+  // 'new_readable_segment' contains the newly active segment, reopened for reading.
+  Status RollOver(scoped_refptr<ReadableLogSegment>* closed_segment,
+                  scoped_refptr<ReadableLogSegment>* new_readable_segment);
 
   // Hooks used to inject faults into the allocator.
   std::shared_ptr<LogFaultHooks> hooks_;
@@ -398,7 +407,7 @@ class Log : public RefCountedThreadSafe<Log> {
   // available in closed, readable segments.
   //
   // This is not thread-safe. Used in test only.
-  Status AllocateSegmentAndRollOver();
+  Status AllocateSegmentAndRollOverForTests();
 
   void SetLogFaultHooksForTests(const std::shared_ptr<LogFaultHooks>& hooks) {
     segment_allocator_.hooks_ = hooks;
@@ -455,13 +464,6 @@ class Log : public RefCountedThreadSafe<Log> {
   Status UpdateIndexForBatch(const LogEntryBatch& batch,
                              int64_t start_offset);
 
-  // Replaces the last "empty" segment in 'log_reader_', i.e. the one currently
-  // being written to, with the same segment once properly closed.
-  Status ReplaceSegmentInReader(scoped_refptr<ReadableLogSegment> segment);
-
-  // Adds the given segment to 'log_reader_'.
-  Status AddEmptySegmentInReader(scoped_refptr<ReadableLogSegment> segment);
-
   Status Sync();
 
   // Helper method to get the segment sequence to GC based on the provided 'retention' struct.
diff --git a/src/kudu/consensus/log_reader.cc b/src/kudu/consensus/log_reader.cc
index 662af85..f4367ee 100644
--- a/src/kudu/consensus/log_reader.cc
+++ b/src/kudu/consensus/log_reader.cc
@@ -190,7 +190,8 @@ Status LogReader::Init(const string& tablet_wal_path) {
       }
       previous_seg_seqno = entry->header().sequence_number();
       previous_seg_path = entry->path();
-      RETURN_NOT_OK(AppendSegmentUnlocked(std::move(entry)));
+      DCHECK(entry->HasFooter());
+      AppendSegmentUnlocked(std::move(entry));
     }
 
     state_ = kLogReaderReading;
@@ -384,12 +385,12 @@ void LogReader::UpdateLastSegmentOffset(int64_t readable_to_offset) {
   CHECK_EQ(state_, kLogReaderReading);
   DCHECK(!segments_.empty());
   // Get the last segment
-  ReadableLogSegment* segment = segments_.back().get();
+  auto* segment = segments_.back().get();
   DCHECK(!segment->HasFooter());
   segment->UpdateReadableToOffset(readable_to_offset);
 }
 
-Status LogReader::ReplaceLastSegment(scoped_refptr<ReadableLogSegment> segment) {
+void LogReader::ReplaceLastSegment(scoped_refptr<ReadableLogSegment> segment) {
   // This is used to replace the last segment once we close it properly so it must
   // have a footer.
   DCHECK(segment->HasFooter());
@@ -397,11 +398,9 @@ Status LogReader::ReplaceLastSegment(scoped_refptr<ReadableLogSegment> segment)
   std::lock_guard<simple_spinlock> lock(lock_);
   CHECK_EQ(state_, kLogReaderReading);
   // Make sure the segment we're replacing has the same sequence number
-  CHECK(!segments_.empty());
+  DCHECK(!segments_.empty());
   CHECK_EQ(segment->header().sequence_number(), segments_.back()->header().sequence_number());
   segments_[segments_.size() - 1] = std::move(segment);
-
-  return Status::OK();
 }
 
 Status LogReader::AppendSegment(scoped_refptr<ReadableLogSegment> segment) {
@@ -410,34 +409,31 @@ Status LogReader::AppendSegment(scoped_refptr<ReadableLogSegment> segment) {
     RETURN_NOT_OK(segment->RebuildFooterByScanning());
   }
   std::lock_guard<simple_spinlock> lock(lock_);
-  return AppendSegmentUnlocked(std::move(segment));
+  AppendSegmentUnlocked(std::move(segment));
+  return Status::OK();
 }
 
-Status LogReader::AppendSegmentUnlocked(scoped_refptr<ReadableLogSegment> segment) {
+void LogReader::AppendSegmentUnlocked(scoped_refptr<ReadableLogSegment> segment) {
   DCHECK(segment->IsInitialized());
-  DCHECK(segment->HasFooter());
+  DCHECK(lock_.is_locked());
 
+#ifndef NDEBUG
   if (!segments_.empty()) {
-    CHECK_EQ(segments_.back()->header().sequence_number() + 1,
-             segment->header().sequence_number());
+    DCHECK_EQ(segments_.back()->header().sequence_number() + 1,
+              segment->header().sequence_number());
   }
+#endif
   segments_.emplace_back(std::move(segment));
-  return Status::OK();
 }
 
-Status LogReader::AppendEmptySegment(scoped_refptr<ReadableLogSegment> segment) {
+void LogReader::AppendEmptySegment(scoped_refptr<ReadableLogSegment> segment) {
   DCHECK(segment->IsInitialized());
   std::lock_guard<simple_spinlock> lock(lock_);
   CHECK_EQ(state_, kLogReaderReading);
-  if (!segments_.empty()) {
-    CHECK_EQ(segments_.back()->header().sequence_number() + 1,
-             segment->header().sequence_number());
-  }
-  segments_.emplace_back(std::move(segment));
-  return Status::OK();
+  AppendSegmentUnlocked(std::move(segment));
 }
 
-const int LogReader::num_segments() const {
+int LogReader::num_segments() const {
   std::lock_guard<simple_spinlock> lock(lock_);
   return segments_.size();
 }
diff --git a/src/kudu/consensus/log_reader.h b/src/kudu/consensus/log_reader.h
index 3325233..ca80179 100644
--- a/src/kudu/consensus/log_reader.h
+++ b/src/kudu/consensus/log_reader.h
@@ -14,8 +14,7 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-#ifndef KUDU_LOG_LOG_READER_H_
-#define KUDU_LOG_LOG_READER_H_
+#pragma once
 
 #include <cstdint>
 #include <memory>
@@ -46,12 +45,19 @@ class ReplicateMsg;
 } // namespace consensus
 
 namespace log {
-class LogIndex;
 class LogEntryBatchPB;
+class LogIndex;
 struct LogIndexEntry;
 
-// Reads a set of segments from a given path. Segment headers and footers
-// are read and parsed, but entries are not.
+// Reads a set of segments from a given path. Segment headers and footers are
+// read and parsed, but entries are not.
+//
+// Once initialized, care should be taken to avoid mutating the segments in a
+// non-thread-safe manner. E.g. segment mutation should be made thread-safe with
+// locking primitives, or done via a copy-then-replace access pattern.
+// UpdateLastSegmentOffset and ReplaceLastSegment are examples of each these
+// patterns respectively.
+//
 // This class is thread safe.
 class LogReader : public enable_make_shared<LogReader> {
  public:
@@ -109,7 +115,7 @@ class LogReader : public enable_make_shared<LogReader> {
   Status LookupOpId(int64_t op_index, consensus::OpId* op_id) const;
 
   // Returns the number of segments.
-  const int num_segments() const;
+  int num_segments() const;
 
   std::string ToString() const;
 
@@ -138,7 +144,7 @@ class LogReader : public enable_make_shared<LogReader> {
 
   // Same as above but for segments without any entries.
   // Used by the Log to add "empty" segments.
-  Status AppendEmptySegment(scoped_refptr<ReadableLogSegment> segment);
+  void AppendEmptySegment(scoped_refptr<ReadableLogSegment> segment);
 
   // Removes segments with sequence numbers less than or equal to
   // 'segment_sequence_number' from this reader.
@@ -150,14 +156,14 @@ class LogReader : public enable_make_shared<LogReader> {
   // Requires that the last segment in 'segments_' has the same sequence
   // number as 'segment'.
   // Expects 'segment' to be properly closed and to have footer.
-  Status ReplaceLastSegment(scoped_refptr<ReadableLogSegment> segment);
+  void ReplaceLastSegment(scoped_refptr<ReadableLogSegment> segment);
 
   // Appends 'segment' to the segment sequence.
   // Assumes that the segment was scanned, if no footer was found.
   // To be used only internally, clients of this class with private access (i.e. friends)
   // should use the thread safe version, AppendSegment(), which will also scan the segment
   // if no footer is present.
-  Status AppendSegmentUnlocked(scoped_refptr<ReadableLogSegment> segment);
+  void AppendSegmentUnlocked(scoped_refptr<ReadableLogSegment> segment);
 
   // Used by Log to update its LogReader on how far it is possible to read
   // the current segment. Requires that the reader has at least one segment
@@ -199,5 +205,3 @@ class LogReader : public enable_make_shared<LogReader> {
 
 }  // namespace log
 }  // namespace kudu
-
-#endif /* KUDU_LOG_LOG_READER_H_ */
diff --git a/src/kudu/consensus/log_util.cc b/src/kudu/consensus/log_util.cc
index e2d4949..0ea8b33 100644
--- a/src/kudu/consensus/log_util.cc
+++ b/src/kudu/consensus/log_util.cc
@@ -21,6 +21,7 @@
 #include <cstring>
 #include <iostream>
 #include <memory>
+#include <utility>
 
 #include <gflags/gflags.h>
 #include <glog/logging.h>
@@ -34,8 +35,8 @@
 #include "kudu/gutil/strings/substitute.h"
 #include "kudu/gutil/strings/util.h"
 #include "kudu/util/array_view.h" // IWYU pragma: keep
-#include "kudu/util/coding-inl.h"
 #include "kudu/util/coding.h"
+#include "kudu/util/coding-inl.h"
 #include "kudu/util/compression/compression.pb.h"
 #include "kudu/util/compression/compression_codec.h"
 #include "kudu/util/crc.h"
@@ -109,7 +110,7 @@ LogOptions::LogOptions()
 // LogEntryReader
 ////////////////////////////////////////////////////////////
 
-LogEntryReader::LogEntryReader(ReadableLogSegment* seg)
+LogEntryReader::LogEntryReader(const ReadableLogSegment* seg)
     : seg_(seg),
       num_batches_read_(0),
       num_entries_read_(0),
@@ -441,14 +442,14 @@ Status ReadableLogSegment::ReadHeader() {
                                 "of Kudu");
   }
 
-  header_.Swap(&header);
+  header_ = std::move(header);
   first_entry_offset_ = header_size + kLogSegmentHeaderMagicAndHeaderLength;
 
   return Status::OK();
 }
 
 
-Status ReadableLogSegment::ReadHeaderMagicAndHeaderLength(uint32_t *len) {
+Status ReadableLogSegment::ReadHeaderMagicAndHeaderLength(uint32_t *len) const {
   uint8_t scratch[kLogSegmentHeaderMagicAndHeaderLength];
   Slice slice(scratch, kLogSegmentHeaderMagicAndHeaderLength);
   RETURN_NOT_OK(readable_file_->Read(0, slice));
@@ -456,8 +457,8 @@ Status ReadableLogSegment::ReadHeaderMagicAndHeaderLength(uint32_t *len) {
   return Status::OK();
 }
 
-Status ReadableLogSegment::ParseHeaderMagicAndHeaderLength(const Slice &data,
-                                                           uint32_t *parsed_len) {
+Status ReadableLogSegment::ParseHeaderMagicAndHeaderLength(
+    const Slice &data, uint32_t *parsed_len) const {
   RETURN_NOT_OK_PREPEND(data.check_size(kLogSegmentHeaderMagicAndHeaderLength),
                         "Log segment file is too small to contain initial magic number");
 
@@ -517,11 +518,11 @@ Status ReadableLogSegment::ReadFooter() {
                                                 footer_size),
                         "Unable to parse protobuf");
 
-  footer_.Swap(&footer);
+  footer_ = std::move(footer);
   return Status::OK();
 }
 
-Status ReadableLogSegment::ReadFooterMagicAndFooterLength(uint32_t *len) {
+Status ReadableLogSegment::ReadFooterMagicAndFooterLength(uint32_t *len) const {
   uint8_t scratch[kLogSegmentFooterMagicAndFooterLength];
   Slice slice(scratch, kLogSegmentFooterMagicAndFooterLength);
 
@@ -533,8 +534,8 @@ Status ReadableLogSegment::ReadFooterMagicAndFooterLength(uint32_t *len) {
   return Status::OK();
 }
 
-Status ReadableLogSegment::ParseFooterMagicAndFooterLength(const Slice &data,
-                                                           uint32_t *parsed_len) {
+Status ReadableLogSegment::ParseFooterMagicAndFooterLength(
+    const Slice &data, uint32_t *parsed_len) {
   RETURN_NOT_OK_PREPEND(data.check_size(kLogSegmentFooterMagicAndFooterLength),
                         "Slice is too small to contain final magic number");
 
@@ -547,7 +548,7 @@ Status ReadableLogSegment::ParseFooterMagicAndFooterLength(const Slice &data,
   return Status::OK();
 }
 
-Status ReadableLogSegment::ReadEntries(LogEntries* entries) {
+Status ReadableLogSegment::ReadEntries(LogEntries* entries) const {
   TRACE_EVENT1("log", "ReadableLogSegment::ReadEntries",
                "path", path_);
   LogEntryReader reader(this);
@@ -571,7 +572,8 @@ size_t ReadableLogSegment::entry_header_size() const {
   return header_.has_deprecated_major_version() ? kEntryHeaderSizeV1 : kEntryHeaderSizeV2;
 }
 
-Status ReadableLogSegment::ScanForValidEntryHeaders(int64_t offset, bool* has_valid_entries) {
+Status ReadableLogSegment::ScanForValidEntryHeaders(
+    int64_t offset, bool* has_valid_entries) const {
   TRACE_EVENT1("log", "ReadableLogSegment::ScanForValidEntryHeaders",
                "path", path_);
   VLOG(1) << "Scanning " << path_ << " for valid entry headers "
@@ -617,7 +619,7 @@ Status ReadableLogSegment::ScanForValidEntryHeaders(int64_t offset, bool* has_va
 
 Status ReadableLogSegment::ReadEntryHeaderAndBatch(int64_t* offset, faststring* tmp_buf,
                                                    unique_ptr<LogEntryBatchPB>* batch,
-                                                   EntryHeaderStatus* status_detail) {
+                                                   EntryHeaderStatus* status_detail) const {
   int64_t cur_offset = *offset;
   EntryHeader header;
   RETURN_NOT_OK(ReadEntryHeader(&cur_offset, &header, status_detail));
@@ -633,7 +635,7 @@ Status ReadableLogSegment::ReadEntryHeaderAndBatch(int64_t* offset, faststring*
 }
 
 Status ReadableLogSegment::ReadEntryHeader(int64_t *offset, EntryHeader* header,
-                                           EntryHeaderStatus* status_detail) {
+                                           EntryHeaderStatus* status_detail) const {
   const size_t header_size = entry_header_size();
   uint8_t scratch[header_size];
   Slice slice(scratch, header_size);
@@ -658,7 +660,7 @@ Status ReadableLogSegment::ReadEntryHeader(int64_t *offset, EntryHeader* header,
 }
 
 EntryHeaderStatus ReadableLogSegment::DecodeEntryHeader(
-    const Slice& data, EntryHeader* header) {
+    const Slice& data, EntryHeader* header) const {
   uint32_t computed_header_crc;
   if (entry_header_size() == kEntryHeaderSizeV2) {
     header->msg_length_compressed = DecodeFixed32(&data[0]);
@@ -689,7 +691,7 @@ EntryHeaderStatus ReadableLogSegment::DecodeEntryHeader(
 Status ReadableLogSegment::ReadEntryBatch(int64_t* offset,
                                           const EntryHeader& header,
                                           faststring* tmp_buf,
-                                          unique_ptr<LogEntryBatchPB>* entry_batch) {
+                                          unique_ptr<LogEntryBatchPB>* entry_batch) const {
   TRACE_EVENT2("log", "ReadableLogSegment::ReadEntryBatch",
                "path", path_,
                "range", Substitute("offset=$0 entry_len=$1",
diff --git a/src/kudu/consensus/log_util.h b/src/kudu/consensus/log_util.h
index 426387e..a858cce 100644
--- a/src/kudu/consensus/log_util.h
+++ b/src/kudu/consensus/log_util.h
@@ -14,9 +14,7 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
-
-#ifndef KUDU_CONSENSUS_LOG_UTIL_H_
-#define KUDU_CONSENSUS_LOG_UTIL_H_
+#pragma once
 
 #include <cstddef>
 #include <cstdint>
@@ -102,7 +100,7 @@ class LogEntryReader {
 
   // Construct a LogEntryReader to read from the provided segment.
   // 'seg' must outlive the LogEntryReader.
-  explicit LogEntryReader(ReadableLogSegment* seg);
+  explicit LogEntryReader(const ReadableLogSegment* seg);
 
   ~LogEntryReader();
 
@@ -131,7 +129,7 @@ class LogEntryReader {
   Status MakeCorruptionStatus(const Status& status) const;
 
   // The segment being read.
-  ReadableLogSegment* seg_;
+  const ReadableLogSegment* seg_;
 
   // The last several entries which were successfully read.
   struct RecentEntry {
@@ -171,6 +169,10 @@ class LogEntryReader {
 // segments are rolled over and the Log continues in a new segment.
 
 // A readable log segment for recovery and follower catch-up.
+//
+// Const methods are thread-safe; non-const methods are not. Care must be taken
+// not to invoke non-thread-safe methods after the segment has been incorporated
+// into the LogReader, as there may be concurrent threads accessing the reader.
 class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
  public:
   // Factory method to construct a ReadableLogSegment from a file on the FS.
@@ -207,12 +209,15 @@ class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
   // If the log is corrupted (i.e. the returned 'Status' is 'Corruption') all
   // the log entries read up to the corrupted one are returned in the 'entries'
   // vector.
-  Status ReadEntries(LogEntries* entries);
+  Status ReadEntries(LogEntries* entries) const;
 
   // Rebuilds this segment's footer by scanning its entries.
   // This is an expensive operation as it reads and parses the whole segment
   // so it should be only used in the case of a crash, where the footer is
   // missing because we didn't have the time to write it out.
+  //
+  // This function is not thread-safe and should only be called when there's no
+  // danger of another thread accessing the segment.
   Status RebuildFooterByScanning();
 
   bool IsInitialized() const {
@@ -311,25 +316,25 @@ class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
   //
   // Returns Uninitialized() if the file appears to be preallocated but never
   // written.
-  Status ReadHeaderMagicAndHeaderLength(uint32_t *len);
+  Status ReadHeaderMagicAndHeaderLength(uint32_t *len) const;
 
   // Parse the magic and the PB-header length prefix from 'data'.
   // In the case that 'data' is all '\0' bytes, indicating a preallocated
   // but never-written segment, returns Status::Uninitialized().
-  Status ParseHeaderMagicAndHeaderLength(const Slice &data, uint32_t *parsed_len);
+  Status ParseHeaderMagicAndHeaderLength(const Slice &data, uint32_t *parsed_len) const;
 
   Status ReadFooter();
 
-  Status ReadFooterMagicAndFooterLength(uint32_t *len);
+  Status ReadFooterMagicAndFooterLength(uint32_t *len) const;
 
-  Status ParseFooterMagicAndFooterLength(const Slice &data, uint32_t *parsed_len);
+  static Status ParseFooterMagicAndFooterLength(const Slice &data, uint32_t *parsed_len);
 
   // Starting at 'offset', read the rest of the log file, looking for any
   // valid log entry headers. If any are found, sets *has_valid_entries to true.
   //
   // Returns a bad Status only in the case that some IO error occurred reading the
   // file.
-  Status ScanForValidEntryHeaders(int64_t offset, bool* has_valid_entries);
+  Status ScanForValidEntryHeaders(int64_t offset, bool* has_valid_entries) const;
 
   // Read an entry header and its associated batch at the given offset.
   // If successful, updates '*offset' to point to the next batch
@@ -339,14 +344,14 @@ class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
   // to indicate the cause of the error.
   Status ReadEntryHeaderAndBatch(int64_t* offset, faststring* tmp_buf,
                                  std::unique_ptr<LogEntryBatchPB>* batch,
-                                 EntryHeaderStatus* status_detail);
+                                 EntryHeaderStatus* status_detail) const;
 
   // Reads a log entry header from the segment.
   //
   // Also increments the passed offset* by the length of the entry on successful
   // read.
   Status ReadEntryHeader(int64_t *offset, EntryHeader* header,
-                         EntryHeaderStatus* status_detail);
+                         EntryHeaderStatus* status_detail) const;
 
   // Decode a log entry header from the given slice. The header length is
   // determined by 'entry_header_size()'.
@@ -354,14 +359,14 @@ class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
   //
   // NOTE: this is performance-critical since it is used by ScanForValidEntryHeaders
   // and thus returns an enum instead of Status.
-  EntryHeaderStatus DecodeEntryHeader(const Slice& data, EntryHeader* header);
+  EntryHeaderStatus DecodeEntryHeader(const Slice& data, EntryHeader* header) const;
 
   // Reads a log entry batch from the provided readable segment, which gets decoded
   // into 'entry_batch' and increments 'offset' by the batch's length.
   Status ReadEntryBatch(int64_t* offset,
                         const EntryHeader& header,
                         faststring* tmp_buf,
-                        std::unique_ptr<LogEntryBatchPB>* entry_batch);
+                        std::unique_ptr<LogEntryBatchPB>* entry_batch) const;
 
   void UpdateReadableToOffset(int64_t readable_to_offset);
 
@@ -378,7 +383,6 @@ class ReadableLogSegment : public RefCountedThreadSafe<ReadableLogSegment> {
   // we can read without the fear of reading garbage/zeros.
   // This is atomic because the Log thread might be updating the segment's readable
   // offset while an async reader is reading the segment's entries.
-  // is reading it.
   AtomicInt<int64_t> readable_to_offset_;
 
   // a readable file for a log segment (used on replay)
@@ -516,5 +520,3 @@ void UpdateFooterForReplicateEntry(
 
 }  // namespace log
 }  // namespace kudu
-
-#endif /* KUDU_CONSENSUS_LOG_UTIL_H_ */
diff --git a/src/kudu/integration-tests/timestamp_advancement-itest.cc b/src/kudu/integration-tests/timestamp_advancement-itest.cc
index 1cfccbe..8166052 100644
--- a/src/kudu/integration-tests/timestamp_advancement-itest.cc
+++ b/src/kudu/integration-tests/timestamp_advancement-itest.cc
@@ -15,11 +15,12 @@
 // specific language governing permissions and limitations
 // under the License.
 
-#include <stdint.h>
-
+#include <cstdint>
+#include <initializer_list>
 #include <memory>
 #include <ostream>
 #include <string>
+#include <unordered_map>
 #include <utility>
 #include <vector>
 
@@ -138,7 +139,7 @@ class TimestampAdvancementITest : public MiniClusterITestBase {
 
     // Flush the current log batch and roll over to get a fresh WAL segment.
     ASSERT_OK(tablet_replica->log()->WaitUntilAllFlushed());
-    ASSERT_OK(tablet_replica->log()->AllocateSegmentAndRollOver());
+    ASSERT_OK(tablet_replica->log()->AllocateSegmentAndRollOverForTests());
 
     // Also flush the MRS so we're free to GC the WAL segment we just wrote.
     ASSERT_OK(tablet_replica->tablet()->Flush());
diff --git a/src/kudu/tablet/tablet_replica-test.cc b/src/kudu/tablet/tablet_replica-test.cc
index d7f3734..2baa9b2 100644
--- a/src/kudu/tablet/tablet_replica-test.cc
+++ b/src/kudu/tablet/tablet_replica-test.cc
@@ -25,7 +25,6 @@
 
 #include <boost/optional/optional.hpp>
 #include <gflags/gflags.h>
-#include <gflags/gflags_declare.h>
 #include <glog/logging.h>
 #include <gtest/gtest.h>
 
@@ -320,9 +319,9 @@ class TabletReplicaTest : public KuduTabletTest {
     return Status::OK();
   }
 
-  Status RollLog(TabletReplica* replica) {
+  static Status RollLog(TabletReplica* replica) {
     RETURN_NOT_OK(replica->log_->WaitUntilAllFlushed());
-    return replica->log_->AllocateSegmentAndRollOver();
+    return replica->log_->AllocateSegmentAndRollOverForTests();
   }
 
   Status ExecuteWriteAndRollLog(TabletReplica* tablet_replica, const WriteRequestPB& req) {
@@ -604,7 +603,7 @@ TEST_F(TabletReplicaTest, TestActiveTransactionPreventsLogGC) {
       << "By the time a transaction is applied, it should have an Opid";
     // The apply will hang until we CountDown() the continue latch.
     // Now, roll the log. Below, we execute a few more insertions with rolling.
-    ASSERT_OK(log->AllocateSegmentAndRollOver());
+    ASSERT_OK(log->AllocateSegmentAndRollOverForTests());
   }
 
   ASSERT_EQ(1, tablet_replica_->txn_tracker_.GetNumPendingForTests());
diff --git a/src/kudu/tserver/tablet_copy-test-base.h b/src/kudu/tserver/tablet_copy-test-base.h
index 7319a6c..0b33b06 100644
--- a/src/kudu/tserver/tablet_copy-test-base.h
+++ b/src/kudu/tserver/tablet_copy-test-base.h
@@ -116,7 +116,7 @@ class TabletCopyTest : public TabletServerTestBase {
       for (int row_id = 0; row_id < kNumLogRolls * kIncr; row_id += kIncr) {
         InsertTestRowsRemote(row_id, kIncr);
         ASSERT_OK(tablet_replica_->tablet()->Flush());
-        ASSERT_OK(tablet_replica_->log()->AllocateSegmentAndRollOver());
+        ASSERT_OK(tablet_replica_->log()->AllocateSegmentAndRollOverForTests());
       }
     }
   }