Skip to content

Commit 307cb1d

Browse files
kostasrimadiholden
authored andcommitted
fix: loading state error type to be compatible with redis (#2629)
* add -LOADING prefix for loading errors * replace -ERR with -LOADING for loading errors
1 parent b1c2f4f commit 307cb1d

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

src/facade/error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern const char kIndexOutOfRange[];
3131
extern const char kOutOfMemory[];
3232
extern const char kInvalidNumericResult[];
3333
extern const char kClusterNotConfigured[];
34+
extern const char kLoadingErr[];
3435

3536
extern const char kSyntaxErrType[];
3637
extern const char kScriptErrType[];

src/facade/facade.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const char kIndexOutOfRange[] = "index out of range";
9191
const char kOutOfMemory[] = "Out of memory";
9292
const char kInvalidNumericResult[] = "result is not a number";
9393
const char kClusterNotConfigured[] = "Cluster is not yet configured";
94+
const char kLoadingErr[] = "-LOADING Dragonfly is loading the dataset in memory";
9495

9596
const char kSyntaxErrType[] = "syntax_error";
9697
const char kScriptErrType[] = "script_error";

src/facade/reply_builder.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,11 @@ void RedisReplyBuilder::SendError(string_view str, string_view err_type) {
308308
if (str[0] == '-') {
309309
iovec v[] = {IoVec(str), IoVec(kCRLF)};
310310
Send(v, ABSL_ARRAYSIZE(v));
311-
} else {
312-
iovec v[] = {IoVec(kErrPref), IoVec(str), IoVec(kCRLF)};
313-
Send(v, ABSL_ARRAYSIZE(v));
311+
return;
314312
}
313+
314+
iovec v[] = {IoVec(kErrPref), IoVec(str), IoVec(kCRLF)};
315+
Send(v, ABSL_ARRAYSIZE(v));
315316
}
316317

317318
void RedisReplyBuilder::SendProtocolError(std::string_view str) {

src/server/main_service.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ std::optional<ErrorReply> Service::VerifyCommandState(const CommandId* cid, CmdA
983983

984984
// Check if the command is allowed to execute under this global state
985985
bool allowed_by_state = true;
986-
switch (etl.gstate()) {
986+
const GlobalState gstate = etl.gstate();
987+
switch (gstate) {
987988
case GlobalState::LOADING:
988989
allowed_by_state = dfly_cntx.journal_emulated || (cid->opt_mask() & CO::LOADING);
989990
break;
@@ -999,8 +1000,13 @@ std::optional<ErrorReply> Service::VerifyCommandState(const CommandId* cid, CmdA
9991000

10001001
if (!allowed_by_state) {
10011002
VLOG(1) << "Command " << cid->name() << " not executed because global state is "
1002-
<< GlobalStateName(etl.gstate());
1003-
return ErrorReply{StrCat("Can not execute during ", GlobalStateName(etl.gstate()))};
1003+
<< GlobalStateName(gstate);
1004+
1005+
if (gstate == GlobalState::LOADING) {
1006+
return ErrorReply(kLoadingErr);
1007+
}
1008+
1009+
return ErrorReply{StrCat("Can not execute during ", GlobalStateName(gstate))};
10041010
}
10051011

10061012
string_view cmd_name{cid->name()};

src/server/server_family.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ void ServerFamily::ReplicaOfInternal(string_view host, string_view port_sv, Conn
21962196

21972197
// We should not execute replica of command while loading from snapshot.
21982198
if (ServerState::tlocal()->is_master && service_.GetGlobalState() == GlobalState::LOADING) {
2199-
cntx->SendError("Can not execute during LOADING");
2199+
cntx->SendError(kLoadingErr);
22002200
return;
22012201
}
22022202

tests/dragonfly/replication_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,8 +1859,8 @@ async def test_replicaof_reject_on_load(df_local_factory, df_seeder_factory):
18591859
try:
18601860
await c_replica.execute_command(f"REPLICAOF localhost {master.port}")
18611861
assert False
1862-
except aioredis.ResponseError as e:
1863-
assert "Can not execute during LOADING" in str(e)
1862+
except aioredis.BusyLoadingError as e:
1863+
assert "Dragonfly is loading the dataset in memory" in str(e)
18641864
# Check one we finish loading snapshot replicaof success
18651865
await wait_available_async(c_replica)
18661866
await c_replica.execute_command(f"REPLICAOF localhost {master.port}")

tests/dragonfly/utility.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ async def wait_available_async(client: aioredis.Redis, timeout=10):
5252
if "MOVED" in str(e):
5353
# MOVED means we *can* serve traffic, but 'key' does not belong to an owned slot
5454
return
55-
assert "Can not execute during LOADING" in str(e)
55+
except aioredis.BusyLoadingError as e:
56+
assert "Dragonfly is loading the dataset in memory" in str(e)
5657

5758
# Print W to indicate test is waiting for replica
5859
print("W", end="", flush=True)

0 commit comments

Comments
 (0)