Repository navigation
Support creating a PropertyKey that is not tied to a specific Http2Connection - #17594
Conversation
…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() { |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Move to a utility class so we can also port this PR to 4.1
|
This was motivated by some other PRs that had to use hacks because state needs to be shared across encoder / decoder |
|
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.
bryce-anderson
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
@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. |
2c5577d to
65bddd5
Compare
🚨 TestLens detected 1 failed test 🚨Here is what you can do:
Test SummaryBuild PR / macos-x86_64-java11-boringssl build > Netty/Transport/Native/KQueue
🏷️ Commit: 6f073c6 Test FailuresKQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithConnect(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-x86_64-java11-boringssl build)Rerun ControlsSelect tests to mute in this pull request:
Reuse successful test results:
Click the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
|
Auto-port PR for 4.1: #17615 |
|
Auto-port PR for 5.0: #17616 |
…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]>
…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]>
…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]>
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:
which is not associated with any particular Http2Connection and so
can be used with the streams of any connection
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
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.