Skip to content

Commit 4a68847

Browse files
committed
curvebs(client): Fix always get file info from cache
Signed-off-by: Hanqing Wu <wuhanqing@corp.netease.com>
1 parent 0ff8409 commit 4a68847

File tree

6 files changed

+187
-7
lines changed

6 files changed

+187
-7
lines changed

src/client/file_instance.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,26 @@ void FileInstance::StopLease() {
276276
}
277277
}
278278

279+
FInfo FileInstance::GetCurrentFileInfo(bool force) {
280+
FInfo info = finfo_;
281+
if (!force) {
282+
return info;
283+
}
284+
285+
FileEpoch fEpoch;
286+
int rc = mdsclient_->GetFileInfo(info.fullPathName, info.userinfo, &info,
287+
&fEpoch);
288+
(void)fEpoch;
289+
if (rc != LIBCURVE_ERROR::OK) {
290+
LOG(WARNING) << "Fail to get current file info from mds, return cached"
291+
"file info, filename: "
292+
<< info.fullPathName;
293+
} else {
294+
finfo_ = info;
295+
}
296+
297+
return info;
298+
}
299+
279300
} // namespace client
280301
} // namespace curve

src/client/file_instance.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,10 @@ class CURVE_CACHELINE_ALIGNMENT FileInstance {
118118

119119
/**
120120
* @brief 获取当前instance对应的文件信息
121-
*
121+
* @param force whether force to get file info from mds
122122
* @return 当前instance对应文件的信息
123123
*/
124-
FInfo GetCurrentFileInfo() const {
125-
return finfo_;
126-
}
124+
FInfo GetCurrentFileInfo(bool force = false);
127125

128126
static FileInstance* NewInitedFileInstance(
129127
const FileServiceOption& fileServiceOption,

src/client/libcurve_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ int FileClient::StatFile(int fd, FileStatInfo *finfo) {
475475
return -LIBCURVE_ERROR::FAILED;
476476
}
477477
FileInstance *instance = fileserviceMap_[fd];
478-
fi = instance->GetCurrentFileInfo();
478+
fi = instance->GetCurrentFileInfo(/*force=*/true);
479479
}
480480
BuildFileStatInfo(fi, finfo);
481481

test/client/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ cc_test(
7979
"@com_google_googletest//:gtest",
8080
"@com_google_googletest//:gtest_main",
8181
"//test/integration/cluster_common:integration_cluster_common",
82+
"//test/client/mock:curve_client_mock",
8283
],
8384
)
8485

test/client/file_instance_test.cpp

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020
* Author: wuhanqing
2121
*/
2222

23-
#include <gtest/gtest.h>
24-
#include <gflags/gflags.h>
2523
#include "src/client/file_instance.h"
2624

