Skip to content

Commit 5320a56

Browse files
author
Tony Cui
committed
Clean up unused memory
1 parent 52a7cb3 commit 5320a56

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

‎java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/CancellationSharer.java‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* It manages the lifecycle of the original attempt and any subsequent hedged attempts.
3737
*/
3838
class CancellationSharer extends AbstractApiFuture<PublishResponse> {
39-
private final Publisher.OutstandingBatch batch;
39+
private Publisher.OutstandingBatch batch;
4040
private final Publisher publisher;
4141

4242
// Guarded by lock
@@ -47,6 +47,11 @@ class CancellationSharer extends AbstractApiFuture<PublishResponse> {
4747
private final Lock lock = new ReentrantLock();
4848
private final AtomicBoolean isInQueue = new AtomicBoolean(false);
4949

50+
private void cleanupLocked() {
51+
runningAttempts.clear();
52+
this.batch = null;
53+
}
54+
5055
CancellationSharer(final Publisher.OutstandingBatch batch, final Publisher publisher) {
5156
this.batch = batch;
5257
this.publisher = publisher;
@@ -90,19 +95,18 @@ private void handleAttemptSuccess(final int attemptNumber, final PublishResponse
9095
batch.successfulAttempt = attemptNumber;
9196
set(response);
9297
cancelAllExceptLocked(attemptNumber);
98+
cleanupLocked();
9399
} finally {
94100
lock.unlock();
95101
}
96102
publisher.refillTokenBucket();
97103
}
98104

99105
private void handleAttemptFailure(final int attemptNumber, final Throwable t) {
100-
boolean shouldRemoveFromQueue = false;
101106
lock.lock();
102107
try {
103108
if (done) {
104-
return; // <-- Exit early before modifying runningAttempts to avoid
105-
// ConcurrentModificationException
109+
return;
106110
}
107111
runningAttempts.remove(attemptNumber);
108112
lastError = t;
@@ -117,17 +121,11 @@ private void handleAttemptFailure(final int attemptNumber, final Throwable t) {
117121
done = true;
118122
setException(lastError);
119123
cancelAllLocked();
120-
if (isInQueue.get()) {
121-
shouldRemoveFromQueue = true;
122-
}
124+
cleanupLocked();
123125
}
124126
} finally {
125127
lock.unlock();
126128
}
127-
128-
if (shouldRemoveFromQueue) {
129-
publisher.removeFromHedgingQueue(this);
130-
}
131129
}
132130

133131
void checkCompletionOnQueueExit() {
@@ -139,6 +137,7 @@ void checkCompletionOnQueueExit() {
139137
lastError != null
140138
? lastError
141139
: new RuntimeException("Hedging failed with no active attempts"));
140+
cleanupLocked();
142141
}
143142
} finally {
144143
lock.unlock();
@@ -148,24 +147,17 @@ void checkCompletionOnQueueExit() {
148147
@Override
149148
public boolean cancel(final boolean mayInterruptIfRunning) {
150149
boolean cancelled = false;
151-
boolean shouldRemoveFromQueue = false;
152150
lock.lock();
153151
try {
154152
if (super.cancel(mayInterruptIfRunning)) {
155153
cancelled = true;
156154
done = true;
157-
if (isInQueue.get()) {
158-
shouldRemoveFromQueue = true;
159-
}
160155
cancelAllLocked();
156+
cleanupLocked();
161157
}
162158
} finally {
163159
lock.unlock();
164160
}
165-
166-
if (shouldRemoveFromQueue) {
167-
publisher.removeFromHedgingQueue(this);
168-
}
169161
return cancelled;
170162
}
171163

@@ -190,7 +182,12 @@ AtomicBoolean isInQueue() {
190182
return isInQueue;
191183
}
192184

193-
Publisher.OutstandingBatch getBatch() {
194-
return batch;
185+
Publisher.OutstandingBatch getBatchIfActive() {
186+
lock.lock();
187+
try {
188+
return done ? null : batch;
189+
} finally {
190+
lock.unlock();
191+
}
195192
}
196193
}

‎java-pubsub/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/Publisher.java‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -760,21 +760,6 @@ public void run() {
760760
}
761761
}
762762

763-
void removeFromHedgingQueue(CancellationSharer coordinator) {
764-
queueLock.lock();
765-
try {
766-
Iterator<HedgedRequest> iterator = hedgingQueue.iterator();
767-
while (iterator.hasNext()) {
768-
if (iterator.next().getCoordinator() == coordinator) {
769-
iterator.remove();
770-
coordinator.isInQueue().set(false);
771-
}
772-
}
773-
} finally {
774-
queueLock.unlock();
775-
}
776-
}
777-
778763
private void processQueue() {
779764
queueLock.lock();
780765
try {
@@ -786,7 +771,8 @@ private void processQueue() {
786771
hedgingQueue.poll();
787772

788773
CancellationSharer coordinator = item.getCoordinator();
789-
if (coordinator.isDone()) {
774+
OutstandingBatch batch = coordinator.getBatchIfActive();
775+
if (batch == null) {
790776
coordinator.isInQueue().set(false);
791777
continue;
792778
}
@@ -799,15 +785,14 @@ private void processQueue() {
799785
hedgingQueue.add(nextItem);
800786

801787
// Start Hedged Attempt
802-
ApiFuture<PublishResponse> hedgedFuture =
803-
publishCall(coordinator.getBatch(), item.getAttemptNumber());
788+
ApiFuture<PublishResponse> hedgedFuture = publishCall(batch, item.getAttemptNumber());
804789
coordinator.addAttempt(item.getAttemptNumber(), hedgedFuture);
805790
} else {
806791
loggingUtil.logPublisher(
807792
LoggingUtil.SubSystem.PUBLISH_HEDGED,
808793
Level.FINER,
809794
"Hedging rate limited due to lack of tokens.",
810-
coordinator.getBatch().getMessageWrappers().get(0));
795+
batch.getMessageWrappers().get(0));
811796
coordinator.isInQueue().set(false);
812797
coordinator.checkCompletionOnQueueExit();
813798
}

0 commit comments

Comments
 (0)