Skip to content

Commit

Permalink
HIVE-28338: HiveMetaStoreClient connection count is not correct (#5310)…
Browse files Browse the repository at this point in the history
… (Wechar Yu, reviewed by Zhihua Deng, Sai Hemanth Gantasala)
  • Loading branch information
wecharyu committed Jun 29, 2024
1 parent 7f6367e commit d14f303
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ public TTransport getTTransport() {
return transport;
}

@VisibleForTesting
public static AtomicInteger getConnCount() {
return connCount;
}

@Override
public boolean isLocalMetaStore() {
return localMetaStore;
Expand Down Expand Up @@ -1007,17 +1012,10 @@ public void close() {
try {
if (null != client) {
client.shutdown();
if ((transport == null) || !transport.isOpen()) {
final int newCount = connCount.decrementAndGet();
LOG.info("Closed a connection to metastore, current connections: {}",
newCount);
}
}
} catch (TException e) {
LOG.debug("Unable to shutdown metastore client. Will try closing transport directly.", e);
}
// Transport would have got closed via client.shutdown(), so we dont need this, but
// just in case, we make this call.
if ((transport != null) && transport.isOpen()) {
transport.close();
final int newCount = connCount.decrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,15 +616,10 @@ public void close() {
try {
if (null != client) {
client.shutdown();
if ((transport == null) || !transport.isOpen()) {
LOG.info("Closed a connection to metastore, current connections: " + connCount.decrementAndGet());
}
}
} catch (TException e) {
LOG.debug("Unable to shutdown metastore client. Will try closing transport directly.", e);
}
// Transport would have got closed via client.shutdown(), so we dont need this, but
// just in case, we make this call.
if ((transport != null) && transport.isOpen()) {
transport.close();
LOG.info("Closed a connection to metastore, current connections: " + connCount.decrementAndGet());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.hadoop.hive.metastore.client;

import org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.minihms.AbstractMetaStoreService;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Connection related tests for HiveMetaStoreClient.
*/
@RunWith(Parameterized.class)
@Category(MetastoreCheckinTest.class)
public class TestHiveMetaStoreClient extends MetaStoreClientTest {
private boolean isRemote;
private AbstractMetaStoreService metaStore;

public TestHiveMetaStoreClient(String name, AbstractMetaStoreService metaStore) {
this.isRemote = name.equals("Remote");
this.metaStore = metaStore;
}

@After
public void cleanUp() throws Exception {
HiveMetaStoreClient.getConnCount().set(0);
}

@Test
public void testTTransport() throws MetaException {
HiveMetaStoreClient client = metaStore.getClient();
assertTrue(isRemote ? client.getTTransport().isOpen() : client.getTTransport() == null);

client.close();
assertTrue(isRemote ? !client.getTTransport().isOpen() : client.getTTransport() == null);
}

@Test
public void testReconnect() throws MetaException {
HiveMetaStoreClient client = metaStore.getClient();
assertEquals(isRemote ? 1 : 0, HiveMetaStoreClient.getConnCount().get());

try {
client.reconnect();
} catch (MetaException e) {
// expected in local metastore
assertFalse(isRemote);
}
assertEquals(isRemote ? 1 : 0, HiveMetaStoreClient.getConnCount().get());

client.close();
assertEquals(0, HiveMetaStoreClient.getConnCount().get());
}

@Test
public void testCloseClient() throws MetaException {
HiveMetaStoreClient client1 = metaStore.getClient();
assertEquals(isRemote ? 1 : 0, HiveMetaStoreClient.getConnCount().get());

HiveMetaStoreClient client2 = metaStore.getClient();
assertEquals(isRemote ? 2 : 0, HiveMetaStoreClient.getConnCount().get());

client1.close();
assertEquals(isRemote ? 1 : 0, HiveMetaStoreClient.getConnCount().get());

client1.close();
assertEquals(isRemote ? 1 : 0, HiveMetaStoreClient.getConnCount().get());

client2.close();
assertEquals(0, HiveMetaStoreClient.getConnCount().get());
}
}

0 comments on commit d14f303

Please sign in to comment.