You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by al...@apache.org on 2026/09/25 16:45:49 UTC

(kudu) branch master updated: [tablet] make tablet_compaction_budget_mb a runtime flag

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3b18f0139 [tablet] make tablet_compaction_budget_mb a runtime flag
3b18f0139 is described below

commit 3b18f0139b1d21a072b7353269301fba16dca54e
Author: Ashwani <as...@apache.org>
AuthorDate: Thu Sep 24 18:44:43 2026 +0530

    [tablet] make tablet_compaction_budget_mb a runtime flag
    
    FLAGS_tablet_compaction_budget_mb previously required a tablet-server
    restart to take effect. This change makes it a runtime flag so that
    operators can adjust the compaction budget without restarting.
    
    BudgetedCompactionPolicy now stores the budget in size_budget_mb_, which
    is initialised at construction time and refreshed at the top of every
    PickRowSets() call. This ensures that RunApproximation() and RunExact()
    both see a consistent, immutable budget for the duration of a single
    compaction selection, while any runtime change to the flag is picked up
    on the next selection.
    
    Newly added test (i.e., TestFlagDrivesPickRowSets) verifies the
    end-to-end behaviour: lowering the budget to 1 MB causes PickRowSets to
    select no rowsets (no merge benefit for a single rowset), and raising it
    to 4 MB causes all three overlapping rowsets to be selected.
    
    Change-Id: Id0515daff32d952cc9dc45d604cf41bf6018d403
    Reviewed-on: http://gerrit.cloudera.org:8080/24932
    Tested-by: Ashwani Raina <ar...@gmail.com>
    Reviewed-by: Marton Greber <gr...@gmail.com>
    Reviewed-by: Alexey Serbin <al...@apache.org>
---
 .../timestamp_advancement-itest.cc                 |  2 +-
 src/kudu/tablet/compaction_policy-test.cc          | 87 +++++++++++++++++++++-
 src/kudu/tablet/compaction_policy.cc               | 23 ++++--
 src/kudu/tablet/compaction_policy.h                |  9 ++-
 src/kudu/tablet/tablet.cc                          |  6 +-
 src/kudu/tablet/tablet.h                           |  1 +
 6 files changed, 114 insertions(+), 14 deletions(-)

diff --git a/src/kudu/integration-tests/timestamp_advancement-itest.cc b/src/kudu/integration-tests/timestamp_advancement-itest.cc
index 23d391fe6..82d4c3761 100644
--- a/src/kudu/integration-tests/timestamp_advancement-itest.cc
+++ b/src/kudu/integration-tests/timestamp_advancement-itest.cc
@@ -85,7 +85,7 @@ DECLARE_double(leader_failure_max_missed_heartbeat_periods);
 DECLARE_int32(consensus_inject_latency_ms_in_notifications);
 DECLARE_int32(heartbeat_interval_ms);
 DECLARE_int32(raft_heartbeat_interval_ms);
-DECLARE_int32(tablet_compaction_budget_mb);
+DECLARE_uint32(tablet_compaction_budget_mb);
 DECLARE_int32(tablet_history_max_age_sec);
 
 #ifndef NDEBUG
diff --git a/src/kudu/tablet/compaction_policy-test.cc b/src/kudu/tablet/compaction_policy-test.cc
index dc650eeb0..39e95af06 100644
--- a/src/kudu/tablet/compaction_policy-test.cc
+++ b/src/kudu/tablet/compaction_policy-test.cc
@@ -24,6 +24,7 @@
 #include <optional>
 #include <ostream>
 #include <string>
+#include <type_traits>
 #include <unordered_set>
 #include <utility>
 #include <vector>
@@ -33,14 +34,21 @@
 #include <glog/stl_logging.h>
 #include <gtest/gtest.h>
 
+#include "kudu/common/common.pb.h"
+#include "kudu/common/partial_row.h"
+#include "kudu/common/schema.h"
 #include "kudu/gutil/stringprintf.h"
 #include "kudu/gutil/strings/numbers.h"
 #include "kudu/gutil/strings/split.h"
 #include "kudu/gutil/strings/substitute.h"
