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

Nessie: Infer default API version from URI #9459

Merged
merged 3 commits into from Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 27 additions & 3 deletions nessie/src/main/java/org/apache/iceberg/nessie/NessieCatalog.java
Expand Up @@ -25,6 +25,8 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.TableOperations;
Expand Down Expand Up @@ -103,9 +105,12 @@ public void initialize(String name, Map<String, String> options) {
.fallbackTo(x -> options.get(removePrefix.apply(x)));
NessieClientBuilder nessieClientBuilder =
NessieClientBuilder.createClientBuilderFromSystemSettings(configSource);
// default version is set to v1.
final String apiVersion =
options.getOrDefault(removePrefix.apply(NessieUtil.CLIENT_API_VERSION), "1");
// default version is inferred by uri.
String apiVersion = options.get(removePrefix.apply(NessieUtil.CLIENT_API_VERSION));
if (apiVersion == null) {
apiVersion = inferVersionFromURI(options.get(CatalogProperties.URI));
}

NessieApiV1 api;
switch (apiVersion) {
case "1":
Expand All @@ -128,6 +133,25 @@ public void initialize(String name, Map<String, String> options) {
catalogOptions);
}

private static String inferVersionFromURI(String uri) {
if (uri == null) {
throw new IllegalArgumentException("URI is not specified in the catalog properties");
}

// match for uri ending with /v1, /v2 etc
Pattern pattern = Pattern.compile("/v(\\d+)$");
Matcher matcher = pattern.matcher(uri);
if (matcher.find()) {
return matcher.group(1);
} else {
throw new IllegalArgumentException(
String.format(
"URI doesn't end with the version: %s. "
+ "Please configure `client-api-version` in the catalog properties explicitly.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder whether we can just assume v2 in this case... but I'm fine with throwing an exception too.

Copy link
Member Author

Choose a reason for hiding this comment

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

If the uri is not ending versions, then it can be a custom implementation which can be based on v1 or v2. So, I didn't want to assume the version there.

uri));
}
}

/**
* An alternative way to initialize the catalog using a pre-configured {@link NessieIcebergClient}
* and {@link FileIO} instance.
Expand Down
Expand Up @@ -35,7 +35,6 @@
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.projectnessie.client.api.NessieApiV1;
import org.projectnessie.client.ext.NessieApiVersion;
import org.projectnessie.client.ext.NessieApiVersions;
import org.projectnessie.client.ext.NessieClientFactory;
import org.projectnessie.client.ext.NessieClientUri;
Expand Down Expand Up @@ -65,7 +64,6 @@ public class TestNessieCatalog extends CatalogTests<NessieCatalog> {

private NessieCatalog catalog;
private NessieApiV1 api;
private NessieApiVersion apiVersion;
private Configuration hadoopConfig;
private String initialHashOfDefaultBranch;
private String uri;
Expand All @@ -74,7 +72,6 @@ public class TestNessieCatalog extends CatalogTests<NessieCatalog> {
public void setUp(NessieClientFactory clientFactory, @NessieClientUri URI nessieUri)
throws NessieNotFoundException {
api = clientFactory.make();
apiVersion = clientFactory.apiVersion();
initialHashOfDefaultBranch = api.getDefaultBranch().getHash();
uri = nessieUri.toASCIIString();
hadoopConfig = new Configuration();
Expand Down Expand Up @@ -122,9 +119,7 @@ private NessieCatalog initNessieCatalog(String ref) {
CatalogProperties.URI,
uri,
CatalogProperties.WAREHOUSE_LOCATION,
temp.toUri().toString(),
"client-api-version",
apiVersion == NessieApiVersion.V2 ? "2" : "1");
temp.toUri().toString());
Copy link
Member Author

Choose a reason for hiding this comment

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

Infers API version using URI.

newCatalog.initialize("nessie", options);
return newCatalog;
}
Expand Down