Skip to content

Commit 2ca5bf1

Browse files
authored
chore: pass max_squash_size for MultiCommandSquasher via option (#4960)
No functionality was changed. Also fix a build error on macos. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
1 parent a42a17f commit 2ca5bf1

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/server/main_service.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ size_t Service::DispatchManyCommands(absl::Span<CmdArgList> args_list, SinkReply
14311431
dfly_cntx->transaction = dist_trans.get();
14321432
size_t squashed_num = MultiCommandSquasher::Execute(absl::MakeSpan(stored_cmds),
14331433
static_cast<RedisReplyBuilder*>(builder),
1434-
dfly_cntx, this, true, false);
1434+
dfly_cntx, this, {.verify_commands = true});
14351435
dfly_cntx->transaction = nullptr;
14361436

14371437
dispatched += stored_cmds.size();
@@ -1732,7 +1732,10 @@ optional<CapturingReplyBuilder::Payload> Service::FlushEvalAsyncCmds(ConnectionC
17321732
tx->MultiSwitchCmd(eval_cid);
17331733

17341734
CapturingReplyBuilder crb{ReplyMode::ONLY_ERR};
1735-
MultiCommandSquasher::Execute(absl::MakeSpan(info->async_cmds), &crb, cntx, this, true, true);
1735+
MultiCommandSquasher::Opts opts;
1736+
opts.verify_commands = true;
1737+
opts.error_abort = true;
1738+
MultiCommandSquasher::Execute(absl::MakeSpan(info->async_cmds), &crb, cntx, this, opts);
17361739

17371740
info->async_cmds_heap_mem = 0;
17381741
info->async_cmds.clear();
@@ -2206,7 +2209,7 @@ void Service::Exec(CmdArgList args, const CommandContext& cmd_cntx) {
22062209

22072210
if (absl::GetFlag(FLAGS_multi_exec_squash) && state != ExecScriptUse::SCRIPT_RUN &&
22082211
!cntx->conn_state.tracking_info_.IsTrackingOn()) {
2209-
MultiCommandSquasher::Execute(absl::MakeSpan(exec_info.body), rb, cntx, this);
2212+
MultiCommandSquasher::Execute(absl::MakeSpan(exec_info.body), rb, cntx, this, {});
22102213
} else {
22112214
CmdArgVec arg_vec;
22122215
for (auto& scmd : exec_info.body) {

src/server/multi_command_squasher.cc

+7-14
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,8 @@ size_t Size(const facade::CapturingReplyBuilder::Payload& payload) {
6565
atomic_uint64_t MultiCommandSquasher::current_reply_size_ = 0;
6666

6767
MultiCommandSquasher::MultiCommandSquasher(absl::Span<StoredCmd> cmds, ConnectionContext* cntx,
68-
Service* service, bool verify_commands, bool error_abort)
69-
: cmds_{cmds},
70-
cntx_{cntx},
71-
service_{service},
72-
base_cid_{nullptr},
73-
verify_commands_{verify_commands},
74-
error_abort_{error_abort} {
68+
Service* service, const Opts& opts)
69+
: cmds_{cmds}, cntx_{cntx}, service_{service}, base_cid_{nullptr}, opts_{opts} {
7570
auto mode = cntx->transaction->GetMultiMode();
7671
base_cid_ = cntx->transaction->GetCId();
7772
atomic_ = mode != Transaction::NON_ATOMIC;
@@ -137,9 +132,7 @@ MultiCommandSquasher::SquashResult MultiCommandSquasher::TrySquash(StoredCmd* cm
137132

138133
num_squashed_++;
139134

140-
// Because the squashed hop is currently blocking, we cannot add more than the max channel size,
141-
// otherwise a deadlock occurs.
142-
bool need_flush = sinfo.cmds.size() >= kMaxSquashing - 1;
135+
bool need_flush = sinfo.cmds.size() >= opts_.max_squash_size;
143136
return need_flush ? SquashResult::SQUASHED_FULL : SquashResult::SQUASHED;
144137
}
145138

@@ -149,11 +142,11 @@ bool MultiCommandSquasher::ExecuteStandalone(facade::RedisReplyBuilder* rb, Stor
149142
cmd->Fill(&tmp_keylist_);
150143
auto args = absl::MakeSpan(tmp_keylist_);
151144

152-
if (verify_commands_) {
145+
if (opts_.verify_commands) {
153146
if (auto err = service_->VerifyCommandState(cmd->Cid(), args, *cntx_); err) {
154147
rb->SendError(std::move(*err));
155148
rb->ConsumeLastError();
156-
return !error_abort_;
149+
return !opts_.error_abort;
157150
}
158151
}
159152

@@ -185,7 +178,7 @@ OpStatus MultiCommandSquasher::SquashedHopCb(EngineShard* es, RespVersion resp_v
185178
auto args = absl::MakeSpan(arg_vec);
186179
cmd->Fill(args);
187180

188-
if (verify_commands_) {
181+
if (opts_.verify_commands) {
189182
// The shared context is used for state verification, the local one is only for replies
190183
if (auto err = service_->VerifyCommandState(cmd->Cid(), args, *cntx_); err) {
191184
crb.SendError(std::move(*err));
@@ -265,7 +258,7 @@ bool MultiCommandSquasher::ExecuteSquashed(facade::RedisReplyBuilder* rb) {
265258
auto& replies = sharded_[idx].replies;
266259
CHECK(!replies.empty());
267260

268-
aborted |= error_abort_ && CapturingReplyBuilder::TryExtractError(replies.back());
261+
aborted |= opts_.error_abort && CapturingReplyBuilder::TryExtractError(replies.back());
269262

270263
current_reply_size_.fetch_sub(Size(replies.back()), std::memory_order_relaxed);
271264
CapturingReplyBuilder::Apply(std::move(replies.back()), rb);

src/server/multi_command_squasher.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ namespace dfly {
2222
// contains a non-atomic multi transaction to execute squashed commands.
2323
class MultiCommandSquasher {
2424
public:
25+
struct Opts {
26+
bool verify_commands = false; // Whether commands need to be verified before execution
27+
bool error_abort = false; // Abort upon receiving error
28+
unsigned max_squash_size = 32; // How many commands to squash at once
29+
};
30+
2531
static size_t Execute(absl::Span<StoredCmd> cmds, facade::RedisReplyBuilder* rb,
26-
ConnectionContext* cntx, Service* service, bool verify_commands = false,
27-
bool error_abort = false) {
28-
return MultiCommandSquasher{cmds, cntx, service, verify_commands, error_abort}.Run(rb);
32+
ConnectionContext* cntx, Service* service, const Opts& opts) {
33+
return MultiCommandSquasher{cmds, cntx, service, opts}.Run(rb);
2934
}
3035

3136
static size_t GetRepliesMemSize() {
@@ -45,11 +50,9 @@ class MultiCommandSquasher {
4550

4651
enum class SquashResult { SQUASHED, SQUASHED_FULL, NOT_SQUASHED, ERROR };
4752

48-
static constexpr int kMaxSquashing = 32;
49-
5053
private:
5154
MultiCommandSquasher(absl::Span<StoredCmd> cmds, ConnectionContext* cntx, Service* Service,
52-
bool verify_commands, bool error_abort);
55+
const Opts& opts);
5356

5457
// Lazy initialize shard info.
5558
ShardExecInfo& PrepareShardInfo(ShardId sid);
@@ -79,8 +82,7 @@ class MultiCommandSquasher {
7982
bool atomic_; // Whether working in any of the atomic modes
8083
const CommandId* base_cid_; // underlying cid (exec or eval) for executing batch hops
8184

82-
bool verify_commands_ = false; // Whether commands need to be verified before execution
83-
bool error_abort_ = false; // Abort upon receiving error
85+
Opts opts_;
8486

8587
std::vector<ShardExecInfo> sharded_;
8688
std::vector<ShardId> order_; // reply order for squashed cmds

src/server/snapshot.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ size_t SliceSnapshot::FlushSerialized(SerializerBase::FlushState flush_state) {
343343
// we counter-balance CPU over-usage by forcing sleep.
344344
// We measure running_cycles before the preemption points, because they reset the counter.
345345
uint64_t sleep_usec = (running_cycles * 1000'000 / base::CycleClock::Frequency()) / 2;
346-
ThisFiber::SleepFor(chrono::microseconds(std::min(sleep_usec, 2000ul)));
346+
ThisFiber::SleepFor(chrono::microseconds(std::min<uint64_t>(sleep_usec, 2000ul)));
347347

348348
return serialized;
349349
}

0 commit comments

Comments
 (0)