Skip to content

Commit

Permalink
fix(server): Fix the cleanup issue with lua scripts.
Browse files Browse the repository at this point in the history
Fixes #713.

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
  • Loading branch information
romange committed Jan 24, 2023
1 parent b0741b4 commit 2069280
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/server/dragonfly_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const char kKey2[] = "b";
const char kKey3[] = "c";
const char kKey4[] = "y";

const char kKeySid0[] = "x";
const char kKeySid1[] = "c";
const char kKeySid2[] = "b";
const char kKey2Sid0[] = "y";

} // namespace

// This test is responsible for server and main service
Expand Down Expand Up @@ -88,6 +93,15 @@ TEST_F(DflyEngineTest, Sds) {
}

TEST_F(DflyEngineTest, MultiAndEval) {
ShardId sid1 = Shard(kKey1, num_threads_ - 1);
ShardId sid2 = Shard(kKey2, num_threads_ - 1);
ShardId sid3 = Shard(kKey3, num_threads_ - 1);
ShardId sid4 = Shard(kKey4, num_threads_ - 1);
EXPECT_EQ(0, sid1);
EXPECT_EQ(2, sid2);
EXPECT_EQ(1, sid3);
EXPECT_EQ(0, sid4);

RespExpr resp = Run({"multi"});
ASSERT_EQ(resp, "OK");

Expand Down Expand Up @@ -412,6 +426,32 @@ return {offset, epoch}
EXPECT_THAT(resp.GetVec(), ElementsAre(IntArg(1), "6"));
}

// Scenario: 1. a lua call A schedules itself on shards 0, 1, 2.
// 2. another lua call B schedules itself on shards 1,2 but on shard 1 (or 2) it
// schedules itself before A.
// the order of scheduling: shard 0: A, shard 1: B, A. shard 2: B, A.
// 3. A is executes its first command first, which coincendently runs only on shard 0,
// hence A finishes before B and then it tries to cleanup.
// 4. There was an incorrect cleanup of multi-transactions that breaks for shard 1 (or 2)
// because it assume the A is at front of the queue.
TEST_F(DflyEngineTest, EvalBug713) {
const char* script = "return redis.call('get', KEYS[1])";

// A
auto fb0 = pp_->at(1)->LaunchFiber([&] {
fibers_ext::Yield();
for (unsigned i = 0; i < 50; ++i) {
Run({"eval", script, "3", kKeySid0, kKeySid1, kKeySid2});
}
});

// B
for (unsigned j = 0; j < 50; ++j) {
Run({"eval", script, "2", kKeySid1, kKeySid2});
}
fb0.Join();
}

TEST_F(DflyEngineTest, EvalSha) {
auto resp = Run({"script", "load", "return 5"});
EXPECT_THAT(resp, ArgType(RespExpr::STRING));
Expand Down
2 changes: 1 addition & 1 deletion src/server/transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ void Transaction::UnlockMultiShardCb(const std::vector<KeyList>& sharded_keys, E
TxQueue* txq = shard->txq();
DCHECK(!txq->Empty());
Transaction* trans = absl::get<Transaction*>(txq->Front());
DCHECK(trans == this);
DCHECK(trans == this) << "Front: " << trans->DebugId();
txq->PopFront();
sd.pq_pos = TxQueue::kEnd;
}
Expand Down

0 comments on commit 2069280

Please sign in to comment.