Chaho12 opened a new pull request, #7806:
URL: https://github.com/apache/kyuubi/pull/7806

   <!--
   Thanks for sending a pull request!
   
   Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://kyuubi.readthedocs.io/en/master/contributing/code/index.html
     2. If the PR is related to an issue in 
https://github.com/apache/kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, 
e.g., '[KYUUBI #XXXX] Your PR title ...'.
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][KYUUBI #XXXX] Your PR title ...'.
   -->
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new feature, clarify the use cases.
     2. If you fix a bug, you can clarify why it is a bug, and what versions 
are affected.
   -->
   
   `KyuubiSessionManager` gives back session limiter counts that the session 
never took. The
   `ipaddress` and `user:ipaddress` counters are shared, so each extra 
give-back frees a place another
   open session holds, and the connection limits stop being enforced.
   
   - **Unlimited users.** `increment` does not count a user in
     `kyuubi.server.limit.connections.user.unlimited.list`, but `closeSession` 
decrements for every
     session, and since #7759 so does a session that fails while it is created. 
Behind a gateway, where
     all users share one address, unlimited traffic keeps the address counter 
near 0 and
     `kyuubi.server.limit.connections.per.ipaddress` is in effect off. Checking 
the list again at close
     time is not enough, since `refresh/unlimited_users` can change it while a 
session is open.
   - **Concurrent close.** `closeSession` decrements in a `finally`, so when 
two callers close the same
     session at once (the idle session checker, the engine alive checker, the 
client), the one that
     gets `Invalid SessionHandle` still decrements.
   
   This patch:
   
   - has `SessionLimiter.increment` return whether it counted the session, and 
marks that on the
     session when it is opened; `closeSession`, a session failing in 
`createSession` and the batch
     path give the count back only for a marked session. The mark is taken with
     `AtomicBoolean.getAndSet(false)`, so a session closed by several callers 
gives it back once.
   - counts the session in `createSession`, right before the session is built. 
Recording the result
     needs the session object, and the `createSession` signature is shared by 
every engine's session
     manager in `kyuubi-common`. Counting before the session is built matters: 
the constructor
     registers a `LaunchEngine` operation and posts a session event, which a 
refused session would
     leave behind.
   - throws a `SessionLimiterRejectedException` (a `KyuubiSQLException` with 
the same sqlState and
     vendor code) for limiter refusals, which `openSession` passes on as it is. 
A refusal therefore
     still reaches the client with the limiter's own message and is not counted 
as `CONN_FAIL`, as
     before.
   
   Affects master and 1.11.1.
   
   close #7804
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   
   Added to `KyuubiSessionManagerLimiterSuite`, with `per.ipaddress=2` and an 
unlimited user. Each
   test fills one address with two sessions and checks that a third is still 
refused:
   
   - closing a session of an unlimited user
   - a session of an unlimited user failing on a restricted key while it is 
created
   - a session closed by two threads at once, 100 times
   - a session counted before its user is made unlimited gives the count back 
on close (guards against
     deciding by the list at close time)
   - a refused session throws `SessionLimiterRejectedException` with the 
limiter's message and leaves
     no operation behind
   
   Added to `SessionLimiterSuite`: `increment` reports an unlimited user as not 
counted.
   
       build/mvn test -pl kyuubi-server -am -Dtest=none \
           
-DwildcardSuites=org.apache.kyuubi.session.SessionLimiterSuite,org.apache.kyuubi.session.KyuubiSessionManagerLimiterSuite
   
   All pass with the change. With the session manager behavior reverted 
(decrement on every close and
   creation failure, refusal wrapped, count taken after the session is built), 
the unlimited close,
   unlimited creation failure and concurrent close tests fail with a third 
session let into the full
   address, and the refusal test fails on the exception type. With only the 
counting moved back after
   the session is built, the refusal test fails with `3 did not equal 2` 
operations.
   
   The suite uses a fresh user for each check that an address is full, since a 
refused open may
   still keep a count of its user until #7802 is fixed.
   
   ### Was this patch assisted by generative AI tooling?
   <!--
   If generative AI tooling has assisted in authoring or reviewing this patch, 
please include
   an 'Assisted-by: ' trailer that identifies the agent and/or model.
   For example: 'Assisted-by: Claude Opus 4.7' or 'Assisted-by: 
Claude:claude-opus-4-7'.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   
   Assisted-by: Claude Opus 5.5
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to