Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] Fix disable base compaction with minute granularity & fe/be recover #52923

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[BugFix] Fix disable base compaction with minute granularity & fe/be …
…recover

Signed-off-by: meegoo <meegoo.sr@gmail.com>
  • Loading branch information
meegoo committed Nov 18, 2024
commit 5d2632ef039ae88fc375e71f49b9044befbad46b
44 changes: 30 additions & 14 deletions be/src/storage/compaction_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ void CompactionManager::update_candidates(std::vector<CompactionCandidate> candi
}
}
for (auto& candidate : candidates) {
if (_check_compaction_disabled(candidate)) {
continue;
}
if (candidate.tablet->enable_compaction()) {
VLOG(2) << "update candidate " << candidate.tablet->tablet_id() << " type "
<< starrocks::to_string(candidate.type) << " score " << candidate.score;
Expand Down Expand Up @@ -200,6 +203,25 @@ void CompactionManager::remove_candidate(int64_t tablet_id) {
}
}

bool CompactionManager::_check_compaction_disabled(const CompactionCandidate& candidate) {
if (candidate.type == CompactionType::BASE_COMPACTION &&
_table_to_disable_deadline_map.find(candidate.tablet->tablet_meta()->table_id()) !=
_table_to_disable_deadline_map.end()) {
int64_t deadline = _table_to_disable_deadline_map[candidate.tablet->tablet_meta()->table_id()];
if (deadline > 0 && UnixSeconds() < deadline) {
return true;
} else {
// disable compaction deadline has passed, remove it from map
_table_to_disable_deadline_map.erase(candidate.tablet->tablet_meta()->table_id());
// check if the tablet should compact now after the deadline
update_tablet_async(candidate.tablet);
LOG(INFO) << "remove disable table compaction, table_id:" << candidate.tablet->tablet_meta()->table_id()
<< ", deadline:" << deadline;
}
}
return false;
}

bool CompactionManager::_check_precondition(const CompactionCandidate& candidate) {
if (!candidate.tablet) {
LOG(WARNING) << "candidate with null tablet";
Expand All @@ -212,19 +234,6 @@ bool CompactionManager::_check_precondition(const CompactionCandidate& candidate
return false;
}

// check if the table base compaction is disabled
if (candidate.type == CompactionType::BASE_COMPACTION &&
_table_to_disable_deadline_map.find(tablet->tablet_meta()->table_id()) !=
_table_to_disable_deadline_map.end()) {
int64_t deadline = _table_to_disable_deadline_map[tablet->tablet_meta()->table_id()];
if (deadline > 0 && UnixSeconds() < deadline) {
VLOG(2) << "skip tablet:" << tablet->tablet_id() << " because table is disabled";
return false;
} else {
_table_to_disable_deadline_map.erase(tablet->tablet_meta()->table_id());
}
}

int64_t last_failure_ts = 0;
DataDir* data_dir = tablet->data_dir();
if (candidate.type == CUMULATIVE_COMPACTION) {
Expand Down Expand Up @@ -291,6 +300,10 @@ bool CompactionManager::pick_candidate(CompactionCandidate* candidate) {

auto iter = _compaction_candidates.begin();
while (iter != _compaction_candidates.end()) {
if (_check_compaction_disabled(*iter)) {
_compaction_candidates.erase(iter++);
continue;
}
if (_check_precondition(*iter)) {
*candidate = *iter;
_compaction_candidates.erase(iter);
Expand Down Expand Up @@ -654,8 +667,11 @@ int CompactionManager::get_waiting_task_num() {

void CompactionManager::disable_table_compaction(int64_t table_id, int64_t deadline) {
std::lock_guard lg(_candidates_mutex);
VLOG(2) << "disable table compaction, table_id:" << table_id << ", deadline:" << deadline;
if (_table_to_disable_deadline_map.find(table_id) == _table_to_disable_deadline_map.end()) {
LOG(INFO) << "start disable table compaction, table_id:" << table_id << ", deadline:" << deadline;
}
_table_to_disable_deadline_map[table_id] = deadline;
VLOG(2) << "disable table compaction, table_id:" << table_id << ", deadline:" << deadline;
}

} // namespace starrocks
1 change: 1 addition & 0 deletions be/src/storage/compaction_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class CompactionManager {
CompactionManager& operator=(CompactionManager&& compaction_manager) = delete;

void _dispatch_worker();
bool _check_compaction_disabled(const CompactionCandidate& candidate);
bool _check_precondition(const CompactionCandidate& candidate);
void _schedule();
void _notify();
Expand Down
2 changes: 2 additions & 0 deletions be/src/storage/compaction_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ void CompactionTask::run() {
_task_info.to_string());
if (!status.ok()) {
LOG(WARNING) << msg;
} else {
LOG(INFO) << msg;
}
}

Expand Down
7 changes: 7 additions & 0 deletions be/src/storage/tablet_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ class TabletMeta {

const TabletSchemaCSPtr& source_schema() const { return _source_schema; }

// for test
void TEST_set_table_id(int64_t table_id);

private:
int64_t _mem_usage() const { return sizeof(TabletMeta); }

Expand Down Expand Up @@ -312,6 +315,10 @@ inline int64_t TabletMeta::table_id() const {
return _table_id;
}

inline void TabletMeta::TEST_set_table_id(int64_t table_id) {
_table_id = table_id;
}

inline int64_t TabletMeta::partition_id() const {
return _partition_id;
}
Expand Down
40 changes: 35 additions & 5 deletions be/test/storage/compaction_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ TEST_F(CompactionManagerTest, test_disable_compaction) {
TabletSharedPtr tablet = std::make_shared<Tablet>();
TabletMetaSharedPtr tablet_meta = std::make_shared<TabletMeta>();
tablet_meta->set_tablet_id(i);
tablet_meta->TEST_set_table_id(1);
tablet->set_tablet_meta(tablet_meta);
tablet->set_data_dir(&data_dir);
tablet->set_tablet_state(TABLET_RUNNING);

CompactionCandidate candidate;
candidate.tablet = tablet;
candidate.score = i;
candidate.type = BASE_COMPACTION;
candidates.push_back(candidate);
}

Expand All @@ -204,9 +206,9 @@ TEST_F(CompactionManagerTest, test_disable_compaction) {

_engine->compaction_manager()->update_candidates(candidates);

{
ASSERT_EQ(10, _engine->compaction_manager()->candidates_size());
_engine->compaction_manager()->disable_table_compaction(1, UnixSeconds() + 5);

{
int64_t valid_condidates = 0;
while (true) {
CompactionCandidate candidate;
Expand All @@ -216,10 +218,38 @@ TEST_F(CompactionManagerTest, test_disable_compaction) {
}
++valid_condidates;
}
ASSERT_EQ(10, valid_condidates);
ASSERT_EQ(0, valid_condidates);
}
}

_engine->compaction_manager()->disable_table_compaction(0, UnixSeconds() + 3600);
TEST_F(CompactionManagerTest, test_remove_disable_compaction) {
std::vector<CompactionCandidate> candidates;
DataDir data_dir("./data_dir");
for (int i = 0; i < 10; i++) {
TabletSharedPtr tablet = std::make_shared<Tablet>();
TabletMetaSharedPtr tablet_meta = std::make_shared<TabletMeta>();
tablet_meta->set_tablet_id(i);
tablet_meta->TEST_set_table_id(2);
tablet->set_tablet_meta(tablet_meta);
tablet->set_data_dir(&data_dir);
tablet->set_tablet_state(TABLET_RUNNING);

CompactionCandidate candidate;
candidate.tablet = tablet;
candidate.score = i;
candidate.type = BASE_COMPACTION;
candidates.push_back(candidate);
}

std::random_device rd;
std::mt19937 g(rd());
std::shuffle(candidates.begin(), candidates.end(), g);

_engine->compaction_manager()->update_candidates(candidates);

_engine->compaction_manager()->disable_table_compaction(2, UnixSeconds());

sleep(1);

{
int64_t valid_condidates = 0;
Expand All @@ -231,7 +261,7 @@ TEST_F(CompactionManagerTest, test_disable_compaction) {
}
++valid_condidates;
}
ASSERT_EQ(0, valid_condidates);
ASSERT_EQ(10, valid_condidates);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ public Void visitModifyTablePropertiesClause(ModifyTablePropertiesClause clause,
schemaChangeHandler.updateTableMeta(db, tableName.getTbl(), properties,
TTabletMetaType.BASE_COMPACTION_FORBIDDEN_TIME_RANGES);
} catch (Exception e) {
LOG.warn("Failed to update base compaction forbidden time ranges: ", e);
throw new DdlException("Failed to update base compaction forbidden time ranges: " + e.getMessage());
LOG.warn("Failed to update base compaction forbidden time ranges: " + tableName.getTbl(), e);
throw new DdlException("Failed to update base compaction forbidden time ranges for "
+ tableName.getTbl() + ": " + e.getMessage());
}
} else if (properties.containsKey(PropertyAnalyzer.PROPERTIES_BINLOG_ENABLE) ||
properties.containsKey(PropertyAnalyzer.PROPERTIES_BINLOG_TTL) ||
Expand Down
15 changes: 15 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/OlapTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,18 @@ public void setBaseCompactionForbiddenTimeRanges(String baseCompactionForbiddenT
tableProperty.buildBaseCompactionForbiddenTimeRanges();
}

public void updateBaseCompactionForbiddenTimeRanges(boolean isDrop) {
try {
if (isDrop && getBaseCompactionForbiddenTimeRanges().isEmpty()) {
return;
}
GlobalStateMgr.getCurrentState().getCompactionControlScheduler().updateTableForbiddenTimeRanges(
getId(), isDrop ? "" : getBaseCompactionForbiddenTimeRanges());
} catch (Exception e) {
LOG.warn("Failed to update base compaction forbidden time ranges for " + getName(), e);
}
}

public TWriteQuorumType writeQuorum() {
if (tableProperty != null) {
return tableProperty.writeQuorum();
Expand Down Expand Up @@ -3126,6 +3138,7 @@ public void onReload() {
analyzePartitionInfo();
analyzeRollupIndexMeta();
tryToAssignIndexId();
updateBaseCompactionForbiddenTimeRanges(false);

// register constraints from global state manager
GlobalConstraintManager globalConstraintManager = GlobalStateMgr.getCurrentState().getGlobalConstraintManager();
Expand Down Expand Up @@ -3264,6 +3277,8 @@ public void onDrop(Database db, boolean force, boolean replay) {
sendDropAutoIncrementMapTask();
}

updateBaseCompactionForbiddenTimeRanges(true);

// unregister constraints from global state manager
GlobalConstraintManager globalConstraintManager = GlobalStateMgr.getCurrentState().getGlobalConstraintManager();
globalConstraintManager.unRegisterConstraint(this);
Expand Down
Loading
Loading