Skip to content

Commit f7c3569

Browse files
mag1c-hMag1c.H
authored andcommitted
new space shard layout with temp dir
1 parent 2a82141 commit f7c3569

File tree

14 files changed

+250
-61
lines changed

14 files changed

+250
-61
lines changed

ucm/store/infra/file/file.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ void File::MUnmap(void* addr, size_t size) { FileImpl{{}}.MUnmap(addr, size); }
8181

8282
void File::ShmUnlink(const std::string& path) { FileImpl{path}.ShmUnlink(); }
8383

84+
void File::Remove(const std::string& path) { FileImpl{path}.Remove(); }
85+
8486
} // namespace UC

ucm/store/infra/file/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class File {
4242
const uintptr_t address, const bool directIo = false);
4343
static void MUnmap(void* addr, size_t size);
4444
static void ShmUnlink(const std::string& path);
45+
static void Remove(const std::string& path);
4546
};
4647

4748
} // namespace UC

ucm/store/infra/file/posix_file.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ void PosixFile::Remove()
121121
auto ret = remove(this->Path().c_str());
122122
auto eno = errno;
123123
if (ret != 0) {
124-
if (eno == ENOENT) {
125-
UC_WARN("Failed to remove file, path: {}, file not found.", this->Path());
126-
}
124+
if (eno != ENOENT) { UC_WARN("Failed({},{}) to remove file({}).", ret, eno, this->Path()); }
127125
}
128126
}
129127

