Skip to content

Commit c3650d0

Browse files
arv1ndnkevin-bates
andauthored
Add hook to modify zmq.Context (#1154)
* Add validation hook for zmq.Context * expose tunable ZMQ_MAX_SOCKETS * expose tunable IO_THREADS * Check current value of ZMQ tunables before update * Use context instantiation hook over validator * Add envs to docs, eliminate horizontal scrollbars on env info Co-authored-by: Kevin Bates <[email protected]>
1 parent 71504b3 commit c3650d0

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

docs/source/operators/config-add-env.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ Besides those environment variables associated with configurable options, the fo
6868
Kubernetes only. Used during Kubernetes deployment, this indicates the name of
6969
the namespace in which the Enterprise Gateway service is deployed. The
7070
namespace is created prior to deployment, and is set into the EG_NAMESPACE env via
71-
deployment.yaml script. This value is then used within Enterprise Gateway to coordinate kernel
72-
configurations. Should this value not be set during deployment, Enterprise Gateway
73-
will default its value to namespace 'default'.
71+
deployment.yaml script. This value is then used within Enterprise Gateway to coordinate
72+
kernel configurations. Should this value not be set during deployment, Enterprise
73+
Gateway will default its value to namespace 'default'.
7474
7575
EG_PROHIBITED_GIDS=0
7676
Containers only. A comma-separated list of group ids (GID) whose values are not
@@ -131,5 +131,16 @@ Besides those environment variables associated with configurable options, the fo
131131
Any other value will error.
132132
133133
EG_YARN_CERT_BUNDLE=<custom_truststore_path>
134-
The path to a .pem or any other custom truststore used as a CA bundle in yarn-api-client.
134+
The path to a .pem or any other custom truststore used as a CA bundle in
135+
yarn-api-client.
136+
137+
EG_ZMQ_IO_THREADS=1
138+
The size of the ZMQ thread pool used to handle I/O operations. Applies only to shared
139+
contexts which are enabled by default but can be specified via
140+
`RemoteMappingKernelManager.shared_context = True`.
141+
142+
EG_ZMQ_MAX_SOCKETS=1023
143+
Specifies the maximum number of sockets to allow on the ZMQ context. Applies only to
144+
shared contexts which are enabled by default but can be specified via
145+
`RemoteMappingKernelManager.shared_context = True`.
135146
```

docs/source/operators/config-env-debug.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ The following environment variables may be useful for troubleshooting:
2828
The interval (in seconds) to wait before checking poll results again.
2929
3030
EG_RESTART_STATUS_POLL_INTERVAL=1.0
31-
The interval (in seconds) to wait before polling for the restart status again when duplicate restart request
32-
for the same kernel is received or when a shutdown request is received while kernel is still restarting.
31+
The interval (in seconds) to wait before polling for the restart status again when
32+
duplicate restart request for the same kernel is received or when a shutdown request
33+
is received while kernel is still restarting.
3334
3435
EG_REMOVE_CONTAINER=True
3536
Used by launch_docker.py, indicates whether the kernel's docker container should be

enterprise_gateway/services/kernels/remotemanager.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from tornado import web
1919
from traitlets import directional_link
2020
from traitlets import log as traitlets_log
21+
from zmq import IO_THREADS, MAX_SOCKETS, Context
2122

2223
from enterprise_gateway.mixins import EnterpriseGatewayConfigMixin
2324

@@ -159,6 +160,28 @@ class RemoteMappingKernelManager(AsyncMappingKernelManager):
159160
Extends the AsyncMappingKernelManager with support for managing remote kernels via the process-proxy.
160161
"""
161162

163+
def _context_default(self) -> Context:
164+
"""
165+
We override the _context_default method in
166+
"""
167+
zmq_context = super()._context_default()
168+
if self.shared_context: # this should be True by default
169+
170+
# pyzmq currently does not expose defaults for these values, so we replicate them here
171+
# libzmq/zmq.h: ZMQ_MAX_SOCKETS_DLFT = 1023; zmq.Context.MAX_SOCKETS
172+
# libzmq/zmq.h: ZMQ_IO_THREADS_DFLT = 1; zmq.Context.IO_THREADS
173+
zmq_max_sock_desired = int(os.getenv("EG_ZMQ_MAX_SOCKETS", zmq_context.MAX_SOCKETS))
174+
if zmq_max_sock_desired != zmq_context.MAX_SOCKETS:
175+
zmq_context.set(MAX_SOCKETS, zmq_max_sock_desired)
176+
self.log.info(f"Set ZMQ_MAX_SOCKETS to {zmq_context.MAX_SOCKETS}")
177+
178+
zmq_io_threads_desired = int(os.getenv("EG_ZMQ_IO_THREADS", zmq_context.IO_THREADS))
179+
if zmq_io_threads_desired != zmq_context.IO_THREADS:
180+
zmq_context.set(IO_THREADS, zmq_io_threads_desired)
181+
self.log.info(f"Set ZMQ_IO_THREADS to {zmq_context.IO_THREADS}")
182+
183+
return zmq_context
184+
162185
pending_requests: TrackPendingRequests = (
163186
TrackPendingRequests()
164187
) # Used to enforce max-kernel limits
@@ -256,9 +279,10 @@ def _enforce_kernel_limits(self, username: str) -> None:
256279
"""
257280

258281
if self.parent.max_kernels is not None or self.parent.max_kernels_per_user >= 0:
259-
pending_all, pending_user = RemoteMappingKernelManager.pending_requests.get_counts(
260-
username
261-
)
282+
(
283+
pending_all,
284+
pending_user,
285+
) = RemoteMappingKernelManager.pending_requests.get_counts(username)
262286

263287
# Enforce overall limit...
264288
if self.parent.max_kernels is not None:

0 commit comments

Comments
 (0)