Feature/cpm Client-server and communication #7792
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ⅰ. Describe what this PR did
II. Client–Server Communication
During the heartbeat sending cycle, the client reports connection-pool metrics (HikariCP, Druid) to the server at a fixed interval.
1. Client-side Heartbeat Reporting of Metrics
1) Modifications
core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingClient.javacreateMetricsMessage()andshouldReportPoolInfo(), used during the Writer Idle event to determine whether connection-pool metrics should be sent.IdleStateEvent.WRITER_IDLE_STATE_EVENTbranch, added reporting logic: whenshouldReportPoolInfo()is true, send the result ofcreateMetricsMessage().Example snippet:
core/src/main/java/org/apache/seata/core/rpc/netty/RmNettyRemotingClient.javacreateMetricsMessage()andshouldReportPoolInfo(),delegating to the client-side processor.setEnableConnectionPoolMetrics(boolean)andsetHttpPort(int),for enabling metrics reporting and setting the client HTTP port.registerProcessor().Example:
core/src/main/java/org/apache/seata/core/protocol/MessageType.javaTYPE_CONNECTION_POOL_METRICS = 122,used for routing connection-pool metrics messages.2) Additions
core/src/main/java/org/apache/seata/core/rpc/processor/client/ClientConnectionPoolMetricsProcessor.javaConnectionPoolMetricsMessage((includes HikariCP and Druid metrics), and controls report throttling (default 10 seconds).clientUrl(ip:port), allowing the server to identify the client address (for update operations).Example:
core/src/main/java/org/apache/seata/core/protocol/ConnectionPoolMetricsMessage.javaUses type code
TYPE_CONNECTION_POOL_METRICS。2. Server-side Handling of Metrics
1)Modifications
core/src/main/java/org/apache/seata/core/rpc/netty/NettyRemotingServer.javaregisterProcessor()inServerConnectionPoolMetricsProcessor,enabling reception and processing of client reports.2)Additions
core/src/main/java/org/apache/seata/core/rpc/processor/server/ServerConnectionPoolMetricsProcessor.javaConnectionPoolInfoCache,where metrics are aggregated bypoolNameacross multiple clients.core/src/main/java/org/apache/seata/core/rpc/processor/server/ConnectionPoolInfoCache.javarefresh(PoolConfigUpdateRequest)to update the configuration snapshot in cache.Auxiliary types:
core/src/main/java/org/apache/seata/core/protocol/PoolType.java3.Serialization/Deserialization: Protobuf
1)Modifications
serializer/seata-serializer-protobuf/src/main/java/org/apache/seata/serializer/protobuf/manager/ProtobufConvertManager.javaPurpose: Protobuf Conversion Manager — responsible for registering and managing converters for all message types.
2)Additions
serializer/seata-serializer-protobuf/src/main/java/org/apache/seata/serializer/protobuf/convertor/ConnectionPoolMetricsMessageConvertor.javaPurpose: Handles Protobuf serialization and deserialization of connection-pool metrics messages.
serializer/seata-serializer-protobuf/src/main/resources/protobuf/org/apache/seata/protocol/transcation/connectionPoolMetricsMessage.protoPurpose: Defines the Protobuf schema for connection-pool metrics messages.
4.Serialization/Deserialization: Seata Codec
1)Modifications
serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/MessageCodecFactory.javaPurpose: Factory for Seata message codecs; creates corresponding codec instances based on message type.
New imports:
ConnectionPoolMetricsMessageConnectionPoolMetricsMessageCodecgetMessageCodec method:
MessageType.TYPE_CONNECTION_POOL_METRICSConnectionPoolMetricsMessageCodecgetMessage method:
MessageType.TYPE_CONNECTION_POOL_METRICSConnectionPoolMetricsMessageinstance2)Additions
serializer/seata-serializer-seata/src/main/java/org/apache/seata/serializer/seata/protocol/ConnectionPoolMetricsMessageCodec.javaPurpose: Implements binary encoding/decoding for
ConnectionPoolMetricsMessagesupporting compact serialization of connection-pool monitoring data.Message type support:
MessageSeataCodecConnectionPoolMetricsMessageEncoding:
Decoding:
ConnectionPoolMetricsMessage5.Explanation of Key Method Changes
Client sending timing (core loop):
AbstractNettyRemotingClientshouldReportPoolInfo():default false; subclasses determine reporting based on switch and time window.createMetricsMessage():default returnsHeartbeatMessage.PING,RM overrides to returnConnectionPoolMetricsMessage。RM client settings:
RmNettyRemotingClientsetEnableConnectionPoolMetrics(boolean):setHttpPort(int):registerProcessor():RegistersClientConnectionPoolMetricsProcessorfor message construction & processing.Metrics collection:
DataSourceConnectionPoolCollectorcollectHikariMetrics(...):reads metrics viaHikariPoolMXBeanandHikariDataSourcecollectDruidMetrics(...):reads pool metrics fromDruidDataSourceand injects SQL execution data.updateConfig(poolName, request):updates pool config using reflection, ensuring safety.Message construction & throttling:
ClientConnectionPoolMetricsProcessorcreateMetricsMessage():wraps application ID, client URL, metrics, sequence, timestamp.shouldReportPoolInfo():throttles based on last report time (default every 10 seconds).Server-side processing chain:
ServerConnectionPoolMetricsProcessor→ConnectionPoolInfoCachepoolNameand stores pool type + client URL.Protocol & serialization:
ConnectionPoolMetricsMessageandMessageType.TYPE_CONNECTION_POOL_METRICS6.Class Relationship Diagram
Ⅴ. Special notes for reviews