ucm/store/nfsstore/cc/api/nfsstore.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class NFSStoreImpl : public NFSStore {
3333
public:
3434
int32_t Setup(const Config& config)
3535
{
36-
auto status = this->spaceMgr_.Setup(config.storageBackends, config.kvcacheBlockSize);
36+
auto status = this->spaceMgr_.Setup(config.storageBackends, config.kvcacheBlockSize,
37+
config.tempDumpDirEnable);
3738
if (status.Failure()) {
3839
UC_ERROR("Failed({}) to setup SpaceManager.", status);
3940
return status.Underlying();
@@ -103,6 +104,7 @@ class NFSStoreImpl : public NFSStore {
103104
UC_INFO("Set UC::IOSize to {}.", config.transferIoSize);
104105
UC_INFO("Set UC::BufferNumber to {}.", config.transferBufferNumber);
105106
UC_INFO("Set UC::TimeoutMs to {}.", config.transferTimeoutMs);
107+
UC_INFO("Set UC::TempDumpDirEnable to {}.", config.tempDumpDirEnable);
106108
}
107109

108110
private:

ucm/store/nfsstore/cc/api/nfsstore.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class NFSStore : public CCStore {
4040
size_t transferIoSize;
4141
size_t transferBufferNumber;
4242
size_t transferTimeoutMs;
43+
bool tempDumpDirEnable;
4344

4445
Config(const std::vector<std::string>& storageBackends, const size_t kvcacheBlockSize,
4546
const bool transferEnable)
4647
: storageBackends{storageBackends}, kvcacheBlockSize{kvcacheBlockSize},
4748
transferEnable{transferEnable}, transferDeviceId{-1}, transferStreamNumber{32},
48-
transferIoSize{262144}, transferBufferNumber{512}, transferTimeoutMs{30000}
49+
transferIoSize{262144}, transferBufferNumber{512}, transferTimeoutMs{30000},
50+
tempDumpDirEnable{false}
4951
{
5052
}
5153
};

ucm/store/nfsstore/cc/domain/space/space_layout.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,10 @@ namespace UC {
3232

3333
class SpaceLayout {
3434
public:
35-
Status Setup(const std::vector<std::string>& storageBackends);
36-
std::string DataFileParent(const std::string& blockId) const;
37-
std::string DataFilePath(const std::string& blockId, bool activated) const;
38-
39-
private:
40-
Status AddStorageBackend(const std::string& path);
41-
Status AddFirstStorageBackend(const std::string& path);
42-
Status AddSecondaryStorageBackend(const std::string& path);
43-
std::string StorageBackend(const std::string& blockId) const;
44-
std::vector<std::string> RelativeRoots() const;
45-
std::string DataFileRoot() const;
46-
47-
private:
48-
std::vector<std::string> storageBackends_;
35+
virtual ~SpaceLayout() = default;
36+
virtual Status Setup(const std::vector<std::string>& storageBackends) = 0;
37+
virtual std::string DataFileParent(const std::string& blockId, bool activated) const = 0;
38+
virtual std::string DataFilePath(const std::string& blockId, bool activated) const = 0;
4939
};
5040

5141
} // namespace UC

ucm/store/nfsstore/cc/domain/space/space_manager.cc

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,41 @@
2424
#include "space_manager.h"
2525
#include "file/file.h"
2626
#include "logger/logger.h"
27+
#include "space_shard_temp_layout.h"
2728

2829
namespace UC {
2930

30-
Status SpaceManager::Setup(const std::vector<std::string>& storageBackends, const size_t blockSize)
31+
std::unique_ptr<SpaceLayout> MakeSpaceLayout(const bool tempDumpDirEnable)
32+
{
33+
try {
34+
if (tempDumpDirEnable) { return std::make_unique<SpaceShardTempLayout>(); }
35+
return std::make_unique<SpaceShardLayout>();
36+
} catch (const std::exception& e) {
37+
UC_ERROR("Failed({}) to make space layout object.", e.what());
38+
}
39+
return nullptr;
40+
}
41+
42+
Status SpaceManager::Setup(const std::vector<std::string>& storageBackends, const size_t blockSize,
43+
const bool tempDumpDirEnable)
3144
{
3245
if (blockSize == 0) {
3346
UC_ERROR("Invalid block size({}).", blockSize);
3447
return Status::InvalidParam();
3548
}
36-
auto status = this->layout_.Setup(storageBackends);
49+
this->layout_ = MakeSpaceLayout(tempDumpDirEnable);
50+
if (!this->layout_) { return Status::OutOfMemory(); }
51+
auto status = this->layout_->Setup(storageBackends);
3752
if (status.Failure()) { return status; }
3853
this->blockSize_ = blockSize;
3954
return Status::OK();
4055
}
4156

4257
Status SpaceManager::NewBlock(const std::string& blockId) const
4358
{
44-
auto parent = File::Make(this->layout_.DataFileParent(blockId));
45-
auto file = File::Make(this->layout_.DataFilePath(blockId, true));
59+
constexpr auto activated = true;
60+
auto parent = File::Make(this->layout_->DataFileParent(blockId, activated));
61+
auto file = File::Make(this->layout_->DataFilePath(blockId, activated));
4662
if (!parent || !file) {
4763
UC_ERROR("Failed to new block({}).", blockId);
4864
return Status::OutOfMemory();
@@ -53,7 +69,7 @@ Status SpaceManager::NewBlock(const std::string& blockId) const
5369
UC_ERROR("Failed({}) to new block({}).", status, blockId);
5470
return status;
5571
}
56-
if ((File::Access(this->layout_.DataFilePath(blockId, false), IFile::AccessMode::EXIST))
72+
if ((File::Access(this->layout_->DataFilePath(blockId, false), IFile::AccessMode::EXIST))
5773
.Success()) {
5874
status = Status::DuplicateKey();
5975
UC_ERROR("Failed({}) to new block({}).", status, blockId);
@@ -77,29 +93,31 @@ Status SpaceManager::NewBlock(const std::string& blockId) const
7793

7894
Status SpaceManager::CommitBlock(const std::string& blockId, bool success) const
7995
{
80-
auto file = File::Make(this->layout_.DataFilePath(blockId, true));
81-
if (!file) {
82-
UC_ERROR("Failed to {} block({}).", success ? "commit" : "cancel", blockId);
83-
return Status::OutOfMemory();
84-
}
85-
if (success) {
86-
auto status = file->Rename(this->layout_.DataFilePath(blockId, false));
87-
if (status.Failure()) { UC_ERROR("Failed({}) to commit block({}).", status, blockId); }
88-
return status;
89-
}
90-
auto parent = File::Make(this->layout_.DataFileParent(blockId));
91-
if (!parent) {
92-
UC_ERROR("Failed to cancel block({}).", blockId);
93-
return Status::OutOfMemory();
96+
const auto activatedParent = this->layout_->DataFileParent(blockId, true);
97+
const auto activatedFile = this->layout_->DataFilePath(blockId, true);
98+
auto status = Status::OK();
99+
do {
100+
if (!success) { break; }
101+
const auto archivedParent = this->layout_->DataFileParent(blockId, false);
102+
const auto archivedFile = this->layout_->DataFilePath(blockId, false);
103+
if (archivedParent != activatedParent) {
104+
status = File::MkDir(archivedParent);
105+
if (status == Status::DuplicateKey()) { status = Status::OK(); }
106+
if (status.Failure()) { break; }
107+
}
108+
status = File::Rename(activatedFile, archivedFile);
109+
} while (0);
110+
File::Remove(activatedFile);
111+
File::RmDir(activatedParent);
112+
if (status.Failure()) {
113+
UC_ERROR("Failed({}) to {} block({}).", status, success ? "commit" : "cancel", blockId);
94114
}
95-
file->Remove();
96-
parent->RmDir();
97-
return Status::OK();
115+
return status;
98116
}
99117

100118
bool SpaceManager::LookupBlock(const std::string& blockId) const
101119
{
102-
auto path = this->layout_.DataFilePath(blockId, false);
120+
auto path = this->layout_->DataFilePath(blockId, false);
103121
auto file = File::Make(path);
104122
if (!file) {
105123
UC_ERROR("Failed to make file smart pointer, path: {}.", path);
@@ -116,6 +134,6 @@ bool SpaceManager::LookupBlock(const std::string& blockId) const
116134
return true;
117135
}
118136

119-
const SpaceLayout* SpaceManager::GetSpaceLayout() const { return &this->layout_; }
137+
const SpaceLayout* SpaceManager::GetSpaceLayout() const { return this->layout_.get(); }
120138

121139
} // namespace UC

ucm/store/nfsstore/cc/domain/space/space_manager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@
2424
#ifndef UNIFIEDCACHE_SPACE_MANAGER_H
2525
#define UNIFIEDCACHE_SPACE_MANAGER_H
2626

27+
#include <memory>
2728
#include "space_layout.h"
2829
#include "status/status.h"
2930

3031
namespace UC {
3132

3233
class SpaceManager {
3334
public:
34-
Status Setup(const std::vector<std::string>& storageBackends, const size_t blockSize);
35+
Status Setup(const std::vector<std::string>& storageBackends, const size_t blockSize,
36+
const bool tempDumpDirEnable);
3537
Status NewBlock(const std::string& blockId) const;
3638
Status CommitBlock(const std::string& blockId, bool success = true) const;
3739
bool LookupBlock(const std::string& blockId) const;
3840
const SpaceLayout* GetSpaceLayout() const;
3941

4042
private:
41-
SpaceLayout layout_;
43+
std::unique_ptr<SpaceLayout> layout_;
4244
size_t blockSize_;
4345
};
4446

ucm/store/nfsstore/cc/domain/space/space_layout.cc renamed to ucm/store/nfsstore/cc/domain/space/space_shard_layout.cc

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
* */
24-
#include "space_layout.h"
24+
#include "space_shard_layout.h"
2525
#include <algorithm>
2626
#include <array>
2727
#include "file/file.h"
@@ -34,7 +34,7 @@ constexpr size_t nU64PerBlock = blockIdSize / sizeof(uint64_t);
3434
using BlockId = std::array<uint64_t, nU64PerBlock>;
3535
static_assert(sizeof(BlockId) == blockIdSize);
3636

37-
Status SpaceLayout::Setup(const std::vector<std::string>& storageBackends)
37+
Status SpaceShardLayout::Setup(const std::vector<std::string>& storageBackends)
3838
{
3939
if (storageBackends.empty()) {
4040
UC_ERROR("Empty backend list.");
@@ -47,21 +47,22 @@ Status SpaceLayout::Setup(const std::vector<std::string>& storageBackends)
4747
return status;
4848
}
4949

50-
std::string SpaceLayout::DataFileParent(const std::string& blockId) const
50+
std::string SpaceShardLayout::DataFileParent(const std::string& blockId, bool activated) const
5151
{
52-
auto id = static_cast<const BlockId*>(static_cast<const void*>(blockId.data()));
53-
return fmt::format("{}{}/{:016x}", this->StorageBackend(blockId), this->DataFileRoot(),
54-
id->front());
52+
uint64_t front, back;
53+
this->ShardBlockId(blockId, front, back);
54+
return fmt::format("{}{}/{:016x}", this->StorageBackend(blockId), this->DataFileRoot(), front);
5555
}
5656

57-
std::string SpaceLayout::DataFilePath(const std::string& blockId, bool activated) const
57+
std::string SpaceShardLayout::DataFilePath(const std::string& blockId, bool activated) const
5858
{
59-
auto id = static_cast<const BlockId*>(static_cast<const void*>(blockId.data()));
59+
uint64_t front, back;
60+
this->ShardBlockId(blockId, front, back);
6061
return fmt::format("{}{}/{:016x}/{:016x}.{}", this->StorageBackend(blockId),
61-
this->DataFileRoot(), id->front(), id->back(), activated ? "act" : "dat");
62+
this->DataFileRoot(), front, back, activated ? "act" : "dat");
6263
}
6364

64-
Status SpaceLayout::AddStorageBackend(const std::string& path)
65+
Status SpaceShardLayout::AddStorageBackend(const std::string& path)
6566
{
6667
auto normalizedPath = path;
6768
if (normalizedPath.back() != '/') { normalizedPath += '/'; }
@@ -77,7 +78,7 @@ Status SpaceLayout::AddStorageBackend(const std::string& path)
7778
return status;
7879
}
7980

80-
Status SpaceLayout::AddFirstStorageBackend(const std::string& path)
81+
Status SpaceShardLayout::AddFirstStorageBackend(const std::string& path)
8182
{
8283
for (const auto& root : this->RelativeRoots()) {
8384
auto dir = File::Make(path + root);
@@ -90,7 +91,7 @@ Status SpaceLayout::AddFirstStorageBackend(const std::string& path)
9091
return Status::OK();
9192
}
9293

93-
Status SpaceLayout::AddSecondaryStorageBackend(const std::string& path)
94+
Status SpaceShardLayout::AddSecondaryStorageBackend(const std::string& path)
9495
{
9596
auto iter = std::find(this->storageBackends_.begin(), this->storageBackends_.end(), path);
9697
if (iter != this->storageBackends_.end()) { return Status::OK(); }
@@ -104,14 +105,22 @@ Status SpaceLayout::AddSecondaryStorageBackend(const std::string& path)
104105
return Status::OK();
105106
}
106107

107-
std::string SpaceLayout::StorageBackend(const std::string& blockId) const
108+
std::string SpaceShardLayout::StorageBackend(const std::string& blockId) const
108109
{
109110
static std::hash<std::string> hasher;
110111
return this->storageBackends_[hasher(blockId) % this->storageBackends_.size()];
111112
}
112113

113-
std::vector<std::string> SpaceLayout::RelativeRoots() const { return {this->DataFileRoot()}; }
114+
std::vector<std::string> SpaceShardLayout::RelativeRoots() const { return {this->DataFileRoot()}; }
115+
116+
std::string SpaceShardLayout::DataFileRoot() const { return "data"; }
114117

115-
std::string SpaceLayout::DataFileRoot() const { return "data"; }
118+
void SpaceShardLayout::ShardBlockId(const std::string& blockId, uint64_t& front,
119+
uint64_t& back) const
120+
{
121+
auto id = static_cast<const BlockId*>(static_cast<const void*>(blockId.data()));
122+
front = id->front();
123+
back = id->back();
124+
}
116125

117126
} // namespace UC
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
* */
24+
#ifndef UNIFIEDCACHE_SPACE_SHARD_LAYOUT_H
25+
#define UNIFIEDCACHE_SPACE_SHARD_LAYOUT_H
26+
27+
#include "space_layout.h"
28+
29+
namespace UC {
30+
31+
class SpaceShardLayout : public SpaceLayout {
32+
public:
33+
Status Setup(const std::vector<std::string>& storageBackends) override;
34+
std::string DataFileParent(const std::string& blockId, bool activated) const override;
35+
std::string DataFilePath(const std::string& blockId, bool activated) const override;
36+
37+
protected:
38+
virtual std::vector<std::string> RelativeRoots() const;
39+
virtual Status AddStorageBackend(const std::string& path);
40+
virtual Status AddFirstStorageBackend(const std::string& path);
41+
virtual Status AddSecondaryStorageBackend(const std::string& path);
42+
virtual std::string StorageBackend(const std::string& blockId) const;
43+
virtual std::string DataFileRoot() const;
44+
virtual void ShardBlockId(const std::string& blockId, uint64_t& front, uint64_t& back) const;
45+
std::vector<std::string> storageBackends_;
46+
};
47+
48+
} // namespace UC
49+
50+
#endif

0 commit comments

Comments
 (0)