Skip to content

Support creating a PropertyKey that is not tied to a specific Http2Connection - #17594

Merged
normanmaurer merged 10 commits into
4.2from
global_property_key
Sep 23, 2026
Merged

normanmaurer merged 10 commits into
4.2from
global_property_key

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation:

Http2Connection.PropertyKey instances could only be created via
Http2Connection.newKey(), which ties every key to the specific
connection instance that created it. This makes it impossible to
share a key, e.g. as a static field, across multiple Http2Connection
instances, or to create a key before an Http2Connection instance is
even available.

Modifications:

  • Add a static PropertyKey.newKey() factory method that returns a key
    which is not associated with any particular Http2Connection and so
    can be used with the streams of any connection
  • Let DefaultStream dispatch on the concrete key type: keys created via
    Http2Connection.newKey() keep using the existing array-indexed
    per-stream storage (fast path), while keys created via
    PropertyKey.newKey() are stored in a lazily allocated per-stream
    IdentityHashMap
  • Add tests covering both key kinds, including cross-connection
    rejection of connection-bound keys and sharing of static keys across
    connections

Result:

It is now possible to create an Http2Connection.PropertyKey without
having access to an Http2Connection instance, while keys created via
Http2Connection.newKey() keep their existing fast-path performance
characteristics.

…nnection

Motivation:

Http2Connection.PropertyKey instances could only be created via
Http2Connection.newKey(), which ties every key to the specific
connection instance that created it. This makes it impossible to
share a key, e.g. as a static field, across multiple Http2Connection
instances, or to create a key before an Http2Connection instance is
even available.

Modifications:

- Add a static PropertyKey.newKey() factory method that returns a key
  which is not associated with any particular Http2Connection and so
  can be used with the streams of any connection
- Let DefaultStream dispatch on the concrete key type: keys created via
  Http2Connection.newKey() keep using the existing array-indexed
  per-stream storage (fast path), while keys created via
  PropertyKey.newKey() are stored in a lazily allocated per-stream
  IdentityHashMap
- Add tests covering both key kinds, including cross-connection
  rejection of connection-bound keys and sharing of static keys across
  connections

Result:

