Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix]Delete sync tables when drop external catalogs #2235

Merged
merged 9 commits into from
Nov 3, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.netease.arctic.AmoroTable;
import com.netease.arctic.TableIDWithFormat;
import com.netease.arctic.ams.api.CatalogMeta;
import com.netease.arctic.server.exception.IllegalMetadataException;
import com.netease.arctic.server.persistence.PersistentBase;
import com.netease.arctic.server.persistence.mapper.CatalogMetaMapper;

Expand Down Expand Up @@ -40,4 +41,15 @@ public void updateMetadata(CatalogMeta metadata) {
public abstract List<TableIDWithFormat> listTables(String database);

public abstract AmoroTable<?> loadTable(String database, String tableName);

public void dispose() {
doAsTransaction(
() ->
doAsExisted(
CatalogMetaMapper.class,
mapper -> mapper.deleteCatalog(name()),
() ->
new IllegalMetadataException(
"Catalog " + name() + " has more than one database or table")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@
@Override
public void dropCatalog(String catalogName) {
checkStarted();
doAsExisted(
CatalogMetaMapper.class,
mapper -> mapper.deleteCatalog(catalogName),
() ->
new IllegalMetadataException(
"Catalog " + catalogName + " has more than one database or table"));
ServerCatalog serverCatalog = getServerCatalog(catalogName);
if (serverCatalog == null) {
throw new ObjectNotExistsException("Catalog " + catalogName);

Check warning on line 145 in ams/server/src/main/java/com/netease/arctic/server/table/DefaultTableService.java

View check run for this annotation

Codecov / codecov/patch

ams/server/src/main/java/com/netease/arctic/server/table/DefaultTableService.java#L145

Added line #L145 was not covered by tests
}

// TableRuntime cleanup is responsibility by exploreExternalCatalog method
serverCatalog.dispose();
internalCatalogMap.remove(catalogName);
externalCatalogMap.remove(catalogName);
}
Expand Down Expand Up @@ -491,8 +492,7 @@
CompletableFuture.runAsync(
() -> {
try {
disposeTable(
externalCatalog, serverTableIdentifiers.get(tableIdentity));
disposeTable(serverTableIdentifiers.get(tableIdentity));
} catch (Exception e) {
LOG.error(
"TableExplorer dispose table {} error",
Expand All @@ -511,6 +511,20 @@
LOG.error("TableExplorer error", e);
}
}

// Clear TableRuntime objects that do not correspond to a catalog.
// This scenario is mainly due to the fact that TableRuntime objects were not cleaned up in a
// timely manner during the process of dropping the catalog due to concurrency considerations.
// It is permissible to have some erroneous states in the middle, as long as the final data is
// consistent.
Set<String> catalogNames =
listCatalogMetas().stream().map(CatalogMeta::getCatalogName).collect(Collectors.toSet());
for (TableRuntime tableRuntime : tableRuntimeMap.values()) {
if (!catalogNames.contains(tableRuntime.getTableIdentifier().getCatalog())) {
disposeTable(tableRuntime.getTableIdentifier());

Check warning on line 524 in ams/server/src/main/java/com/netease/arctic/server/table/DefaultTableService.java

View check run for this annotation

Codecov / codecov/patch

ams/server/src/main/java/com/netease/arctic/server/table/DefaultTableService.java#L524

Added line #L524 was not covered by tests
}
}

long end = System.currentTimeMillis();
LOG.info("Syncing external catalogs took {} ms.", end - start);
}
Expand Down Expand Up @@ -600,9 +614,14 @@
}
}

private void disposeTable(
ExternalCatalog externalCatalog, ServerTableIdentifier tableIdentifier) {
externalCatalog.disposeTable(tableIdentifier.getDatabase(), tableIdentifier.getTableName());
private void disposeTable(ServerTableIdentifier tableIdentifier) {
doAs(
TableMetaMapper.class,
mapper ->
mapper.deleteTableIdByName(
tableIdentifier.getCatalog(),
tableIdentifier.getDatabase(),
tableIdentifier.getTableName()));
Optional.ofNullable(tableRuntimeMap.remove(tableIdentifier))
.ifPresent(
tableRuntime -> {
Expand Down