forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
259 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
paimon-core/src/main/java/org/apache/paimon/rest/RefreshCredentialFileIO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* 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.paimon.rest; | ||
|
||
import org.apache.paimon.catalog.CatalogContext; | ||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.fs.FileIO; | ||
import org.apache.paimon.fs.FileStatus; | ||
import org.apache.paimon.fs.Path; | ||
import org.apache.paimon.fs.PositionOutputStream; | ||
import org.apache.paimon.fs.SeekableInputStream; | ||
import org.apache.paimon.options.CatalogOptions; | ||
import org.apache.paimon.options.Options; | ||
import org.apache.paimon.rest.auth.AuthSession; | ||
import org.apache.paimon.rest.responses.GetTableCredentialsResponse; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
/** A {@link FileIO} to support refresh credential. */ | ||
public class RefreshCredentialFileIO implements FileIO { | ||
private final ResourcePaths resourcePaths; | ||
private final AuthSession catalogAuth; | ||
private RESTClient client; | ||
protected Options options; | ||
private Identifier identifier; | ||
private Date expireAt; | ||
private transient volatile FileIO lazyFileIO; | ||
|
||
public RefreshCredentialFileIO( | ||
ResourcePaths resourcePaths, | ||
AuthSession catalogAuth, | ||
Options options, | ||
RESTClient client, | ||
Identifier identifier) { | ||
this.resourcePaths = resourcePaths; | ||
this.catalogAuth = catalogAuth; | ||
this.options = options; | ||
this.identifier = identifier; | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public void configure(CatalogContext context) { | ||
// Do not get Hadoop Configuration in CatalogOptions | ||
// The class is in different classloader from pluginClassLoader! | ||
this.options = context.options(); | ||
} | ||
|
||
@Override | ||
public SeekableInputStream newInputStream(Path path) throws IOException { | ||
return fileIO().newInputStream(path); | ||
} | ||
|
||
@Override | ||
public PositionOutputStream newOutputStream(Path path, boolean overwrite) throws IOException { | ||
return fileIO().newOutputStream(path, overwrite); | ||
} | ||
|
||
@Override | ||
public FileStatus getFileStatus(Path path) throws IOException { | ||
return fileIO().getFileStatus(path); | ||
} | ||
|
||
@Override | ||
public FileStatus[] listStatus(Path path) throws IOException { | ||
return fileIO().listStatus(path); | ||
} | ||
|
||
@Override | ||
public boolean exists(Path path) throws IOException { | ||
return fileIO().exists(path); | ||
} | ||
|
||
@Override | ||
public boolean delete(Path path, boolean recursive) throws IOException { | ||
return fileIO().delete(path, recursive); | ||
} | ||
|
||
@Override | ||
public boolean mkdirs(Path path) throws IOException { | ||
return fileIO().mkdirs(path); | ||
} | ||
|
||
@Override | ||
public boolean rename(Path src, Path dst) throws IOException { | ||
return fileIO().rename(src, dst); | ||
} | ||
|
||
@Override | ||
public boolean isObjectStore() { | ||
try { | ||
return fileIO().isObjectStore(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private FileIO fileIO() throws IOException { | ||
if (lazyFileIO == null || shouldRefresh()) { | ||
synchronized (this) { | ||
if (lazyFileIO == null || shouldRefresh()) { | ||
GetTableCredentialsResponse response = getCredential(); | ||
expireAt = response.getExpiresAt(); | ||
Map<String, String> conf = | ||
RESTUtil.merge(options.toMap(), response.getCredential()); | ||
Options updateCredentialOption = new Options(conf); | ||
lazyFileIO = | ||
FileIO.get( | ||
new Path(updateCredentialOption.get(CatalogOptions.WAREHOUSE)), | ||
CatalogContext.create(updateCredentialOption)); | ||
} | ||
} | ||
} | ||
return lazyFileIO; | ||
} | ||
|
||
private GetTableCredentialsResponse getCredential() { | ||
return client.get( | ||
resourcePaths.tableCredentials( | ||
identifier.getDatabaseName(), identifier.getObjectName()), | ||
GetTableCredentialsResponse.class, | ||
catalogAuth.getHeaders()); | ||
} | ||
|
||
private boolean shouldRefresh() { | ||
return expireAt != null && expireAt.getTime() > System.currentTimeMillis(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
paimon-core/src/main/java/org/apache/paimon/rest/responses/GetTableCredentialsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.paimon.rest.responses; | ||
|
||
import org.apache.paimon.rest.RESTResponse; | ||
|
||
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator; | ||
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonGetter; | ||
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
/** Response for table credentials. */ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class GetTableCredentialsResponse implements RESTResponse { | ||
|
||
private static final String FIELD_CREDENTIAL = "credential"; | ||
private static final String FIELD_EXPIREAT = "expiresAt"; | ||
|
||
@JsonProperty(FIELD_CREDENTIAL) | ||
private final Map<String, String> credential; | ||
|
||
@JsonProperty(FIELD_EXPIREAT) | ||
private Date expiresAt; | ||
|
||
@JsonCreator | ||
public GetTableCredentialsResponse( | ||
@JsonProperty(FIELD_EXPIREAT) Date expiresAt, | ||
@JsonProperty(FIELD_CREDENTIAL) Map<String, String> credential) { | ||
this.expiresAt = expiresAt; | ||
this.credential = credential; | ||
} | ||
|
||
@JsonGetter(FIELD_CREDENTIAL) | ||
public Map<String, String> getCredential() { | ||
return credential; | ||
} | ||
|
||
@JsonGetter(FIELD_EXPIREAT) | ||
public Date getExpiresAt() { | ||
return expiresAt; | ||
} | ||
} |