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

chore: Forbid replicating a replica #3779

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/server/server_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,10 @@ void ServerFamily::ReplTakeOver(CmdArgList args, ConnectionContext* cntx) {
}

void ServerFamily::ReplConf(CmdArgList args, ConnectionContext* cntx) {
if (!ServerState::tlocal()->is_master) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should be checked with the replicaof_mu_ locked

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?
It's a thread-local variable, so it can't be read non-atomically.
Also, there are many places that use it, all without that lock. Furthermore, the lock is only available in ServerFamily, while other places use it as well..?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the is_master is eventually consistent. It can be that the server is currenly configured as a replica and did not get to set the is_master flag on this thread right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, updated :)

return cntx->SendError("Replicating a replica is unsupported");
}

if (args.size() % 2 == 1)
goto err;
for (unsigned i = 0; i < args.size(); i += 2) {
Expand Down
22 changes: 22 additions & 0 deletions tests/dragonfly/replication_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2663,3 +2663,25 @@ async def check_master_status():
assert await seeder.compare(capture, port=master.port)

await disconnect_clients(c_master, c_replica)


@pytest.mark.asyncio
async def test_replica_of_replica(df_factory):
# Can't connect a replica to a replica, but OK to connect 2 replicas to the same master
master = df_factory.create(proactor_threads=2)
replica = df_factory.create(proactor_threads=2)
replica2 = df_factory.create(proactor_threads=2)

df_factory.start_all([master, replica, replica2])

c_replica = replica.client()
c_replica2 = replica2.client()

assert await c_replica.execute_command(f"REPLICAOF localhost {master.port}") == "OK"

with pytest.raises(redis.exceptions.ResponseError):
await c_replica2.execute_command(f"REPLICAOF localhost {replica.port}")

assert await c_replica2.execute_command(f"REPLICAOF localhost {master.port}") == "OK"

await disconnect_clients(c_replica, c_replica2)
Loading