-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
90805de
055789d
302c1b4
3033ab0
85b4d7d
48de51b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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. | ||
|
@@ -365,6 +370,14 @@ public boolean purgeTable(Identifier ident) { | |
String metadataFileLocation = | ||
((HasTableOperations) table).operations().current().metadataFileLocation(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's probably simpler here to just do
|
||
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) { | ||
|
@@ -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 */); | ||
|
@@ -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) { | ||
|
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
@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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. UnlessCachingCatalog
is only used in tests, I think we need to keep this method?