Skip to content

Commit

Permalink
*: add a new directory BaseFile (#8692)
Browse files Browse the repository at this point in the history
ref #8691
  • Loading branch information
Lloyd-Pottiger authored Jan 18, 2024
1 parent 5124839 commit a4df770
Show file tree
Hide file tree
Showing 64 changed files with 458 additions and 311 deletions.
1 change: 1 addition & 0 deletions dbms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(dbms_sources)

include(${TiFlash_SOURCE_DIR}/cmake/dbms_glob_sources.cmake)

add_headers_and_sources(tiflash_common_io src/BaseFile)
add_headers_and_sources(tiflash_common_io src/Common)
add_headers_and_sources(tiflash_common_io src/Common/HashTable)
add_headers_and_sources(tiflash_common_io src/Common/SyncPoint)
Expand Down
13 changes: 13 additions & 0 deletions dbms/src/BaseFile/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 PingCAP, Inc.
#
# 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
#
# 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.
174 changes: 174 additions & 0 deletions dbms/src/BaseFile/IORateLimitConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright 2023 PingCAP, Inc.
//
// 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
//
// 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 <BaseFile/IORateLimitConfig.h>
#include <Common/Logger.h>

#if !__clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
#include <cpptoml.h>
#if !__clang__
#pragma GCC diagnostic pop
#endif

namespace
{
template <typename T>
void readConfig(const std::shared_ptr<cpptoml::table> & table, const String & name, T & value)
{
#ifndef NDEBUG
if (!table->contains_qualified(name))
return;
#endif
if (auto p = table->get_qualified_as<typename std::remove_reference<decltype(value)>::type>(name); p)
{
value = *p;
}
}
} // namespace

namespace DB
{

void IORateLimitConfig::parse(const String & storage_io_rate_limit, const LoggerPtr & log)
{
std::istringstream ss(storage_io_rate_limit);
cpptoml::parser p(ss);
auto config = p.parse();

readConfig(config, "max_bytes_per_sec", max_bytes_per_sec);
readConfig(config, "max_read_bytes_per_sec", max_read_bytes_per_sec);
readConfig(config, "max_write_bytes_per_sec", max_write_bytes_per_sec);
readConfig(config, "foreground_write_weight", fg_write_weight);
readConfig(config, "background_write_weight", bg_write_weight);
readConfig(config, "foreground_read_weight", fg_read_weight);
readConfig(config, "background_read_weight", bg_read_weight);
readConfig(config, "emergency_pct", emergency_pct);
readConfig(config, "high_pct", high_pct);
readConfig(config, "medium_pct", medium_pct);
readConfig(config, "tune_base", tune_base);
readConfig(config, "min_bytes_per_sec", min_bytes_per_sec);
readConfig(config, "auto_tune_sec", auto_tune_sec);

use_max_bytes_per_sec = (max_read_bytes_per_sec == 0 && max_write_bytes_per_sec == 0);

LOG_DEBUG(log, "storage.io_rate_limit {}", toString());
}

std::string IORateLimitConfig::toString() const
{
return fmt::format(
"max_bytes_per_sec {} max_read_bytes_per_sec {} max_write_bytes_per_sec {} use_max_bytes_per_sec {} "
"fg_write_weight {} bg_write_weight {} fg_read_weight {} bg_read_weight {} fg_write_max_bytes_per_sec {} "
"bg_write_max_bytes_per_sec {} fg_read_max_bytes_per_sec {} bg_read_max_bytes_per_sec {} emergency_pct {} "
"high_pct {} "
"medium_pct {} tune_base {} min_bytes_per_sec {} auto_tune_sec {}",
max_bytes_per_sec,
max_read_bytes_per_sec,
max_write_bytes_per_sec,
use_max_bytes_per_sec,
fg_write_weight,
bg_write_weight,
fg_read_weight,
bg_read_weight,
getFgWriteMaxBytesPerSec(),
getBgWriteMaxBytesPerSec(),
getFgReadMaxBytesPerSec(),
getBgReadMaxBytesPerSec(),
emergency_pct,
high_pct,
medium_pct,
tune_base,
min_bytes_per_sec,
auto_tune_sec);
}

UInt64 IORateLimitConfig::readWeight() const
{
return fg_read_weight + bg_read_weight;
}

UInt64 IORateLimitConfig::writeWeight() const
{
return fg_write_weight + bg_write_weight;
}

UInt64 IORateLimitConfig::totalWeight() const
{
return readWeight() + writeWeight();
}

UInt64 IORateLimitConfig::getFgWriteMaxBytesPerSec() const
{
if (writeWeight() <= 0 || totalWeight() <= 0)
{
return 0;
}
return use_max_bytes_per_sec ? static_cast<UInt64>(1.0 * max_bytes_per_sec / totalWeight() * fg_write_weight)
: static_cast<UInt64>(1.0 * max_write_bytes_per_sec / writeWeight() * fg_write_weight);
}

UInt64 IORateLimitConfig::getBgWriteMaxBytesPerSec() const
{
if (writeWeight() <= 0 || totalWeight() <= 0)
{
return 0;
}
return use_max_bytes_per_sec ? static_cast<UInt64>(1.0 * max_bytes_per_sec / totalWeight() * bg_write_weight)
: static_cast<UInt64>(1.0 * max_write_bytes_per_sec / writeWeight() * bg_write_weight);
}

UInt64 IORateLimitConfig::getFgReadMaxBytesPerSec() const
{
if (readWeight() <= 0 || totalWeight() <= 0)
{
return 0;
}
return use_max_bytes_per_sec ? static_cast<UInt64>(1.0 * max_bytes_per_sec / totalWeight() * fg_read_weight)
: static_cast<UInt64>(1.0 * max_read_bytes_per_sec / readWeight() * fg_read_weight);
}

UInt64 IORateLimitConfig::getBgReadMaxBytesPerSec() const
{
if (readWeight() <= 0 || totalWeight() <= 0)
{
return 0;
}
return use_max_bytes_per_sec ? static_cast<UInt64>(1.0 * max_bytes_per_sec / totalWeight() * bg_read_weight)
: static_cast<UInt64>(1.0 * max_read_bytes_per_sec / readWeight() * bg_read_weight);
}

UInt64 IORateLimitConfig::getWriteMaxBytesPerSec() const
{
return getBgWriteMaxBytesPerSec() + getFgWriteMaxBytesPerSec();
}

UInt64 IORateLimitConfig::getReadMaxBytesPerSec() const
{
return getBgReadMaxBytesPerSec() + getFgReadMaxBytesPerSec();
}

bool IORateLimitConfig::operator==(const IORateLimitConfig & config) const
{
return config.max_bytes_per_sec == max_bytes_per_sec && config.max_read_bytes_per_sec == max_read_bytes_per_sec
&& config.max_write_bytes_per_sec == max_write_bytes_per_sec && config.bg_write_weight == bg_write_weight
&& config.fg_write_weight == fg_write_weight && config.bg_read_weight == bg_read_weight
&& config.fg_read_weight == fg_read_weight && config.emergency_pct == emergency_pct
&& config.high_pct == high_pct && config.medium_pct == medium_pct && config.tune_base == tune_base
&& config.min_bytes_per_sec == min_bytes_per_sec && config.auto_tune_sec == auto_tune_sec;
}

} // namespace DB
85 changes: 85 additions & 0 deletions dbms/src/BaseFile/IORateLimitConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2023 PingCAP, Inc.
//
// 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
//
// 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.

#pragma once

#include <common/logger_useful.h>
#include <common/types.h>

namespace DB
{

struct IORateLimitConfig
{
public:
// For disk that read bandwidth and write bandwith are calculated together, such as AWS's EBS.
UInt64 max_bytes_per_sec;
// For disk that read bandwidth and write bandwith are calculated separatly, such as GCP's persistent disks.
UInt64 max_read_bytes_per_sec;
UInt64 max_write_bytes_per_sec;

bool use_max_bytes_per_sec;

// Currently, IORateLimiter supports 4 I/O type: foreground write, foreground read, background write and background read.
// Initially, We calculate bandwidth for each I/O type according to the proportion of weights.
// If *_weight is 0, the corresponding I/O type is not limited.
UInt32 fg_write_weight;
UInt32 bg_write_weight;
UInt32 fg_read_weight;
UInt32 bg_read_weight;

Int32 emergency_pct;
Int32 high_pct;
Int32 medium_pct;

Int32 tune_base;
Int64 min_bytes_per_sec;

Int32 auto_tune_sec;

IORateLimitConfig()
: max_bytes_per_sec(0)
, max_read_bytes_per_sec(0)
, max_write_bytes_per_sec(0)
, use_max_bytes_per_sec(true)
, fg_write_weight(25)
, bg_write_weight(25)
, fg_read_weight(25)
, bg_read_weight(25)
, emergency_pct(96)
, high_pct(85)
, medium_pct(60)
, tune_base(2)
, min_bytes_per_sec(2 * 1024 * 1024)
, auto_tune_sec(5)
{}

void parse(const String & storage_io_rate_limit, const LoggerPtr & log);

std::string toString() const;

UInt64 getFgWriteMaxBytesPerSec() const;
UInt64 getBgWriteMaxBytesPerSec() const;
UInt64 getFgReadMaxBytesPerSec() const;
UInt64 getBgReadMaxBytesPerSec() const;
UInt64 getWriteMaxBytesPerSec() const;
UInt64 getReadMaxBytesPerSec() const;
UInt64 readWeight() const;
UInt64 writeWeight() const;
UInt64 totalWeight() const;

bool operator==(const IORateLimitConfig & config) const;
};

} // namespace DB
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <BaseFile/PosixRandomAccessFile.h>
#include <BaseFile/RateLimiter.h>
#include <Common/Exception.h>
#include <Common/ProfileEvents.h>
#include <Common/TiFlashMetrics.h>
#include <Encryption/PosixRandomAccessFile.h>
#include <Encryption/RateLimiter.h>
#include <Storages/S3/FileCache.h>
#include <fcntl.h>
#include <unistd.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

#pragma once

#include <BaseFile/RandomAccessFile.h>
#include <Common/CurrentMetrics.h>
#include <Encryption/RandomAccessFile.h>

#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <BaseFile/PosixWritableFile.h>
#include <Common/Exception.h>
#include <Common/ProfileEvents.h>
#include <Common/TiFlashMetrics.h>
#include <Encryption/PosixWritableFile.h>
#include <fcntl.h>
#include <unistd.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include <BaseFile/RateLimiter.h>
#include <BaseFile/WritableFile.h>
#include <Common/CurrentMetrics.h>
#include <Encryption/RateLimiter.h>
#include <Encryption/WritableFile.h>

#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <BaseFile/PosixWriteReadableFile.h>
#include <Common/Exception.h>
#include <Common/ProfileEvents.h>
#include <Common/TiFlashMetrics.h>
#include <Encryption/PosixWriteReadableFile.h>
#include <fcntl.h>
#include <unistd.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

#pragma once

#include <BaseFile/RateLimiter.h>
#include <BaseFile/WriteReadableFile.h>
#include <Common/CurrentMetrics.h>
#include <Encryption/RateLimiter.h>
#include <Encryption/WriteReadableFile.h>
#include <common/types.h>

namespace CurrentMetrics
Expand Down
14 changes: 14 additions & 0 deletions dbms/src/BaseFile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# BaseFile

BaseFile is a file system abstraction layer that provides a common interface for file system operations. All file system operations should be performed through BaseFile.

**Note**

> To avoid unnecessary dependencies, all classes in this directory should only depend on classes in this directory or `Common` directory.
- WritableFile: A writable file abstraction. It provides all the functions that a file system should support for writing.
- RandomAccessFile: A random access file abstraction. It provides all the functions that a file system should support for random access.
- WriteReadableFile: A writable and readable file abstraction. It provides all the functions that a file system should support for both writing and reading.
- PosixXxxFile: A file abstraction for posix file system.
- RateLimiter: Used to control read/write rate.
- fwd.h: Forward declaration of all classes in this directory. It is recommended to include this file in other header files instead of including the header files directly to avoid unnecessary dependencies.
File renamed without changes.
Loading

0 comments on commit a4df770

Please sign in to comment.