Skip to content

Commit d9dfe9e

Browse files
committed
issue-5293: refactored TNodeCache iface and moved NodeCacheLock into TNodeCache
1 parent cf5e7a9 commit d9dfe9e

File tree

6 files changed

+49
-79
lines changed

6 files changed

+49
-79
lines changed

cloud/filestore/libs/vfs_fuse/fs_impl.cpp

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,17 @@ bool TFileSystem::UpdateNodeAttrsInCache(
173173
entry.ino = attrs.GetId();
174174
entry.generation = 0; // TODO
175175

176-
with_lock (NodeCacheLock) {
177-
auto* node = NodeCache.TryAddNode(attrs, version);
178-
Y_ABORT_UNLESS(node);
179-
180-
if (node->GetVersion() == version) {
181-
entry.attr_timeout = Config->GetAttrTimeout().SecondsFloat();
182-
entry.entry_timeout = GetEntryCacheTimeout(attrs).SecondsFloat();
183-
184-
ConvertAttr(
185-
Config->GetPreferredBlockSize(),
186-
node->GetAttrs(),
187-
entry.attr);
188-
return true;
189-
}
176+
const bool updated = NodeCache.UpdateNode(attrs, version);
177+
if (updated) {
178+
entry.attr_timeout = Config->GetAttrTimeout().SecondsFloat();
179+
entry.entry_timeout = GetEntryCacheTimeout(attrs).SecondsFloat();
180+
} else {
181+
entry.attr_timeout = 0;
182+
entry.entry_timeout = 0;
190183
}
191184

192-
entry.attr_timeout = 0;
193-
entry.entry_timeout = 0;
194185
ConvertAttr(Config->GetPreferredBlockSize(), attrs, entry.attr);
195-
return false;
186+
return updated;
196187
}
197188

198189
void TFileSystem::InvalidateNodeInCache(ui64 nodeId)
@@ -203,9 +194,7 @@ void TFileSystem::InvalidateNodeInCache(ui64 nodeId)
203194
STORAGE_TRACE("invalidating node: " << nodeId
204195
<< ", version: " << newVersion);
205196

206-
with_lock (NodeCacheLock) {
207-
NodeCache.InvalidateNode(nodeId, newVersion);
208-
}
197+
NodeCache.InvalidateNode(nodeId, newVersion);
209198
}
210199

