config增加设置wal文件大小配置

Signed-off-by: MengYao <mengyao17@huawei.com>
This commit is contained in:
MengYao 2024-11-20 22:18:28 +08:00
parent 8c270c5a05
commit e493885472
5 changed files with 57 additions and 4 deletions

View File

@ -31,7 +31,8 @@ public:
static constexpr int DB_JOURNAL_SIZE = 1024 * 1024; /* default file size : 1M */
static constexpr ssize_t DB_WAL_SIZE_LIMIT_MIN = 20 * 1024 * 1024; /* default wal file maximum size : 20M */
static constexpr ssize_t DB_WAL_WARNING_SIZE = 256 * 1024 * 1024; /* default wal file maximum size : 256M */
static constexpr ssize_t DB_WAL_SIZE_LIMIT_MAX = 512 * 1024 * 1024; /* default wal file maximum size : 512M */
static constexpr ssize_t DB_WAL_DEFAULT_SIZE = 0x20000000; /* default wal file size 512M */
static constexpr ssize_t DB_WAL_SIZE_LIMIT_MAX = 0x7FFFFFFF; /* default wal file maximum size : 2G - 1 */
static constexpr int WAL_AUTO_CHECKPOINT = 100; /* 100 pages */
static constexpr int APP_DEFAULT_UMASK = 0002;
static constexpr int SQLITE_MAX_COLUMN = 2000;

View File

@ -21,6 +21,7 @@
#include "rdb_errno.h"
#include "rdb_security_manager.h"
#include "string_utils.h"
#include "sqlite_global_config.h"
namespace OHOS::NativeRdb {
using namespace OHOS::Rdb;
@ -35,6 +36,9 @@ RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode,
{
name_ = StringUtils::ExtractFileName(name);
cryptoParam_.encryptKey_ = encryptKey;
walLimitSize_ = GlobalExpr::DB_WAL_DEFAULT_SIZE;
checkpointSize_ = GlobalExpr::DB_WAL_WARNING_SIZE;
startCheckpointSize_ = GlobalExpr::DB_WAL_SIZE_LIMIT_MIN;
}
RdbStoreConfig::~RdbStoreConfig()
@ -592,6 +596,36 @@ void RdbStoreConfig::SetPromiseInfo(PromiseInfo promiseInfo)
promiseInfo_ = promiseInfo;
}
ssize_t RdbStoreConfig::GetWalLimitSize() const
{
return walLimitSize_;
}
void RdbStoreConfig::SetWalLimitSize(ssize_t size)
{
if (size < GlobalExpr::DB_WAL_DEFAULT_SIZE) {
size = GlobalExpr::DB_WAL_DEFAULT_SIZE;
}
if (size > GlobalExpr::DB_WAL_SIZE_LIMIT_MAX) {
size = GlobalExpr::DB_WAL_SIZE_LIMIT_MAX;
}
walLimitSize_ = size;
// '(size >> 1) + (size >> 2)' Size of the WAL file that does not checkpoint within 5 minutes when sqlite_busy
checkpointSize_ = (size >> 1) + (size >> 2);
// '(size >> 5) + (size >> 7)' Size of the WAL file for starting checkpoint.
startCheckpointSize_ = (size >> 5) + (size >> 7);
}
ssize_t RdbStoreConfig::GetCheckpointSize() const
{
return checkpointSize_;
}
ssize_t RdbStoreConfig::GetStartCheckpointSize() const
{
return startCheckpointSize_;
}
void RdbStoreConfig::EnableRekey(bool enable)
{
autoRekey_ = enable;

View File

@ -983,11 +983,11 @@ int SqliteConnection::TryCheckPoint(bool timeout)
return E_ERROR;
}
if (size <= GlobalExpr::DB_WAL_SIZE_LIMIT_MIN) {
if (size <= config_.GetStartCheckpointSize()) {
return E_OK;
}
if (!timeout && size < GlobalExpr::DB_WAL_WARNING_SIZE) {
if (!timeout && size < config_.GetCheckpointSize()) {
return E_INNER_WARNING;
}
@ -1008,7 +1008,7 @@ int SqliteConnection::LimitWalSize()
std::string walName = sqlite3_filename_wal(sqlite3_db_filename(dbHandle_, "main"));
ssize_t fileSize = SqliteUtils::GetFileSize(walName);
if (fileSize < 0 || fileSize > GlobalExpr::DB_WAL_SIZE_LIMIT_MAX) {
if (fileSize < 0 || fileSize > config_.GetWalLimitSize()) {
LOG_ERROR("The WAL file size exceeds the limit, %{public}s size is %{public}zd",
SqliteUtils::Anonymous(walName).c_str(), fileSize);
return E_WAL_SIZE_OVER_LIMIT;

View File

@ -671,6 +671,14 @@ public:
PromiseInfo GetPromiseInfo() const;
void SetPromiseInfo(PromiseInfo promiseInfo);
ssize_t GetCheckpointSize() const;
ssize_t GetStartCheckpointSize() const;
ssize_t GetWalLimitSize() const;
void SetWalLimitSize(ssize_t size);
int32_t GetHaMode() const;
@ -720,6 +728,9 @@ private:
std::string syncMode_;
std::string databaseFileType;
PromiseInfo promiseInfo_;
ssize_t walLimitSize_;
ssize_t checkpointSize_;
ssize_t startCheckpointSize_;
// distributed rdb
std::string bundleName_;
std::string moduleName_;

View File

@ -255,6 +255,10 @@ public:
int32_t GetHaMode() const;
PromiseInfo GetPromiseInfo() const;
void SetPromiseInfo(PromiseInfo promiseInfo);
ssize_t GetCheckpointSize() const;
ssize_t GetStartCheckpointSize() const;
ssize_t GetWalLimitSize() const;
void SetWalLimitSize(ssize_t size);
void SetHaMode(int32_t haMode);
void SetScalarFunctions(const std::map<std::string, ScalarFunctionInfo> functions);
void SetCryptoParam(CryptoParam cryptoParam);
@ -295,6 +299,9 @@ private:
std::string syncMode_;
std::string databaseFileType;
PromiseInfo promiseInfo_;
ssize_t walLimitSize_;
ssize_t checkpointSize_;
ssize_t startCheckpointSize_;
// distributed rdb
std::string bundleName_;
std::string moduleName_;