25+
#include <brpc/server.h>
26+
#include <gmock/gmock.h>
27+
#include <gtest/gtest.h>
28+
29+
#include "test/client/mock/mock_namespace_service.h"
30+
2731
namespace curve {
2832
namespace client {
2933

34+
using ::testing::_;
35+
using ::testing::DoAll;
36+
using ::testing::Invoke;
37+
using ::testing::SaveArgPointee;
38+
3039
TEST(FileInstanceTest, CommonTest) {
3140
UserInfo userInfo{"test", "passwd"};
3241
std::shared_ptr<MDSClient> mdsclient = std::make_shared<MDSClient>();
@@ -72,5 +81,145 @@ TEST(FileInstanceTest, IoAlignmentTest) {
7281
ASSERT_FALSE(CheckAlign(511, 511, 512));
7382
}
7483

84+
constexpr size_t kMiB = 1 << 20;
85+
constexpr size_t kGiB = 1 << 30;
86+
87+
class FileInstanceGetFileInfoTest : public ::testing::Test {
88+
protected:
89+
void SetUp() override {
90+
FileServiceOption opts;
91+
opts.metaServerOpt.mdsAddrs = {kSvrAddr};
92+
93+
ASSERT_EQ(0, mdsclient_->Initialize(opts.metaServerOpt));
94+
ASSERT_EQ(0, server_.AddService(&nameService_,
95+
brpc::SERVER_DOESNT_OWN_SERVICE));
96+
ASSERT_EQ(0, server_.Start(kSvrAddr, nullptr));
97+
98+
UserInfo user("test", "");
99+
instance_.reset(FileInstance::NewInitedFileInstance(
100+
opts, mdsclient_, "/test", user, OpenFlags{},
101+
/*readonly=*/false));
102+
103+
EXPECT_CALL(nameService_, OpenFile(_, _, _, _))
104+
.WillOnce(Invoke([](::google::protobuf::RpcController* cntl,
105+
const curve::mds::OpenFileRequest*,
106+
curve::mds::OpenFileResponse* response,
107+
google::protobuf::Closure* done) {
108+
response->set_statuscode(curve::mds::StatusCode::kOK);
109+
auto* info = response->mutable_fileinfo();
110+
info->set_id(2);
111+
info->set_parentid(1);
112+
info->set_chunksize(16ULL * kMiB);
113+
info->set_segmentsize(1ULL * kGiB);
114+
info->set_length(1ULL * kGiB);
115+
auto* session = response->mutable_protosession();
116+
session->set_sessionid("1");
117+
session->set_leasetime(60);
118+
session->set_createtime(0);
119+
session->set_sessionstatus(
120+
curve::mds::SessionStatus::kSessionOK);
121+
done->Run();
122+
}));
123+
124+
EXPECT_CALL(nameService_, CloseFile(_, _, _, _))
125+
.WillOnce(Invoke([](::google::protobuf::RpcController* cntl,
126+
const curve::mds::CloseFileRequest*,
127+
curve::mds::CloseFileResponse* response,
128+
google::protobuf::Closure* done) {
129+
response->set_statuscode(curve::mds::StatusCode::kOK);
130+
done->Run();
131+
}));
132+
133+
EXPECT_CALL(nameService_, RefreshSession(_, _, _, _))
134+
.WillRepeatedly(
135+
Invoke([](::google::protobuf::RpcController* cntl,
136+
const curve::mds::ReFreshSessionRequest*,
137+
curve::mds::ReFreshSessionResponse* response,
138+
google::protobuf::Closure* done) {
139+
response->set_statuscode(curve::mds::StatusCode::kOK);
140+
response->set_sessionid("");
141+
done->Run();
142+
}));
143+
144+
ASSERT_EQ(0, instance_->Open());
145+
}
146+
147+
void TearDown() override {
148+
ASSERT_EQ(0, instance_->Close());
149+
150+
server_.Stop(0);
151+
server_.Join();
152+
}
153+
154+
protected:
155+
const char* kSvrAddr = "127.0.0.1:9610";
156+
157+
std::shared_ptr<MDSClient> mdsclient_ = std::make_shared<MDSClient>();
158+
std::unique_ptr<FileInstance> instance_;
159+
160+
brpc::Server server_;
161+
curve::mds::MockNameService nameService_;
162+
};
163+
164+
TEST_F(FileInstanceGetFileInfoTest, TestGetInfoFromCache) {
165+
auto info = instance_->GetCurrentFileInfo();
166+
ASSERT_EQ(1ULL * kGiB, info.length);
167+
}
168+
169+
TEST_F(FileInstanceGetFileInfoTest, TestForceGetInfo) {
170+
// get info from mds success, return cached fileinfo
171+
{
172+
EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _))
173+
.WillOnce(Invoke([](::google::protobuf::RpcController* cntl,
174+
const curve::mds::GetFileInfoRequest*,
175+
curve::mds::GetFileInfoResponse* response,
176+
google::protobuf::Closure* done) {
177+
response->set_statuscode(
178+
curve::mds::StatusCode::kFileNotExists);
179+
done->Run();
180+
}));
181+
182+
auto info = instance_->GetCurrentFileInfo(/*force=*/true);
183+
ASSERT_EQ(1ULL * kGiB, info.length);
184+
}
185+
186+
// get info from mds success, return new fileinfo
187+
{
188+
EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _))
189+
.WillOnce(Invoke([](::google::protobuf::RpcController* cntl,
190+
const curve::mds::GetFileInfoRequest*,
191+
curve::mds::GetFileInfoResponse* response,
192+
google::protobuf::Closure* done) {
193+
response->set_statuscode(curve::mds::StatusCode::kOK);
194+
auto* info = response->mutable_fileinfo();
195+
info->set_id(2);
196+
info->set_parentid(1);
197+
info->set_chunksize(16ULL * kMiB);
198+
info->set_segmentsize(1ULL * kGiB);
199+
info->set_length(2ULL * kGiB); // increase file size to 2GiB
200+
done->Run();
201+
}));
202+
203+
auto info = instance_->GetCurrentFileInfo(/*force=*/true);
204+
ASSERT_EQ(2ULL * kGiB, info.length);
205+
}
206+
207+
// get info from mds success, return cached fileinfo
208+
{
209+
EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _))
210+
.WillOnce(Invoke([](::google::protobuf::RpcController* cntl,
211+
const curve::mds::GetFileInfoRequest*,
212+
curve::mds::GetFileInfoResponse* response,
213+
google::protobuf::Closure* done) {
214+
response->set_statuscode(
215+
curve::mds::StatusCode::kFileNotExists);
216+
done->Run();
217+
}));
218+
219+
auto info = instance_->GetCurrentFileInfo(/*force=*/true);
220+
ASSERT_EQ(2ULL * kGiB, info.length);
221+
}
222+
}
223+
75224
} // namespace client
76225
} // namespace curve

test/client/mock/mock_namespace_service.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class MockNameService : public CurveFSService {
3838
OpenFileResponse* response,
3939
google::protobuf::Closure* done));
4040

41+
MOCK_METHOD4(CloseFile, void(google::protobuf::RpcController* cntl,
42+
const CloseFileRequest* request,
43+
CloseFileResponse* response,
44+
google::protobuf::Closure* done));
45+
4146
MOCK_METHOD4(DeleteFile, void(google::protobuf::RpcController* cntl,
4247
const DeleteFileRequest* request,
4348
DeleteFileResponse* response,
@@ -64,6 +69,12 @@ class MockNameService : public CurveFSService {
6469
const curve::mds::IncreaseFileEpochRequest* request,
6570
curve::mds::IncreaseFileEpochResponse* response,
6671
::google::protobuf::Closure* done));
72+
73+
MOCK_METHOD4(GetFileInfo,
74+
void(::google::protobuf::RpcController* controller,
75+
const ::curve::mds::GetFileInfoRequest* request,
76+
::curve::mds::GetFileInfoResponse* response,
77+
::google::protobuf::Closure* done));
6778
};
6879

6980
} // namespace mds

0 commit comments

Comments
 (0)