-
Notifications
You must be signed in to change notification settings - Fork 526
use butil::IOBuf in WriteChunk #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,6 +329,33 @@ int Ext4FileSystemImpl::Write(int fd, | |
return length; | ||
} | ||
|
||
int Ext4FileSystemImpl::Write(int fd, | ||
butil::IOBuf buf, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const & There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是需要把iobuf写入到fd中,会涉及到对iobuf的修改,所以不能用const引用。 |
||
uint64_t offset, | ||
int length) { | ||
int remainLength = length; | ||
int relativeOffset = 0; | ||
int retryTimes = 0; | ||
|
||
while (remainLength > 0) { | ||
ssize_t ret = buf.pcut_into_file_descriptor(fd, offset, remainLength); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 会分批写入文件吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 正常来说不会。这里内部还是用的pwritev来做的,有可能会返回EINTR。 |
||
if (ret < 0) { | ||
if (errno == EINTR || retryTimes < MAX_RETYR_TIME) { | ||
++retryTimes; | ||
continue; | ||
} | ||
LOG(ERROR) << "IOBuf::pcut_into_file_descriptor failed: " | ||
<< strerror(errno); | ||
return -errno; | ||
} | ||
|
||
remainLength -= ret; | ||
offset += ret; | ||
} | ||
|
||
return length; | ||
} | ||
|
||
int Ext4FileSystemImpl::Append(int fd, | ||
const char *buf, | ||
int length) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,12 @@ | |
#ifndef SRC_FS_EXT4_FILESYSTEM_IMPL_H_ | ||
#define SRC_FS_EXT4_FILESYSTEM_IMPL_H_ | ||
|
||
#include <butil/iobuf.h> | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
#include <map> | ||
|
||
#include "src/fs/local_filesystem.h" | ||
#include "src/fs/wrap_posix.h" | ||
|
@@ -52,6 +54,7 @@ class Ext4FileSystemImpl : public LocalFileSystem { | |
int List(const string& dirPath, vector<std::string>* names) override; | ||
int Read(int fd, char* buf, uint64_t offset, int length) override; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read这里返回也会从char*拷贝一次到iobuf? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 读请求的buffer,会通过iobuf::append_user_data的方式由iobuf管理,这种情况不会拷贝。 |
||
int Write(int fd, const char* buf, uint64_t offset, int length) override; | ||
int Write(int fd, butil::IOBuf buf, uint64_t offset, int length) override; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const & |
||
int Append(int fd, const char* buf, int length) override; | ||
int Fallocate(int fd, int op, uint64_t offset, | ||
int length) override; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
#include <inttypes.h> | ||
#include <assert.h> | ||
#include <sys/stat.h> | ||
#include <butil/iobuf.h> | ||
#include <memory> | ||
#include <vector> | ||
#include <map> | ||
|
@@ -159,6 +160,17 @@ class LocalFileSystem { | |
*/ | ||
virtual int Write(int fd, const char* buf, uint64_t offset, int length) = 0; | ||
|
||
/** | ||
* 向文件指定区域写入数据 | ||
* @param fd:文件句柄id,通过Open接口获取 | ||
* @param buf:待写入数据 | ||
* @param offset:写入区域的起始偏移 | ||
* @param length:写入数据的长度 | ||
* @return 返回成功写入的数据长度,失败返回-1 | ||
*/ | ||
virtual int Write(int fd, butil::IOBuf buf, uint64_t offset, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
int length) = 0; | ||
|
||
/** | ||
* 向文件末尾追加数据 | ||
* @param fd:文件句柄id,通过Open接口获取 | ||
|
@@ -218,4 +230,3 @@ class LocalFsFactory { | |
} // namespace fs | ||
} // namespace curve | ||
#endif // SRC_FS_LOCAL_FILESYSTEM_H_ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ using ::testing::Ge; | |
using ::testing::Gt; | ||
using ::testing::Return; | ||
using ::testing::NotNull; | ||
using ::testing::Matcher; | ||
using ::testing::Mock; | ||
using ::testing::Truly; | ||
using ::testing::DoAll; | ||
|
@@ -161,7 +162,7 @@ TEST_F(CSChunkfilePoolMockTest, PersistEnCodeMetaInfoTest) { | |
{ | ||
EXPECT_CALL(*lfs_, Open(poolMetaPath, _)) | ||
.WillOnce(Return(-1)); | ||
EXPECT_CALL(*lfs_, Write(_, _, _, _)) | ||
EXPECT_CALL(*lfs_, Write(_, Matcher<const char*>(_), _, _)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为啥这些还是会带char*的参数? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是因为写入操作可能会与快照的metapage有关,更新metapage调用的是Write(_, const char*,....)这个接口。 |
||
.Times(0); | ||
EXPECT_CALL(*lfs_, Close(_)) | ||
.Times(0); | ||
|
@@ -176,7 +177,7 @@ TEST_F(CSChunkfilePoolMockTest, PersistEnCodeMetaInfoTest) { | |
{ | ||
EXPECT_CALL(*lfs_, Open(poolMetaPath, _)) | ||
.WillOnce(Return(1)); | ||
EXPECT_CALL(*lfs_, Write(1, NotNull(), 0, 4096)) | ||
EXPECT_CALL(*lfs_, Write(1, Matcher<const char*>(NotNull()), 0, 4096)) | ||
.WillOnce(Return(-1)); | ||
EXPECT_CALL(*lfs_, Close(1)) | ||
.Times(1); | ||
|
@@ -191,7 +192,7 @@ TEST_F(CSChunkfilePoolMockTest, PersistEnCodeMetaInfoTest) { | |
{ | ||
EXPECT_CALL(*lfs_, Open(poolMetaPath, _)) | ||
.WillOnce(Return(1)); | ||
EXPECT_CALL(*lfs_, Write(1, NotNull(), 0, 4096)) | ||
EXPECT_CALL(*lfs_, Write(1, Matcher<const char*>(NotNull()), 0, 4096)) | ||
.WillOnce(Return(4096)); | ||
EXPECT_CALL(*lfs_, Close(1)) | ||
.Times(1); | ||
|
@@ -770,7 +771,8 @@ TEST_F(CSChunkfilePoolMockTest, GetChunkTest) { | |
EXPECT_CALL(*lfs_, Fallocate(1, 0, 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(0)); | ||
EXPECT_CALL(*lfs_, Write(1, NotNull(), 0, fileSize)) | ||
EXPECT_CALL(*lfs_, | ||
Write(1, Matcher<const char*>(NotNull()), 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(-1)); | ||
EXPECT_CALL(*lfs_, Close(1)) | ||
|
@@ -787,7 +789,8 @@ TEST_F(CSChunkfilePoolMockTest, GetChunkTest) { | |
EXPECT_CALL(*lfs_, Fallocate(1, 0, 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(0)); | ||
EXPECT_CALL(*lfs_, Write(1, NotNull(), 0, fileSize)) | ||
EXPECT_CALL(*lfs_, | ||
Write(1, Matcher<const char*>(NotNull()), 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(fileSize)); | ||
EXPECT_CALL(*lfs_, Fsync(1)) | ||
|
@@ -807,7 +810,8 @@ TEST_F(CSChunkfilePoolMockTest, GetChunkTest) { | |
EXPECT_CALL(*lfs_, Fallocate(1, 0, 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(0)); | ||
EXPECT_CALL(*lfs_, Write(1, NotNull(), 0, fileSize)) | ||
EXPECT_CALL(*lfs_, | ||
Write(1, Matcher<const char*>(NotNull()), 0, fileSize)) | ||
.Times(retryTimes) | ||
.WillRepeatedly(Return(fileSize)); | ||
EXPECT_CALL(*lfs_, Fsync(1)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deprecated? 现在的测试中还是要用的吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试代码里还是用了很多这个接口,所以暂时没改掉。
这里备注了一下,正常情况下不应该使用这个接口,只有单元测试/集成测试需要。