refac: Refactor Publish module to Improve code quality by removing implementation smell #2382
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.
Refactor for Improved Readability and Modularity
Changes
Simplified Complex Conditional in
publishOutstandingBatch
com.google.cloud.pubsub.v1
Publisher.java
publishOutstandingBatch
if (!activeAlarm.get() && outstandingBatch.orderingKey != null && !outstandingBatch.orderingKey.isEmpty())
) mixing multiple checks, increasing cognitive load.shouldPublishMoreForKey(boolean isAlarmActive, String orderingKey)
to isolate the logic and improve readability.Extracted
acquire
Method into Modular Helper Methodscom.google.cloud.pubsub.v1
Publisher.java
(nested classMessageFlowController
)acquire
checkMessageLimit()
: Validates message count against the limit.checkByteLimit(long messageSize)
: Validates byte count against the limit.acquireMessageSlot()
: Acquires a message slot, waiting if necessary.acquireByteSpace(long messageSize)
: Acquires byte space, handling partial allocation.waitForResource(CountDownLatch waiter)
: Centralizes waiting logic with lock management.Renamed Variables for Clarity and Consistency
com.google.cloud.pubsub.v1
Publisher.java
topicNameSize
→serializedTopicNameSize
: Reflects it’s the serialized size, not string length.messagesBatches
→batchesByOrderingKey
: Clarifies it’s a map keyed by ordering keys.activeAlarm
→isBatchPublishScheduled
: Indicates it tracks scheduled batch publishing.messagesWaiter
→pendingPublishesTracker
: Specifies it tracks pending publishes.outstandingMessages
→inFlightMessages
: Uses standard “in-flight” term for processing items.outstandingBytes
→inFlightBytes
: Consistent withinFlightMessages
.batchedBytes
→totalMessageBytesInBatch
: Describes it’s the total byte size in a batch.Refactor
SchemaServiceGrpc
to Eliminate Duplicated Code (Extract Class)MethodDescriptor
creation logic fromSchemaServiceGrpc
into a newMethodDescriptorFactory
class.getCreateSchemaMethod
).OpenTelemetryPubsubTracer
startSubscribeRpcSpan
inOpenTelemetryPubsubTracer
to use anRpcOperationHandler
interface and concrete handlers (AckHandler
,ModAckHandler
,NackHandler
), eliminating conditional logic (if/switch
statements).Publisher
Class (acquire
Method)acquire
method of thePublisher
class to a more appropriate class (e.g., a resource manager or semaphore wrapper).Publisher
class’s responsibilities.Testing