211200
void TFileSystem::UpdateXAttrCache(
@@ -251,9 +240,7 @@ void TFileSystem::ReplyCreateWithCache(
251240
const int res = ReplyCreate(callContext, error, req, &entry, &fi);
252241
if (res == -ENOENT) {
253242
// syscall was interrupted
254-
with_lock (NodeCacheLock) {
255-
NodeCache.ForgetNode(entry.ino, 1);
256-
}
243+
NodeCache.ForgetNode(entry.ino, 1);
257244
}
258245
}
259246

@@ -278,9 +265,7 @@ void TFileSystem::ReplyEntryWithCache(
278265
const int res = ReplyEntry(callContext, error, req, &entry);
279266
if (res == -ENOENT) {
280267
// syscall was interrupted
281-
with_lock (NodeCacheLock) {
282-
NodeCache.ForgetNode(entry.ino, 1);
283-
}
268+
NodeCache.ForgetNode(entry.ino, 1);
284269
}
285270
}
286271

cloud/filestore/libs/vfs_fuse/fs_impl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class TFileSystem final
9696
std::unique_ptr<NVFS::IFSyncQueue> FSyncQueue;
9797

9898
TNodeCache NodeCache;
99-
TAdaptiveLock NodeCacheLock;
10099

101100
THashMap<ui64, std::shared_ptr<TDirectoryHandle>> DirectoryHandles;
102101
TMutex DirectoryHandlesLock;

cloud/filestore/libs/vfs_fuse/fs_impl_list.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ void TFileSystem::ReadDir(
134134
TBuffer c(content.GetData(), content.GetSize());
135135

136136
ResetAttrTimeout(c.Data(), c.Size(), [&] (ui64 ino) {
137-
with_lock (NodeCacheLock) {
138-
const auto* node = NodeCache.FindNode(ino);
139-
return node && node->GetVersion() > content.AttrVersion;
140-
}
137+
return NodeCache.GetNodeVersion(ino) > content.AttrVersion;
141138
});
142139

143140
fs.ReplyBuf(*callContext, {}, req, c.Data(), c.Size());

cloud/filestore/libs/vfs_fuse/fs_impl_node.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ void TFileSystem::Forget(
7777
fuse_ino_t ino,
7878
unsigned long nlookup)
7979
{
80-
with_lock (NodeCacheLock) {
81-
NodeCache.ForgetNode(ino, nlookup);
82-
}
83-
80+
NodeCache.ForgetNode(ino, nlookup);
8481
ReplyNone(*callContext, {}, req);
8582
}
8683

@@ -90,10 +87,8 @@ void TFileSystem::ForgetMulti(
9087
size_t count,
9188
fuse_forget_data* forgets)
9289
{
93-
with_lock (NodeCacheLock) {
94-
for (size_t i = 0; i < count; ++i) {
95-
NodeCache.ForgetNode(forgets[i].ino, forgets[i].nlookup);
96-
}
90+
for (size_t i = 0; i < count; ++i) {
91+
NodeCache.ForgetNode(forgets[i].ino, forgets[i].nlookup);
9792
}
9893

9994
ReplyNone(*callContext, {}, req);

cloud/filestore/libs/vfs_fuse/node_cache.cpp

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,45 @@ namespace NCloud::NFileStore::NFuse {
1111

1212
////////////////////////////////////////////////////////////////////////////////
1313

14-
TNode* TNodeCache::AddNode(const NProto::TNodeAttr& attrs)
14+
bool TNodeCache::UpdateNode(const NProto::TNodeAttr& attrs, ui64 version)
1515
{
16-
auto [it, inserted] = Id2Node.emplace(attrs.GetId(), attrs);
17-
STORAGE_VERIFY_C(
18-
inserted,
19-
TWellKnownEntityTypes::FILESYSTEM,
20-
FileSystemId,
21-
TStringBuilder() << "failed to insert node "
22-
<< attrs.ShortUtf8DebugString());
23-
24-
STORAGE_VERIFY_C(
25-
it->second.IsValid(),
26-
TWellKnownEntityTypes::FILESYSTEM,
27-
FileSystemId,
28-
TStringBuilder() << "invalid attrs: " << attrs.ShortUtf8DebugString());
29-
30-
return &it->second;
31-
}
32-
33-
TNode* TNodeCache::TryAddNode(const NProto::TNodeAttr& attrs, ui64 version)
34-
{
35-
auto* node = FindNode(attrs.GetId());
36-
if (!node) {
37-
node = AddNode(attrs);
38-
node->LastUpdateVersion = version;
16+
auto g = Guard(Lock);
17+
18+
auto [it, inserted] = Id2Node.emplace(attrs.GetId(), TNode(attrs));
19+
auto& node = it->second;
20+
bool updated = false;
21+
if (inserted) {
22+
node.LastUpdateVersion = version;
23+
updated = true;
3924
} else {
40-
if (version >= node->LastUpdateVersion) {
41-
node->UpdateAttrs(attrs, version);
42-
node->LastUpdateVersion = version;
25+
if (version >= node.LastUpdateVersion) {
26+
node.UpdateAttrs(attrs, version);
27+
node.LastUpdateVersion = version;
4328

4429
STORAGE_VERIFY_C(
45-
node->IsValid(),
30+
node.IsValid(),
4631
TWellKnownEntityTypes::FILESYSTEM,
4732
FileSystemId,
4833
TStringBuilder() << "invalid attrs: "
4934
<< attrs.ShortUtf8DebugString()
5035
<< ", version: " << version);
36+
37+
updated = true;
5138
}
5239

53-
node->Ref();
40+
node.Ref();
5441
}
5542

56-
return node;
43+
return updated;
5744
}
5845

5946
void TNodeCache::InvalidateNode(ui64 ino, ui64 version)
6047
{
48+
auto g = Guard(Lock);
49+
6150
NProto::TNodeAttr attrs;
6251
attrs.SetId(ino);
63-
auto [it, inserted] = Id2Node.emplace(ino, attrs);
52+
auto [it, inserted] = Id2Node.emplace(ino, TNode(std::move(attrs)));
6453
if (version >= it->second.LastUpdateVersion) {
6554
if (!inserted) {
6655
it->second.Attrs = std::move(attrs);
@@ -72,6 +61,8 @@ void TNodeCache::InvalidateNode(ui64 ino, ui64 version)
7261

7362
void TNodeCache::ForgetNode(ui64 ino, size_t count)
7463
{
64+
auto g = Guard(Lock);
65+
7566
auto it = Id2Node.find(ino);
7667
if (it == Id2Node.end()) {
7768
// we lose our cache after restart, so we should expect forget requests
@@ -88,9 +79,12 @@ void TNodeCache::ForgetNode(ui64 ino, size_t count)
8879
}
8980
}
9081

91-
TNode* TNodeCache::FindNode(ui64 ino)
82+
ui64 TNodeCache::GetNodeVersion(ui64 ino) const
9283
{
93-
return Id2Node.FindPtr(ino);
84+
auto g = Guard(Lock);
85+
86+
const auto* node = Id2Node.FindPtr(ino);
87+
return node ? node->LastUpdateVersion : 0;
9488
}
9589

9690
////////////////////////////////////////////////////////////////////////////////

cloud/filestore/libs/vfs_fuse/node_cache.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class TNode
2323
ui64 RefCount = 1;
2424
ui64 LastUpdateVersion = 1;
2525

26-
public:
27-
explicit TNode(const NProto::TNodeAttr& attrs) noexcept
28-
: Attrs(attrs)
26+
private:
27+
explicit TNode(NProto::TNodeAttr attrs) noexcept
28+
: Attrs(std::move(attrs))
2929
{
30-
Y_ABORT_UNLESS(attrs.GetId() != InvalidNodeId);
30+
Y_ABORT_UNLESS(Attrs.GetId() != InvalidNodeId);
3131
}
3232

3333
public:
@@ -94,18 +94,18 @@ class TNodeCache
9494
private:
9595
const TString FileSystemId;
9696
THashMap<ui64, TNode> Id2Node;
97+
TAdaptiveLock Lock;
9798

9899
public:
99100
explicit TNodeCache(TString fileSystemId)
100101
: FileSystemId(std::move(fileSystemId))
101102
{}
102103

103104
public:
104-
TNode* AddNode(const NProto::TNodeAttr& attrs);
105-
TNode* TryAddNode(const NProto::TNodeAttr& attrs, ui64 version);
105+
bool UpdateNode(const NProto::TNodeAttr& attrs, ui64 version);
106106
void InvalidateNode(ui64 ino, ui64 version);
107-
TNode* FindNode(ui64 ino);
108107
void ForgetNode(ui64 ino, size_t count);
108+
ui64 GetNodeVersion(ui64 ino) const;
109109
};
110110

111111
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)