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

[Draft] Refactor config classes #312

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions api/src/main/java/io/onetable/client/ExternalTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 io.onetable.client;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

import org.apache.hadoop.fs.Path;

import com.google.common.base.Preconditions;

@Getter
@EqualsAndHashCode
public class ExternalTable {
@NonNull String name;
@NonNull String formatName;
@NonNull String basePath;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: A javadoc comment for this member would clarify its purpose and avoid any confusion.
Suggestion:
basePath is the absolute addressable path of the table, which consists of the scheme, the namespace, and the table directories. For example, the basePath of a table, myTable, in ADLS in a namespace named myNamespace would be abfss://[email protected]/foo/bar/myNamespace/myTable

@NonNull String dataPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

Clarification: is dataPath generic and applicable to all table formats or just iceberg? IIUC, the default data path for iceberg tables is data/. It is null for both Hudi and Delta by default. If this is correct, then dataPath could be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Delta lake can also handle absolute paths so the metadata and data do not need to be under the same path. The data path is always set if you read through the constructor code below this.

Copy link
Contributor

Choose a reason for hiding this comment

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

The dataPath appears to a property of the TargetTable. The dataPath currently seems to point at the folder in which TargetTable's metadata would be generated. As such the TargetTable's basePath should represent it. Does it have any other significance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean SourceTable here? See my comment above about relative vs absolute path requirements. Only Hudi currently requires data and metadata to be under the same prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or do you mean that we only need to set a "target metadata path" in the TargetTable? If we want to go that route, does it make sense for the SourceTable to take in a metadata and data path args to be more specific than the current base and data paths

Copy link
Contributor

@ashvina ashvina Jan 22, 2024

Choose a reason for hiding this comment

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

Iceberg table properties to control location of files include write.data.path, write.metadata.path, and table location. As such these 3 properties could be added to source table.

However, even though Delta supports absolute path for data files, Delta table properties ref do not seem to provide option to control the location of metadata files. Hudi also requires same table location prefix for both data and metadata, and does not seem to provide options to configure these at table level.

As metadata location and data location do not apply to Hudi and Delta, I am wondering if the 3 properties should be part of the generic SourceTable config. The metadata and data locations could be confusing when the table format does not support it. We could document it clearly to avoid confusion. And that maybe better than creating format specific SourceTables (e.g. IcebergSourceTable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the dataPath to the source only. The targets will have a metadata path only and for Hudi that will be required to be the same as the source dataPath for reads to actually work. For the others, you can technically write these metadata files to some arbitrary location and it should work if the file paths are absolute.

String[] namespace;
CatalogConfig catalogConfig;

ExternalTable(
@NonNull String name,
@NonNull String formatName,
@NonNull String basePath,
String dataPath,
String[] namespace,
CatalogConfig catalogConfig) {
this.name = name;
this.formatName = formatName;
this.basePath = sanitizeBasePath(basePath);
this.dataPath = dataPath == null ? this.basePath : sanitizeBasePath(dataPath);
this.namespace = namespace;
this.catalogConfig = catalogConfig;
}

private String sanitizeBasePath(String tableBasePath) {
Path path = new Path(tableBasePath);
Preconditions.checkArgument(path.isAbsolute(), "Table base path must be absolute");
if (path.isAbsoluteAndSchemeAuthorityNull()) {
// assume this is local file system and append scheme
return "file://" + path;
} else if (path.toUri().getScheme().equals("file")) {
// add extra slashes
return "file://" + path.toUri().getPath();
} else {
return path.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@

package io.onetable.client;

import io.onetable.spi.extractor.SourcePartitionSpecExtractor;
import lombok.Builder;
import lombok.EqualsAndHashCode;

public interface HudiSourceConfig {
public String getPartitionSpecExtractorClass();

SourcePartitionSpecExtractor loadSourcePartitionSpecExtractor();
@EqualsAndHashCode(callSuper = true)
public class SourceTable extends ExternalTable {
@Builder(toBuilder = true)
public SourceTable(
String name,
String formatName,
String basePath,
String dataPath,
String[] namespace,
CatalogConfig catalogConfig) {
super(name, formatName, basePath, dataPath, namespace, catalogConfig);
}
}
43 changes: 43 additions & 0 deletions api/src/main/java/io/onetable/client/TargetTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 io.onetable.client;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode(callSuper = true)
public class TargetTable extends ExternalTable {
int metadataRetentionInHours;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add a javadoc.
This is the retention period for the generated metadata. The translator will automatically delete any generated version in the TargetTable that is older than this period when it runs.

I wonder if using the duration type would make it easier to configure this field with standard time APIs. That way, we could convert days to hours more easily, or set expiry durations that are suitable for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I've updated the type and will clean things up once this PR is in better shape


@Builder(toBuilder = true)
public TargetTable(
String name,
String formatName,
String basePath,
String dataPath,
String[] namespace,
CatalogConfig catalogConfig,
Integer metadataRetentionInHours) {
super(name, formatName, basePath, dataPath, namespace, catalogConfig);
this.metadataRetentionInHours =
metadataRetentionInHours == null ? 24 * 7 : metadataRetentionInHours;
}
}
4 changes: 2 additions & 2 deletions api/src/main/java/io/onetable/spi/sync/TargetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import org.apache.hadoop.conf.Configuration;

import io.onetable.client.PerTableConfig;
import io.onetable.client.TargetTable;
import io.onetable.model.OneTable;
import io.onetable.model.OneTableMetadata;
import io.onetable.model.schema.OnePartitionField;
Expand Down Expand Up @@ -89,5 +89,5 @@ public interface TargetClient {
String getTableFormat();

/** Initializes the client with provided configuration */
void init(PerTableConfig perTableConfig, Configuration configuration);
void init(TargetTable targetTable, Configuration configuration);
}
58 changes: 58 additions & 0 deletions api/src/test/java/io/onetable/client/TestSourceTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 io.onetable.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class TestSourceTable {
@Test
void sanitizePath() {
SourceTable tooManySlashes =
SourceTable.builder().basePath("s3://bucket//path").name("name").formatName("hudi").build();
assertEquals("s3://bucket/path", tooManySlashes.getBasePath());

SourceTable localFilePath =
SourceTable.builder().basePath("/local/data//path").name("name").formatName("hudi").build();
assertEquals("file:///local/data/path", localFilePath.getBasePath());

SourceTable properLocalFilePath =
SourceTable.builder()
.basePath("file:///local/data//path")
.name("name")
.formatName("hudi")
.build();
assertEquals("file:///local/data/path", properLocalFilePath.getBasePath());
}

@Test
void errorIfRequiredArgsNotSet() {
assertThrows(NullPointerException.class, () -> SourceTable.builder().name("name").build());

assertThrows(
NullPointerException.class,
() -> SourceTable.builder().basePath("file://bucket/path").build());

assertThrows(
NullPointerException.class,
() -> SourceTable.builder().basePath("file://bucket/path").name("name").build());
}
}
73 changes: 73 additions & 0 deletions api/src/test/java/io/onetable/client/TestTargetTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 io.onetable.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

public class TestTargetTable {
@Test
void sanitizePath() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The test in TestSourceTable seems identical to this one.

TargetTable tooManySlashes =
TargetTable.builder().basePath("s3://bucket//path").name("name").formatName("hudi").build();
assertEquals("s3://bucket/path", tooManySlashes.getBasePath());

TargetTable localFilePath =
TargetTable.builder().basePath("/local/data//path").name("name").formatName("hudi").build();
assertEquals("file:///local/data/path", localFilePath.getBasePath());

TargetTable properLocalFilePath =
TargetTable.builder()
.basePath("file:///local/data//path")
.name("name")
.formatName("hudi")
.build();
assertEquals("file:///local/data/path", properLocalFilePath.getBasePath());
}

@Test
void defaultValueSet() {
TargetTable table =
TargetTable.builder()
.basePath("file://bucket/path")
.name("name")
.formatName("hudi")
.build();

assertEquals(24 * 7, table.getMetadataRetentionInHours());
assertNull(table.getNamespace());
assertNull(table.getCatalogConfig());
}

@Test
void errorIfRequiredArgsNotSet() {
assertThrows(NullPointerException.class, () -> SourceTable.builder().name("name").build());
Copy link
Contributor

Choose a reason for hiding this comment

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

SourceTable?
Would it be better to create a TestExternalTables to avoid duplicating the tests?


assertThrows(
NullPointerException.class,
() -> SourceTable.builder().basePath("file://bucket/path").build());
Copy link
Contributor

Choose a reason for hiding this comment

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

SourceTable?


assertThrows(
NullPointerException.class,
() -> SourceTable.builder().basePath("file://bucket/path").name("name").build());
}
}
24 changes: 12 additions & 12 deletions core/src/main/java/io/onetable/client/OneTableClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -52,7 +51,7 @@

/**
* Responsible for completing the entire lifecycle of the sync process given {@link
* PerTableConfigImpl}. This is done in three steps,
* TableSyncConfig}. This is done in three steps,
*
* <ul>
* <li>1. Extracting snapshot {@link OneSnapshot} from the source table format.
Expand All @@ -77,26 +76,27 @@ public OneTableClient(Configuration conf) {
* @param config A per table level config containing tableBasePath, partitionFieldSpecConfig,
* targetTableFormats and syncMode
* @param sourceClientProvider A provider for the source client instance, {@link
* SourceClientProvider#init(Configuration, Map)} must be called before calling this method.
* SourceClientProvider#init(Configuration)} must be called before calling this method.
* @return Returns a map containing the table format, and it's sync result. Run sync for a table
* with the provided per table level configuration.
*/
public <COMMIT> Map<String, SyncResult> sync(
PerTableConfig config, SourceClientProvider<COMMIT> sourceClientProvider) {
if (config.getTargetTableFormats() == null || config.getTargetTableFormats().isEmpty()) {
TableSyncConfig config, SourceClientProvider<COMMIT> sourceClientProvider) {
if (config.getTargetTables() == null || config.getTargetTables().isEmpty()) {
throw new IllegalArgumentException("Please provide at-least one format to sync");
}

try (SourceClient<COMMIT> sourceClient = sourceClientProvider.getSourceClientInstance(config)) {
try (SourceClient<COMMIT> sourceClient =
sourceClientProvider.getSourceClientInstance(
config.getSourceTable(), config.getProperties())) {
ExtractFromSource<COMMIT> source = ExtractFromSource.of(sourceClient);

Map<String, TargetClient> syncClientByFormat =
config.getTargetTableFormats().stream()
config.getTargetTables().stream()
.collect(
Collectors.toMap(
Function.identity(),
tableFormat ->
tableFormatClientFactory.createForFormat(tableFormat, config, conf)));
TargetTable::getFormatName,
targetTable -> tableFormatClientFactory.createForFormat(targetTable, conf)));
// State for each TableFormat
Map<String, Optional<OneTableMetadata>> lastSyncMetadataByFormat =
syncClientByFormat.entrySet().stream()
Expand Down Expand Up @@ -147,11 +147,11 @@ private static String getFormatsWithStatusCode(
}

private <COMMIT> Map<String, TargetClient> getFormatsToSyncIncrementally(
PerTableConfig perTableConfig,
TableSyncConfig syncConfig,
Map<String, TargetClient> syncClientByFormat,
Map<String, Optional<OneTableMetadata>> lastSyncMetadataByFormat,
SourceClient<COMMIT> sourceClient) {
if (perTableConfig.getSyncMode() == SyncMode.FULL) {
if (syncConfig.getSyncMode() == SyncMode.FULL) {
// Full sync requested by config, hence no incremental sync.
return Collections.emptyMap();
}
Expand Down
Loading