You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "traeak (via GitHub)" <gi...@apache.org> on 2026/10/06 20:14:47 UTC
[PR] Shorten the session pool lock for origin connections [trafficserver]
traeak opened a new pull request, #13806:
URL: https://github.com/apache/trafficserver/pull/13806
The shared session pool mutex was held across migrating a pooled origin connection to another thread and across closing sessions, which put socket and TLS work inside a lock every thread contends for. The lock now covers only the pool tables and handing a connection off:
- Migration is split into a detach under the lock and an attach after it is released.
- Sessions that cannot be pooled, time out, or are closed by the origin are closed after the lock is released.
- A pool purge has each thread close its own sessions.
This fixes three race conditions in the migration of a pooled origin connection, all present before this change:
- The migrating thread stopped the old connection's EventIO while the owning thread could still be rescheduling that connection, having failed the try lock on its VIO mutex. The owning thread then used the stopped EventIO, which hit ink_assert(event_loop) in debug builds and dereferenced a null event loop with kqueue. EventIO::modify and EventIO::refresh now do nothing once stopped.
- With kqueue, stopping an EventIO did not remove its registration, and a migrated socket is never closed. The old thread's kqueue could keep delivering events addressed to a connection that thread had since freed. Migration now removes the registration, and a refresh racing with the removal undoes its own add.
- When creating the new connection failed, the old one was closed a second time, after the owning thread could already have freed or reused it. The session no longer refers to the old connection once it has been detached.
The pooled_server_connections gauge also no longer drifts after a purge.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#discussion_r4276710019
##########
src/iocore/net/EventIO.cc:
##########
@@ -102,10 +140,11 @@ EventIO::modify(int e)
EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_ADD | INK_EV_EDGE_TRIGGER, 0, 0, this);
}
events = ee;
- if (n)
- return kevent(event_loop->kqueue_fd, &ev[0], n, nullptr, 0, nullptr);
- else
+ if (n) {
+ return kevent(loop->kqueue_fd, &ev[0], n, nullptr, 0, nullptr);
Review Comment:
`refresh()` re-checks `event_loop` after its `kevent` add and undoes the add if a migration raced it. This level-triggered kqueue `modify()` path has the same load/add window but no undo. It's probably not built anywhere in practice, since kqueue is edge-triggered by default. Still, it might be worth either matching the `refresh()` handling or adding a comment that this path is not race-safe against `stop_for_migration()`.
##########
src/iocore/net/unit_tests/test_EventIOMigration.cc:
##########
@@ -0,0 +1,87 @@
+/** @file
+
+ Tests for kqueue registration teardown when a socket is migrated.
+
+ @section license License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#include "../P_UnixPollDescriptor.h"
+#include "iocore/net/EventIO.h"
+#include "iocore/net/ReadWriteEventIO.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cerrno>
+#include <unistd.h>
+
+#if TS_USE_KQUEUE
Review Comment:
These tests are all `TS_USE_KQUEUE`, so on Linux/epoll CI this file compiles to nothing. The session pool restructuring itself has no coverage either: the async purge, the deferred close of shared pool sessions, and the detach/attach migration. Could you add an AuTest that runs with `proxy.config.http.server_session_sharing.pool: global` (and `hybrid`) and exercises cross-thread reuse? One that also sets `proxy.config.http.server_max_connections` low enough to trigger `purge_keepalives()` would cover the new purge path, and it could check that `proxy.process.http.pooled_server_connections` returns to 0 afterward, which is the gauge-drift fix. If it fits, `AtsReplayTest` with a replay file would keep it short. Otherwise the `Test` class style in `ats_probe.test.py` works well.
##########
src/proxy/http/HttpSessionManager.cc:
##########
@@ -285,6 +313,17 @@ ServerSessionPool::eventHandler(int event, void *data)
PoolableSession *s = nullptr;
switch (event) {
+ case EVENT_IMMEDIATE:
+ case EVENT_INTERVAL:
+ if (m_purge_requested.exchange(false) && !httpSessionManager.purge_thread_keepalives()) {
Review Comment:
Small thing: the deferred-close event and the purge event both come through this case, so a deferred-close event can end up doing a pending purge early, and the purge's own event then does nothing. That's harmless, but a short comment saying both producers share this handler on purpose would help the next reader.
##########
src/iocore/net/UnixNetVConnection.cc:
##########
@@ -1311,43 +1311,55 @@ UnixNetVConnection::is_default_inactivity_timeout()
/*
* Close down the current netVC. Save aside the socket and SSL information
- * and create new netVC in the current thread/netVC
+ * so a new netVC can be created from them in another thread.
*/
-UnixNetVConnection *
-UnixNetVConnection::migrateToCurrentThread(Continuation *cont, EThread *t)
+void
+UnixNetVConnection::detachForMigration(MigrationState &ms)
{
- NetHandler *client_nh = get_NetHandler(t);
- ink_assert(client_nh);
- if (this->nh == client_nh) {
- // We're already there!
- return this;
- }
-
- Connection hold_con;
- hold_con.move(this->con);
-
- void *arg = this->_prepareForMigration();
+ ms.con.move(this->con);
+ ms.arg = this->_prepareForMigration();
+ ms.options = this->options;
+ ms.context = this->get_context();
+ ms.inactivity_timeout_in = this->inactivity_timeout_in;
+ ms.processor = this->_getNetProcessor();
// Do_io_close will signal the VC to be freed on the original thread
// Since we moved the con context, the fd will not be closed
- // Go ahead and remove the fd from the original thread's epoll structure, so it is not
+ // Go ahead and remove the fd from the original thread's poller, so it is not
// processed on two threads simultaneously
- this->ep.stop();
+ //
+ // This stop is not synchronized with the original thread. That thread may have already seen
+ // this VC as open and enabled in NetHandler::process_ready_list, in which case it goes on to
+ // net_read_io, fails the try lock on the VIO mutex held by the caller, and reschedules the VC.
+ // The reschedule calls ep.refresh on the EventIO stopped here, so EventIO::modify and
+ // EventIO::refresh do nothing on a stopped EventIO. A delay before the try lock in net_read_io
+ // makes the original thread hit this reliably.
+ //
+ // Unlike an ordinary close, migration keeps the fd open, so kqueue registrations must
+ // also be removed here before the old VC can be freed.
+ ink_release_assert(this->ep.stop_for_migration() == 0);
Review Comment:
Before this change the return value of `ep.stop()` was ignored. Now a failed `epoll_ctl(EPOLL_CTL_DEL)` or `kevent(EV_DELETE)` aborts the process. On epoll, `stop()` can return -1/ENOENT when `event_loop` is set but the fd was already removed from the set. That can happen in a level-triggered build, where `modify()` issues `EPOLL_CTL_DEL` once the events reach zero. Would logging and treating this as a migration failure be enough here, maybe `Error()` plus `origin_shutdown_migration_failure`? A failed delete on a pooled origin connection doesn't seem worth taking down the host for. If you want to keep the assert, an `ink_assert` for debug builds plus a runtime fallback would still catch it in CI.
##########
src/proxy/http/HttpSessionManager.cc:
##########
@@ -356,24 +400,72 @@ ServerSessionPool::eventHandler(int event, void *data)
return 0;
}
+void
+ServerSessionPool::deferClose(PoolableSession *ssn)
+{
+ // The session is not tracked by a pool anymore, so nothing can be allowed to fire on it before
+ // it is closed.
+ ssn->do_io_read(nullptr, 0, nullptr);
+ ssn->do_io_write(nullptr, 0, nullptr);
+ ssn->cancel_inactivity_timeout();
+ ssn->cancel_active_timeout();
+
+ if (m_deferred_close.empty()) {
+ this_ethread()->schedule_imm_local(this);
+ }
+ m_deferred_close.push_back(ssn);
+}
+
+void
+ServerSessionPool::closeDeferred()
+{
+ // Closing a session can run plugin hooks, so take the list in case that ends up adding to it.
+ std::vector<PoolableSession *> sessions;
+
+ sessions.swap(m_deferred_close);
+ closeSessions(sessions);
+}
+
void
HttpSessionManager::init()
{
m_g_pool = new ServerSessionPool;
eventProcessor.schedule_spawn(&initialize_thread_for_http_sessions, ET_NET);
}
-// TODO: Should this really purge all keep-alive sessions?
-// Does this make any sense, since we always do the global pool and not the per thread?
+// Only the shared pool is purged, not the per thread pools.
+//
+// A session is closed by the thread that owns its connection, so this asks every thread to close
+// its own. That keeps the closes out of the pool lock and away from other threads' connections.
void
HttpSessionManager::purge_keepalives()
{
- EThread *ethread = this_ethread();
+ auto const &group = eventProcessor.thread_group[ET_NET];
- MUTEX_TRY_LOCK(lock, m_g_pool->mutex, ethread);
- if (lock.is_locked()) {
- m_g_pool->purge();
- } // should we do something clever if we don't get the lock?
+ for (int i = 0; i < group._count; ++i) {
+ if (EThread *thread = group._thread[i]; thread != nullptr && thread->server_session_pool != nullptr) {
+ thread->server_session_pool->requestPurge(thread);
Review Comment:
Behavior note: `purge_keepalives()` used to free connections synchronously on the calling thread (when it got the lock). Now it only schedules work. The request in `HttpSM` that triggers it fails either way, so that doesn't change. But for a while after hitting `server_max_connections`, `current_server_connections` will stay at the limit until every net thread runs its event, so a few more requests may get ENFILE than before. That seems fine, but could you mention it in the commit message? Also nit: `requestPurge(thread)` passes the thread that already owns the pool. A pool could just remember its owning thread in `initialize_thread_for_http_sessions` instead of taking it as an argument.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6037625376
[approve ci autest 2]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Reduce contention in the shared origin connection pool [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak merged PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6088933412
[approve ci analyzer]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6198998139
We tried this in production. I cherry-picked the commit onto our 10.2.x-based build and compared two identical hosts in the same pod: same hardware, same traffic mix, `server_session_sharing.pool: global`. One ran the stock build and the other the same build plus this patch. Both were restarted within 16 seconds of each other.
A window here is a 60-second interval in which we read each counter at the start and end on both hosts at the same moment, then divided the increase by the requests served in that interval. Per-request counters over 8 such paired windows, starting about an hour after the restarts, stock → patched:
| Metric | Change | Windows lower |
|---|---|---|
| `origin_shutdown.pool_lock_contention` | −14% | 8 of 8 |
| `origin.reuse_fail` | −14% | 8 of 8 |
| `total_server_connections` (new origin connections) | −8% | 8 of 8 |
In the access log, the share of origin requests that went out on an already-open connection rose from 75.0% to 76.8%, higher in all four 15-minute windows from the first hour after the restarts. Before the patch, with both hosts on the same build, the patched host actually opened slightly *more* origin connections than its partner. So the improvement isn't a quirk of that host.
Client TTFB, origin wait, 5xx share and cache hit ratio didn't change outside noise. ATS CPU per request read about 3% lower on the patched host, but we have no same-build baseline for that window, so I wouldn't read anything into it in either direction. For comparison, you saw about 2% higher CPU with the patch.
Stability: no crashes or restarts over about 1.5 hours in rotation, and `origin_shutdown.migration_failure` stayed at 0 throughout.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6025384921
[approve ci autest 2]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6218746110
@traeak, could we use “origin connections” in the title and opening paragraph? `HttpSessionManager` handles shared outbound server sessions for connections to both origin servers and parent proxies, so “parent connections” makes the scope sound narrower than it is.
Suggested title:
> Reduce contention in the shared origin connection pool
Suggested description:
---
With global and hybrid origin connection pools, the shared session pool mutex was held while migrating pooled connections between net threads and closing sessions. This put socket and TLS work inside a lock shared by the net threads, increasing contention when reusing origin connections.
Limit the shared pool lock to session lookup, table updates, and detaching a connection for transfer. Attach migrated connections and close removed sessions after releasing the lock. This applies to pooled connections to origin servers and parent proxies.
The change also fixes migration lifetime and event-registration races:
- Make stopped `EventIO` objects safe to encounter while the owning thread finishes rescheduling a connection.
- Remove old kqueue registrations during migration and undo a refresh that races with removal, preventing events from referring to a freed connection.
- Detach the session from its old VC before handling an attach failure, avoiding a second close of a VC that may already have been freed.
- Publish the completed socket/state transfer through an atomic `closed` flag before the owning thread reclaims the old VC.
When `server_max_connections` triggers a purge, each net thread closes its own pooled connections outside the shared pool lock. The calling thread closes its sessions immediately if it can acquire the pool lock; other threads purge when they process their events. This can leave the connection count at the limit briefly and cause additional requests to fail compared with the previous synchronous purge. Removing sessions through the normal pool bookkeeping also fixes drift in `pooled_server_connections` after a purge.
Add kqueue unit tests and `ATSReplayTest` coverage for cross-thread reuse with global and hybrid pools, plus a single-thread test that checks the request triggering a purge succeeds on retry. The replay tests also check that the pooled and current connection gauges return to zero.
---
The existing performance measurements could stay below this description as a separate results paragraph.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6038101742
[approve ci clang]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6125733597
[approve ci clang]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#discussion_r4288535455
##########
src/iocore/net/UnixNetVConnection.cc:
##########
@@ -1311,43 +1311,63 @@ UnixNetVConnection::is_default_inactivity_timeout()
/*
* Close down the current netVC. Save aside the socket and SSL information
- * and create new netVC in the current thread/netVC
+ * so a new netVC can be created from them in another thread.
*/
-UnixNetVConnection *
-UnixNetVConnection::migrateToCurrentThread(Continuation *cont, EThread *t)
+bool
+UnixNetVConnection::detachForMigration(MigrationState &ms)
{
- NetHandler *client_nh = get_NetHandler(t);
- ink_assert(client_nh);
- if (this->nh == client_nh) {
- // We're already there!
- return this;
+ // Remove the fd from the original thread's poller first, so it is not processed on two threads
+ // simultaneously. While this VC still owns the socket and SSL information a failure only needs
+ // an ordinary close.
+ //
+ // This stop is not synchronized with the original thread. That thread may have already seen
+ // this VC as open and enabled in NetHandler::process_ready_list, in which case it goes on to
+ // net_read_io, fails the try lock on the VIO mutex held by the caller, and reschedules the VC.
+ // The reschedule calls ep.refresh on the EventIO stopped here, so EventIO::modify and
+ // EventIO::refresh do nothing on a stopped EventIO. A delay before the try lock in net_read_io
+ // makes the original thread hit this reliably.
+ //
+ // Unlike an ordinary close, migration keeps the fd open, so kqueue registrations must
+ // also be removed here before the old VC can be freed.
+ if (this->ep.stop_for_migration() != 0) {
+ Error("Failed to remove migrating socket %d from its event loop, errno=%d (%s)", this->con.sock.get_fd(), errno,
+ strerror(errno));
+ ink_assert(!"stop_for_migration failed");
+ this->do_io_close();
+ return false;
}
- Connection hold_con;
- hold_con.move(this->con);
-
- void *arg = this->_prepareForMigration();
+ ms.con.move(this->con);
+ ms.arg = this->_prepareForMigration();
+ ms.options = this->options;
+ ms.context = this->get_context();
+ ms.inactivity_timeout_in = this->inactivity_timeout_in;
+ ms.processor = this->_getNetProcessor();
// Do_io_close will signal the VC to be freed on the original thread
// Since we moved the con context, the fd will not be closed
- // Go ahead and remove the fd from the original thread's epoll structure, so it is not
- // processed on two threads simultaneously
- this->ep.stop();
+ // Do not mark this closed until the end so it does not get freed by the other thread too soon
+ this->do_io_close();
Review Comment:
[P1] Synchronize publication of the completed detach before reclamation
Following up on Bryan's reported TSan races: after moving `stop_for_migration()` before `ms.con.move()` and the state copies, its atomic `event_loop.exchange(nullptr)` cannot publish those later accesses to the owning thread. The remaining publication is `do_io_close()` writing the plain `NetEvent::closed` integer; `INK_WRITE_MEMORY_BARRIER` is empty. Meanwhile `NetHandler::process_ready_list()` reads `closed` and calls `free_netevent()` without acquiring the pool/VIO mutex. Holding that mutex here therefore does not order `Connection::close()` / `options.reset()` in `free_thread()` after these moves/copies. On a weakly ordered target the owner can observe the close before the socket relinquishment, close the transferred fd, or reset state that detach is still reading. Please add a release/acquire handoff covering the completed detach and the owner's reclamation path (or otherwise synchronize reclamation); moving the stop first for the failure fallback needs this corresponding lif
etime ordering.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on code in PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#discussion_r4294770624
##########
src/iocore/net/UnixNetVConnection.cc:
##########
@@ -1311,43 +1311,55 @@ UnixNetVConnection::is_default_inactivity_timeout()
/*
* Close down the current netVC. Save aside the socket and SSL information
- * and create new netVC in the current thread/netVC
+ * so a new netVC can be created from them in another thread.
*/
-UnixNetVConnection *
-UnixNetVConnection::migrateToCurrentThread(Continuation *cont, EThread *t)
+void
+UnixNetVConnection::detachForMigration(MigrationState &ms)
{
- NetHandler *client_nh = get_NetHandler(t);
- ink_assert(client_nh);
- if (this->nh == client_nh) {
- // We're already there!
- return this;
- }
-
- Connection hold_con;
- hold_con.move(this->con);
-
- void *arg = this->_prepareForMigration();
+ ms.con.move(this->con);
+ ms.arg = this->_prepareForMigration();
+ ms.options = this->options;
+ ms.context = this->get_context();
+ ms.inactivity_timeout_in = this->inactivity_timeout_in;
+ ms.processor = this->_getNetProcessor();
// Do_io_close will signal the VC to be freed on the original thread
// Since we moved the con context, the fd will not be closed
- // Go ahead and remove the fd from the original thread's epoll structure, so it is not
+ // Go ahead and remove the fd from the original thread's poller, so it is not
// processed on two threads simultaneously
- this->ep.stop();
+ //
+ // This stop is not synchronized with the original thread. That thread may have already seen
+ // this VC as open and enabled in NetHandler::process_ready_list, in which case it goes on to
+ // net_read_io, fails the try lock on the VIO mutex held by the caller, and reschedules the VC.
+ // The reschedule calls ep.refresh on the EventIO stopped here, so EventIO::modify and
+ // EventIO::refresh do nothing on a stopped EventIO. A delay before the try lock in net_read_io
+ // makes the original thread hit this reliably.
+ //
+ // Unlike an ordinary close, migration keeps the fd open, so kqueue registrations must
+ // also be removed here before the old VC can be freed.
+ ink_release_assert(this->ep.stop_for_migration() == 0);
Review Comment:
Done in f068f1f95. The stop now runs before the socket and SSL state are moved out of the old VC, so on failure the VC still owns them and an ordinary do_io_close() recovers. detachForMigration() logs an Error(), ink_asserts in debug builds and returns false; _acquire_session() then drops the session and increments origin_shutdown_migration_failure instead of aborting.
##########
src/iocore/net/EventIO.cc:
##########
@@ -102,10 +140,11 @@ EventIO::modify(int e)
EV_SET(&ev[n++], fd, EVFILT_WRITE, EV_ADD | INK_EV_EDGE_TRIGGER, 0, 0, this);
}
events = ee;
- if (n)
- return kevent(event_loop->kqueue_fd, &ev[0], n, nullptr, 0, nullptr);
- else
+ if (n) {
+ return kevent(loop->kqueue_fd, &ev[0], n, nullptr, 0, nullptr);
Review Comment:
Added a comment in f068f1f95 saying this level-triggered kqueue path is not race safe against stop_for_migration() and would need the same undo as refresh() before it is built. Since USE_EDGE_TRIGGER is hard defined for kqueue the path is not compiled, so I did not add untestable handling.
##########
src/iocore/net/unit_tests/test_EventIOMigration.cc:
##########
@@ -0,0 +1,87 @@
+/** @file
+
+ Tests for kqueue registration teardown when a socket is migrated.
+
+ @section license License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+
+#include "../P_UnixPollDescriptor.h"
+#include "iocore/net/EventIO.h"
+#include "iocore/net/ReadWriteEventIO.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cerrno>
+#include <unistd.h>
+
+#if TS_USE_KQUEUE
Review Comment:
Added tests/gold_tests/session_sharing/pool_lock.test.py in f068f1f95 with ATSReplayTest replay files for the global and hybrid pools. They run 4 net threads with server_max_connections 3, verify a pooled connection is reused and migrated between threads, verify the request that hits the limit triggers purge_keepalives() and succeeds, and check that proxy.process.http.pooled_server_connections is back to 0 at the end.
##########
src/proxy/http/HttpSessionManager.cc:
##########
@@ -356,24 +400,72 @@ ServerSessionPool::eventHandler(int event, void *data)
return 0;
}
+void
+ServerSessionPool::deferClose(PoolableSession *ssn)
+{
+ // The session is not tracked by a pool anymore, so nothing can be allowed to fire on it before
+ // it is closed.
+ ssn->do_io_read(nullptr, 0, nullptr);
+ ssn->do_io_write(nullptr, 0, nullptr);
+ ssn->cancel_inactivity_timeout();
+ ssn->cancel_active_timeout();
+
+ if (m_deferred_close.empty()) {
+ this_ethread()->schedule_imm_local(this);
+ }
+ m_deferred_close.push_back(ssn);
+}
+
+void
+ServerSessionPool::closeDeferred()
+{
+ // Closing a session can run plugin hooks, so take the list in case that ends up adding to it.
+ std::vector<PoolableSession *> sessions;
+
+ sessions.swap(m_deferred_close);
+ closeSessions(sessions);
+}
+
void
HttpSessionManager::init()
{
m_g_pool = new ServerSessionPool;
eventProcessor.schedule_spawn(&initialize_thread_for_http_sessions, ET_NET);
}
-// TODO: Should this really purge all keep-alive sessions?
-// Does this make any sense, since we always do the global pool and not the per thread?
+// Only the shared pool is purged, not the per thread pools.
+//
+// A session is closed by the thread that owns its connection, so this asks every thread to close
+// its own. That keeps the closes out of the pool lock and away from other threads' connections.
void
HttpSessionManager::purge_keepalives()
{
- EThread *ethread = this_ethread();
+ auto const &group = eventProcessor.thread_group[ET_NET];
- MUTEX_TRY_LOCK(lock, m_g_pool->mutex, ethread);
- if (lock.is_locked()) {
- m_g_pool->purge();
- } // should we do something clever if we don't get the lock?
+ for (int i = 0; i < group._count; ++i) {
+ if (EThread *thread = group._thread[i]; thread != nullptr && thread->server_session_pool != nullptr) {
+ thread->server_session_pool->requestPurge(thread);
Review Comment:
Both done in f068f1f95. The pool now remembers its owning thread from initialize_thread_for_http_sessions and requestPurge() takes no argument. The timing change is described in that commit message and I added it to the PR description too. Note the current thread now closes its own pooled sessions before purge_keepalives() returns; without that the request which hit the limit ran out of retries before any purge event ran.
##########
src/proxy/http/HttpSessionManager.cc:
##########
@@ -285,6 +313,17 @@ ServerSessionPool::eventHandler(int event, void *data)
PoolableSession *s = nullptr;
switch (event) {
+ case EVENT_IMMEDIATE:
+ case EVENT_INTERVAL:
+ if (m_purge_requested.exchange(false) && !httpSessionManager.purge_thread_keepalives()) {
Review Comment:
Added the comment in f068f1f95: both requestPurge() and deferClose() schedule the pool and an event from either does whatever work is pending.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6129541630
[approve ci clang-analyzer]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6088884953
[approve ci autest 2]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6088908229
[approve ci clang]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for parent connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6124509982
[approve ci autest 2]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Reduce contention in the shared origin connection pool [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6223441758
changed the PR title and description.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Shorten the session pool lock for origin connections [trafficserver]
Posted by "traeak (via GitHub)" <gi...@apache.org>.
traeak commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6037644179
[approve ci autest 2]
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Reduce contention in the shared origin connection pool [trafficserver]
Posted by "cmcfarlen (via GitHub)" <gi...@apache.org>.
cmcfarlen commented on PR #13806:
URL: https://github.com/apache/trafficserver/pull/13806#issuecomment-6508404159
Cherry-picked to the 10.2.x branch as v10.2.2 for the 10.2.2 release.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org