Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
budevg committed Oct 8, 2024
1 parent e3e1c7b commit 20caee1
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 68 deletions.
2 changes: 1 addition & 1 deletion cloud/filestore/config/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ message TLocalServiceConfig
optional string StatePath = 6;

// Maximum number of inodes which can be created by all sessions
optional uint32 MaxInodeCount = 7;
optional uint32 MaxNodeCount = 7;

// Maximum number of file handles which can be opened by single session
optional uint32 MaxHandlePerSessionCount = 8;
Expand Down
2 changes: 1 addition & 1 deletion cloud/filestore/libs/service_local/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace {
xxx(IdleSessionTimeout, TDuration, TDuration::Seconds(30) )\
xxx(NumThreads, ui32, 4 )\
xxx(StatePath, TString, "./" )\
xxx(MaxInodeCount, ui32, 1000000 )\
xxx(MaxNodeCount, ui32, 1000000 )\
xxx(MaxHandlePerSessionCount, ui32, 10000 )\
// FILESTORE_SERVICE_CONFIG

Expand Down
2 changes: 1 addition & 1 deletion cloud/filestore/libs/service_local/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TLocalFileStoreConfig
TDuration GetIdleSessionTimeout() const;
ui32 GetNumThreads() const;
TString GetStatePath() const;
ui32 GetMaxInodeCount() const;
ui32 GetMaxNodeCount() const;
ui32 GetMaxHandlePerSessionCount() const;

void Dump(IOutputStream& out) const;
Expand Down
2 changes: 1 addition & 1 deletion cloud/filestore/libs/service_local/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TLocalFileSystem::TLocalFileSystem(
Index = std::make_shared<TLocalIndex>(
Root,
StatePath,
Config->GetMaxInodeCount(),
Config->GetMaxNodeCount(),
Log);

ScheduleCleanupSessions();
Expand Down
10 changes: 7 additions & 3 deletions cloud/filestore/libs/service_local/fs_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ NProto::TCreateSessionResponse TLocalFileSystem::CreateSession(
Index,
Logging);

session->Init(request.GetRestoreClientSession(), Config->GetMaxHandlePerSessionCount());
session->Init(
request.GetRestoreClientSession(),
Config->GetMaxHandlePerSessionCount());
session->AddSubSession(sessionSeqNo, readOnly);

SessionsList.push_front(session);

auto [_, inserted1] = SessionsByClient.emplace(clientId, SessionsList.begin());
auto [_, inserted1] =
SessionsByClient.emplace(clientId, SessionsList.begin());
Y_ABORT_UNLESS(inserted1);

auto [dummyIt, inserted2] = SessionsById.emplace(session->SessionId, SessionsList.begin());
auto [dummyIt, inserted2] =
SessionsById.emplace(session->SessionId, SessionsList.begin());
Y_ABORT_UNLESS(inserted2);

NProto::TCreateSessionResponse response;
Expand Down
67 changes: 44 additions & 23 deletions cloud/filestore/libs/service_local/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class TIndexNode
private:
const ui64 NodeId;
const TFileHandle NodeFd;
ui64 RecordIndex;
ui64 TableIndex;
ui64 RecordIndex = -1;

public:
TIndexNode(ui64 nodeId, TFileHandle node)
Expand Down Expand Up @@ -124,8 +123,8 @@ class TLocalIndex

struct TNodeTableRecord
{
ui64 NodeId;
ui64 ParentNodeId;
ui64 NodeId = 0;
ui64 ParentNodeId = 0;
char Name[NAME_MAX + 1];
};

Expand All @@ -135,19 +134,19 @@ class TLocalIndex
using TNodeTable = TPersistentTable<TNodeTableHeader, TNodeTableRecord>;

TNodeMap Nodes;
std::unique_ptr<TNodeTable> NodesTable;
std::unique_ptr<TNodeTable> NodeTable;
TRWMutex NodesLock;
const TLog& Log;

public:
TLocalIndex(
const TFsPath& root,
const TFsPath& statePath,
ui32 maxInodeCount,
ui32 maxNodeCount,
const TLog& log)
: Log(log)
{
Init(root, statePath, maxInodeCount);
Init(root, statePath, maxNodeCount);
}

TIndexNodePtr LookupNode(ui64 nodeId)
Expand All @@ -172,20 +171,20 @@ class TLocalIndex
return true;
}

auto recordIndex = NodesTable->AllocRecord();
auto recordIndex = NodeTable->AllocRecord();
if (recordIndex == TNodeTable::InvalidIndex) {
return false;
}

auto* record = NodesTable->RecordData(recordIndex);
auto* record = NodeTable->RecordData(recordIndex);

record->NodeId = node->GetNodeId();
record->ParentNodeId = parentNodeId;

std::strncpy(record->Name, name.c_str(), NAME_MAX);
record->Name[NAME_MAX] = 0;

NodesTable->CommitRecord(recordIndex);
NodeTable->CommitRecord(recordIndex);

node->SetRecordIndex(recordIndex);
Nodes.emplace(std::move(node));
Expand All @@ -201,76 +200,99 @@ class TLocalIndex
auto it = Nodes.find(nodeId);
if (it != Nodes.end()) {
node = *it;
NodesTable->DeleteRecord(node->GetRecordIndex());
NodeTable->DeleteRecord(node->GetRecordIndex());
Nodes.erase(it);
}

return node;
}

