Skip to content

Commit

Permalink
[improvement](disk) pick disk randomly when usage is less than 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dataroaring committed Jan 1, 2024
1 parent 3c6c652 commit 05b0b45
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
3 changes: 1 addition & 2 deletions be/src/olap/data_dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,7 @@ size_t DataDir::tablet_size() const {
}

bool DataDir::reach_capacity_limit(int64_t incoming_data_size) {
double used_pct = (_disk_capacity_bytes - _available_bytes + incoming_data_size) /
(double)_disk_capacity_bytes;
double used_pct = get_usage(incoming_data_size);
int64_t left_bytes = _available_bytes - incoming_data_size;
if (used_pct >= config::storage_flood_stage_usage_percent / 100.0 &&
left_bytes <= config::storage_flood_stage_left_capacity_bytes) {
Expand Down
5 changes: 5 additions & 0 deletions be/src/olap/data_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class DataDir {

void disks_compaction_num_increment(int64_t delta);

double get_usage(int64_t incoming_data_size) const {
return _disk_capacity_bytes == 0 ? 0 :
(_disk_capacity_bytes - _available_bytes + incoming_data_size) / (double)_disk_capacity_bytes;
}

// Move tablet to trash.
Status move_to_trash(const std::string& tablet_path);

Expand Down
37 changes: 22 additions & 15 deletions be/src/olap/storage_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,28 +455,35 @@ std::vector<DataDir*> StorageEngine::get_stores_for_create_tablet(
std::lock_guard<std::mutex> l(_store_lock);
for (auto& it : _store_map) {
if (it.second->is_used()) {
if (_available_storage_medium_type_count == 1 ||
it.second->storage_medium() == storage_medium) {
if ((_available_storage_medium_type_count == 1 ||
it.second->storage_medium() == storage_medium) &&
!it.second->reach_capacity_limit(0)) {
stores.push_back(it.second);
}
}
}
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(stores.begin(), stores.end(), g);
// Two random choices
for (int i = 0; i < stores.size(); i++) {
int j = i + 1;
if (j < stores.size()) {
if (stores[i]->tablet_size() > stores[j]->tablet_size()) {
std::swap(stores[i], stores[j]);
}
std::shuffle(stores.begin() + j, stores.end(), g);
} else {
break;
std::sort(stores.begin(), stores.end(), [](DataDir* a, DataDir* b) {
return a->get_usage(0) < b->get_usage(0);
});

size_t seventy_percent_index = stores.size() - 1;
size_t eighty_five_percent_index = stores.size() - 1;
for (size_t index = 0; index < stores.size(); index++) {
// If the usage of the store is less than 70%, we choose disk randomly.
if (stores[index]->get_usage(0) > 0.7) {
seventy_percent_index = index;
}
if (stores[index]->get_usage(0) > 0.85) {
eighty_five_percent_index = index;
}
}

std::random_device rd;
std::mt19937 g(rd());
std::shuffle(stores.begin(), stores.begin() + seventy_percent_index, g);
std::shuffle(stores.begin() + seventy_percent_index, stores.begin() + eighty_five_percent_index, g);

return stores;
}

Expand Down

0 comments on commit 05b0b45

Please sign in to comment.