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

feat(worker): Allow passing a socket FD to dup and listen on #2598

Merged
merged 7 commits into from
Oct 18, 2024

Conversation

nathanlo-hrt
Copy link
Contributor

When spawning Kvrocks as a part of a script, it's sometimes helpful to first reserve a port with a socket, and pass that socket to Kvrocks as a child process.

This change implements this change:

  1. Adds a config item socket-fd which defaults to -1 (no fd specified).
  2. If socket-fd is provided, will skip checking that the supplied port is available (since it will already be grabbed by the provided fd). Instead, it will check that the supplied fd is indeed a socket associated to the provided port.
  3. If socket-fd is provided, Worker's will dup the supplied fd and listen to that instead of trying to make their own socket from scratch.
  4. Close socket-fd on shut-down.

As a bonus, removed some unneeded indirect includes and cleaned up some surrounding code.

@nathanlo-hrt nathanlo-hrt marked this pull request as ready for review October 14, 2024 15:19
@PragmaTwice
Copy link
Member

PragmaTwice commented Oct 14, 2024

The implementation looks good.

But could you elabrate more about the use case? i.e. in which cases it's better than connecting to tcp/unix sockets? Maybe you can attach a shell script or something to illustrate it.

@nathanlo-hrt
Copy link
Contributor Author

Currently, the Kvrocks port listening implementation has a race condition: since Kvrocks checks whether the port is in use before actually grabbing exclusive use of the port, it's possible to spawn two Kvrocks servers listening on the same ports.

The minimal repro of this is to run build/kvrocks --port 8888 & build/kvrocks --port 8888 a few times: eventually both will succeed and you'll be able to issue commands to port 8888 and cause undefined behavior.

As such, I have a script like below which reserves a port, and passes the socket's FD to spawn Kvrocks:

import socket

s = socket.create_server(...)  # Grab a port which is guaranteed to be free
port = s.getsockname()[1]
socket_fd = s.fileno()
subprocess.run(f"./kvrocks --port {port} --socket-fd {socket_fd}")

# Now, Kvrocks can safely duplicate and use the FD `socket_fd` without fear of other Kvrocks instances trampling it

Copy link

sonarcloud bot commented Oct 17, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
29.5% Coverage on New Code (required ≥ 50%)

See analysis details on SonarCloud

@git-hulk git-hulk merged commit 576c431 into apache:unstable Oct 18, 2024
31 of 32 checks passed
@nathanlo-hrt nathanlo-hrt deleted the socket-fd branch October 18, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants