Skip to content

Commit d7a7591

Browse files
authored
chore: clean ups around command squasher (#5011)
1. Eliminate replies reverse call - will allow to unite replies and cmd vectors into one. 2. Introduce SwitchTxCmd function to avoid code duplication. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
1 parent d373187 commit d7a7591

File tree

6 files changed

+23
-20
lines changed

6 files changed

+23
-20
lines changed

src/server/conn_context.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ void ConnectionContext::ChangeMonitor(bool start) {
157157
EnableMonitoring(start);
158158
}
159159

160+
void ConnectionContext::SwitchTxCmd(const CommandId* cid) {
161+
transaction->MultiSwitchCmd(cid);
162+
this->cid = cid;
163+
}
164+
160165
void ConnectionContext::ChangeSubscription(bool to_add, bool to_reply, CmdArgList args,
161166
facade::RedisReplyBuilder* rb) {
162167
vector<unsigned> result = ChangeSubscriptions(args, false, to_add, to_reply);

src/server/conn_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class ConnectionContext : public facade::ConnectionContext {
301301
void UnsubscribeAll(bool to_reply, facade::RedisReplyBuilder* rb);
302302
void PUnsubscribeAll(bool to_reply, facade::RedisReplyBuilder* rb);
303303
void ChangeMonitor(bool start); // either start or stop monitor on a given connection
304+
void SwitchTxCmd(const CommandId* cid);
304305

305306
size_t UsedMemory() const override;
306307

src/server/debugcmd.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,9 +1309,7 @@ void DebugCmd::DoPopulateBatch(const PopulateOptions& options, const PopulateBat
13091309
args_view.push_back(arg);
13101310
}
13111311
auto args_span = absl::MakeSpan(args_view);
1312-
1313-
stub_tx->MultiSwitchCmd(cid);
1314-
local_cntx.cid = cid;
1312+
local_cntx.SwitchTxCmd(cid);
13151313
crb.SetReplyMode(ReplyMode::NONE);
13161314
stub_tx->InitByArgs(cntx_->ns, local_cntx.conn_state.db_index, args_span);
13171315

@@ -1332,8 +1330,7 @@ void DebugCmd::DoPopulateBatch(const PopulateOptions& options, const PopulateBat
13321330
args_view.push_back(arg);
13331331
}
13341332
auto args_span = absl::MakeSpan(args_view);
1335-
stub_tx->MultiSwitchCmd(cid);
1336-
local_cntx.cid = cid;
1333+
local_cntx.SwitchTxCmd(cid);
13371334
crb.SetReplyMode(ReplyMode::NONE);
13381335
stub_tx->InitByArgs(cntx_->ns, local_cntx.conn_state.db_index, args_span);
13391336
sf_.service().InvokeCmd(cid, args_span, &crb, &local_cntx);

src/server/main_service.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,7 @@ void Service::Exec(CmdArgList args, const CommandContext& cmd_cntx) {
22202220
for (auto& scmd : exec_info.body) {
22212221
VLOG(2) << "TX CMD " << scmd.Cid()->name() << " " << scmd.NumArgs();
22222222

2223-
cmd_cntx.tx->MultiSwitchCmd(scmd.Cid());
2224-
cntx->cid = scmd.Cid();
2223+
cntx->SwitchTxCmd(scmd.Cid());
22252224

22262225
arg_vec.resize(scmd.NumArgs());
22272226
scmd.Fill(&arg_vec);

src/server/multi_command_squasher.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ bool MultiCommandSquasher::ExecuteStandalone(facade::RedisReplyBuilder* rb, Stor
151151
}
152152

153153
auto* tx = cntx_->transaction;
154-
tx->MultiSwitchCmd(cmd->Cid());
155-
cntx_->cid = cmd->Cid();
154+
cntx_->SwitchTxCmd(cmd->Cid());
156155

157156
if (cmd->Cid()->IsTransactional())
158157
tx->InitByArgs(cntx_->ns, cntx_->conn_state.db_index, args);
@@ -189,8 +188,7 @@ OpStatus MultiCommandSquasher::SquashedHopCb(EngineShard* es, RespVersion resp_v
189188
}
190189
}
191190

192-
local_tx->MultiSwitchCmd(cmd->Cid());
193-
local_cntx.cid = cmd->Cid();
191+
local_cntx.SwitchTxCmd(cmd->Cid());
194192
crb.SetReplyMode(cmd->ReplyMode());
195193

196194
local_tx->InitByArgs(cntx_->ns, local_cntx.conn_state.db_index, args);
@@ -205,7 +203,6 @@ OpStatus MultiCommandSquasher::SquashedHopCb(EngineShard* es, RespVersion resp_v
205203
CheckConnStateClean(local_state);
206204
}
207205

208-
reverse(sinfo.replies.begin(), sinfo.replies.end());
209206
return OpStatus::OK;
210207
}
211208

@@ -255,24 +252,27 @@ bool MultiCommandSquasher::ExecuteSquashed(facade::RedisReplyBuilder* rb) {
255252
bool aborted = false;
256253

257254
for (auto idx : order_) {
258-
auto& replies = sharded_[idx].replies;
259-
CHECK(!replies.empty());
255+
auto& sinfo = sharded_[idx];
256+
auto& replies = sinfo.replies;
257+
DCHECK_LT(sinfo.reply_id, replies.size());
260258

261-
aborted |= opts_.error_abort && CapturingReplyBuilder::TryExtractError(replies.back());
262-
263-
current_reply_size_.fetch_sub(Size(replies.back()), std::memory_order_relaxed);
264-
CapturingReplyBuilder::Apply(std::move(replies.back()), rb);
265-
replies.pop_back();
259+
auto& reply = replies[sinfo.reply_id++];
260+
aborted |= opts_.error_abort && CapturingReplyBuilder::TryExtractError(reply);
266261

262+
current_reply_size_.fetch_sub(Size(reply), std::memory_order_relaxed);
263+
CapturingReplyBuilder::Apply(std::move(reply), rb);
267264
if (aborted)
268265
break;
269266
}
270267
uint64_t after_reply = proactor->GetMonotonicTimeNs();
271268
ServerState::SafeTLocal()->stats.multi_squash_exec_hop_usec += (after_hop - start) / 1000;
272269
ServerState::SafeTLocal()->stats.multi_squash_exec_reply_usec += (after_reply - after_hop) / 1000;
273270

274-
for (auto& sinfo : sharded_)
271+
for (auto& sinfo : sharded_) {
275272
sinfo.cmds.clear();
273+
sinfo.replies.clear();
274+
sinfo.reply_id = 0;
275+
}
276276

277277
order_.clear();
278278
return !aborted;

src/server/multi_command_squasher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class MultiCommandSquasher {
4545

4646
std::vector<StoredCmd*> cmds; // accumulated commands
4747
std::vector<facade::CapturingReplyBuilder::Payload> replies;
48+
unsigned reply_id = 0;
4849
boost::intrusive_ptr<Transaction> local_tx; // stub-mode tx for use inside shard
4950
};
5051

0 commit comments

Comments
 (0)