forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adaptive enable the exchange compression with Thompson sampling
Signed-off-by: Murphy <[email protected]>
- Loading branch information
1 parent
e267ea0
commit bc15ba8
Showing
12 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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 "serde/compress_strategy.h" | ||
|
||
#include <cmath> | ||
|
||
#include "common/config.h" | ||
|
||
namespace starrocks::serde { | ||
|
||
CompressStrategy::CompressStrategy() : _rd(), _gen(_rd()), _dis(0.0, 1.0) {} | ||
|
||
void CompressStrategy::feedback(uint64_t uncompressed_bytes, uint64_t compressed_bytes, uint64_t serialization_time_ns, | ||
uint64_t compression_time_ns) { | ||
// TODO: consider the compression_time as reward factor | ||
double compress_ratio = (uncompressed_bytes + 1) / (compressed_bytes + 1); | ||
double reward_ratio = compress_ratio / config::lz4_expected_compression_ratio; | ||
if (reward_ratio > 1.0) { | ||
_alpha += reward_ratio * reward_ratio; | ||
} else { | ||
_beta += 1 / (reward_ratio * reward_ratio); | ||
} | ||
} | ||
|
||
bool CompressStrategy::decide() { | ||
double theta = _dis(_gen); | ||
double probability = _alpha / (_alpha + _beta); | ||
return theta < probability; | ||
} | ||
|
||
} // namespace starrocks::serde |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed 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 | ||
// | ||
// https://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. | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <random> | ||
|
||
namespace starrocks::serde { | ||
|
||
// Compression strategy based on Thompson Sampling | ||
class CompressStrategy { | ||
public: | ||
CompressStrategy(); | ||
~CompressStrategy() = default; | ||
|
||
// Give the feedback based on previous compression | ||
void feedback(uint64_t uncompressed_bytes, uint64_t compressed_bytes, uint64_t serialization_time_ns, | ||
uint64_t compression_time_ns); | ||
|
||
// Make the decision for the next compression | ||
bool decide(); | ||
|
||
private: | ||
// Generate a random number within [0, 1] | ||
std::random_device _rd; | ||
std::mt19937 _gen; | ||
std::uniform_real_distribution<double> _dis; | ||
|
||
// Thompson sampling parameters, biased for TRUE value | ||
double _alpha = 3.0; // Success count | ||
double _beta = 1.0; // Failure count | ||
}; | ||
|
||
} // namespace starrocks::serde |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters