Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ManagedChannel shutdown issues (#700)
* fix: ManagedChannel shutdown issues ManagedChannel emits a warning when it is marked for finalization; calling close() in the finalize() method is insufficient to remove this warning. Add ManagedBacklogReaderFactory to tie ManagedChannel shutdown to the SDF finalize method * fix: reformat
- Loading branch information
1 parent
2f55375
commit 2d0cbde
Showing
7 changed files
with
156 additions
and
23 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...e-beam-io/src/main/java/com/google/cloud/pubsublite/beam/ManagedBacklogReaderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite.beam; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* A ManagedBacklogReaderFactory produces TopicBacklogReaders and tears down any produced readers | ||
* when it is itself closed. | ||
* | ||
* <p>close() should never be called on produced readers. | ||
*/ | ||
public interface ManagedBacklogReaderFactory extends AutoCloseable, Serializable { | ||
TopicBacklogReader newReader(SubscriptionPartition subscriptionPartition); | ||
|
||
@Override | ||
void close(); | ||
} |
67 changes: 67 additions & 0 deletions
67
...am-io/src/main/java/com/google/cloud/pubsublite/beam/ManagedBacklogReaderFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.pubsublite.beam; | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.pubsublite.Offset; | ||
import com.google.cloud.pubsublite.proto.ComputeMessageStatsResponse; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.concurrent.GuardedBy; | ||
import org.apache.beam.sdk.transforms.SerializableFunction; | ||
|
||
public class ManagedBacklogReaderFactoryImpl implements ManagedBacklogReaderFactory { | ||
private final SerializableFunction<SubscriptionPartition, TopicBacklogReader> newReader; | ||
|
||
@GuardedBy("this") | ||
private final Map<SubscriptionPartition, TopicBacklogReader> readers = new HashMap<>(); | ||
|
||
ManagedBacklogReaderFactoryImpl( | ||
SerializableFunction<SubscriptionPartition, TopicBacklogReader> newReader) { | ||
this.newReader = newReader; | ||
} | ||
|
||
private static final class NonCloseableTopicBacklogReader implements TopicBacklogReader { | ||
private final TopicBacklogReader underlying; | ||
|
||
NonCloseableTopicBacklogReader(TopicBacklogReader underlying) { | ||
this.underlying = underlying; | ||
} | ||
|
||
@Override | ||
public ComputeMessageStatsResponse computeMessageStats(Offset offset) throws ApiException { | ||
return underlying.computeMessageStats(offset); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
throw new IllegalArgumentException( | ||
"Cannot call close() on a reader returned from ManagedBacklogReaderFactory."); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized TopicBacklogReader newReader(SubscriptionPartition subscriptionPartition) { | ||
return new NonCloseableTopicBacklogReader( | ||
readers.computeIfAbsent(subscriptionPartition, newReader::apply)); | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
readers.values().forEach(TopicBacklogReader::close); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters