patch 4.0

This commit is contained in:
wangzelin.wzl
2022-10-24 10:34:53 +08:00
parent 4ad6e00ec3
commit 93a1074b0c
10533 changed files with 2588271 additions and 2299373 deletions

View File

@ -18,28 +18,37 @@
#include "ob_tmp_file_cache.h"
#include "ob_tmp_file.h"
#include "ob_tmp_file_store.h"
#include "ob_store_file.h"
#include "ob_block_manager.h"
using namespace oceanbase::storage;
using namespace oceanbase::share;
namespace oceanbase {
namespace blocksstable {
namespace oceanbase
{
namespace blocksstable
{
ObTmpPageCacheKey::ObTmpPageCacheKey() : block_id_(-1), page_id_(-1), tenant_id_(0)
{}
ObTmpPageCacheKey::ObTmpPageCacheKey()
: block_id_(-1), page_id_(-1), tenant_id_(0)
{
}
ObTmpPageCacheKey::ObTmpPageCacheKey(const int64_t block_id, const int64_t page_id, const uint64_t tenant_id)
: block_id_(block_id), page_id_(page_id), tenant_id_(tenant_id)
{}
ObTmpPageCacheKey::ObTmpPageCacheKey(const int64_t block_id, const int64_t page_id,
const uint64_t tenant_id)
: block_id_(block_id), page_id_(page_id), tenant_id_(tenant_id)
{
}
ObTmpPageCacheKey::~ObTmpPageCacheKey()
{}
bool ObTmpPageCacheKey::operator==(const ObIKVCacheKey& other) const
{
const ObTmpPageCacheKey& other_key = reinterpret_cast<const ObTmpPageCacheKey&>(other);
return block_id_ == other_key.block_id_ && page_id_ == other_key.page_id_ && tenant_id_ == other_key.tenant_id_;
}
bool ObTmpPageCacheKey::operator ==(const ObIKVCacheKey &other) const
{
const ObTmpPageCacheKey &other_key = reinterpret_cast<const ObTmpPageCacheKey &> (other);
return block_id_ == other_key.block_id_
&& page_id_ == other_key.page_id_
&& tenant_id_ == other_key.tenant_id_;
}
uint64_t ObTmpPageCacheKey::get_tenant_id() const
@ -57,7 +66,7 @@ int64_t ObTmpPageCacheKey::size() const
return sizeof(*this);
}
int ObTmpPageCacheKey::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheKey*& key) const
int ObTmpPageCacheKey::deep_copy(char *buf, const int64_t buf_len, ObIKVCacheKey *&key) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(NULL == buf || buf_len < size())) {
@ -77,28 +86,32 @@ bool ObTmpPageCacheKey::is_valid() const
return OB_LIKELY(block_id_ > 0 && page_id_ >= 0 && tenant_id_ > 0 && size() > 0);
}
ObTmpPageCacheValue::ObTmpPageCacheValue(char* buf) : buf_(buf), size_(ObTmpMacroBlock::get_default_page_size())
{}
ObTmpPageCacheValue::ObTmpPageCacheValue(char *buf)
: buf_(buf), size_(ObTmpMacroBlock::get_default_page_size())
{
}
ObTmpPageCacheValue::~ObTmpPageCacheValue()
{}
{
}
int64_t ObTmpPageCacheValue::size() const
{
return sizeof(*this) + size_;
}
int ObTmpPageCacheValue::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheValue*& value) const
int ObTmpPageCacheValue::deep_copy(char *buf, const int64_t buf_len, ObIKVCacheValue *&value) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(nullptr == buf || buf_len < size())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), KP(buf), K(buf_len), "request_size", size());
STORAGE_LOG(WARN, "invalid arguments", K(ret), KP(buf), K(buf_len),
"request_size", size());
} else if (OB_UNLIKELY(!is_valid())) {
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "invalid tmp page cache value", K(ret));
} else {
ObTmpPageCacheValue* pblk_value = new (buf) ObTmpPageCacheValue(buf + sizeof(*this));
ObTmpPageCacheValue *pblk_value = new (buf) ObTmpPageCacheValue(buf + sizeof(*this));
MEMCPY(buf + sizeof(*this), buf_, size() - sizeof(*this));
pblk_value->size_ = size_;
value = pblk_value;
@ -106,10 +119,13 @@ int ObTmpPageCacheValue::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheV
return ret;
}
int ObTmpPageCache::prefetch(const ObTmpPageCacheKey &key, const ObTmpBlockIOInfo &info, ObMacroBlockHandle &mb_handle)
int ObTmpPageCache::prefetch(
const ObTmpPageCacheKey &key,
const ObTmpBlockIOInfo &info,
ObMacroBlockHandle &mb_handle)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!key.is_valid())) {
if (OB_UNLIKELY(!key.is_valid() )) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "Invalid arguments", K(ret), K(key));
} else {
@ -138,7 +154,9 @@ int ObTmpPageCache::prefetch(const ObTmpPageCacheKey &key, const ObTmpBlockIOInf
}
int ObTmpPageCache::prefetch(
const ObTmpBlockIOInfo &info, const common::ObIArray<ObTmpPageIOInfo> &page_io_infos, ObMacroBlockHandle &mb_handle)
const ObTmpBlockIOInfo &info,
const common::ObIArray<ObTmpPageIOInfo> &page_io_infos,
ObMacroBlockHandle &mb_handle)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(page_io_infos.count() <= 0)) {
@ -150,7 +168,7 @@ int ObTmpPageCache::prefetch(
callback.offset_ = info.offset_;
callback.buf_size_ = info.size_;
callback.allocator_ = &allocator_;
void* buf = allocator_.alloc(sizeof(common::ObSEArray<ObTmpPageIOInfo, ObTmpFilePageBuddy::MAX_PAGE_NUMS>));
void *buf = allocator_.alloc(sizeof(common::ObSEArray<ObTmpPageIOInfo, ObTmpFilePageBuddy::MAX_PAGE_NUMS>));
if (NULL == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
STORAGE_LOG(WARN, "fail to alloc a buf", K(ret), K(info));
@ -178,25 +196,26 @@ int ObTmpPageCache::prefetch(
return ret;
}
int ObTmpPageCache::get_cache_page(const ObTmpPageCacheKey& key, ObTmpPageValueHandle& handle)
int ObTmpPageCache::get_cache_page(const ObTmpPageCacheKey &key, ObTmpPageValueHandle &handle)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!key.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key));
} else {
const ObTmpPageCacheValue* value = NULL;
const ObTmpPageCacheValue *value = NULL;
if (OB_FAIL(get(key, value, handle.handle_))) {
if (OB_UNLIKELY(OB_ENTRY_NOT_EXIST != ret)) {
STORAGE_LOG(WARN, "fail to get key from page cache", K(ret));
} else {
EVENT_INC(ObStatEventIds::TMP_PAGE_CACHE_MISS);
}
EVENT_INC(ObStatEventIds::TMP_PAGE_CACHE_MISS);
} else {
if (OB_ISNULL(value)) {
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "unexpected error, the value must not be NULL", K(ret));
} else {
handle.value_ = const_cast<ObTmpPageCacheValue*>(value);
handle.value_ = const_cast<ObTmpPageCacheValue *>(value);
EVENT_INC(ObStatEventIds::TMP_PAGE_CACHE_HIT);
}
}
@ -205,7 +224,8 @@ int ObTmpPageCache::get_cache_page(const ObTmpPageCacheKey& key, ObTmpPageValueH
}
ObTmpPageCache::ObITmpPageIOCallback::ObITmpPageIOCallback()
: cache_(NULL), allocator_(NULL), offset_(0), buf_size_(0), io_buf_(NULL), io_buf_size_(0), data_buf_(NULL)
: cache_(NULL), allocator_(NULL), offset_(0), buf_size_(0), io_buf_(NULL),
io_buf_size_(0), data_buf_(NULL)
{
static_assert(sizeof(*this) <= CALLBACK_BUF_SIZE, "IOCallback buf size not enough");
}
@ -220,7 +240,8 @@ ObTmpPageCache::ObITmpPageIOCallback::~ObITmpPageIOCallback()
}
}
int ObTmpPageCache::ObITmpPageIOCallback::alloc_io_buf(char*& io_buf, int64_t& io_buf_size, int64_t& aligned_offset)
int ObTmpPageCache::ObITmpPageIOCallback::alloc_io_buf(
char *&io_buf, int64_t &io_buf_size, int64_t &aligned_offset)
{
int ret = OB_SUCCESS;
io_buf_size = 0;
@ -230,7 +251,7 @@ int ObTmpPageCache::ObITmpPageIOCallback::alloc_io_buf(char*& io_buf, int64_t& i
if (OB_UNLIKELY(NULL == allocator_)) {
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "Invalid data, the allocator is NULL, ", K(ret));
} else if (OB_UNLIKELY(NULL == (io_buf_ = (char*)(allocator_->alloc(io_buf_size_))))) {
} else if (OB_UNLIKELY(NULL == (io_buf_ = (char*) (allocator_->alloc(io_buf_size_))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
STORAGE_LOG(ERROR, "Fail to allocate memory, ", K(ret));
} else {
@ -240,25 +261,28 @@ int ObTmpPageCache::ObITmpPageIOCallback::alloc_io_buf(char*& io_buf, int64_t& i
return ret;
}
int ObTmpPageCache::ObITmpPageIOCallback::process_page(const ObTmpPageCacheKey& key, const ObTmpPageCacheValue& value)
int ObTmpPageCache::ObITmpPageIOCallback::process_page(
const ObTmpPageCacheKey &key, const ObTmpPageCacheValue &value)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!key.is_valid() || !value.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key), K(value));
} else if (OB_FAIL(cache_->put(key, value, true /*overwrite*/))) {
STORAGE_LOG(WARN, "fail to put row to row cache", K(ret), K(key), K(value));
} else if (OB_FAIL(cache_->put(key, value, true/*overwrite*/))) {
STORAGE_LOG(WARN, "fail to put tmp page into cache", K(ret), K(key), K(value));
}
return ret;
}
ObTmpPageCache::ObTmpPageIOCallback::ObTmpPageIOCallback() : key_()
ObTmpPageCache::ObTmpPageIOCallback::ObTmpPageIOCallback()
: key_()
{
static_assert(sizeof(*this) <= CALLBACK_BUF_SIZE, "IOCallback buf size not enough");
}
ObTmpPageCache::ObTmpPageIOCallback::~ObTmpPageIOCallback()
{}
{
}
int ObTmpPageCache::ObTmpPageIOCallback::inner_process(const bool is_success)
{
@ -267,7 +291,7 @@ int ObTmpPageCache::ObTmpPageIOCallback::inner_process(const bool is_success)
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "Invalid tmp page cache callback, ", KP_(cache), K(ret));
} else if (is_success) {
ObTmpPageCacheValue value(const_cast<char*>(get_data()));
ObTmpPageCacheValue value(const_cast<char *>(get_data()));
if (OB_FAIL(process_page(key_, value))) {
STORAGE_LOG(WARN, "fail to process tmp page cache in callback", K(ret));
}
@ -285,8 +309,8 @@ int64_t ObTmpPageCache::ObTmpPageIOCallback::size() const
return sizeof(*this);
}
int ObTmpPageCache::ObTmpPageIOCallback::inner_deep_copy(
char* buf, const int64_t buf_len, ObIOCallback*& callback) const
int ObTmpPageCache::ObTmpPageIOCallback::inner_deep_copy(char *buf,
const int64_t buf_len, ObIOCallback *&callback) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(NULL == buf || buf_len < size())) {
@ -296,25 +320,27 @@ int ObTmpPageCache::ObTmpPageIOCallback::inner_deep_copy(
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "The tmp page io callback is not valid, ", KP_(cache), KP_(allocator), K(ret));
} else {
ObTmpPageIOCallback* pcallback = new (buf) ObTmpPageIOCallback();
ObTmpPageIOCallback *pcallback = new (buf) ObTmpPageIOCallback();
*pcallback = *this;
callback = pcallback;
}
return ret;
}
const char* ObTmpPageCache::ObTmpPageIOCallback::get_data()
const char *ObTmpPageCache::ObTmpPageIOCallback::get_data()
{
return data_buf_;
}
ObTmpPageCache::ObTmpMultiPageIOCallback::ObTmpMultiPageIOCallback() : page_io_infos_()
ObTmpPageCache::ObTmpMultiPageIOCallback::ObTmpMultiPageIOCallback()
: page_io_infos_()
{
static_assert(sizeof(*this) <= CALLBACK_BUF_SIZE, "IOCallback buf size not enough");
}
ObTmpPageCache::ObTmpMultiPageIOCallback::~ObTmpMultiPageIOCallback()
{}
{
}
int ObTmpPageCache::ObTmpMultiPageIOCallback::inner_process(const bool is_success)
{
@ -323,9 +349,11 @@ int ObTmpPageCache::ObTmpMultiPageIOCallback::inner_process(const bool is_succes
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "Invalid tmp page cache callback, ", KP_(cache), K(ret));
} else if (is_success) {
char* buf = const_cast<char*>(get_data());
char *buf = const_cast<char *>(get_data());
for (int32_t i = 0; OB_SUCC(ret) && i < page_io_infos_->count(); i++) {
int64_t offset = page_io_infos_->at(i).key_.get_page_id() * ObTmpMacroBlock::get_default_page_size() - offset_;
int64_t offset = page_io_infos_->at(i).key_.get_page_id()
* ObTmpMacroBlock::get_default_page_size() - offset_;
offset += ObTmpMacroBlock::get_header_padding();
ObTmpPageCacheValue value(buf + offset);
if (OB_FAIL(process_page(page_io_infos_->at(i).key_, value))) {
STORAGE_LOG(WARN, "fail to process tmp page cache in callback", K(ret));
@ -347,8 +375,8 @@ int64_t ObTmpPageCache::ObTmpMultiPageIOCallback::size() const
return sizeof(*this);
}
int ObTmpPageCache::ObTmpMultiPageIOCallback::inner_deep_copy(
char* buf, const int64_t buf_len, ObIOCallback*& callback) const
int ObTmpPageCache::ObTmpMultiPageIOCallback::inner_deep_copy(char *buf,
const int64_t buf_len, ObIOCallback *&callback) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(NULL == buf || buf_len < size())) {
@ -358,62 +386,60 @@ int ObTmpPageCache::ObTmpMultiPageIOCallback::inner_deep_copy(
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "The tmp page io callback is not valid, ", KP_(cache), KP_(allocator), K(ret));
} else {
ObTmpMultiPageIOCallback* pcallback = new (buf) ObTmpMultiPageIOCallback();
ObTmpMultiPageIOCallback *pcallback = new (buf) ObTmpMultiPageIOCallback();
*pcallback = *this;
callback = pcallback;
}
return ret;
}
const char* ObTmpPageCache::ObTmpMultiPageIOCallback::get_data()
const char *ObTmpPageCache::ObTmpMultiPageIOCallback::get_data()
{
return data_buf_;
}
int ObTmpPageCache::read_io(const ObTmpBlockIOInfo& io_info, ObITmpPageIOCallback& callback, ObMacroBlockHandle& handle)
int ObTmpPageCache::read_io(const ObTmpBlockIOInfo &io_info, ObITmpPageIOCallback &callback,
ObMacroBlockHandle &handle)
{
int ret = OB_SUCCESS;
common::ObArenaAllocator allocator(ObModIds::OB_MACRO_FILE);
ObStoreFileCtx file_ctx(allocator);
ObMacroBlockCtx macro_block_ctx; // TODO(): fix it for ofs later
file_ctx.file_system_type_ = STORE_FILE_SYSTEM_LOCAL;
macro_block_ctx.file_ctx_ = &file_ctx;
// fill the read info
ObMacroBlockReadInfo read_info;
read_info.io_desc_ = io_info.io_desc_;
macro_block_ctx.sstable_block_id_.macro_block_id_ = io_info.macro_block_id_;
read_info.macro_block_ctx_ = &macro_block_ctx;
read_info.macro_block_id_ = io_info.macro_block_id_;
read_info.io_callback_ = &callback;
common::align_offset_size(io_info.offset_, io_info.size_, read_info.offset_, read_info.size_);
handle.set_file(file_handle_.get_storage_file());
if (OB_FAIL(file_handle_.get_storage_file()->async_read_block(read_info, handle))) {
if (OB_FAIL(ObBlockManager::async_read_block(read_info, handle))) {
STORAGE_LOG(WARN, "fail to async read block", K(ret));
}
return ret;
}
ObTmpPageCache& ObTmpPageCache::get_instance()
ObTmpPageCache &ObTmpPageCache::get_instance()
{
static ObTmpPageCache instance;
return instance;
}
ObTmpPageCache::ObTmpPageCache() : file_handle_()
{}
ObTmpPageCache::ObTmpPageCache()
{
}
ObTmpPageCache::~ObTmpPageCache()
{}
{
}
int ObTmpPageCache::init(const char* cache_name, const int64_t priority, const ObStorageFileHandle& file_handle)
int ObTmpPageCache::init(const char *cache_name, const int64_t priority)
{
int ret = OB_SUCCESS;
const int64_t mem_limit = 4 * 1024 * 1024 * 1024LL;
if (OB_FAIL((common::ObKVCache<ObTmpPageCacheKey, ObTmpPageCacheValue>::init(cache_name, priority)))) {
if (OB_FAIL((common::ObKVCache<ObTmpPageCacheKey, ObTmpPageCacheValue>::init(
cache_name, priority)))) {
STORAGE_LOG(WARN, "Fail to init kv cache, ", K(ret));
} else if (OB_FAIL(allocator_.init(mem_limit, OB_MALLOC_BIG_BLOCK_SIZE, OB_MALLOC_BIG_BLOCK_SIZE))) {
} else if (OB_FAIL(allocator_.init(mem_limit,
OB_MALLOC_BIG_BLOCK_SIZE,
OB_MALLOC_BIG_BLOCK_SIZE))) {
STORAGE_LOG(WARN, "Fail to init io allocator, ", K(ret));
} else if (OB_FAIL(file_handle_.assign(file_handle))) {
STORAGE_LOG(WARN, "Fail to assign file handle", K(ret), K(file_handle));
} else {
allocator_.set_label(ObModIds::OB_TMP_PAGE_CACHE);
}
@ -424,25 +450,24 @@ void ObTmpPageCache::destroy()
{
common::ObKVCache<ObTmpPageCacheKey, ObTmpPageCacheValue>::destroy();
allocator_.destroy();
file_handle_.reset();
}
int ObTmpPageCache::put_page(const ObTmpPageCacheKey& key, const ObTmpPageCacheValue& value)
int ObTmpPageCache::put_page(const ObTmpPageCacheKey &key, const ObTmpPageCacheValue &value)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!key.is_valid() || !value.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key), K(value));
} else if (OB_FAIL(put(key, value, true /*overwrite*/))) {
} else if (OB_FAIL(put(key, value, true/*overwrite*/))) {
STORAGE_LOG(WARN, "fail to put page to page cache", K(ret), K(key), K(value));
}
return ret;
}
int ObTmpPageCache::get_page(const ObTmpPageCacheKey& key, ObTmpPageValueHandle& handle)
int ObTmpPageCache::get_page(const ObTmpPageCacheKey &key, ObTmpPageValueHandle &handle)
{
int ret = OB_SUCCESS;
const ObTmpPageCacheValue* value = NULL;
const ObTmpPageCacheValue *value = NULL;
if (OB_UNLIKELY(!key.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key));
@ -457,7 +482,7 @@ int ObTmpPageCache::get_page(const ObTmpPageCacheKey& key, ObTmpPageValueHandle&
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "unexpected error, the value must not be NULL", K(ret));
} else {
handle.value_ = const_cast<ObTmpPageCacheValue*>(value);
handle.value_ = const_cast<ObTmpPageCacheValue *>(value);
EVENT_INC(ObStatEventIds::TMP_PAGE_CACHE_HIT);
}
}
@ -465,13 +490,15 @@ int ObTmpPageCache::get_page(const ObTmpPageCacheKey& key, ObTmpPageValueHandle&
}
ObTmpBlockCacheKey::ObTmpBlockCacheKey(const int64_t block_id, const uint64_t tenant_id)
: block_id_(block_id), tenant_id_(tenant_id)
{}
bool ObTmpBlockCacheKey::operator==(const ObIKVCacheKey& other) const
: block_id_(block_id), tenant_id_(tenant_id)
{
const ObTmpBlockCacheKey& other_key = reinterpret_cast<const ObTmpBlockCacheKey&>(other);
return block_id_ == other_key.block_id_ && tenant_id_ == other_key.tenant_id_;
}
bool ObTmpBlockCacheKey::operator ==(const ObIKVCacheKey &other) const
{
const ObTmpBlockCacheKey &other_key = reinterpret_cast<const ObTmpBlockCacheKey &> (other);
return block_id_ == other_key.block_id_
&& tenant_id_ == other_key.tenant_id_;
}
uint64_t ObTmpBlockCacheKey::get_tenant_id() const
@ -489,7 +516,7 @@ int64_t ObTmpBlockCacheKey::size() const
return sizeof(*this);
}
int ObTmpBlockCacheKey::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheKey*& key) const
int ObTmpBlockCacheKey::deep_copy(char *buf, const int64_t buf_len, ObIKVCacheKey *&key) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(NULL == buf || buf_len < size())) {
@ -499,30 +526,33 @@ int ObTmpBlockCacheKey::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheKe
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "invalid tmp block cache key, ", K(*this), K(ret));
} else {
key = new (buf) ObTmpBlockCacheKey(block_id_, tenant_id_);
key = new(buf) ObTmpBlockCacheKey(block_id_, tenant_id_);
}
return ret;
}
ObTmpBlockCacheValue::ObTmpBlockCacheValue(char* buf) : buf_(buf), size_(OB_TMP_FILE_STORE.get_block_size())
{}
ObTmpBlockCacheValue::ObTmpBlockCacheValue(char *buf)
: buf_(buf), size_(OB_TMP_FILE_STORE.get_block_size())
{
}
int64_t ObTmpBlockCacheValue::size() const
{
return sizeof(*this) + size_;
}
int ObTmpBlockCacheValue::deep_copy(char* buf, const int64_t buf_len, ObIKVCacheValue*& value) const
int ObTmpBlockCacheValue::deep_copy(char *buf, const int64_t buf_len, ObIKVCacheValue *&value) const
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(nullptr == buf || buf_len < size())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(ret), KP(buf), K(buf_len), "request_size", size());
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(ret), KP(buf), K(buf_len),
"request_size", size());
} else if (OB_UNLIKELY(!is_valid())) {
ret = OB_INVALID_DATA;
STORAGE_LOG(WARN, "invalid tmp block cache value", K(ret));
} else {
ObTmpBlockCacheValue* pblk_value = new (buf) ObTmpBlockCacheValue(buf + sizeof(*this));
ObTmpBlockCacheValue *pblk_value = new (buf) ObTmpBlockCacheValue(buf + sizeof(*this));
MEMCPY(buf + sizeof(*this), buf_, size() - sizeof(*this));
pblk_value->size_ = size_;
value = pblk_value;
@ -530,74 +560,75 @@ int ObTmpBlockCacheValue::deep_copy(char* buf, const int64_t buf_len, ObIKVCache
return ret;
}
ObTmpBlockCache& ObTmpBlockCache::get_instance()
ObTmpBlockCache &ObTmpBlockCache::get_instance()
{
static ObTmpBlockCache instance;
return instance;
}
int ObTmpBlockCache::init(const char* cache_name, const int64_t priority)
int ObTmpBlockCache::init(const char *cache_name, const int64_t priority)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(NULL == cache_name) || OB_UNLIKELY(priority <= 0)) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), K(cache_name), K(priority));
} else if (OB_FAIL((common::ObKVCache<ObTmpBlockCacheKey, ObTmpBlockCacheValue>::init(cache_name, priority)))) {
} else if (OB_FAIL((common::ObKVCache<ObTmpBlockCacheKey,
ObTmpBlockCacheValue>::init(cache_name, priority)))) {
STORAGE_LOG(WARN, "Fail to init kv cache, ", K(ret));
}
return ret;
}
int ObTmpBlockCache::get_block(const ObTmpBlockCacheKey& key, ObTmpBlockValueHandle& handle)
int ObTmpBlockCache::get_block(const ObTmpBlockCacheKey &key, ObTmpBlockValueHandle &handle)
{
int ret = OB_SUCCESS;
const ObTmpBlockCacheValue* value = NULL;
const ObTmpBlockCacheValue *value = NULL;
if (OB_UNLIKELY(!key.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key));
} else if (OB_FAIL(get(key, value, handle.handle_))) {
if (OB_UNLIKELY(OB_ENTRY_NOT_EXIST != ret)) {
STORAGE_LOG(WARN, "fail to get key-value from block cache", K(ret));
} else {
EVENT_INC(ObStatEventIds::TMP_BLOCK_CACHE_MISS);
}
EVENT_INC(ObStatEventIds::TMP_BLOCK_CACHE_MISS);
} else {
if (OB_ISNULL(value)) {
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "unexpected error, the value must not be NULL", K(ret));
} else {
handle.value_ = const_cast<ObTmpBlockCacheValue*>(value);
handle.value_ = const_cast<ObTmpBlockCacheValue *>(value);
EVENT_INC(ObStatEventIds::TMP_BLOCK_CACHE_HIT);
}
}
return ret;
}
int ObTmpBlockCache::alloc_buf(const ObTmpBlockCacheKey& key, ObTmpBlockValueHandle& handle)
int ObTmpBlockCache::alloc_buf(const ObTmpBlockCacheKey &key, ObTmpBlockValueHandle &handle)
{
int ret = OB_SUCCESS;
if (OB_FAIL(alloc(key.get_tenant_id(),
key.size(),
sizeof(ObTmpBlockCacheValue) + OB_TMP_FILE_STORE.get_block_size(),
handle.kvpair_,
handle.handle_,
handle.inst_handle_))) {
if (OB_FAIL(alloc(key.get_tenant_id(), key.size(),
sizeof(ObTmpBlockCacheValue) + OB_TMP_FILE_STORE.get_block_size(),
handle.kvpair_, handle.handle_, handle.inst_handle_))) {
STORAGE_LOG(WARN, "failed to alloc kvcache buf", K(ret));
} else if (OB_FAIL(key.deep_copy(reinterpret_cast<char*>(handle.kvpair_->key_), key.size(), handle.kvpair_->key_))) {
} else if (OB_FAIL(key.deep_copy(reinterpret_cast<char *>(handle.kvpair_->key_),
key.size(), handle.kvpair_->key_))) {
STORAGE_LOG(WARN, "failed to deep copy key", K(ret), K(key));
} else {
char* buf = reinterpret_cast<char*>(handle.kvpair_->value_);
char *buf = reinterpret_cast<char *>(handle.kvpair_->value_);
handle.value_ = new (buf) ObTmpBlockCacheValue(buf + sizeof(ObTmpBlockCacheValue));
}
return ret;
}
int ObTmpBlockCache::put_block(const ObTmpBlockCacheKey& key, ObTmpBlockValueHandle& handle)
int ObTmpBlockCache::put_block(const ObTmpBlockCacheKey &key, ObTmpBlockValueHandle &handle)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!key.is_valid())) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid arguments", K(ret), K(key));
} else if (OB_FAIL(put_kvpair(handle.inst_handle_, handle.kvpair_, handle.handle_, true /*overwrite*/))) {
} else if (OB_FAIL(put_kvpair(handle.inst_handle_, handle.kvpair_,
handle.handle_, true/*overwrite*/))) {
STORAGE_LOG(WARN, "fail to put tmp block to block cache", K(ret));
} else {
handle.reset();
@ -611,35 +642,36 @@ void ObTmpBlockCache::destory()
}
ObTmpTenantMemBlockManager::ObTmpTenantMemBlockManager()
: write_handles_(),
t_mblk_map_(),
dir_to_blk_map_(),
free_page_nums_(0),
blk_nums_threshold_(0),
block_cache_(NULL),
allocator_(NULL),
file_handle_(),
tenant_id_(0),
block_write_ctx_(),
last_access_tenant_config_ts_(0),
last_tenant_mem_block_num_(1),
is_inited_(false)
{}
: write_handles_(),
t_mblk_map_(),
dir_to_blk_map_(),
free_page_nums_(0),
blk_nums_threshold_(0),
block_cache_(NULL),
allocator_(NULL),
tenant_id_(0),
block_write_ctx_(),
last_access_tenant_config_ts_(0),
last_tenant_mem_block_num_(1),
is_inited_(false)
{
}
ObTmpTenantMemBlockManager::~ObTmpTenantMemBlockManager()
{}
{
}
int ObTmpTenantMemBlockManager::init(const uint64_t tenant_id, common::ObIAllocator& allocator,
const ObStorageFileHandle& file_handle, double blk_nums_threshold)
int ObTmpTenantMemBlockManager::init(const uint64_t tenant_id,
common::ObIAllocator &allocator,
double blk_nums_threshold)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(is_inited_)) {
ret = OB_INIT_TWICE;
STORAGE_LOG(WARN, "ObTmpBlockCache has been inited", K(ret));
} else if (OB_UNLIKELY(blk_nums_threshold <= 0) || OB_UNLIKELY(blk_nums_threshold > 1) ||
OB_UNLIKELY(!file_handle.is_valid())) {
} else if (OB_UNLIKELY(blk_nums_threshold <= 0) || OB_UNLIKELY(blk_nums_threshold > 1)) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), K(blk_nums_threshold), K(file_handle));
STORAGE_LOG(WARN, "invalid argument", K(ret), K(blk_nums_threshold));
} else if (OB_FAIL(t_mblk_map_.create(MBLK_HASH_BUCKET_NUM, ObModIds::OB_TMP_BLOCK_MAP))) {
STORAGE_LOG(WARN, "Fail to create allocating block map, ", K(ret));
} else if (OB_FAIL(dir_to_blk_map_.create(MBLK_HASH_BUCKET_NUM, ObModIds::OB_TMP_MAP))) {
@ -647,12 +679,6 @@ int ObTmpTenantMemBlockManager::init(const uint64_t tenant_id, common::ObIAlloca
} else if (OB_ISNULL(block_cache_ = &ObTmpBlockCache::get_instance())) {
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "fail to get the block cache", K(ret));
} else if (OB_FAIL(OB_FILE_SYSTEM.init_file_ctx(STORE_FILE_MACRO_BLOCK, block_write_ctx_.file_ctx_))) {
STORAGE_LOG(WARN, "failed to init write ctx", K(ret));
} else if (OB_FAIL(file_handle_.assign(file_handle))) {
STORAGE_LOG(WARN, "fail to assign file handle", K(ret), K(file_handle));
} else if (!block_write_ctx_.file_handle_.is_valid() && OB_FAIL(block_write_ctx_.file_handle_.assign(file_handle))) {
STORAGE_LOG(WARN, "fail to assign file handle", K(ret), K(file_handle));
} else {
blk_nums_threshold_ = blk_nums_threshold;
tenant_id_ = tenant_id;
@ -669,9 +695,9 @@ void ObTmpTenantMemBlockManager::destroy()
{
int ret = OB_SUCCESS;
TmpMacroBlockMap::iterator iter;
ObTmpMacroBlock* tmp = NULL;
ObTmpMacroBlock *tmp = NULL;
if (is_inited_ && OB_FAIL(wait_write_io_finish())) {
ObMacroBlockHandle* mb_handle = NULL;
ObMacroBlockHandle *mb_handle = NULL;
while (write_handles_.count() > 0) {
if (OB_FAIL(write_handles_.pop_back(mb_handle))) {
STORAGE_LOG(WARN, "fail to pop write handle", K(ret));
@ -694,23 +720,25 @@ void ObTmpTenantMemBlockManager::destroy()
if (NULL != block_cache_) {
block_cache_ = NULL;
}
file_handle_.reset();
allocator_ = NULL;
block_write_ctx_.reset();
is_inited_ = false;
}
int ObTmpTenantMemBlockManager::get_block(const ObTmpBlockCacheKey& key, ObTmpBlockValueHandle& handle)
int ObTmpTenantMemBlockManager::get_block(const ObTmpBlockCacheKey &key,
ObTmpBlockValueHandle &handle)
{
return block_cache_->get_block(key, handle);
}
int ObTmpTenantMemBlockManager::alloc_buf(const ObTmpBlockCacheKey& key, ObTmpBlockValueHandle& handle)
int ObTmpTenantMemBlockManager::alloc_buf(const ObTmpBlockCacheKey &key,
ObTmpBlockValueHandle &handle)
{
return block_cache_->alloc_buf(key, handle);
}
int ObTmpTenantMemBlockManager::try_wash(const uint64_t tenant_id, common::ObIArray<ObTmpMacroBlock*>& free_blocks)
int ObTmpTenantMemBlockManager::try_wash(const uint64_t tenant_id,
common::ObIArray<ObTmpMacroBlock *> &free_blocks)
{
int ret = OB_SUCCESS;
const int64_t count = t_mblk_map_.size();
@ -719,7 +747,8 @@ int ObTmpTenantMemBlockManager::try_wash(const uint64_t tenant_id, common::ObIAr
STORAGE_LOG(WARN, "ObTmpBlockCache has not been inited", K(ret));
} else if (get_tenant_mem_block_num() <= count) {
int64_t wash_nums = 1;
if (OB_FAIL(wash(tenant_id, std::max(wash_nums, count - get_tenant_mem_block_num() + 1), free_blocks))) {
if (OB_FAIL(wash(tenant_id, std::max(wash_nums, count - get_tenant_mem_block_num() + 1),
free_blocks))) {
STORAGE_LOG(WARN, "cannot wash a tmp macro block", K(ret), K(tenant_id));
}
}
@ -743,13 +772,13 @@ int ObTmpTenantMemBlockManager::free_macro_block(const int64_t block_id)
return ret;
}
int ObTmpTenantMemBlockManager::alloc_extent(const int64_t dir_id, const uint64_t tenant_id, const int64_t size,
ObTmpFileExtent& extent, common::ObIArray<ObTmpMacroBlock*>& free_blocks)
int ObTmpTenantMemBlockManager::alloc_extent(const int64_t dir_id, const uint64_t tenant_id,
const int64_t size, ObTmpFileExtent &extent, common::ObIArray<ObTmpMacroBlock *> &free_blocks)
{
int ret = OB_SUCCESS;
int64_t block_id = -1;
int64_t page_nums = std::ceil(size * 1.0 / ObTmpMacroBlock::get_default_page_size());
ObTmpMacroBlock* t_mblk = NULL;
ObTmpMacroBlock *t_mblk = NULL;
if (OB_UNLIKELY(!is_inited_)) {
ret = OB_NOT_INIT;
STORAGE_LOG(WARN, "ObTmpBlockCache has not been inited", K(ret));
@ -770,7 +799,8 @@ int ObTmpTenantMemBlockManager::alloc_extent(const int64_t dir_id, const uint64_
} else if (OB_FAIL(dir_to_blk_map_.set_refactored(dir_id, t_mblk->get_block_id(), 1))) {
STORAGE_LOG(WARN, "fail to set dir_to_blk_map", K(ret));
}
} else if (t_mblk->get_max_cont_page_nums() < page_nums || t_mblk->get_tenant_id() != tenant_id) {
} else if (t_mblk->get_max_cont_page_nums() < page_nums
|| t_mblk->get_tenant_id() != tenant_id) {
if (OB_FAIL(get_macro_block(dir_id, tenant_id, page_nums, t_mblk, free_blocks))) {
if (OB_ITER_END != ret) {
STORAGE_LOG(WARN, "fail to get macro block", K(ret));
@ -778,7 +808,10 @@ int ObTmpTenantMemBlockManager::alloc_extent(const int64_t dir_id, const uint64_
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(t_mblk->alloc(page_nums, extent))) {
if (OB_ISNULL(t_mblk)) {
ret = OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "unexpected error, t_mblk is nullptr", K(ret), KP(t_mblk));
} else if (OB_FAIL(t_mblk->alloc(page_nums, extent))){
STORAGE_LOG(WARN, "fail to alloc tmp extent", K(ret));
} else if (OB_FAIL(refresh_dir_to_blk_map(t_mblk->get_dir_id(), t_mblk))) {
STORAGE_LOG(WARN, "fail to refresh dir_to_blk_map", K(ret), K(*t_mblk));
@ -789,7 +822,8 @@ int ObTmpTenantMemBlockManager::alloc_extent(const int64_t dir_id, const uint64_
return ret;
}
int ObTmpTenantMemBlockManager::free_extent(const int64_t free_page_nums, const ObTmpMacroBlock* t_mblk)
int ObTmpTenantMemBlockManager::free_extent(const int64_t free_page_nums,
const ObTmpMacroBlock *t_mblk)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_inited_)) {
@ -806,8 +840,11 @@ int ObTmpTenantMemBlockManager::free_extent(const int64_t free_page_nums, const
return ret;
}
int ObTmpTenantMemBlockManager::get_macro_block(const int64_t dir_id, const uint64_t tenant_id, const int64_t page_nums,
ObTmpMacroBlock*& t_mblk, common::ObIArray<ObTmpMacroBlock*>& free_blocks)
int ObTmpTenantMemBlockManager::get_macro_block(const int64_t dir_id,
const uint64_t tenant_id,
const int64_t page_nums,
ObTmpMacroBlock *&t_mblk,
common::ObIArray<ObTmpMacroBlock *> &free_blocks)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_inited_)) {
@ -832,11 +869,12 @@ int ObTmpTenantMemBlockManager::get_macro_block(const int64_t dir_id, const uint
const int64_t count = t_mblk_map_.size();
if (OB_UNLIKELY(t_mblk_map_.size() == 0)) {
// nothing to do.
} else if (get_tenant_mem_block_num() <= count ||
blk_nums_threshold_ >
(free_page_nums_ * 1.0) / (t_mblk_map_.size() * ObTmpFilePageBuddy::MAX_PAGE_NUMS)) {
} else if ( get_tenant_mem_block_num() <= count ||
blk_nums_threshold_ > (free_page_nums_ * 1.0) / (t_mblk_map_.size() * ObTmpFilePageBuddy::MAX_PAGE_NUMS)) {
int64_t wash_nums = 1;
if (OB_FAIL(wash(tenant_id, std::max(wash_nums, count - get_tenant_mem_block_num() + 1), free_blocks))) {
if (OB_FAIL(wash(tenant_id,
std::max(wash_nums, count - get_tenant_mem_block_num() + 1),
free_blocks))) {
STORAGE_LOG(WARN, "cannot wash a tmp macro block", K(ret), K(dir_id), K(tenant_id));
}
}
@ -848,8 +886,8 @@ int ObTmpTenantMemBlockManager::get_macro_block(const int64_t dir_id, const uint
return ret;
}
int ObTmpTenantMemBlockManager::wash(
const uint64_t tenant_id, int64_t block_nums, common::ObIArray<ObTmpMacroBlock*>& free_blocks)
int ObTmpTenantMemBlockManager::wash(const uint64_t tenant_id, int64_t block_nums,
common::ObIArray<ObTmpMacroBlock *> &free_blocks)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_inited_)) {
@ -862,14 +900,16 @@ int ObTmpTenantMemBlockManager::wash(
int64_t count = t_mblk_map_.size();
if (OB_SUCC(ret)) {
if (OB_UNLIKELY(count < get_tenant_mem_block_num())) {
STORAGE_LOG(WARN, "Tenant memory has not been used up, not need to wash ", K(ret), K(count));
STORAGE_LOG(WARN, "Tenant memory has not been used up, not need to wash ", K(ret),
K(count));
} else {
TmpMacroBlockMap::iterator iter;
ObTmpMacroBlock* wash_block = NULL;
ObTmpMacroBlock *wash_block = NULL;
for (iter = t_mblk_map_.begin(); count > 0 && iter != t_mblk_map_.end(); ++iter) {
if (iter->second->get_tenant_id() == tenant_id) {
if (!iter->second->is_washing()) {
if (NULL == wash_block || wash_block->get_free_page_nums() > iter->second->get_free_page_nums()) {
if (NULL == wash_block
|| wash_block->get_free_page_nums() > iter->second->get_free_page_nums()) {
if (NULL != wash_block) {
wash_block->set_washing_status(false);
}
@ -895,11 +935,10 @@ int ObTmpTenantMemBlockManager::wash(
return ret;
}
int ObTmpTenantMemBlockManager::add_macro_block(const uint64_t tenant_id, ObTmpMacroBlock*& t_mblk)
int ObTmpTenantMemBlockManager::add_macro_block(const uint64_t tenant_id, ObTmpMacroBlock *&t_mblk)
{
int ret = OB_SUCCESS;
UNUSED(tenant_id);
int64_t count = 0;
if (OB_UNLIKELY(!is_inited_)) {
ret = OB_NOT_INIT;
STORAGE_LOG(WARN, "ObTmpBlockCache has not been inited", K(ret));
@ -911,7 +950,8 @@ int ObTmpTenantMemBlockManager::add_macro_block(const uint64_t tenant_id, ObTmpM
return ret;
}
int ObTmpTenantMemBlockManager::refresh_dir_to_blk_map(const int64_t dir_id, const ObTmpMacroBlock* t_mblk)
int ObTmpTenantMemBlockManager::refresh_dir_to_blk_map(const int64_t dir_id,
const ObTmpMacroBlock *t_mblk)
{
int ret = OB_SUCCESS;
int64_t block_id = 0;
@ -925,7 +965,7 @@ int ObTmpTenantMemBlockManager::refresh_dir_to_blk_map(const int64_t dir_id, con
}
}
} else {
ObTmpMacroBlock* dir_mblk = NULL;
ObTmpMacroBlock *dir_mblk = NULL;
if (OB_FAIL(t_mblk_map_.get_refactored(block_id, dir_mblk))) {
if (OB_HASH_NOT_EXIST == ret) {
ret = dir_to_blk_map_.set_refactored(dir_id, t_mblk->get_block_id(), 1);
@ -940,7 +980,9 @@ int ObTmpTenantMemBlockManager::refresh_dir_to_blk_map(const int64_t dir_id, con
return ret;
}
int ObTmpTenantMemBlockManager::wash(const uint64_t tenant_id, ObTmpMacroBlock* wash_block, bool& is_empty)
int ObTmpTenantMemBlockManager::wash(const uint64_t tenant_id, ObTmpMacroBlock *wash_block,
bool &is_empty)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_inited_)) {
@ -956,10 +998,10 @@ int ObTmpTenantMemBlockManager::wash(const uint64_t tenant_id, ObTmpMacroBlock*
return ret;
}
int ObTmpTenantMemBlockManager::wash_with_no_wait(const uint64_t tenant_id, ObTmpMacroBlock* wash_block, bool& is_empty)
int ObTmpTenantMemBlockManager::wash_with_no_wait(const uint64_t tenant_id,
ObTmpMacroBlock *wash_block, bool &is_empty)
{
int ret = OB_SUCCESS;
int64_t count = 0;
// close all of extents in this block.
if (OB_UNLIKELY(!is_inited_)) {
ret = OB_NOT_INIT;
@ -980,22 +1022,19 @@ int ObTmpTenantMemBlockManager::wash_with_no_wait(const uint64_t tenant_id, ObTm
}
} else if (wash_block->is_inited() && !wash_block->is_disked()) {
ObTmpBlockIOInfo info;
info.tenant_id_ = tenant_id;
info.io_desc_ = wash_block->get_io_desc();
info.buf_ = wash_block->get_buffer();
info.size_ = ObTmpFilePageBuddy::MAX_PAGE_NUMS * ObTmpMacroBlock::get_default_page_size();
ObMacroBlockHandle& mb_handle = wash_block->get_macro_block_handle();
ObTmpBlockCacheKey key(wash_block->get_block_id(), tenant_id);
mb_handle.set_file(file_handle_.get_storage_file());
if (OB_FAIL(write_io(info, mb_handle))) {
ObMacroBlockHandle &mb_handle = wash_block->get_macro_block_handle();
if (OB_FAIL(wash_block->get_wash_io_info(info))) {
STORAGE_LOG(WARN, "fail to get wash io info", K(ret), K(tenant_id));
} else if (OB_FAIL(write_io(info, wash_block->get_tmp_block_header(), mb_handle))) {
STORAGE_LOG(WARN, "fail to write tmp block", K(ret), K(tenant_id));
} else if (OB_FAIL(write_handles_.push_back(&mb_handle))) {
STORAGE_LOG(WARN, "fail to push back into write_handles", K(ret));
} else if (wash_block->is_disked()) {
// nothing to do
} else if (OB_FAIL(wash_block->give_back_buf_into_cache(true /*is_wash*/))) {
} else if (OB_FAIL(wash_block->give_back_buf_into_cache(true/*is_wash*/))) {
STORAGE_LOG(WARN, "fail to put tmp block cache", K(ret), K(tenant_id));
} else {
OB_TMP_FILE_STORE.dec_block_cache_num(tenant_id, 1);
free_page_nums_ -= wash_block->get_free_page_nums();
if (OB_FAIL(t_mblk_map_.erase_refactored(wash_block->get_block_id(), &wash_block))) {
STORAGE_LOG(WARN, "fail to erase t_mblk_map", K(ret));
@ -1023,78 +1062,55 @@ int ObTmpTenantMemBlockManager::wait_write_io_finish()
ret = OB_NOT_INIT;
STORAGE_LOG(WARN, "ObTmpFileStore has not been inited", K(ret));
} else if (write_handles_.count() > 0) {
ObMacroBlockHandle* mb_handle = NULL;
ObMacroBlockHandle *mb_handle = NULL;
while (OB_SUCC(ret) && write_handles_.count() > 0) {
if (OB_FAIL(write_handles_.pop_back(mb_handle))) {
STORAGE_LOG(WARN, "fail to pop write handle", K(ret));
} else if (OB_FAIL(mb_handle->wait(io_timeout_ms))) {
} else if(OB_FAIL(mb_handle->wait(io_timeout_ms))) {
STORAGE_LOG(WARN, "fail to wait tmp write io", K(ret));
}
mb_handle->get_io_handle().reset();
}
block_write_ctx_.clear();
int tmp_ret = OB_SUCCESS;
if (!block_write_ctx_.file_handle_.is_valid() &&
OB_SUCCESS != (tmp_ret = block_write_ctx_.file_handle_.assign(file_handle_))) {
STORAGE_LOG(WARN, "fail to assign file handle", K(ret), K(tmp_ret), K(file_handle_));
}
}
return ret;
}
int ObTmpTenantMemBlockManager::write_io(const ObTmpBlockIOInfo& io_info, ObMacroBlockHandle& handle)
int ObTmpTenantMemBlockManager::write_io(
const ObTmpBlockIOInfo &io_info,
const ObTmpFileMacroBlockHeader &tmp_block_header,
ObMacroBlockHandle &handle)
{
int ret = OB_SUCCESS;
const int64_t buf_size = OB_SERVER_BLOCK_MGR.get_macro_block_size();
const int64_t page_size = ObTmpMacroBlock::get_default_page_size();
int64_t pos = 0;
if (OB_UNLIKELY(!is_inited_)) {
ret = OB_NOT_INIT;
STORAGE_LOG(WARN, "ObTmpFileStore has not been inited", K(ret));
} else if (OB_FAIL(THE_IO_DEVICE->check_space_full(OB_SERVER_BLOCK_MGR.get_macro_block_size()))) {
STORAGE_LOG(WARN, "fail to check space full", K(ret));
} else {
ObMacroBlockWriteInfo write_info;
ObFullMacroBlockMeta full_meta;
ObMacroBlockMetaV2 macro_block_meta;
ObMacroBlockSchemaInfo macro_schema;
full_meta.meta_ = &macro_block_meta;
full_meta.schema_ = &macro_schema;
if (OB_FAIL(build_macro_meta(io_info.tenant_id_, full_meta))) {
STORAGE_LOG(WARN, "fail to build macro meta", K(ret));
} else if (OB_FAIL(OB_STORE_FILE.check_disk_full(OB_FILE_SYSTEM.get_macro_block_size()))) {
STORAGE_LOG(WARN, "fail to check space full", K(ret));
} else {
write_info.io_desc_ = io_info.io_desc_;
write_info.meta_ = full_meta;
write_info.block_write_ctx_ = &block_write_ctx_;
write_info.buffer_ = io_info.buf_;
write_info.size_ = io_info.size_;
if (OB_FAIL(file_handle_.get_storage_file()->async_write_block(write_info, handle))) {
STORAGE_LOG(WARN, "Fail to async write block", K(ret), K(full_meta));
}
write_info.io_desc_ = io_info.io_desc_;
write_info.buffer_ = io_info.buf_;
write_info.offset_ = ObTmpMacroBlock::get_header_padding();
write_info.size_ = io_info.size_;
if (OB_FAIL(ObBlockManager::async_write_block(write_info, handle))) {
STORAGE_LOG(WARN, "Fail to async write block", K(ret), K(write_info), K(handle));
} else if (OB_FAIL(block_write_ctx_.add_macro_block_id(handle.get_macro_id()))) {
STORAGE_LOG(WARN, "fail to add macro id", K(ret), "macro id", handle.get_macro_id());
}
}
return ret;
}
int ObTmpTenantMemBlockManager::build_macro_meta(const uint64_t tenant_id, ObFullMacroBlockMeta& full_meta)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_inited_)) {
ret = OB_NOT_INIT;
STORAGE_LOG(WARN, "ObTmpFileStore has not been inited", K(ret));
} else if (OB_INVALID_ID == tenant_id || nullptr == full_meta.meta_ || nullptr == full_meta.schema_) {
ret = OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), K(tenant_id), K(full_meta));
} else {
ObMacroBlockMetaV2* macro_meta = const_cast<ObMacroBlockMetaV2*>(full_meta.meta_);
macro_meta->attr_ = ObMacroBlockCommonHeader::SortTempData;
macro_meta->table_id_ = combine_id(tenant_id, 1);
}
return ret;
}
int64_t ObTmpTenantMemBlockManager::get_tenant_mem_block_num()
{
int64_t tenant_mem_block_num = TENANT_MEM_BLOCK_NUM;
int64_t last_access_ts = ATOMIC_LOAD(&last_access_tenant_config_ts_);
if (last_access_ts > 0 && common::ObClockGenerator::getClock() - last_access_ts < 10000000) {
if (last_access_ts > 0
&& common::ObClockGenerator::getClock() - last_access_ts < 10000000) {
tenant_mem_block_num = ATOMIC_LOAD(&last_tenant_mem_block_num_);
} else {
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id_));
@ -1104,8 +1120,8 @@ int64_t ObTmpTenantMemBlockManager::get_tenant_mem_block_num()
tenant_mem_block_num = 1L;
} else {
const int64_t bytes = common::upper_align(
lib::get_tenant_memory_limit(tenant_id_) * tenant_config->_temporary_file_io_area_size / 100,
OB_TMP_FILE_STORE.get_block_size());
lib::get_tenant_memory_limit(tenant_id_) * tenant_config->_temporary_file_io_area_size / 100,
OB_TMP_FILE_STORE.get_block_size());
tenant_mem_block_num = bytes / OB_TMP_FILE_STORE.get_block_size();
}
ATOMIC_STORE(&last_tenant_mem_block_num_, tenant_mem_block_num);