advancedxy commented on code in PR #1838: URL: https://github.com/apache/incubator-uniffle/pull/1838#discussion_r1689561209
########## common/src/main/java/org/apache/uniffle/common/util/CloseStateful.java: ########## @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Closeable; + +/** CloseStateful is an interface that utilizes the ExpiringCloseableSupplier delegate. */ +public interface CloseStateful extends Closeable { Review Comment: nit: let's call it `StatefulCloseable`? ########## common/src/test/java/org/apache/uniffle/common/util/ExpiringCloseableSupplierTest.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; + +import com.google.common.util.concurrent.Uninterruptibles; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ExpiringCloseableSupplierTest { + + @Test + void testCacheable() { + Supplier<MockClient> cf = () -> new MockClient(false); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + + MockClient mockClient = mockClientSupplier.get(); + MockClient mockClient2 = mockClientSupplier.get(); + assertSame(mockClient, mockClient2); + mockClientSupplier.close(); + mockClientSupplier.close(); + } + + @Test + void testAutoCloseable() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = + new ExpiringCloseableSupplier<>(cf, 10); + MockClient mockClient1 = mockClientSupplier.get(); + assertNotNull(mockClient1); + Uninterruptibles.sleepUninterruptibly(30, TimeUnit.MILLISECONDS); + assertTrue(mockClient1.isClosed()); + MockClient mockClient2 = mockClientSupplier.get(); + assertNotSame(mockClient1, mockClient2); + mockClientSupplier.close(); + } + + @Test + void testRenew() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + MockClient mockClient = mockClientSupplier.get(); + mockClientSupplier.close(); + MockClient mockClient2 = mockClientSupplier.get(); + assertNotSame(mockClient, mockClient2); + } + + @Test + void testReClose() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + mockClientSupplier.get(); + mockClientSupplier.close(); + mockClientSupplier.close(); + } + + @Test + void testDelegateExtendClose() throws IOException { Review Comment: Thanks for this test code, looking good. ########## common/src/main/java/org/apache/uniffle/common/util/ExpiringCloseableSupplier.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Serializable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Supplier for T cacheable and autocloseable with delay By using ExpiringCloseableSupplier to + * obtain an object, manual closure may not be necessary. + */ +public class ExpiringCloseableSupplier<T extends CloseStateful> + implements Supplier<T>, Serializable { + private static final long serialVersionUID = 0; Review Comment: nit: let's use another more meaningful UID? ########## client-spark/common/src/main/java/org/apache/uniffle/shuffle/manager/RssShuffleManagerBase.java: ########## @@ -606,25 +609,28 @@ protected synchronized MutableShuffleHandleInfo getRemoteShuffleHandleInfoWithBl RssPartitionToShuffleServerRequest rssPartitionToShuffleServerRequest = new RssPartitionToShuffleServerRequest(shuffleId); RssReassignOnBlockSendFailureResponse rpcPartitionToShufflerServer = - getOrCreateShuffleManagerClient() + getOrCreateShuffleManagerClientWrapper() + .get() .getPartitionToShufflerServerWithBlockRetry(rssPartitionToShuffleServerRequest); MutableShuffleHandleInfo shuffleHandleInfo = MutableShuffleHandleInfo.fromProto(rpcPartitionToShufflerServer.getHandle()); return shuffleHandleInfo; } - // todo: automatic close client when the client is idle to avoid too much connections for spark - // driver. - protected ShuffleManagerClient getOrCreateShuffleManagerClient() { - if (shuffleManagerClient == null) { + protected synchronized ExpireCloseableSupplier<ShuffleManagerClient> + getOrCreateShuffleManagerClientWrapper() { + if (managerClientSupplier == null) { RssConf rssConf = RssSparkConfig.toRssConf(sparkConf); String driver = rssConf.getString("driver.host", ""); int port = rssConf.get(RssClientConf.SHUFFLE_MANAGER_GRPC_PORT); - this.shuffleManagerClient = - ShuffleManagerClientFactory.getInstance() - .createShuffleManagerClient(ClientType.GRPC, driver, port); + long rpcTimeout = rssConf.getLong(RssBaseConf.RSS_CLIENT_TYPE_GRPC_TIMEOUT_MS); Review Comment: I see. ########## common/src/main/java/org/apache/uniffle/common/util/ExpiringCloseableSupplier.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Serializable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Supplier for T cacheable and autocloseable with delay By using ExpiringCloseableSupplier to + * obtain an object, manual closure may not be necessary. + */ +public class ExpiringCloseableSupplier<T extends CloseStateful> + implements Supplier<T>, Serializable { + private static final long serialVersionUID = 0; + private static final Logger LOG = LoggerFactory.getLogger(ExpiringCloseableSupplier.class); + private static final int DEFAULT_DELAY_CLOSE_INTERVAL = 60000; + private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + private ScheduledFuture<?> future; + private volatile T t; + private final Supplier<T> delegate; + private transient volatile long freshTime; + private final long delayCloseInterval; + + public ExpiringCloseableSupplier(Supplier<T> delegate) { + this(delegate, DEFAULT_DELAY_CLOSE_INTERVAL); + } + + public ExpiringCloseableSupplier(Supplier<T> delegate, long delayCloseInterval) { + this.delegate = delegate; + this.delayCloseInterval = delayCloseInterval; + } + + public synchronized T get() { + freshTime = System.currentTimeMillis(); + if (t == null || t.isClosed()) { + t = delegate.get(); + startDelayCloseScheduler(); + } + return t; + } + + public synchronized void close() { + try { + if (t != null && !t.isClosed()) { + t.close(); + } + } catch (Exception e) { + LOG.warn("Failed to close {} the resource", t.getClass().getName(), e); + } finally { + t = null; + freshTime = 0; Review Comment: hmm, don't we need to reset the freshTime to current time millis? ########## common/src/main/java/org/apache/uniffle/common/util/ExpiringCloseableSupplier.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Serializable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Supplier for T cacheable and autocloseable with delay By using ExpiringCloseableSupplier to + * obtain an object, manual closure may not be necessary. + */ +public class ExpiringCloseableSupplier<T extends CloseStateful> + implements Supplier<T>, Serializable { + private static final long serialVersionUID = 0; + private static final Logger LOG = LoggerFactory.getLogger(ExpiringCloseableSupplier.class); + private static final int DEFAULT_DELAY_CLOSE_INTERVAL = 60000; Review Comment: hmm, this doesn't seem right. I filed a PR against your branch, please take a look https://github.com/xumanbu/incubator-uniffle/pull/1 ########## common/src/main/java/org/apache/uniffle/common/util/ExpiringCloseableSupplier.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Serializable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Supplier for T cacheable and autocloseable with delay By using ExpiringCloseableSupplier to + * obtain an object, manual closure may not be necessary. + */ +public class ExpiringCloseableSupplier<T extends CloseStateful> + implements Supplier<T>, Serializable { + private static final long serialVersionUID = 0; + private static final Logger LOG = LoggerFactory.getLogger(ExpiringCloseableSupplier.class); + private static final int DEFAULT_DELAY_CLOSE_INTERVAL = 60000; + private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + private ScheduledFuture<?> future; + private volatile T t; + private final Supplier<T> delegate; + private transient volatile long freshTime; + private final long delayCloseInterval; + + public ExpiringCloseableSupplier(Supplier<T> delegate) { + this(delegate, DEFAULT_DELAY_CLOSE_INTERVAL); + } + + public ExpiringCloseableSupplier(Supplier<T> delegate, long delayCloseInterval) { Review Comment: nit: these constructors are usually hided by a static factory method. ########## common/src/test/java/org/apache/uniffle/common/util/ExpiringCloseableSupplierTest.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; + +import com.google.common.util.concurrent.Uninterruptibles; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ExpiringCloseableSupplierTest { + + @Test + void testCacheable() { + Supplier<MockClient> cf = () -> new MockClient(false); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + + MockClient mockClient = mockClientSupplier.get(); + MockClient mockClient2 = mockClientSupplier.get(); + assertSame(mockClient, mockClient2); + mockClientSupplier.close(); + mockClientSupplier.close(); + } + + @Test + void testAutoCloseable() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = + new ExpiringCloseableSupplier<>(cf, 10); + MockClient mockClient1 = mockClientSupplier.get(); + assertNotNull(mockClient1); + Uninterruptibles.sleepUninterruptibly(30, TimeUnit.MILLISECONDS); + assertTrue(mockClient1.isClosed()); + MockClient mockClient2 = mockClientSupplier.get(); + assertNotSame(mockClient1, mockClient2); + mockClientSupplier.close(); + } + + @Test + void testRenew() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + MockClient mockClient = mockClientSupplier.get(); + mockClientSupplier.close(); + MockClient mockClient2 = mockClientSupplier.get(); + assertNotSame(mockClient, mockClient2); + } + + @Test + void testReClose() { + Supplier<MockClient> cf = () -> new MockClient(true); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + mockClientSupplier.get(); + mockClientSupplier.close(); + mockClientSupplier.close(); + } + + @Test + void testDelegateExtendClose() throws IOException { + Supplier<MockClient> cf = () -> new MockClient(false); + ExpiringCloseableSupplier<MockClient> mockClientSupplier = new ExpiringCloseableSupplier<>(cf); + MockClient mockClient = mockClientSupplier.get(); + mockClient.close(); + assertTrue(mockClient.isClosed()); + + MockClient mockClient1 = mockClientSupplier.get(); + assertNotSame(mockClient, mockClient1); + MockClient mockClient2 = mockClientSupplier.get(); + assertSame(mockClient1, mockClient2); + mockClientSupplier.close(); + } + + static class MockClient implements CloseStateful { Review Comment: nit: private static class. ########## common/src/main/java/org/apache/uniffle/common/util/ExpiringCloseableSupplier.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.uniffle.common.util; + +import java.io.Serializable; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Supplier for T cacheable and autocloseable with delay By using ExpiringCloseableSupplier to + * obtain an object, manual closure may not be necessary. + */ +public class ExpiringCloseableSupplier<T extends CloseStateful> + implements Supplier<T>, Serializable { + private static final long serialVersionUID = 0; Review Comment: Never mind, it looks like guava also uses 0. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