private:
void Init(const TFsPath& root, const TFsPath& statePath, ui32 maxInodeCount)
void Init(const TFsPath& root, const TFsPath& statePath, ui32 maxNodeCount)
{
STORAGE_INFO(
"Init index, Root=" << root <<
", StatePath=" << statePath
<< ", MaxInodeCount=" << maxInodeCount);
<< ", MaxNodeCount=" << maxNodeCount);

Nodes.insert(TIndexNode::CreateRoot(root));

NodesTable = std::make_unique<TNodeTable>(
NodeTable = std::make_unique<TNodeTable>(
(statePath / "nodes").GetPath(),
maxInodeCount);
maxNodeCount);

RecoverNodesFromPersistentTable();

}

void RecoverNodesFromPersistentTable()
{
// enties are ordered by NodeId in TMap but this doesn't mean that
// a/b/c/d has order a, b, c, d usually inode number increased but
// directories can move so directory a which was created later can
// contain directory b which was created before so and NodeId(a) >
// NodeId(b) for a/b
TMap<ui64, ui64> unresolvedRecords;
for (auto it = NodesTable->begin(); it != NodesTable->end(); it++) {
for (auto it = NodeTable->begin(); it != NodeTable->end(); it++) {
unresolvedRecords[it->NodeId] = it.GetIndex();
STORAGE_TRACE(
"Unresolved record, NodeId=" << it->NodeId <<
", ParentNodeId=" << it->ParentNodeId <<
", Name=" << it->Name);
"Unresolved record, NodeId=" << it->NodeId << ", ParentNodeId="
<< it->ParentNodeId
<< ", Name=" << it->Name);
}

while (!unresolvedRecords.empty()) {
TStack<ui64> unresolvedPath;
unresolvedPath.push(unresolvedRecords.begin()->second);

// For entry /a we can resolve immideatly and create TIndexNode
// but for entry d in /a/b/c/d path we must resolve the whole path
// recursively
while (!unresolvedPath.empty()) {
auto pathElemIndex = unresolvedPath.top();
auto pathElemRecord = NodesTable->RecordData(pathElemIndex);
auto pathElemRecord = NodeTable->RecordData(pathElemIndex);

STORAGE_TRACE(
"Resolve node start, NodeId=" << pathElemRecord->NodeId);

auto parentNodeIt = Nodes.find(pathElemRecord->ParentNodeId);
if (parentNodeIt == Nodes.end()) {
// parent is not resloved

STORAGE_TRACE(
"Need to resolve parent NodeId="
<< pathElemRecord->ParentNodeId);
auto parentRecordIt =
unresolvedRecords.find(pathElemRecord->ParentNodeId);
if (parentRecordIt == unresolvedRecords.end()) {
// parent was not saved in persistent table so we can't
// resolve it in cased of d in path /a/b/c/d if we
// discover that b can't be resolved we need to discard
// b, c, d inodes
STORAGE_ERROR(
"Parent node is missing in table, NodeId="
<< pathElemRecord->ParentNodeId);
while (!unresolvedPath.empty()) {
auto discardedIndex = unresolvedPath.top();
auto* discardedRecord =
NodesTable->RecordData(discardedIndex);
NodeTable->RecordData(discardedIndex);
STORAGE_WARN(
"Discarding NodeId="
<< discardedRecord->NodeId);
unresolvedRecords.erase(discardedRecord->NodeId);
NodesTable->DeleteRecord(discardedIndex);
NodeTable->DeleteRecord(discardedIndex);
unresolvedPath.pop();
}
continue;
}

// add to unresolvedPath and solve recursively
unresolvedPath.push(parentRecordIt->second);
continue;
}

// parent already resolved so we can create node and resolve
// this entry
auto node =
TIndexNode::Create(**parentNodeIt, pathElemRecord->Name);
node->SetRecordIndex(pathElemIndex);
Expand All @@ -296,7 +318,6 @@ class TLocalIndex
{
return value;
}

};

} // namespace NCloud::NFileStore
Loading

0 comments on commit 20caee1

Please sign in to comment.