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
publishOutstandingBatchcom.google.cloud.pubsub.v1Publisher.javapublishOutstandingBatchif (!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
acquireMethod into Modular Helper Methodscom.google.cloud.pubsub.v1Publisher.java(nested classMessageFlowController)acquirecheckMessageLimit(): 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.v1Publisher.javatopicNameSize→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
SchemaServiceGrpcto Eliminate Duplicated Code (Extract Class)MethodDescriptorcreation logic fromSchemaServiceGrpcinto a newMethodDescriptorFactoryclass.getCreateSchemaMethod).OpenTelemetryPubsubTracerstartSubscribeRpcSpaninOpenTelemetryPubsubTracerto use anRpcOperationHandlerinterface and concrete handlers (AckHandler,ModAckHandler,NackHandler), eliminating conditional logic (if/switchstatements).PublisherClass (acquireMethod)acquiremethod of thePublisherclass to a more appropriate class (e.g., a resource manager or semaphore wrapper).Publisherclass’s responsibilities.Testing