Skip to content

Commit d24c364

Browse files
authored
[Enhancement] invalidate table cache after hive table do not need to refresh by ConnectorTableMetadataProcessor (#37549)
Signed-off-by: Youngwb <[email protected]>
1 parent dd511b4 commit d24c364

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

fe/fe-core/src/main/java/com/starrocks/connector/hive/CachingHiveMetastore.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,20 @@ public List<HivePartitionName> refreshTableBackground(String hiveDbName, String
556556
if (lastAccessTimeMap.containsKey(hiveTableName)) {
557557
long lastAccessTime = lastAccessTimeMap.get(hiveTableName);
558558
long intervalSec = (System.currentTimeMillis() - lastAccessTime) / 1000;
559-
if (intervalSec > Config.background_refresh_metadata_time_secs_since_last_access_secs) {
559+
long refreshIntervalSinceLastAccess = Config.background_refresh_metadata_time_secs_since_last_access_secs;
560+
if (refreshIntervalSinceLastAccess >= 0 && intervalSec > refreshIntervalSinceLastAccess) {
561+
// invalidate table cache
562+
invalidateTable(hiveDbName, hiveTblName);
563+
lastAccessTimeMap.remove(hiveTableName);
560564
LOG.info("{}.{} skip refresh because of the last access time is {}", hiveDbName, hiveTblName,
561565
LocalDateTime.ofInstant(Instant.ofEpochMilli(lastAccessTime), ZoneId.systemDefault()));
562566
return null;
563567
}
564568
}
565569

566570
List<HivePartitionName> refreshPartitionNames = refreshTable(hiveDbName, hiveTblName, onlyCachedPartitions);
567-
lastAccessTimeMap.keySet().removeIf(tableName -> !getCachedTableNames().contains(tableName));
571+
Set<HiveTableName> cachedTableNames = getCachedTableNames();
572+
lastAccessTimeMap.keySet().removeIf(tableName -> !(cachedTableNames.contains(tableName)));
568573
return refreshPartitionNames;
569574
}
570575

fe/fe-core/src/test/java/com/starrocks/connector/hive/CachingHiveMetastoreTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.starrocks.catalog.PrimitiveType;
2424
import com.starrocks.catalog.ScalarType;
2525
import com.starrocks.catalog.Type;
26+
import com.starrocks.common.Config;
2627
import com.starrocks.connector.MetastoreType;
2728
import com.starrocks.connector.PartitionUtil;
2829
import com.starrocks.connector.exception.StarRocksConnectorException;
@@ -47,7 +48,7 @@ public class CachingHiveMetastoreTest {
4748
private HiveMetaClient client;
4849
private HiveMetastore metastore;
4950
private ExecutorService executor;
50-
private long expireAfterWriteSec = 10;
51+
private long expireAfterWriteSec = 30;
5152
private long refreshAfterWriteSec = -1;
5253

5354
@Before
@@ -169,6 +170,49 @@ public void testRefreshTableSync() {
169170
Assert.assertEquals(1, cachingHiveMetastore.tableNameLockMap.size());
170171
}
171172

173+
@Test
174+
public void testRefreshTableBackground() throws InterruptedException {
175+
CachingHiveMetastore cachingHiveMetastore = new CachingHiveMetastore(
176+
metastore, executor, expireAfterWriteSec, refreshAfterWriteSec, 1000, false);
177+
Assert.assertFalse(cachingHiveMetastore.tableNameLockMap.containsKey(
178+
HiveTableName.of("db1", "tbl1")));
179+
try {
180+
// mock query table tbl1
181+
List<String> partitionNames = cachingHiveMetastore.getPartitionKeysByValue("db1", "tbl1",
182+
HivePartitionValue.ALL_PARTITION_VALUES);
183+
cachingHiveMetastore.getPartitionsByNames("db1",
184+
"tbl1", partitionNames);
185+
// put table tbl1 in table cache
186+
cachingHiveMetastore.refreshTable("db1", "tbl1", true);
187+
} catch (Exception e) {
188+
Assert.fail();
189+
}
190+
Assert.assertTrue(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));
191+
192+
try {
193+
cachingHiveMetastore.refreshTableBackground("db1", "tbl1", true);
194+
} catch (Exception e) {
195+
Assert.fail();
196+
}
197+
// not skip refresh table, table cache still exist
198+
Assert.assertTrue(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));
199+
// sleep 1s, background refresh table will be skipped
200+
Thread.sleep(1000);
201+
long oldValue = Config.background_refresh_metadata_time_secs_since_last_access_secs;
202+
// not refresh table, just skip refresh table
203+
Config.background_refresh_metadata_time_secs_since_last_access_secs = 0;
204+
205+
try {
206+
cachingHiveMetastore.refreshTableBackground("db1", "tbl1", true);
207+
} catch (Exception e) {
208+
Assert.fail();
209+
} finally {
210+
Config.background_refresh_metadata_time_secs_since_last_access_secs = oldValue;
211+
}
212+
// table cache will be removed because of skip refresh table
213+
Assert.assertFalse(cachingHiveMetastore.isTablePresent(HiveTableName.of("db1", "tbl1")));
214+
}
215+
172216
@Test
173217
public void testRefreshHiveView() {
174218
CachingHiveMetastore cachingHiveMetastore = new CachingHiveMetastore(

0 commit comments

Comments
 (0)