Skip to content

Commit 0ea8dce

Browse files
authored
[da-vinci] Do not throw exception when trying to close a closed/not-ready DVC client (#1289)
1 parent 4cf1d0a commit 0ea8dce

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

clients/da-vinci-client/src/main/java/com/linkedin/davinci/client/AvroGenericDaVinciClient.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,24 +842,38 @@ public synchronized void start() {
842842

843843
@Override
844844
public synchronized void close() {
845-
throwIfNotReady();
845+
if (isReady()) {
846+
closeInner();
847+
} else {
848+
getClientLogger()
849+
.warn("Client is not ready or already closed, will ignore close request, storeName=" + getStoreName());
850+
}
851+
}
852+
853+
@Override
854+
public String toString() {
855+
return this.getClass().getSimpleName();
856+
}
857+
858+
// Visible for testing
859+
void closeInner() {
846860
try {
847861
logger.info("Closing client, storeName=" + getStoreName());
848862
ready.set(false);
849863
if (cacheBackend != null) {
850864
cacheBackend.close();
851865
}
852866
daVinciBackend.release();
853-
logger.info("Client is closed successfully, storeName=" + getStoreName());
867+
logger.info("Client is closed successfully, storeName={}", getStoreName());
854868
} catch (Throwable e) {
855869
String msg = "Unable to close Da Vinci client, storeName=" + getStoreName();
856870
logger.error(msg, e);
857871
throw new VeniceClientException(msg, e);
858872
}
859873
}
860874

861-
@Override
862-
public String toString() {
863-
return this.getClass().getSimpleName();
875+
Logger getClientLogger() {
876+
return logger;
864877
}
878+
865879
}

clients/da-vinci-client/src/test/java/com/linkedin/davinci/client/AvroGenericDaVinciClientTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import static org.mockito.ArgumentMatchers.any;
55
import static org.mockito.ArgumentMatchers.anyInt;
66
import static org.mockito.Mockito.anyString;
7+
import static org.mockito.Mockito.doCallRealMethod;
78
import static org.mockito.Mockito.doNothing;
89
import static org.mockito.Mockito.doReturn;
910
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.never;
1012
import static org.mockito.Mockito.spy;
13+
import static org.mockito.Mockito.times;
14+
import static org.mockito.Mockito.verify;
1115
import static org.mockito.Mockito.when;
1216
import static org.testng.Assert.assertEquals;
1317
import static org.testng.Assert.assertThrows;
@@ -42,6 +46,7 @@
4246
import java.util.concurrent.ExecutionException;
4347
import java.util.concurrent.Executor;
4448
import org.apache.avro.Schema;
49+
import org.apache.logging.log4j.LogManager;
4550
import org.testng.Assert;
4651
import org.testng.annotations.Test;
4752

@@ -230,5 +235,20 @@ public void constructorTest() {
230235
readChunkExecutor);
231236
assertEquals(daVinciClient.getReadChunkExecutorForLargeRequest(), readChunkExecutor);
232237

238+
// Close a not-ready client won't throw exception.
239+
daVinciClient.close();
240+
}
241+
242+
@Test
243+
public void closeTest() {
244+
AvroGenericDaVinciClient client = mock(AvroGenericDaVinciClient.class);
245+
doCallRealMethod().when(client).close();
246+
doReturn(LogManager.getLogger(AvroGenericDaVinciClient.class)).when(client).getClientLogger();
247+
doReturn(false).when(client).isReady();
248+
client.close();
249+
verify(client, never()).closeInner();
250+
doReturn(true).when(client).isReady();
251+
client.close();
252+
verify(client, times(1)).closeInner();
233253
}
234254
}

internal/venice-test-common/src/integrationTest/java/com/linkedin/venice/endToEnd/DaVinciClientTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,8 @@ public void testHybridStore() throws Exception {
11061106

11071107
// Verify that closed cached client can be restarted.
11081108
client.close();
1109+
// Verify that 2nd close call on the same store won't throw exception.
1110+
client.close();
11091111
DaVinciClient<Integer, Integer> client1 = factory.getAndStartGenericAvroClient(storeName, new DaVinciConfig());
11101112
assertEquals((int) client1.get(1).get(), 1);
11111113

0 commit comments

Comments
 (0)