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

Spark: add property to disable client-side purging in spark #11317

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/org/apache/iceberg/CachingCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public static Catalog wrap(
return new CachingCatalog(catalog, caseSensitive, expirationIntervalMillis);
}

public boolean wrapped_is_instance(Class<?> cls) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be removed once #11093 gets in

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for checking the type of the catalog impl that CachingCatalog is wrapping so that we can correctly respect the purge property at https://github.com/apache/iceberg/pull/11317/files#diff-bd61838d4e3a9aef52a670696750b30deac10b90f526435e32beacb0107eea24R376. Unless CachingCatalog is only used in tests, I think we need to keep this method?

return cls.isInstance(catalog);
}

private final Catalog catalog;
private final boolean caseSensitive;

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/apache/iceberg/CatalogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ private CatalogProperties() {}

public static final boolean IO_MANIFEST_CACHE_ENABLED_DEFAULT = false;

/**
* Controls whether engines using a REST Catalog should delegate the drop table purge requests to the Catalog.
* Defaults to false, allowing the engine to use its own implementation for purging.
*/
public static final String REST_CATALOG_PURGE = "rest.catalog-purge";

public static final boolean REST_CATALOG_PURGE_DEFAULT = false;

/**
* Controls the maximum duration for which an entry stays in the manifest cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.iceberg.spark;

import static org.apache.iceberg.CatalogProperties.REST_CATALOG_PURGE;
import static org.apache.iceberg.CatalogProperties.REST_CATALOG_PURGE_DEFAULT;
import static org.apache.iceberg.TableProperties.GC_ENABLED;
import static org.apache.iceberg.TableProperties.GC_ENABLED_DEFAULT;

Expand Down Expand Up @@ -60,6 +62,8 @@
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.rest.RESTCatalog;
import org.apache.iceberg.rest.RESTSessionCatalog;
import org.apache.iceberg.spark.actions.SparkActions;
import org.apache.iceberg.spark.source.SparkChangelogTable;
import org.apache.iceberg.spark.source.SparkTable;
Expand Down Expand Up @@ -137,6 +141,7 @@ public class SparkCatalog extends BaseCatalog
private ViewCatalog asViewCatalog = null;
private String[] defaultNamespace = null;
private HadoopTables tables;
private boolean restCatalogPurge;

/**
* Build an Iceberg {@link Catalog} to be used by this Spark catalog adapter.
Expand Down Expand Up @@ -365,6 +370,14 @@ public boolean purgeTable(Identifier ident) {
String metadataFileLocation =
((HasTableOperations) table).operations().current().metadataFileLocation();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably simpler here to just do

// Remote Purge and early exit
if (catalog.instanceOf[Rest...] and property = true) {
   remote drop purge
   return
} 

// Original Code Path

if ((this.icebergCatalog instanceof RESTCatalog
|| this.icebergCatalog instanceof RESTSessionCatalog
|| (this.icebergCatalog instanceof CachingCatalog
&& ((CachingCatalog) this.icebergCatalog).wrapped_is_instance(RESTCatalog.class)))
&& this.restCatalogPurge) {
return dropTableWithPurging(ident);
}

boolean dropped = dropTableWithoutPurging(ident);

if (dropped) {
Expand All @@ -383,6 +396,14 @@ public boolean purgeTable(Identifier ident) {
}
}

private boolean dropTableWithPurging(Identifier ident) {
if (isPathIdentifier(ident)) {
return tables.dropTable(((PathIdentifier) ident).location(), true);
} else {
return icebergCatalog.dropTable(buildIdentifier(ident), true);
}
}

private boolean dropTableWithoutPurging(Identifier ident) {
if (isPathIdentifier(ident)) {
return tables.dropTable(((PathIdentifier) ident).location(), false /* don't purge data */);
Expand Down Expand Up @@ -744,6 +765,9 @@ public final void initialize(String name, CaseInsensitiveStringMap options) {
CatalogProperties.CACHE_EXPIRATION_INTERVAL_MS,
CatalogProperties.CACHE_EXPIRATION_INTERVAL_MS_DEFAULT);

this.restCatalogPurge =
PropertyUtil.propertyAsBoolean(options, REST_CATALOG_PURGE, REST_CATALOG_PURGE_DEFAULT);

