Skip to content

Commit 04fa4af

Browse files
committed
[fix](fe) Keep external database cache identities locale independent
### What problem does this PR solve? Issue Number: None Related PR: #68196 Problem Summary: Database discovery and HMS CREATE events fold mode-1 names with Locale.ROOT, but lookup and DROP used the process default locale. On a Turkish-locale FE, remote I was cached as i while later lookup and DROP sought dotless ı, leaving the dropped object visible. Use one locale-independent fold for database discovery, event IDs, lookup, routing, and Paimon SDK invalidation. Separately, catalog-wide row-count generation state was retained after permanent DROP even when no count was loaded. Avoid allocating unused generations and release a dropped catalog generation without letting an outstanding old read recreate it. ### Release note External database name lookup and cache invalidation are consistent across FE locale settings. ### Check List (For Author) - Test: Unit Test: four related FE classes, 164 tests passed; full FE build and Checkstyle passed - Behavior changed: Yes, external database case folding is locale independent and permanent catalog drop releases row-count generation state - Does this need documentation: No
1 parent 0c37f51 commit 04fa4af

8 files changed

Lines changed: 173 additions & 29 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ private void buildMetaCache() {
429429
Math.max(Config.max_meta_object_cache_num, 1),
430430
ignored -> getFilteredDatabaseNames(),
431431
this::updateLowerCaseToDatabaseName,
432-
(remoteName, localName) -> lowerCaseToDatabaseName.put(remoteName.toLowerCase(), remoteName),
433-
localName -> lowerCaseToDatabaseName.remove(localName.toLowerCase()),
432+
(remoteName, localName) -> lowerCaseToDatabaseName.put(foldDatabaseName(remoteName), remoteName),
433+
localName -> lowerCaseToDatabaseName.remove(foldDatabaseName(localName)),
434434
localDbName -> Optional.ofNullable(
435435
buildDbForInit(null, localDbName, Util.genIdByName(name, localDbName), logType,
436436
true)),
@@ -595,7 +595,7 @@ dbName, includeDatabaseMap, excludeDatabaseMap, getLowerCaseDatabaseNames() != 0
595595

596596
// Collect lowercased local names and their remote counterparts
597597
for (Pair<String, String> pair : remoteToLocalPairs) {
598-
String lowerCaseLocalName = pair.second.toLowerCase();
598+
String lowerCaseLocalName = foldDatabaseName(pair.second);
599599
lowerCaseToRemoteNames.computeIfAbsent(lowerCaseLocalName, k -> Lists.newArrayList()).add(pair.first);
600600
}
601601

@@ -623,12 +623,16 @@ protected final String localDatabaseNameFromRemote(String remoteDbName) {
623623
String localDbName = fromRemoteDatabaseName(remoteDbName);
624624
int mode = getLowerCaseDatabaseNames();
625625
if (mode == 1) {
626-
return localDbName.toLowerCase(Locale.ROOT);
626+
return foldDatabaseName(localDbName);
627627
}
628628
// Mode 2 preserves remote spelling for display and deterministic database IDs.
629629
return mode == 2 ? remoteDbName : localDbName;
630630
}
631631

632+
private static String foldDatabaseName(String dbName) {
633+
return dbName.toLowerCase(Locale.ROOT);
634+
}
635+
632636
protected boolean isDatabaseAllowedByFilter(String dbName) {
633637
return isDatabaseAllowedByFilter(dbName, getIncludeDatabaseMap(), getExcludeDatabaseMap(),
634638
getLowerCaseDatabaseNames() != 0);
@@ -650,14 +654,14 @@ private boolean containsDatabaseName(Map<String, Boolean> databaseMap, String db
650654
if (!ignoreCase) {
651655
return databaseMap.containsKey(dbName);
652656
}
653-
String normalizedDbName = dbName.toLowerCase(Locale.ROOT);
657+
String normalizedDbName = foldDatabaseName(dbName);
654658
return databaseMap.keySet().stream()
655-
.anyMatch(configuredName -> configuredName.toLowerCase(Locale.ROOT).equals(normalizedDbName));
659+
.anyMatch(configuredName -> foldDatabaseName(configuredName).equals(normalizedDbName));
656660
}
657661

658662
private void updateLowerCaseToDatabaseName(List<Pair<String, String>> names) {
659663
Map<String, String> updated = Maps.newConcurrentMap();
660-
names.forEach(pair -> updated.put(pair.key().toLowerCase(), pair.key()));
664+
names.forEach(pair -> updated.put(foldDatabaseName(pair.key()), pair.key()));
661665
lowerCaseToDatabaseName = updated;
662666
}
663667

@@ -1542,14 +1546,14 @@ private String getLocalDatabaseName(String dbName, boolean isReplay) {
15421546

15431547
if (mode == 1) {
15441548
// Mode 1: Store as lowercase
1545-
finalName = dbName.toLowerCase();
1549+
finalName = foldDatabaseName(dbName);
15461550
} else if (mode == 2) {
15471551
// Mode 2: Case-insensitive comparison
1548-
finalName = lowerCaseToDatabaseName.get(dbName.toLowerCase());
1552+
finalName = lowerCaseToDatabaseName.get(foldDatabaseName(dbName));
15491553
if (finalName == null && !isReplay) {
15501554
try {
15511555
metaCache.refreshNames();
1552-
finalName = lowerCaseToDatabaseName.get(dbName.toLowerCase());
1556+
finalName = lowerCaseToDatabaseName.get(foldDatabaseName(dbName));
15531557
} catch (Exception e) {
15541558
if (Thread.currentThread().isInterrupted()
15551559
&& e instanceof java.util.concurrent.CompletionException

‎fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalMetaCacheMgr.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,13 @@ public void removeCatalogPermanently(long catalogId) {
468468
}
469469
}
470470
} finally {
471-
lifecycleLock.unlock();
471+
// DROP never reuses the catalog ID. The earlier row-count fence protects readers;
472+
// release its generation after permanent engine cleanup, not on rename.
473+
try {
474+
rowCountCache.releaseCatalog(catalogId);
475+
} finally {
476+
lifecycleLock.unlock();
477+
}
472478
}
473479
}
474480

‎fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalRowCountCache.java‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ private long currentCatalogGeneration(long catalogId) {
243243
ignored -> new AtomicLong(nextCatalogGeneration.incrementAndGet())).get();
244244
}
245245

246+
private boolean isCatalogGenerationCurrent(long catalogId, long generation) {
247+
AtomicLong current = catalogGenerations.get(catalogId);
248+
return current != null && current.get() == generation;
249+
}
250+
251+
int getCatalogGenerationCountForTest() {
252+
return catalogGenerations.size();
253+
}
254+
246255
static Optional<Long> loadRowCount(RowCountKey rowCountKey, boolean fillMetaCache) {
247256
try {
248257
ExternalTable table = (ExternalTable) StatisticsUtil.findTable(
@@ -294,12 +303,12 @@ public long getCachedRowCount(long catalogId, long dbId, long tableId, boolean f
294303
if (ConnectContext.get() == null
295304
|| ConnectContext.get().getSessionVariable().fetchHiveRowCountSync) {
296305
Optional<Long> value = f.get();
297-
return generation == currentCatalogGeneration(catalogId)
306+
return isCatalogGenerationCurrent(catalogId, generation)
298307
? value.orElse(TableIf.UNKNOWN_ROW_COUNT) : TableIf.UNKNOWN_ROW_COUNT;
299308
} else {
300309
if (f.isDone()) {
301310
Optional<Long> value = f.get();
302-
return generation == currentCatalogGeneration(catalogId)
311+
return isCatalogGenerationCurrent(catalogId, generation)
303312
? value.orElse(TableIf.UNKNOWN_ROW_COUNT) : TableIf.UNKNOWN_ROW_COUNT;
304313
}
305314
LOG.info("Row count for table {}.{}.{} is still processing.", catalogId, dbId, tableId);
@@ -321,15 +330,19 @@ public long getCachedRowCountIfPresent(long catalogId, long dbId, long tableId)
321330
long generation;
322331
publicationLock.readLock().lock();
323332
try {
324-
generation = currentCatalogGeneration(catalogId);
333+
AtomicLong current = catalogGenerations.get(catalogId);
334+
if (current == null) {
335+
return -1;
336+
}
337+
generation = current.get();
325338
RowCountKey key = new RowCountKey(catalogId, dbId, tableId, generation);
326339
f = rowCountCache.getIfPresent(key);
327340
} finally {
328341
publicationLock.readLock().unlock();
329342
}
330343
if (f == null) {
331344
return -1;
332-
} else if (f.isDone() && generation == currentCatalogGeneration(catalogId)) {
345+
} else if (f.isDone() && isCatalogGenerationCurrent(catalogId, generation)) {
333346
return f.get().orElse(-1L);
334347
}
335348
} catch (Exception e) {
@@ -343,8 +356,20 @@ public long getCachedRowCountIfPresent(long catalogId, long dbId, long tableId)
343356
void invalidateCatalog(long catalogId) {
344357
publicationLock.writeLock().lock();
345358
try {
346-
catalogGenerations.computeIfAbsent(catalogId, ignored -> new AtomicLong())
347-
.set(nextCatalogGeneration.incrementAndGet());
359+
AtomicLong current = catalogGenerations.get(catalogId);
360+
if (current != null) {
361+
current.set(nextCatalogGeneration.incrementAndGet());
362+
}
363+
} finally {
364+
publicationLock.writeLock().unlock();
365+
}
366+
}
367+
368+
/** Catalog IDs are never reused after DROP; old readers still fail the generation check. */
369+
void releaseCatalog(long catalogId) {
370+
publicationLock.writeLock().lock();
371+
try {
372+
catalogGenerations.remove(catalogId);
348373
} finally {
349374
publicationLock.writeLock().unlock();
350375
}
@@ -367,7 +392,11 @@ void invalidateDb(long catalogId, long dbId) {
367392
void invalidateTable(long catalogId, long dbId, long tableId) {
368393
publicationLock.writeLock().lock();
369394
try {
370-
RowCountKey key = new RowCountKey(catalogId, dbId, tableId, currentCatalogGeneration(catalogId));
395+
AtomicLong current = catalogGenerations.get(catalogId);
396+
if (current == null) {
397+
return;
398+
}
399+
RowCountKey key = new RowCountKey(catalogId, dbId, tableId, current.get());
371400
Set<LoadFence> fences = inFlightLoads.get(new LoadKey(key));
372401
if (fences != null) {
373402
fences.forEach(fence -> fence.invalidated = true);

‎fe/fe-core/src/main/java/org/apache/doris/datasource/paimon/PaimonExternalCatalog.java‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,9 @@ public synchronized void invalidatePaimonDatabaseByLocalName(String localDbName)
179179
}
180180
withSdkCatalogCacheWriteLock(() -> executionAuthenticator.execute(() -> {
181181
boolean caseSensitive = catalog.caseSensitive();
182-
int nameMode = getLowerCaseDatabaseNames();
183182
invalidateCachedPaimonTables(identifier -> {
184183
String remoteName = identifier.getDatabaseName();
185-
String candidateLocalName = nameMode == 2 ? remoteName : fromRemoteDatabaseName(remoteName);
186-
if (nameMode == 1) {
187-
// Match ExternalCatalog's mode-1 local database name and deterministic ID.
188-
candidateLocalName = candidateLocalName.toLowerCase();
189-
}
184+
String candidateLocalName = localDatabaseNameFromRemote(remoteName);
190185
return identifierPartEquals(candidateLocalName, localDbName, caseSensitive);
191186
});
192187
return null;

‎fe/fe-core/src/test/java/org/apache/doris/datasource/CatalogMgrTest.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.Collections;
5555
import java.util.HashMap;
5656
import java.util.List;
57+
import java.util.Locale;
5758
import java.util.Map;
5859
import java.util.Optional;
5960
import java.util.OptionalLong;
@@ -165,6 +166,65 @@ void testModeOneDatabaseEventCreateAndRenameUseDiscoveryIdentity() throws Except
165166
Mockito.verify(cacheMgr).invalidateRowCountCache(catalogId, barId);
166167
}
167168

169+
@Test
170+
void testModeOneDatabaseEventLookupAndDropUseRootLocale() throws Exception {
171+
Locale previousLocale = Locale.getDefault();
172+
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
173+
try {
174+
long catalogId = 88L;
175+
long dbId = Util.genIdByName("hms", "i");
176+
TestingHmsEventCatalog catalog = new TestingHmsEventCatalog(catalogId,
177+
ImmutableMap.of(ExternalCatalog.LOWER_CASE_DATABASE_NAMES, "1"));
178+
CatalogMgr catalogMgr = new CatalogMgr();
179+
addNamedCatalog(catalogMgr, catalog);
180+
ExternalMetaCacheMgr cacheMgr = Mockito.mock(ExternalMetaCacheMgr.class);
181+
Env env = Mockito.mock(Env.class);
182+
Mockito.when(env.getExtMetaCacheMgr()).thenReturn(cacheMgr);
183+
Mockito.when(catalog.metaCache.updateCache(Mockito.anyString(), Mockito.anyString(),
184+
Mockito.any(), Mockito.anyLong(), Mockito.anyLong())).thenReturn(true);
185+
@SuppressWarnings("unchecked")
186+
ExternalDatabase<? extends ExternalTable> db = Mockito.mock(ExternalDatabase.class);
187+
Mockito.when(catalog.metaCache.getMetaObj("i", dbId)).thenReturn(Optional.of(db));
188+
189+
try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class)) {
190+
mockedEnv.when(Env::getCurrentEnv).thenReturn(env);
191+
catalogMgr.registerExternalDatabaseFromEvent("I", "hms");
192+
Assertions.assertSame(db, catalog.getDbNullable("I"));
193+
catalogMgr.unregisterExternalDatabase("I", "hms");
194+
}
195+
196+
Mockito.verify(catalog.metaCache).updateCache(Mockito.eq("I"), Mockito.eq("i"),
197+
Mockito.any(), Mockito.eq(dbId), Mockito.anyLong());
198+
Mockito.verify(catalog.metaCache).invalidate("i", dbId);
199+
Mockito.verify(cacheMgr).invalidateRowCountCache(catalogId, dbId);
200+
} finally {
201+
Locale.setDefault(previousLocale);
202+
}
203+
}
204+
205+
@Test
206+
void testModeTwoDatabaseLookupUsesRootLocale() throws Exception {
207+
Locale previousLocale = Locale.getDefault();
208+
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
209+
try {
210+
TestingUnregisterCatalog catalog = new TestingUnregisterCatalog(89L,
211+
ImmutableMap.of(ExternalCatalog.LOWER_CASE_DATABASE_NAMES, "2"));
212+
@SuppressWarnings("unchecked")
213+
MetaCache<ExternalDatabase<? extends ExternalTable>> metaCache = Mockito.mock(MetaCache.class);
214+
catalog.installMetaCache(metaCache);
215+
Field mappingField = ExternalCatalog.class.getDeclaredField("lowerCaseToDatabaseName");
216+
mappingField.setAccessible(true);
217+
@SuppressWarnings("unchecked")
218+
Map<String, String> mapping = (Map<String, String>) mappingField.get(catalog);
219+
mapping.put("i", "I");
220+
221+
Assertions.assertEquals(Pair.of("I", Util.genIdByName("testing_catalog", "I")),
222+
catalog.getDbIdentityForReplay("I", 0L).orElseThrow(AssertionError::new));
223+
} finally {
224+
Locale.setDefault(previousLocale);
225+
}
226+
}
227+
168228
private static void addCatalog(CatalogMgr catalogMgr, ExternalCatalog catalog) throws Exception {
169229
Field idToCatalogField = CatalogMgr.class.getDeclaredField("idToCatalog");
170230
idToCatalogField.setAccessible(true);

‎fe/fe-core/src/test/java/org/apache/doris/datasource/ExternalMetaCacheRouteResolverTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,21 @@ public void testOperationalContextChangeFencesRowCountForEvictedDatabases() {
511511
Mockito.verify(rowCountCache).invalidateCatalog(catalogId);
512512
}
513513

514+
@Test
515+
public void testPermanentCatalogRemovalReleasesRowCountGenerationButRenameDoesNot() {
516+
long catalogId = 29L;
517+
HMSExternalCatalog catalog = Mockito.mock(HMSExternalCatalog.class);
518+
mockCurrentCatalog(catalogId, catalog);
519+
ExternalMetaCacheMgr metaCacheMgr = new ExternalMetaCacheMgr(true);
520+
ExternalRowCountCache rowCountCache = Mockito.mock(ExternalRowCountCache.class);
521+
metaCacheMgr.replaceRowCountCacheForTest(rowCountCache);
522+
523+
metaCacheMgr.removeCatalog(catalogId);
524+
Mockito.verify(rowCountCache, Mockito.never()).releaseCatalog(catalogId);
525+
metaCacheMgr.removeCatalogPermanently(catalogId);
526+
Mockito.verify(rowCountCache).releaseCatalog(catalogId);
527+
}
528+
514529
@Test
515530
public void testRollbackRetiresEngineCachesWhenResetCleanupThrows() throws Exception {
516531
ExternalMetaCacheMgr metaCacheMgr = new ExternalMetaCacheMgr(true);

‎fe/fe-core/src/test/java/org/apache/doris/datasource/ExternalRowCountCacheTest.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,43 @@ protected Optional<Long> doLoad(ExternalRowCountCache.RowCountKey key) {
333333
}
334334
}
335335

336+
@Test
337+
public void testPermanentCatalogRemovalReleasesGenerationWithoutRevivingOldReader() throws Exception {
338+
CountDownLatch loadStarted = new CountDownLatch(1);
339+
CountDownLatch releaseLoad = new CountDownLatch(1);
340+
ExternalRowCountCache.RowCountCacheLoader loader = new ExternalRowCountCache.RowCountCacheLoader() {
341+
@Override
342+
protected Optional<Long> doLoad(ExternalRowCountCache.RowCountKey key) {
343+
loadStarted.countDown();
344+
Uninterruptibles.awaitUninterruptibly(releaseLoad);
345+
return Optional.of(100L);
346+
}
347+
};
348+
ExecutorService cacheExecutor = Executors.newSingleThreadExecutor();
349+
ExecutorService readerExecutor = Executors.newSingleThreadExecutor();
350+
try {
351+
ExternalRowCountCache cache = new ExternalRowCountCache(cacheExecutor, null, loader);
352+
cache.invalidateCatalog(2L);
353+
Assertions.assertEquals(0, cache.getCatalogGenerationCountForTest());
354+
Future<Long> oldRead = readerExecutor.submit(() -> cache.getCachedRowCount(1L, 10L, 100L, false));
355+
Assertions.assertTrue(loadStarted.await(10, TimeUnit.SECONDS));
356+
Assertions.assertEquals(1, cache.getCatalogGenerationCountForTest());
357+
358+
cache.invalidateCatalog(1L);
359+
cache.releaseCatalog(1L);
360+
Assertions.assertEquals(0, cache.getCatalogGenerationCountForTest());
361+
releaseLoad.countDown();
362+
363+
Assertions.assertEquals(TableIf.UNKNOWN_ROW_COUNT, oldRead.get(10, TimeUnit.SECONDS));
364+
Assertions.assertEquals(TableIf.UNKNOWN_ROW_COUNT, cache.getCachedRowCountIfPresent(1L, 10L, 100L));
365+
Assertions.assertEquals(0, cache.getCatalogGenerationCountForTest());
366+
} finally {
367+
releaseLoad.countDown();
368+
readerExecutor.shutdownNow();
369+
cacheExecutor.shutdownNow();
370+
}
371+
}
372+
336373
@Test
337374
public void testInvalidateWhileRefreshIsRunningDoesNotRepublishStaleValue() throws Exception {
338375
AtomicInteger loadCount = new AtomicInteger();

‎fe/fe-core/src/test/java/org/apache/doris/datasource/paimon/PaimonExternalMetaCacheTest.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ public void testReplayDropInvalidatesSdkOnlyPaimonCache() throws Exception {
26262626
}
26272627

26282628
@Test
2629-
public void testModeOneSdkOnlyDatabaseInvalidationUsesCatalogLocale() throws Exception {
2629+
public void testModeOneSdkOnlyDatabaseInvalidationUsesRootLocale() throws Exception {
26302630
Locale previousLocale = Locale.getDefault();
26312631
Locale.setDefault(new Locale("tr", "TR"));
26322632
PaimonExternalCatalog dorisCatalog = null;
@@ -2654,8 +2654,8 @@ public void testModeOneSdkOnlyDatabaseInvalidationUsesCatalogLocale() throws Exc
26542654
externalCatalog.makeSureInitialized();
26552655
externalCatalog.catalog.dropTable(identifier, false);
26562656

2657-
// Doris mode 1 stores local "I" as the Turkish dotless "ı" on this FE.
2658-
dorisCatalog.invalidatePaimonDatabaseByLocalName("ı");
2657+
// Doris mode 1 uses a locale-independent local name on every FE.
2658+
dorisCatalog.invalidatePaimonDatabaseByLocalName("i");
26592659
PaimonExternalCatalog cachedCatalog = dorisCatalog;
26602660
Assert.assertThrows(Catalog.TableNotExistException.class,
26612661
() -> cachedCatalog.catalog.getTable(identifier));
@@ -3815,5 +3815,3 @@ public void testReplayRefreshTableColdTargetKeepsCachedSibling() throws Exceptio
38153815
}
38163816
}
38173817
}
3818-
3819-

0 commit comments

Comments
 (0)