It is now possible to create an Http2Connection.PropertyKey without
having access to an Http2Connection instance, while keys created via
Http2Connection.newKey() keep their existing fast-path performance
characteristics.
* associated with a key returned by {@link Http2Connection#newKey()}. Prefer the latter whenever a specific
* {@link Http2Connection} instance is available up-front.
*/
static PropertyKey newKey() {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This only works for 4.2 tho.. Maybe we need to add some static method in some class to also make it possible in 4.1 and so make some of the PRs possible...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Move to a utility class so we can also port this PR to 4.1

@normanmaurer

Copy link
Copy Markdown
Member Author

This was motivated by some other PRs that had to use hacks because state needs to be shared across encoder / decoder

@LI123456mo

Copy link
Copy Markdown
Contributor

Hi @normanmaurer , I have a foundation of netty-in-action , can I contribute here

Motivation:

PropertyKey.newKey() was added as a static interface method, which
requires Java 8. Another branch still needs to support Java 7, so a
static method on the interface itself is not an option there.

Modifications:

Add a new public Http2ConnectionPropertyKeys utility class with a
static newKey() method that creates a PropertyKey which is not tied to
a specific Http2Connection, and remove the static method from the
PropertyKey interface, restoring it to a plain marker interface.

Result:

Http2Connection.PropertyKey no longer relies on static interface
methods, making the connection-agnostic key creation mechanism
portable to a Java 7 codebase.
Motivation:

newKey() did not make it obvious that the returned key is global,
i.e. not tied to a specific Http2Connection, as opposed to
Http2Connection.newKey() which creates a connection-bound key.

Modifications:

Rename Http2ConnectionPropertyKeys.newKey() to newGlobalKey() and
update all references and tests accordingly.

Result:

The method name now makes the distinction to Http2Connection.newKey()
clear.
@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Sep 22, 2026

@bryce-anderson bryce-anderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All my suggestions are nits. Structurally, this looks like it will be a lot more useful. I think the performance is likely acceptable and we can optimize it later as needed.

Motivation:

bryce-anderson left three nit-level review comments on the PR: the
PropertyKey javadoc did not mention that keys are compared by strict
reference equality, verifyKey(PropertyKey) still allowed a
ClassCastException to occur from within the method itself even though
its javadoc no longer documented one, and a couple of tests still used
"static" terminology instead of the "global" terminology settled on
for the new key kind.

Modifications:

- Document that PropertyKey instances are compared by identity, not
  Object.equals()
- Change verifyKey() to accept a DefaultPropertyKey instead of a
  PropertyKey, so the cast (and its ClassCastException) happens once,
  at the call sites that already guard it with an instanceof check,
  instead of being hidden (and undocumented) inside verifyKey() itself
- Rename the "static key" tests/variables to "global key" to match the
  newGlobalKey() naming

Result:

All nits raised in review are addressed.
Motivation:

Documenting that PropertyKey is compared by strict reference equality
locks in an implementation detail of how keys happen to be looked up
today. We may want to change that later, so it should not be part of
the public contract.

Modifications:

Drop the javadoc paragraph added in response to review that described
PropertyKey comparison semantics.

Result:

The javadoc no longer commits to a specific equality semantic for
PropertyKey.

@ejona86 ejona86 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yay!

FWIW, we could use the array storage for these, but we can't change our mind to use the array later as someone may already be making too many of these. I can live with the map, but if you ever might want an array you need to do it now.

@normanmaurer

Copy link
Copy Markdown
Member Author

Yay!

FWIW, we could use the array storage for these, but we can't change our mind to use the array later as someone may already be making too many of these. I can live with the map, but if you ever might want an array you need to do it now.

Yeah I mean we could and just make the method package private to create these . I think beside our own classes there shouldn't be anyone that need it. @bryce-anderson thoughts ?

@bryce-anderson

Copy link
Copy Markdown
Contributor

Yay!
FWIW, we could use the array storage for these, but we can't change our mind to use the array later as someone may already be making too many of these. I can live with the map, but if you ever might want an array you need to do it now.

Yeah I mean we could and just make the method package private to create these . I think beside our own classes there shouldn't be anyone that need it. @bryce-anderson thoughts ?

I'm 👍 to making it package private for now irrespective of if you want to give the array storage a go.

Motivation:

Http2ConnectionPropertyKeys was introduced to let internal code create
a PropertyKey that is not tied to a specific Http2Connection, e.g. to
share state across an encoder/decoder pair without the hacks that
motivated this change. It is not meant to be public API at this point.

Modifications:

- Drop the public modifier from Http2ConnectionPropertyKeys and its
  newGlobalKey() method
- Remove the now dangling javadoc references to
  Http2ConnectionPropertyKeys from Http2Connection and
  DefaultHttp2Connection

Result:

Http2ConnectionPropertyKeys is only usable from within the
io.netty.handler.codec.http2 package.
Motivation:

Keys created via Http2ConnectionPropertyKeys.newGlobalKey() were
stored per-stream in a lazily allocated IdentityHashMap. Global keys
are expected to be created rarely, typically once per use case and
stored in a static field, so a hash map is overkill: we can reuse the
same index-based array storage already used for connection-bound
keys.

Modifications:

- Give GlobalPropertyKey (now a concrete, named class instead of an
  anonymous PropertyKey) an index assigned from a shared, monotonically
  increasing AtomicInteger counter in Http2ConnectionPropertyKeys
- Replace PropertyMap.genericValues (an IdentityHashMap) with a plain
  Object[] array, grown lazily the same way as the connection-scoped
  values array, sized to Http2ConnectionPropertyKeys.keyCount()
- DefaultStream.setProperty/getProperty/removeProperty now dispatch
  explicitly on DefaultPropertyKey vs GlobalPropertyKey and reject any
  other PropertyKey implementation with an IllegalArgumentException,
  since only these two kinds of keys carry the index needed for array
  storage

Result:

Global keys are now accessed via array indexing instead of a hash map
lookup, avoiding a per-stream map allocation for the common case of a
handful of global keys.
Motivation:

Global keys are meant to be created rarely, typically once per
use-case and stored in a static field. There is no legitimate reason
to ever create more than a handful of them, so runaway creation (e.g.
in a loop, or by mistake on every request) is almost certainly a bug.

Modifications:

Throw an IllegalStateException from newGlobalKey() once the 9th key
would be created (i.e. once the assigned index would reach 8), and
restore the shared index counter to its previous value in that case
so a failed attempt does not permanently consume part of the budget.

Result:

Misuse that creates an unbounded number of global keys now fails fast
with a clear exception instead of silently growing every stream's
backing array forever.
@normanmaurer

Copy link
Copy Markdown
Member Author

@bryce-anderson @ejona86 done... it's package-private now and also use array based backend. I also added a check to ensure we never create more then 8 of such keys for now. As these are only used internally we should see tests failing once we hit the limit and can revisit if we need to upper the limit. This was mainly done to guard against bugs that could cause high memory usage.

@testlens-app

testlens-app Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 1 failed test 🚨

Here is what you can do:

  1. Inspect the test failures carefully.
  2. If you are convinced that some of the tests are flaky, you can mute them below.
  3. Finally, trigger a rerun by checking the rerun checkbox.

Test Summary

Build PR / macos-x86_64-java11-boringssl build > Netty/Transport/Native/KQueue

Test Runs Flakiness
KQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithConnect(TestInfo) ❌ 4% 🟡

🏷️ Commit: 6f073c6
▶️ Tests: 456283 executed
⚪️ Checks: 26/26 completed

Test Failures

KQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithConnect(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-x86_64-java11-boringssl build)
org.opentest4j.AssertionFailedError
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendWithConnect0(DatagramUnicastTest.java:430)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendWithConnect(DatagramUnicastTest.java:383)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendWithConnect(DatagramUnicastTest.java:212)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest$7.run(DatagramUnicastTest.java:206)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest$7.run(DatagramUnicastTest.java:203)
	at io.netty.testsuite.transport.AbstractComboTestsuiteTest.run(AbstractComboTestsuiteTest.java:52)
	at io.netty.testsuite.transport.socket.DatagramUnicastTest.testSimpleSendWithConnect(DatagramUnicastTest.java:203)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)

Rerun Controls

Select tests to mute in this pull request:

  • KQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithConnect(TestInfo)

Reuse successful test results:

  • ♻️ Only rerun the tests that failed or were muted before

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit cd967a4 into 4.2 Sep 23, 2026
26 of 27 checks passed
@normanmaurer
normanmaurer deleted the global_property_key branch September 23, 2026 17:36
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Sep 24, 2026
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17615

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17616

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Sep 24, 2026
normanmaurer added a commit that referenced this pull request Sep 24, 2026
…pecific Http2Connection (#17615)

Auto-port of #17594 to 4.1
Cherry-picked commit: cd967a4

---
Motivation:

Http2Connection.PropertyKey instances could only be created via
Http2Connection.newKey(), which ties every key to the specific
connection instance that created it. This makes it impossible to
share a key, e.g. as a static field, across multiple Http2Connection
instances, or to create a key before an Http2Connection instance is
even available.

Modifications:

- Add a static PropertyKey.newKey() factory method that returns a key
  which is not associated with any particular Http2Connection and so
  can be used with the streams of any connection
- Let DefaultStream dispatch on the concrete key type: keys created via
  Http2Connection.newKey() keep using the existing array-indexed
  per-stream storage (fast path), while keys created via
  PropertyKey.newKey() are stored in a lazily allocated per-stream
  IdentityHashMap
- Add tests covering both key kinds, including cross-connection
  rejection of connection-bound keys and sharing of static keys across
  connections

Result:

It is now possible to create an Http2Connection.PropertyKey without
having access to an Http2Connection instance, while keys created via
Http2Connection.newKey() keep their existing fast-path performance
characteristics.

---------

Co-authored-by: Norman Maurer <[email protected]>
normanmaurer added a commit that referenced this pull request Sep 24, 2026
…pecific Http2Connection (#17616)

Auto-port of #17594 to 5.0
Cherry-picked commit: cd967a4

---
Motivation:

Http2Connection.PropertyKey instances could only be created via
Http2Connection.newKey(), which ties every key to the specific
connection instance that created it. This makes it impossible to
share a key, e.g. as a static field, across multiple Http2Connection
instances, or to create a key before an Http2Connection instance is
even available.

Modifications:

- Add a static PropertyKey.newKey() factory method that returns a key
  which is not associated with any particular Http2Connection and so
  can be used with the streams of any connection
- Let DefaultStream dispatch on the concrete key type: keys created via
  Http2Connection.newKey() keep using the existing array-indexed
  per-stream storage (fast path), while keys created via
  PropertyKey.newKey() are stored in a lazily allocated per-stream
  IdentityHashMap
- Add tests covering both key kinds, including cross-connection
  rejection of connection-bound keys and sharing of static keys across
  connections

Result:

It is now possible to create an Http2Connection.PropertyKey without
having access to an Http2Connection instance, while keys created via
Http2Connection.newKey() keep their existing fast-path performance
characteristics.

Co-authored-by: Norman Maurer <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…pecific Http2Connection (netty#17615)

Auto-port of netty#17594 to 4.1
Cherry-picked commit: cd967a4

---
Motivation:

Http2Connection.PropertyKey instances could only be created via
Http2Connection.newKey(), which ties every key to the specific
connection instance that created it. This makes it impossible to
share a key, e.g. as a static field, across multiple Http2Connection
instances, or to create a key before an Http2Connection instance is
even available.

Modifications:

- Add a static PropertyKey.newKey() factory method that returns a key
  which is not associated with any particular Http2Connection and so
  can be used with the streams of any connection
- Let DefaultStream dispatch on the concrete key type: keys created via
  Http2Connection.newKey() keep using the existing array-indexed
  per-stream storage (fast path), while keys created via
  PropertyKey.newKey() are stored in a lazily allocated per-stream
  IdentityHashMap
- Add tests covering both key kinds, including cross-connection
  rejection of connection-bound keys and sharing of static keys across
  connections

Result:

It is now possible to create an Http2Connection.PropertyKey without
having access to an Http2Connection instance, while keys created via
Http2Connection.newKey() keep their existing fast-path performance
characteristics.

---------

Co-authored-by: Norman Maurer <[email protected]>
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.

5 participants