Trace util is helpful for diagnosing compaction performance problems,
we can get trace log for base compaction like:
```
W0610 11:26:33.804431 56452 storage_engine.cpp:552] Trace:
0610 11:23:03.727535 (+ 0us) storage_engine.cpp:554] start to perform base compaction
0610 11:23:03.728961 (+ 1426us) storage_engine.cpp:560] found best tablet 546859
0610 11:23:03.728963 (+ 2us) base_compaction.cpp:40] got base compaction lock
0610 11:23:03.729029 (+ 66us) base_compaction.cpp:44] rowsets picked
0610 11:24:51.784439 (+108055410us) compaction.cpp:46] got concurrency lock and start to do compaction
0610 11:24:51.784818 (+ 379us) compaction.cpp:74] prepare finished
0610 11:26:33.359265 (+101574447us) compaction.cpp:87] merge rowsets finished
0610 11:26:33.484481 (+125216us) compaction.cpp:102] output rowset built
0610 11:26:33.484482 (+ 1us) compaction.cpp:106] check correctness finished
0610 11:26:33.513197 (+ 28715us) compaction.cpp:110] modify rowsets finished
0610 11:26:33.513300 (+ 103us) base_compaction.cpp:49] compaction finished
0610 11:26:33.513441 (+ 141us) base_compaction.cpp:56] unused rowsets have been moved to GC queue
Metrics: {"filtered_rows":0,"input_row_num":3346807,"input_rowsets_count":42,"input_rowsets_data_size":1256413170,"input_segments_num":44,"merge_rowsets_latency_us":101574444,"merged_rows":0,"output_row_num":3346807,"output_rowset_data_size":1228439659,"output_segments_num":6}
```
for cumulative compaction like:
```
W0610 11:14:18.714366 56468 storage_engine.cpp:518] Trace:
0610 11:14:08.068484 (+ 0us) storage_engine.cpp:520] start to perform cumulative compaction
0610 11:14:08.069844 (+ 1360us) storage_engine.cpp:526] found best tablet 547083
0610 11:14:08.069846 (+ 2us) cumulative_compaction.cpp:42] got cumulative compaction lock
0610 11:14:08.069947 (+ 101us) cumulative_compaction.cpp:46] calculated cumulative point
0610 11:14:08.070141 (+ 194us) cumulative_compaction.cpp:50] rowsets picked
0610 11:14:08.070143 (+ 2us) compaction.cpp:46] got concurrency lock and start to do compaction
0610 11:14:08.070518 (+ 375us) compaction.cpp:74] prepare finished
0610 11:14:15.389893 (+7319375us) compaction.cpp:87] merge rowsets finished
0610 11:14:15.390916 (+ 1023us) compaction.cpp:102] output rowset built
0610 11:14:15.390917 (+ 1us) compaction.cpp:106] check correctness finished
0610 11:14:15.409460 (+ 18543us) compaction.cpp:110] modify rowsets finished
0610 11:14:15.409496 (+ 36us) cumulative_compaction.cpp:55] compaction finished
0610 11:14:15.410138 (+ 642us) cumulative_compaction.cpp:65] unused rowsets have been moved to GC queue
Metrics: {"filtered_rows":0,"input_row_num":136707,"input_rowsets_count":302,"input_rowsets_data_size":76617836,"input_segments_num":302,"merge_rowsets_latency_us":7319372,"merged_rows":0,"output_row_num":136707,"output_rowset_data_size":53893280,"output_segments_num":1}
```
146 lines
5.9 KiB
C++
146 lines
5.9 KiB
C++
// Licensed to the Apache Software Foundation (ASF) under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
#include "olap/base_compaction.h"
|
|
#include "util/doris_metrics.h"
|
|
#include "util/trace.h"
|
|
|
|
namespace doris {
|
|
|
|
BaseCompaction::BaseCompaction(TabletSharedPtr tablet)
|
|
: Compaction(tablet)
|
|
{ }
|
|
|
|
BaseCompaction::~BaseCompaction() { }
|
|
|
|
OLAPStatus BaseCompaction::compact() {
|
|
if (!_tablet->init_succeeded()) {
|
|
return OLAP_ERR_INPUT_PARAMETER_ERROR;
|
|
}
|
|
|
|
MutexLock lock(_tablet->get_base_lock(), TRY_LOCK);
|
|
if (!lock.own_lock()) {
|
|
LOG(WARNING) << "another base compaction is running. tablet=" << _tablet->full_name();
|
|
return OLAP_ERR_BE_TRY_BE_LOCK_ERROR;
|
|
}
|
|
TRACE("got base compaction lock");
|
|
|
|
// 1. pick rowsets to compact
|
|
RETURN_NOT_OK(pick_rowsets_to_compact());
|
|
TRACE("rowsets picked");
|
|
TRACE_COUNTER_INCREMENT("input_rowsets_count", _input_rowsets.size());
|
|
|
|
// 2. do base compaction, merge rowsets
|
|
RETURN_NOT_OK(do_compaction());
|
|
TRACE("compaction finished");
|
|
|
|
// 3. set state to success
|
|
_state = CompactionState::SUCCESS;
|
|
|
|
// 4. garbage collect input rowsets after base compaction
|
|
RETURN_NOT_OK(gc_unused_rowsets());
|
|
TRACE("unused rowsets have been moved to GC queue");
|
|
|
|
// 5. add metric to base compaction
|
|
DorisMetrics::instance()->base_compaction_deltas_total.increment(_input_rowsets.size());
|
|
DorisMetrics::instance()->base_compaction_bytes_total.increment(_input_rowsets_size);
|
|
|
|
return OLAP_SUCCESS;
|
|
}
|
|
|
|
OLAPStatus BaseCompaction::pick_rowsets_to_compact() {
|
|
_input_rowsets.clear();
|
|
_tablet->pick_candicate_rowsets_to_base_compaction(&_input_rowsets);
|
|
if (_input_rowsets.size() <= 1) {
|
|
return OLAP_ERR_BE_NO_SUITABLE_VERSION;
|
|
}
|
|
|
|
std::sort(_input_rowsets.begin(), _input_rowsets.end(), Rowset::comparator);
|
|
RETURN_NOT_OK(check_version_continuity(_input_rowsets));
|
|
RETURN_NOT_OK(_check_rowset_overlapping(_input_rowsets));
|
|
|
|
if (_input_rowsets.size() == 2 && _input_rowsets[0]->end_version() == 1) {
|
|
// the tablet is with rowset: [0-1], [2-y]
|
|
// and [0-1] has no data. in this situation, no need to do base compaction.
|
|
return OLAP_ERR_BE_NO_SUITABLE_VERSION;
|
|
}
|
|
|
|
// 1. cumulative rowset must reach base_compaction_num_cumulative_deltas threshold
|
|
if (_input_rowsets.size() > config::base_compaction_num_cumulative_deltas) {
|
|
LOG(INFO) << "satisfy the base compaction policy. tablet="<< _tablet->full_name()
|
|
<< ", num_cumulative_rowsets=" << _input_rowsets.size() - 1
|
|
<< ", base_compaction_num_cumulative_rowsets=" << config::base_compaction_num_cumulative_deltas;
|
|
return OLAP_SUCCESS;
|
|
}
|
|
|
|
// 2. the ratio between base rowset and all input cumulative rowsets reachs the threshold
|
|
int64_t base_size = 0;
|
|
int64_t cumulative_total_size = 0;
|
|
for (auto& rowset : _input_rowsets) {
|
|
if (rowset->start_version() != 0) {
|
|
cumulative_total_size += rowset->data_disk_size();
|
|
} else {
|
|
base_size = rowset->data_disk_size();
|
|
}
|
|
}
|
|
|
|
double base_cumulative_delta_ratio = config::base_cumulative_delta_ratio;
|
|
if (base_size == 0) {
|
|
// base_size == 0 means this may be a base version [0-1], which has no data.
|
|
// set to 1 to void devide by zero
|
|
base_size = 1;
|
|
}
|
|
double cumulative_base_ratio = static_cast<double>(cumulative_total_size) / base_size;
|
|
|
|
if (cumulative_base_ratio > base_cumulative_delta_ratio) {
|
|
LOG(INFO) << "satisfy the base compaction policy. tablet=" << _tablet->full_name()
|
|
<< ", cumualtive_total_size=" << cumulative_total_size
|
|
<< ", base_size=" << base_size
|
|
<< ", cumulative_base_ratio=" << cumulative_base_ratio
|
|
<< ", policy_ratio=" << base_cumulative_delta_ratio;
|
|
return OLAP_SUCCESS;
|
|
}
|
|
|
|
// 3. the interval since last base compaction reachs the threshold
|
|
int64_t base_creation_time = _input_rowsets[0]->creation_time();
|
|
int64_t interval_threshold = config::base_compaction_interval_seconds_since_last_operation;
|
|
int64_t interval_since_last_base_compaction = time(NULL) - base_creation_time;
|
|
if (interval_since_last_base_compaction > interval_threshold) {
|
|
LOG(INFO) << "satisfy the base compaction policy. tablet=" << _tablet->full_name()
|
|
<< ", interval_since_last_base_compaction=" << interval_since_last_base_compaction
|
|
<< ", interval_threshold=" << interval_threshold;
|
|
return OLAP_SUCCESS;
|
|
}
|
|
|
|
LOG(INFO) << "don't satisfy the base compaction policy. tablet=" << _tablet->full_name()
|
|
<< ", num_cumulative_rowsets=" << _input_rowsets.size() - 1
|
|
<< ", cumulative_base_ratio=" << cumulative_base_ratio
|
|
<< ", interval_since_last_base_compaction=" << interval_since_last_base_compaction;
|
|
return OLAP_ERR_BE_NO_SUITABLE_VERSION;
|
|
}
|
|
|
|
OLAPStatus BaseCompaction::_check_rowset_overlapping(const vector<RowsetSharedPtr>& rowsets) {
|
|
for (auto& rs : rowsets) {
|
|
if (rs->rowset_meta()->is_segments_overlapping()) {
|
|
return OLAP_ERR_BE_SEGMENTS_OVERLAPPING;
|
|
}
|
|
}
|
|
return OLAP_SUCCESS;
|
|
}
|
|
|
|
} // namespace doris
|