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

Port the pantsd nailgun server to rust #9722

Merged
merged 6 commits into from
May 13, 2020

Conversation

stuhood
Copy link
Member

@stuhood stuhood commented May 8, 2020

Problem

The setup and teardown of each request made to the nailgun server in pantsd had become quite complicated over time... and consequently, slower than it needed to be.

Solution

Port pantsd's nailgun server to rust using the nails crate. Additionally, remove the Exiter class, which had accumulated excess responsibilities that can instead be handled by returning ExitCode values. Finally, fix a few broken windows including: double logging to pantsd, double help output, closed file errors on pantsd shutdown, and redundant setup codepaths.

Result

There is less code to maintain, and runs of ./pants --enable-pantsd help take ~1.7s, of which ~400ms are spent in the server. Fixes #9448, fixes #8243, fixes #8206, fixes #8127, fixes #7653, fixes #7613, fixes #7597.

@stuhood stuhood force-pushed the stuhood/pantsd-server-in-rust branch 2 times, most recently from 3e7ad64 to 48dc7bf Compare May 10, 2020 03:18
@stuhood stuhood marked this pull request as ready for review May 10, 2020 03:35
@stuhood stuhood force-pushed the stuhood/pantsd-server-in-rust branch from 48dc7bf to c69395e Compare May 10, 2020 03:46
@stuhood
Copy link
Member Author

stuhood commented May 10, 2020

The commits are useful to review independently, although the last one is a doozy... sorry about that.

@stuhood stuhood force-pushed the stuhood/pantsd-server-in-rust branch 2 times, most recently from 0970711 to 89b159e Compare May 12, 2020 04:07
@stuhood
Copy link
Member Author

stuhood commented May 12, 2020

Ok, expecting this to pass CI now. I went a bit further and fully removed the exiter function... sys.excepthook doesn't need the hook to actually exit the process itself: it already handles exiting.

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Awesome. Great work.

src/python/pants/base/exception_sink.py Outdated Show resolved Hide resolved
class _PantsRunFinishedWithFailureException(Exception):
"""Allows representing a pants run that failed for legitimate reasons (e.g. the target failed to
compile).
def __init__(self, scheduler_service: SchedulerService):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def __init__(self, scheduler_service: SchedulerService):
def __init__(self, scheduler_service: SchedulerService) -> None:

Copy link
Contributor

Choose a reason for hiding this comment

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

In general we should be trying to add useful type annotations to old code when we modify it.

Will be raised by the exiter passed to LocalPantsRunner.
"""
@staticmethod
def _send_stderr(stderr_fd: int, msg: str):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _send_stderr(stderr_fd: int, msg: str):
def _send_stderr(stderr_fd: int, msg: str) -> None:

), hermetic_environment_as(**env), argv_as((command,) + args):
# NB: Run implements exception handling, so only the most primitive errors will escape
# this function, where they will be logged to the pantsd.log by the server.
logger.info(f"handling request: `{' '.join(args)}`")
Copy link
Contributor

Choose a reason for hiding this comment

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

Why logger.info? That will show up by default.

Copy link
Member Author

Choose a reason for hiding this comment

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

It will only go to pantsd's log.

src/python/pants/bin/daemon_pants_runner.py Show resolved Hide resolved
termios.tcdrain(dst_fd)
else:
new_dst.flush()
except: # noqa: E722
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
except: # noqa: E722
except BaseException:

@@ -84,6 +85,17 @@ def hermetic_environment_as(**kwargs: Optional[str]) -> Iterator[None]:
_restore_env(old_environment)


@contextmanager
def argv_as(args: Tuple[str, ...]) -> Iterator[None]:
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably have a unit test.

"""
return cast(int, self._native.nailgun_server_await_bound(self._scheduler, nailgun_server))

def new_nailgun_server(self, port_requested: int, runner: RawFdRunner) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this Any because it truly could be Any, or because you don't know the more precise type? If the latter, drop the return type.

Copy link
Member Author

@stuhood stuhood May 12, 2020

Choose a reason for hiding this comment

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

We don't know the precise type because it is coming out of CFFI. Will drop.

result = self.lib.nailgun_server_await_bound(scheduler, nailgun_server)
return cast(int, self.context.raise_or_return(result))

def new_nailgun_server(self, scheduler, port: int, runner: RawFdRunner) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

See comment in scheduler.py on the Any return type.

@gshuflin
Copy link
Contributor

Excited to see a lot of the cruft related to Exiters go away :)

Stu Hood added 6 commits May 12, 2020 20:27
…restart.

# Delete this line to force CI to run Clippy and the Rust tests.
[ci skip-rust-tests]
# Delete this line to force CI to run the JVM tests.
[ci skip-jvm-tests]
[ci skip-jvm-tests]
…process (and only called for a signal in pantsd).

[ci skip-jvm-tests]
…hook` doesn't need us to actually exit.

[ci skip-jvm-tests]
# Delete this line to force CI to run the JVM tests.
[ci skip-jvm-tests]
@stuhood stuhood force-pushed the stuhood/pantsd-server-in-rust branch from 7dde6d3 to 2e3b8fc Compare May 13, 2020 03:39
@stuhood stuhood merged commit 9506fbf into pantsbuild:master May 13, 2020
@stuhood stuhood deleted the stuhood/pantsd-server-in-rust branch May 13, 2020 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants