From a06eb55d41b0ed997ab0a0ffa319b8f91f6176db Mon Sep 17 00:00:00 2001 From: LiuLele Date: Sun, 23 Oct 2022 00:22:48 +0800 Subject: [PATCH] fix issue 9 (https://github.com/cocalele/ViveNAS/issues/9). like associateMergeOperator, remove `AllowSingleOperand` and `ShouldMerge` solve this problem --- include/vn_data_merge.h | 6 ++++-- src/vn_data_merge.cpp | 44 ++++++++++++++--------------------------- src/vn_main.cpp | 18 +++++++++-------- testing/vntest_1.sh | 4 ++-- 4 files changed, 31 insertions(+), 41 deletions(-) diff --git a/include/vn_data_merge.h b/include/vn_data_merge.h index c947f6c..2dceb74 100644 --- a/include/vn_data_merge.h +++ b/include/vn_data_merge.h @@ -6,6 +6,8 @@ using ROCKSDB_NAMESPACE::MergeOperator; using ROCKSDB_NAMESPACE::Slice; using ROCKSDB_NAMESPACE::Logger; using ROCKSDB_NAMESPACE::AssociativeMergeOperator; + +//#define USE_ASSOCIATE_MERGE 1 #ifdef USE_ASSOCIATE_MERGE class ViveDataMergeOperator : public AssociativeMergeOperator { @@ -59,8 +61,8 @@ class ViveDataMergeOperator : public MergeOperator // Logger* logger) const; virtual bool FullMergeV2(const MergeOperationInput& merge_in, MergeOperationOutput* merge_out) const override; - virtual bool AllowSingleOperand() const override { return true; } - virtual bool ShouldMerge(const std::vector& /*operands*/) const override { return true; } + //virtual bool AllowSingleOperand() const override { return true; } + //virtual bool ShouldMerge(const std::vector& /*operands*/) const override { return true; } virtual const char* Name() const { diff --git a/src/vn_data_merge.cpp b/src/vn_data_merge.cpp index 85a0a7e..e8e0f6c 100644 --- a/src/vn_data_merge.cpp +++ b/src/vn_data_merge.cpp @@ -66,34 +66,6 @@ static inline int is_bit_set(int16_t& bmp, int index) // } // return true; //} -bool ViveDataMergeOperator::PartialMerge(const Slice& key, - const Slice& left_operand, - const Slice& right_operand, - std::string* new_value, - Logger* logger) const -{ - const struct pfs_extent_head* left_ext_head = (const struct pfs_extent_head* )left_operand.data(); - const char* left_data_buf = ((const char*)left_ext_head) + sizeof(struct pfs_extent_head); - const struct pfs_extent_head* right_ext_head = (const struct pfs_extent_head*)right_operand.data(); - char* right_data_buf = ((char*)right_ext_head) + sizeof(struct pfs_extent_head); - const struct pfs_extent_key* ext_key = (const struct pfs_extent_key*)key.data(); - S5LOG_DEBUG("PartialMerge extent:%s with left:%d + right:%d bytes", ext_key->to_string(), - left_operand.size() - sizeof(struct pfs_extent_head), - right_operand.size() - sizeof(struct pfs_extent_head)); - - - size_t ext_begin = min(left_ext_head->merge_off, right_ext_head->merge_off); - size_t ext_end = max(left_ext_head->merge_off + left_operand.size() - sizeof(struct pfs_extent_head), - right_ext_head->merge_off + right_operand.size() - sizeof(struct pfs_extent_head)); - - new_value->resize(ext_end - ext_begin + sizeof(struct pfs_extent_head)); - struct pfs_extent_head* new_ext_head = (struct pfs_extent_head* )new_value->data(); - char* new_data_buf = ((char*)new_ext_head) + sizeof(struct pfs_extent_head); - memcpy(new_data_buf + (left_ext_head->merge_off - ext_begin), left_data_buf, left_operand.size() - sizeof(struct pfs_extent_head)); - memcpy(new_data_buf + (right_ext_head->merge_off - ext_begin), right_data_buf, right_operand.size() - sizeof(struct pfs_extent_head)); - new_ext_head->merge_off = (int16_t)ext_begin; - return true; -} //bool ViveDataMergeOperator::PartialMergeMulti(const Slice& key, @@ -144,6 +116,19 @@ static bool _merge(const Slice& key, new_ext_head->merge_off = (int16_t)ext_begin; return true; } +bool ViveDataMergeOperator::PartialMerge(const Slice& key, + const Slice& left_operand, + const Slice& right_operand, + std::string* new_value, + Logger* logger) const +{ + + //这个函数实现和AssociativeMergeOperator 不同,导致了bug? + return _merge(key, &left_operand, right_operand, new_value, logger); + +} + + bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in, MergeOperationOutput* merge_out) const { @@ -170,7 +155,7 @@ bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in, // The result will be in *new_value. All merges succeeded. return true; -#endif +#else const struct pfs_extent_key* ext_key = (const struct pfs_extent_key*)merge_in.key.data(); S5LOG_DEBUG("FullMergeV2 extent:%s with %d operand", ext_key->to_string(), merge_in.operand_list.size()); @@ -204,6 +189,7 @@ bool ViveDataMergeOperator::FullMergeV2(const MergeOperationInput& merge_in, } return true; +#endif } #else bool ViveDataMergeOperator::Merge(const Slice& key, diff --git a/src/vn_main.cpp b/src/vn_main.cpp index 7cb6d61..c4009ee 100644 --- a/src/vn_main.cpp +++ b/src/vn_main.cpp @@ -127,6 +127,7 @@ ViveFsContext* vn_mount(const char* db_path) { ctx->root_inode.i_links_count = 2; ctx->root_inode.i_size = 4096; ctx->root_inode.i_atime = ctx->root_inode.i_ctime = time(NULL); + ctx->root_inode.ref_cnt = 1; string v; s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v); @@ -156,6 +157,7 @@ ViveFsContext* vn_mount(const char* db_path) { return NULL; } f->i_no = VN_ROOT_INO; + vn_add_inode_ref(&ctx->root_inode); f->inode = &ctx->root_inode; f->file_name = "/"; ctx->root_file.reset(f); @@ -177,14 +179,14 @@ int vn_umount(ViveFsContext* ctx) cfs.push_back(ctx->default_cf); //TODO: the following Get may cause segfault. reason still unknown - PinnableSlice v; - Status s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v); - if (!s.ok()) { - S5LOG_ERROR("Failed GET key:%s, %s", INODE_SEED_KEY, s.ToString().c_str()); - } - else { - fprintf(stderr, "Succeed get key:%s\n", INODE_SEED_KEY); - } + //PinnableSlice v; + //Status s = ctx->db->Get(ctx->read_opt, ctx->meta_cf, INODE_SEED_KEY, &v); + //if (!s.ok()) { + // S5LOG_ERROR("Failed GET key:%s, %s", INODE_SEED_KEY, s.ToString().c_str()); + //} + //else { + // fprintf(stderr, "Succeed get key:%s\n", INODE_SEED_KEY); + //} ctx->db->Flush(FlushOptions(), cfs); ctx->db->SyncWAL(); CancelAllBackgroundWork(ctx->db, true); diff --git a/testing/vntest_1.sh b/testing/vntest_1.sh index df2805b..7c7e34f 100644 --- a/testing/vntest_1.sh +++ b/testing/vntest_1.sh @@ -43,7 +43,7 @@ assert_equal $? 0 info "Now umount and shutdown" assert umount $MNT_DIR -kill -s 2 $NFSD_PID +kill $NFSD_PID assert wait $NFSD_PID info "Restart nfsd" @@ -66,7 +66,7 @@ assert_equal "$str2" "$(cat $MNT_DIR/dd/f2.txt)" info "Now umount and shutdown" assert umount $MNT_DIR -kill -s 2 $NFSD_PID +kill $NFSD_PID assert wait $NFSD_PID info "Test OK" \ No newline at end of file