+#include "kudu/tablet/compaction.h"
+#include "kudu/tablet/local_tablet_writer.h"
 #include "kudu/tablet/mock-rowsets.h"
 #include "kudu/tablet/rowset.h"
 #include "kudu/tablet/rowset_info.h"
 #include "kudu/tablet/rowset_tree.h"
+#include "kudu/tablet/tablet-test-util.h"
+#include "kudu/tablet/tablet.h"
 #include "kudu/util/env.h"
 #include "kudu/util/faststring.h"
 #include "kudu/util/path_util.h"
@@ -55,6 +63,7 @@ using std::vector;
 
 DECLARE_double(compaction_minimum_improvement);
 DECLARE_double(compaction_small_rowset_tradeoff);
+DECLARE_uint32(tablet_compaction_budget_mb);
 DECLARE_int64(budgeted_compaction_target_rowset_size);
 
 namespace kudu {
@@ -69,7 +78,8 @@ class TestCompactionPolicy : public KuduTest {
     RowSetTree tree;
     ASSERT_OK(tree.Reset(vec));
 
-    BudgetedCompactionPolicy policy(size_budget_mb);
+    FLAGS_tablet_compaction_budget_mb = size_budget_mb;
+    BudgetedCompactionPolicy policy;
 
     ASSERT_OK(policy.PickRowSets(tree, picked, quality, /*log=*/nullptr));
   }
@@ -274,7 +284,8 @@ TEST_F(TestCompactionPolicy, TestYcsbCompaction) {
   ASSERT_OK(tree.Reset(rowsets));
   vector<double> qualities;
   for (int budget_mb : {128, 256, 512, 1024}) {
-    BudgetedCompactionPolicy policy(budget_mb);
+    FLAGS_tablet_compaction_budget_mb = budget_mb;
+    BudgetedCompactionPolicy policy;
 
     CompactionSelection picked;
     double quality = 0.0;
@@ -638,5 +649,77 @@ TEST_F(TestCompactionPolicy, TestKUDU2704) {
   ASSERT_EQ(2, picked.size());
   ASSERT_GT(quality, 0.0);
 }
+
+class TestCompactionBudgetRuntimeFlag
+    : public KuduTabletTest {
+ public:
+  TestCompactionBudgetRuntimeFlag()
+      : KuduTabletTest(Schema(
+            {ColumnSchema("key", STRING), ColumnSchema("val", INT64)}, 1)) {}
+};
+
+// Verify that changing 'tablet_compaction_budget_mb' at runtime causes
+// Tablet::PickRowSetsToCompact() to use the new budget value on its very next
+// call, without any tablet-server restart.
+//
+// Setup: three DiskRowSets with interleaved (and therefore overlapping) key
+// ranges. Each is tiny (well below 1 MB) so it is assigned the 1 MB minimum
+// weight by RowSetInfo. The budget values are chosen so that:
+//
+//   budget = 1 MB - at most one rowset can fit in the knapsack (1*1 MB = 1);
+//                   two would overflow (2*1 MB > 1 MB budget).
+//   budget = 4 MB - all three rowsets fit (3*1 MB <= 4 MB budget) and,
+//                   because they overlap, selecting all three maximises the
+//                   compaction quality score.
+TEST_F(TestCompactionBudgetRuntimeFlag, TestFlagDrivesPickRowSets) {
+  // Build three DiskRowSets with interleaved keys so that every pair of
+  // rowsets has overlapping key bounds:
+  //
+  // RS0: keys  0, 3, 6, ..., 297  ->  ["0000000000", "0000000297"]
+  // RS1: keys  1, 4, 7, ..., 298  ->  ["0000000001", "0000000298"]
+  // RS2: keys  2, 5, 8, ..., 299  ->  ["0000000002", "0000000299"]
+  {
+    LocalTabletWriter writer(tablet().get(), &client_schema());
+    KuduPartialRow row(&client_schema());
+    constexpr int kStride = 3;   // one row per RS per stride
+    constexpr int kTotal  = 300; // 100 rows per RS
+    for (int rs = 0; rs < kStride; rs++) {
+      for (int i = rs; i < kTotal; i += kStride) {
+        ASSERT_OK(row.SetStringCopy("key", StringPrintf("%010d", i)));
+        ASSERT_OK(row.SetInt64("val", i));
+        ASSERT_OK(writer.Insert(row));
+      }
+      ASSERT_OK(tablet()->Flush());
+    }
+  }
+
+  // Phase 1: tight budget - no rowsets should be selected.
+  FLAGS_tablet_compaction_budget_mb = 1;
+  {
+    RowSetsInCompactionOrFlush input;
+    ASSERT_OK(tablet()->PickRowSetsToCompact(&input, Tablet::COMPACT_NO_FLAGS));
+    // Selecting a single rowset produces no merge benefit (nothing to combine
+    // it with under a 1 MB budget), so the quality score is effectively zero
+    // and falls below FLAGS_compaction_minimum_improvement. The policy should
+    // therefore pick nothing.
+    ASSERT_EQ(0, input.num_rowsets())
+        << "Expected 0 rowsets with budget=1 MB, got "
+        << input.num_rowsets();
+    // Locks released when 'input' goes out of scope.
+  }
+
+  // Phase 2: raised budget - all three overlapping rowsets selected.
+  FLAGS_tablet_compaction_budget_mb = 4;
+  {
+    RowSetsInCompactionOrFlush input;
+    ASSERT_OK(tablet()->PickRowSetsToCompact(&input, Tablet::COMPACT_NO_FLAGS));
+    // All three rowsets fit (3 MB - 4 MB) and their key-range overlap gives a
+    // positive quality score, so all three should be selected together.
+    ASSERT_EQ(3, input.num_rowsets())
+        << "Expected all 3 rowsets with budget=4 MB, got "
+        << input.num_rowsets();
+  }
+}
+
 } // namespace tablet
 } // namespace kudu
diff --git a/src/kudu/tablet/compaction_policy.cc b/src/kudu/tablet/compaction_policy.cc
index 6da9b205b..34c73e12a 100644
--- a/src/kudu/tablet/compaction_policy.cc
+++ b/src/kudu/tablet/compaction_policy.cc
@@ -51,6 +51,12 @@ using std::make_optional;
 using std::vector;
 using strings::Substitute;
 
+// --tablet_compaction_budget_mb is defined in tablet.cc because it is a
+// tablet-level property. It is declared here so that BudgetedCompactionPolicy
+// can read the current value directly inside PickRowSets(), picking up any
+// runtime change without needing the caller to push the value in.
+DECLARE_uint32(tablet_compaction_budget_mb);
+
 DEFINE_int64(budgeted_compaction_target_rowset_size, 32 * 1024 * 1024,
              "The target size in bytes for DiskRowSets produced by flushes or "
              "compactions when the budgeted compaction policy is used.");
@@ -150,11 +156,9 @@ static const double kSupportAdjust = 1.003;
 // BudgetedCompactionPolicy
 ////////////////////////////////////////////////////////////
 
-BudgetedCompactionPolicy::BudgetedCompactionPolicy(int size_budget_mb,
-                                                   const TabletMetrics* metrics)
-    : size_budget_mb_(size_budget_mb),
+BudgetedCompactionPolicy::BudgetedCompactionPolicy(const TabletMetrics* metrics)
+    : size_budget_mb_(FLAGS_tablet_compaction_budget_mb),
       metrics_(metrics) {
-  CHECK_GT(size_budget_mb, 0);
 }
 
 uint64_t BudgetedCompactionPolicy::target_rowset_size() const {
@@ -520,7 +524,16 @@ Status BudgetedCompactionPolicy::PickRowSets(
   DCHECK(picked);
   DCHECK(quality);
 
-  vector<RowSetInfo> asc_min_key, asc_max_key;
+  // Snapshot the runtime flag once. This ensures RunApproximation() and
+  // RunExact() see the same immutable budget for the entire selection, even if
+  // an operator changes FLAGS_tablet_compaction_budget_mb concurrently. The
+  // flag is defined in tablet.cc (a tablet-level property) and declared here
+  // via DECLARE_uint32 so BudgetedCompactionPolicy can read it without the
+  // caller having to push the value in.
+  size_budget_mb_ = FLAGS_tablet_compaction_budget_mb;
+
+  vector<RowSetInfo> asc_min_key;
+  vector<RowSetInfo> asc_max_key;
   SetupKnapsackInput(tree, &asc_min_key, &asc_max_key);
   if (asc_max_key.empty()) {
     if (log) {
diff --git a/src/kudu/tablet/compaction_policy.h b/src/kudu/tablet/compaction_policy.h
index e04e7442c..56559d870 100644
--- a/src/kudu/tablet/compaction_policy.h
+++ b/src/kudu/tablet/compaction_policy.h
@@ -78,8 +78,7 @@ class CompactionPolicy {
 // See docs/design-docs/compaction-policy.md for details.
 class BudgetedCompactionPolicy : public CompactionPolicy {
  public:
-  explicit BudgetedCompactionPolicy(int size_budget_mb,
-                                    const TabletMetrics* metrics = nullptr);
+  explicit BudgetedCompactionPolicy(const TabletMetrics* metrics = nullptr);
 
   Status PickRowSets(const RowSetTree &tree,
                      CompactionSelection* picked,
@@ -126,7 +125,11 @@ class BudgetedCompactionPolicy : public CompactionPolicy {
                 const std::vector<double>& best_upper_bounds,
                 SolutionAndValue* best_solution) const;
 
-  const size_t size_budget_mb_;
+  // The compaction size budget in MB. Initialized at construction time and
+  // refreshed at the start of every PickRowSets() call from
+  // FLAGS_tablet_compaction_budget_mb, so that runtime changes to the flag
+  // are picked up on the next compaction without a tablet-server restart.
+  size_t size_budget_mb_;
   const TabletMetrics* metrics_;
 };
 
diff --git a/src/kudu/tablet/tablet.cc b/src/kudu/tablet/tablet.cc
index 6b0eedb6e..6b0872874 100644
--- a/src/kudu/tablet/tablet.cc
+++ b/src/kudu/tablet/tablet.cc
@@ -108,10 +108,11 @@ DEFINE_bool(prevent_kudu_2233_corruption, true,
             "Whether or not to prevent KUDU-2233 corruptions. Used for testing only!");
 TAG_FLAG(prevent_kudu_2233_corruption, unsafe);
 
-DEFINE_int32(tablet_compaction_budget_mb, 256,
+DEFINE_uint32(tablet_compaction_budget_mb, 256,
              "Budget for a single compaction. Consider setting this back to 128 if "
              "--rowset_deltas_size_include_undo is set to false.");
 TAG_FLAG(tablet_compaction_budget_mb, experimental);
+TAG_FLAG(tablet_compaction_budget_mb, runtime);
 
 DEFINE_int32(tablet_bloom_block_size, 4096,
              "Block size of the bloom filters used for tablet keys.");
@@ -385,8 +386,7 @@ Tablet::Tablet(scoped_refptr<TabletMetadata> metadata,
         ->AutoDetach(&metric_detacher_);
   }
 
-  compaction_policy_.reset(new BudgetedCompactionPolicy(
-      FLAGS_tablet_compaction_budget_mb, metrics_.get()));
+  compaction_policy_.reset(new BudgetedCompactionPolicy(metrics_.get()));
 
   if (FLAGS_tablet_throttler_rpc_per_sec > 0 || FLAGS_tablet_throttler_bytes_per_sec > 0) {
     throttler_.reset(new Throttler(FLAGS_tablet_throttler_rpc_per_sec,
diff --git a/src/kudu/tablet/tablet.h b/src/kudu/tablet/tablet.h
index c01bf4a55..1ccfd258d 100644
--- a/src/kudu/tablet/tablet.h
+++ b/src/kudu/tablet/tablet.h
@@ -551,6 +551,7 @@ class Tablet {
   FRIEND_TEST(TabletReplicaTest, TestActiveOpPreventsLogGC);
   FRIEND_TEST(TabletReplicaTest, TestDMSAnchorPreventsLogGC);
   FRIEND_TEST(TabletReplicaTest, TestRestartAfterGCDeletedRowsets);
+  FRIEND_TEST(TestCompactionBudgetRuntimeFlag, TestFlagDrivesPickRowSets);
   FRIEND_TEST(TestHighMemCompaction, HighDeltaVolumeWithFrequentFlushForRowsetCompaction);
   FRIEND_TEST(TestMajorDeltaCompaction, TestCarryDeletesOver);
   FRIEND_TEST(TestMajorDeltaCompaction, TestCompact);