Skip to content
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

fix handle renametx retry failed with same sequence number #2677

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion curvefs/src/metaserver/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "curvefs/src/metaserver/dentry_storage.h"
#include "curvefs/src/metaserver/transaction.h"
#include "src/common/concurrent/name_lock.h"

namespace curvefs {
namespace metaserver {
Expand Down Expand Up @@ -112,12 +113,16 @@ MetaStatusCode TxManager::HandleRenameTx(const std::vector<Dentry>& dentrys) {
return rc;
}

// deal with retry request with same tx sequence
curve::common::NameLockGuard lg(seqNameLock_,
std::to_string(dentrys[0].txsequence()));

// Handle pending TX
RenameTx pendingTx;
if (FindPendingTx(&pendingTx)) {
auto txId = dentrys[0].txid();
auto txSequence = dentrys[0].txsequence();
if (txSequence != 0 && txSequence <= pendingTx.GetTxSequence()) {
if (txSequence != 0 && txSequence < pendingTx.GetTxSequence()) {
LOG(ERROR) << "HandlePendingTx failed, current transaction is stale"
<< ", we will discard it. current tx sequence = "
<< txSequence << ", pending tx sequence = "
Expand Down
3 changes: 3 additions & 0 deletions curvefs/src/metaserver/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <memory>
#include <unordered_map>

#include "src/common/concurrent/name_lock.h"
#include "src/common/concurrent/rw_lock.h"
#include "curvefs/src/metaserver/dentry_storage.h"

Expand Down Expand Up @@ -89,6 +90,8 @@ class TxManager {
std::shared_ptr<DentryStorage> storage_;

RenameTx EMPTY_TX, pendingTx_;

curve::common::NameLock seqNameLock_;
};

} // namespace metaserver
Expand Down
27 changes: 26 additions & 1 deletion curvefs/test/metaserver/transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>

#include "curvefs/src/metaserver/dentry_manager.h"
#include "curvefs/src/metaserver/storage/storage.h"
Expand Down Expand Up @@ -89,14 +90,16 @@ class TransactionTest : public ::testing::Test {
const std::string& name,
uint64_t txId,
uint64_t inodeId,
uint32_t flag) {
uint32_t flag,
uint64_t txSequence = 0) {
Dentry dentry;
dentry.set_fsid(fsId);
dentry.set_parentinodeid(parentId);
dentry.set_name(name);
dentry.set_txid(txId);
dentry.set_inodeid(inodeId);
dentry.set_flag(flag);
dentry.set_txsequence(txSequence);
return dentry;
}

Expand Down Expand Up @@ -347,5 +350,27 @@ TEST_F(TransactionTest, HandleTxWithTargetExist) {
ASSERT_EQ(dentryStorage_->Size(), 3); // /B /B/A /C(pending)
}

TEST_F(TransactionTest, HandleTxWithSameSequence) {
InsertDentrys(dentryStorage_, std::vector<Dentry>{
// { fsId, parentId, name, txId, inodeId, flag }
GenDentry(1, 0, "A", 0, 1, 0),
});

// step-1: prepare tx success (rename A B)
auto dentrys = std::vector<Dentry> {
// { fsId, parentId, name, txId, inodeId, flag }
GenDentry(1, 0, "A", 1, 1, DELETE_FLAG, 1),
GenDentry(1, 0, "B", 1, 1, 0, 1),
};
MetaStatusCode rc1, rc2;
std::thread t1([&]() {rc1 = txManager_->HandleRenameTx(dentrys);});
std::thread t2([&]() {rc2 = txManager_->HandleRenameTx(dentrys);});

t1.join();
t2.join();
ASSERT_EQ(rc1, MetaStatusCode::OK);
ASSERT_EQ(rc2, MetaStatusCode::OK);
}

} // namespace metaserver
} // namespace curvefs