// An expiration interval of 0ms effectively disables caching.
// Do not wrap with CachingCatalog.
if (cacheExpirationIntervalMs == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.iceberg.spark.sql;

import static org.apache.iceberg.CatalogProperties.CATALOG_IMPL;
import static org.apache.iceberg.CatalogProperties.REST_CATALOG_PURGE;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;
import org.apache.iceberg.Parameter;
import org.apache.iceberg.ParameterizedTestExtension;
import org.apache.iceberg.Parameters;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Table;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.inmemory.InMemoryCatalog;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.rest.RESTCatalog;
import org.apache.iceberg.spark.SparkCatalog;
import org.apache.iceberg.spark.TestBase;
import org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException;
import org.apache.spark.sql.catalyst.analysis.NoSuchNamespaceException;
import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException;
import org.apache.spark.sql.connector.catalog.Column;
import org.apache.spark.sql.connector.catalog.Identifier;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.util.CaseInsensitiveStringMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(ParameterizedTestExtension.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably still be a part of the other test classes, or a new file called TestSparkCatalogREST

On a higher point I think looking at this we should just add a new method to the SparkCatalog

  /**
   * Reset the delegate Iceberg Catalog to a test Mock Catalog object
   */
  @VisibleForTesting
  void injectCatalog(Catalog iceberg) {
    this.icebergCatalog = icebergCatalog;
  }

Which can use to just inject a mock Catalog into the Spark environment so then the code would look something like

Psuedo Code Follows

Spark Config Here
RestCatalog mockCatalog = mock(new RestCatalog)
Spark3Util.loadCatalog("name").injectCatalog(mockCatalog)
sql("drop table purge ...")
verify(mockCatalog).called(drop, purge true)

@nastra and @amogh-jahagirdar , I know y'all have written a bunch of REST testing, how does that sound to you?

I am wondering whether we should have parameter for the normal test code that uses Hive, Hadoop, and add in a REST that injects like ^ ... but that may be overkill

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need any of the injection logic. We should rather wait for #11093 to get in, which would enable much easier testing with Spark + REST catalog

public class TestRestDropPurgeTable extends TestBase {

private SparkCatalog sparkCatalog;
private static final Identifier id = Identifier.of(new String[] {"a"}, "db");

@Parameter(index = 0)
private boolean purge;

@Parameters(name = "purge = {0}")
protected static Object[][] parameters() {
return new Object[][] {{true}, {false}};
}

@BeforeEach
public void before() {
sparkCatalog = new SparkCatalog();
sparkCatalog.initialize(
"test_rest_catalog_purge",
new CaseInsensitiveStringMap(
ImmutableMap.of(
CATALOG_IMPL,
MockCatalog.class.getName(),
REST_CATALOG_PURGE,
Boolean.toString(purge),
MockCatalog.EXPECTATION,
Boolean.toString(purge))));
try {
sparkCatalog.createNamespace(new String[] {"a"}, ImmutableMap.of());
sparkCatalog.createTable(
id, new Column[] {Column.create("a", DataTypes.FloatType)}, null, ImmutableMap.of());

} catch (TableAlreadyExistsException
| NoSuchNamespaceException
| NamespaceAlreadyExistsException e) {
throw new RuntimeException(e);
}
}

@TestTemplate
public void testDropPurgeTableForwardsPurge() {
boolean dropped = sparkCatalog.purgeTable(id);
// we assert that dropped is True to make sure the code path in purgeTable we're testing
// actually ran
assertThat(dropped).isTrue();
}

public static class MockCatalog extends RESTCatalog {
private final InMemoryCatalog catalog;
private static final String EXPECTATION = "expectation";
private boolean expectation;

public MockCatalog() {
this.catalog = new InMemoryCatalog();
this.catalog.initialize("test", new HashMap<>());
}

@Override
public void initialize(String name, Map<String, String> props) {
this.expectation = Boolean.parseBoolean(props.get(EXPECTATION));
}

@Override
public Table createTable(TableIdentifier identifier, org.apache.iceberg.Schema schema) {
return catalog.createTable(identifier, schema);
}

@Override
public void createNamespace(Namespace ns, Map<String, String> props) {
catalog.createNamespace(ns, props);
}

@Override
public TableBuilder buildTable(TableIdentifier identifier, Schema schema) {
return catalog.buildTable(identifier, schema);
}

@Override
public boolean dropTable(TableIdentifier identifier, boolean purge) {
assertThat(purge).isEqualTo(expectation);
return true;
}

@Override
public Table loadTable(TableIdentifier identifier) {
return catalog.loadTable(identifier);
}
}
}