Skip to content

Commit bca983c

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

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
@@ -553,15 +553,20 @@ public List<HivePartitionName> refreshTableBackground(String hiveDbName, String
553553
if (lastAccessTimeMap.containsKey(hiveTableName)) {
554554
long lastAccessTime = lastAccessTimeMap.get(hiveTableName);
555555
long intervalSec = (System.currentTimeMillis() - lastAccessTime) / 1000;
556-
if (intervalSec > Config.background_refresh_metadata_time_secs_since_last_access_secs) {
556+
long refreshIntervalSinceLastAccess = Config.background_refresh_metadata_time_secs_since_last_access_secs;
557+
if (refreshIntervalSinceLastAccess >= 0 && intervalSec > refreshIntervalSinceLastAccess) {
558+
// invalidate table cache
559+
invalidateTable(hiveDbName, hiveTblName);
560+
lastAccessTimeMap.remove(hiveTableName);
557561
LOG.info("{}.{} skip refresh because of the last access time is {}", hiveDbName, hiveTblName,
558562
LocalDateTime.ofInstant(Instant.ofEpochMilli(lastAccessTime), ZoneId.systemDefault()));
559563
return null;
560564
}
561565
}
562566

563567
List<HivePartitionName> refreshPartitionNames = refreshTable(hiveDbName, hiveTblName, onlyCachedPartitions);
564-
lastAccessTimeMap.keySet().removeIf(tableName -> !getCachedTableNames().contains(tableName));
568+
Set<HiveTableName> cachedTableNames = getCachedTableNames();
569+
lastAccessTimeMap.keySet().removeIf(tableName -> !(cachedTableNames.contains(tableName)));
565570
return refreshPartitionNames;
566571
}
567572

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
@@ -21,6 +21,7 @@
2121
import com.starrocks.catalog.HiveTable;
2222
import com.starrocks.catalog.PrimitiveType;
2323
import com.starrocks.catalog.ScalarType;
24+
import com.starrocks.common.Config;
2425
import com.starrocks.connector.MetastoreType;
2526
import com.starrocks.connector.PartitionUtil;
2627
import com.starrocks.connector.exception.StarRocksConnectorException;
@@ -44,7 +45,7 @@ public class CachingHiveMetastoreTest {
4445
private HiveMetaClient client;
4546
private HiveMetastore metastore;
4647
private ExecutorService executor;
47-
private long expireAfterWriteSec = 10;
48+
private long expireAfterWriteSec = 30;
4849
private long refreshAfterWriteSec = -1;
4950

5051
@Before
@@ -166,6 +167,49 @@ public void testRefreshTableSync() {
166167
Assert.assertEquals(1, cachingHiveMetastore.tableNameLockMap.size());
167168
}
168169

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

0 commit comments

Comments
 (0)