-
Notifications
You must be signed in to change notification settings - Fork 224
Initial POC - streaming table metadata rather than de/serializing in memory #1395
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
base: main
Are you sure you want to change the base?
Initial POC - streaming table metadata rather than de/serializing in memory #1395
Conversation
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.
Interesting improvement 👍
.getConfiguration( | ||
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA) | ||
.equals(true)) { |
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.
nit:
.getConfiguration( | |
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA) | |
.equals(true)) { | |
.getConfiguration( | |
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA)) { |
public static class StreamingLoadTableResponse implements RESTResponse { | ||
private String metadataLocation; | ||
private InputStream metadata; | ||
private Map<String, String> config; | ||
private List<Credential> credentials; | ||
|
||
public StreamingLoadTableResponse() {} | ||
|
||
public StreamingLoadTableResponse( | ||
String metadataLocation, | ||
InputStream metadata, | ||
Map<String, String> config, | ||
List<Credential> credentials) { | ||
this.metadataLocation = metadataLocation; | ||
this.metadata = metadata; | ||
this.config = config; | ||
this.credentials = credentials; | ||
} | ||
|
||
@Override | ||
public void validate() { | ||
Preconditions.checkNotNull(this.metadata, "Invalid metadata: null"); | ||
} | ||
|
||
@JsonProperty("metadata-location") | ||
public String metadataLocation() { | ||
return this.metadataLocation; | ||
} | ||
|
||
@JsonProperty("metadata") | ||
@JsonSerialize(using = InputStreamSerializer.class) | ||
public InputStream tableMetadata() { | ||
return this.metadata; | ||
} | ||
|
||
@JsonProperty("config") | ||
public Map<String, String> config() { | ||
return this.config != null ? this.config : Map.of(); | ||
} | ||
|
||
@JsonProperty("storage-credentials") | ||
public List<Credential> credentials() { | ||
return this.credentials != null ? this.credentials : List.of(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("metadataLocation", this.metadataLocation) | ||
.add("metadata", this.metadata) | ||
.add("config", this.config) | ||
.toString(); | ||
} | ||
} |
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.
Could be a record:
public static class StreamingLoadTableResponse implements RESTResponse { | |
private String metadataLocation; | |
private InputStream metadata; | |
private Map<String, String> config; | |
private List<Credential> credentials; | |
public StreamingLoadTableResponse() {} | |
public StreamingLoadTableResponse( | |
String metadataLocation, | |
InputStream metadata, | |
Map<String, String> config, | |
List<Credential> credentials) { | |
this.metadataLocation = metadataLocation; | |
this.metadata = metadata; | |
this.config = config; | |
this.credentials = credentials; | |
} | |
@Override | |
public void validate() { | |
Preconditions.checkNotNull(this.metadata, "Invalid metadata: null"); | |
} | |
@JsonProperty("metadata-location") | |
public String metadataLocation() { | |
return this.metadataLocation; | |
} | |
@JsonProperty("metadata") | |
@JsonSerialize(using = InputStreamSerializer.class) | |
public InputStream tableMetadata() { | |
return this.metadata; | |
} | |
@JsonProperty("config") | |
public Map<String, String> config() { | |
return this.config != null ? this.config : Map.of(); | |
} | |
@JsonProperty("storage-credentials") | |
public List<Credential> credentials() { | |
return this.credentials != null ? this.credentials : List.of(); | |
} | |
@Override | |
public String toString() { | |
return MoreObjects.toStringHelper(this) | |
.add("metadataLocation", this.metadataLocation) | |
.add("metadata", this.metadata) | |
.add("config", this.config) | |
.toString(); | |
} | |
} | |
public record StreamingLoadTableResponse( | |
@JsonProperty("metadata-location") String metadataLocation, | |
@JsonProperty("metadata") @JsonSerialize(using = InputStreamSerializer.class) | |
InputStream metadata, | |
@JsonProperty("config") Map<String, String> config, | |
@JsonProperty("storage-credentials") List<Credential> credentials) | |
implements RESTResponse { | |
@Override | |
public void validate() { | |
Preconditions.checkNotNull(this.metadata, "Invalid metadata: null"); | |
} | |
} |
(fileIO, tableEntity) -> { | ||
return new StreamingLoadTableResponse( | ||
tableEntity.getMetadataLocation(), | ||
fileIO.newInputFile(tableEntity.getMetadataLocation()).newStream(), | ||
Map.of(), | ||
List.of()); | ||
}, |
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.
nit:
(fileIO, tableEntity) -> { | |
return new StreamingLoadTableResponse( | |
tableEntity.getMetadataLocation(), | |
fileIO.newInputFile(tableEntity.getMetadataLocation()).newStream(), | |
Map.of(), | |
List.of()); | |
}, | |
(fileIO, tableEntity) -> | |
new StreamingLoadTableResponse( | |
tableEntity.getMetadataLocation(), | |
fileIO.newInputFile(tableEntity.getMetadataLocation()).newStream(), | |
Map.of(), | |
List.of()), |
} | ||
} | ||
|
||
public static class InputStreamSerializer extends RawSerializer<InputStream> { |
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.
Nit: I would extend StdSerializer
instead. In spite of its name, RawSerializer
is not a generic serializer for "raw" values.
throws IOException { | ||
LoggerFactory.getLogger(getClass()).warn("Using custom serializer for input stream"); | ||
byte[] buffer = new byte[4096]; | ||
try (inputStream) { |
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.
Nit: I would rather model the field as an InputFile
, then create a serializer for InputFile
. This would make it clear when the input stream is created and closed.
try (inputStream) { | |
try (InputStream inputStream = inputFile.newStream()) { |
// tell jackson we're about to write a value | ||
// this ensures we get the correct token prefixing the value (i.e., a : for a field value or | ||
// a , for an array) | ||
jsonGenerator.writeRawValue(""); |
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.
The following alternative looks slightly less hacky imho:
jsonGenerator.writeRawValue("");
jsonGenerator.flush();
if (jsonGenerator.getOutputTarget() instanceof OutputStream out) {
inputStream.transferTo(out);
} else if (jsonGenerator.getOutputTarget() instanceof Writer writer) {
new InputStreamReader(inputStream, StandardCharsets.UTF_8).transferTo(writer);
} else {
throw new IllegalStateException(
"Cannot serialize InputFile to unknown output target: "
+ jsonGenerator.getOutputTarget());
}
TableIdentifier tableIdentifier, String snapshots) { | ||
return loadTableWithAccessDelegationIfStale(tableIdentifier, null, snapshots).get(); | ||
} | ||
|
||
public LoadCredentialsResponse loadAccessDelegation( |
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 addition looks unrelated?
@@ -36,4 +38,12 @@ Map<String, String> getCredentialConfig( | |||
TableIdentifier tableIdentifier, | |||
TableMetadata tableMetadata, | |||
Set<PolarisStorageActions> storageActions); | |||
|
|||
Map<String, String> getCredentialConfig( |
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.
Unrelated?
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.
Streaming metadata sounds like a valuable improvement to me 👍
(fileIO, tableEntity) -> { | ||
return new StreamingLoadTableResponse( | ||
tableEntity.getMetadataLocation(), | ||
fileIO.newInputFile(tableEntity.getMetadataLocation()).newStream(), |
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.
How is the stream closed? Could you add a comment about that?
@@ -201,4 +201,11 @@ protected FeatureConfiguration( | |||
+ " requires experimentation in the specific deployment environment") | |||
.defaultValue(100 * EntityWeigher.WEIGHT_PER_MB) | |||
.buildFeatureConfiguration(); | |||
|
|||
public static final FeatureConfiguration<Boolean> ENABLE_STREAMING_TABLE_METADATA = |
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.
Instead of just an enable/disable, can we have something like STREAMING_TABLE_METADATA_MIN_SIZE
?
Initial POC for streaming table metadata directly from storage rather than deserializing in memory, then serializing back out. The goal is to avoid parsing very large metadata files and holding them in memory. We can avoid the need to throttle based on large file sizes and use fixed memory for handling any size TableMetadata.
Some aspects need consideration, such as reading table properties, which we currently do by parsing the TableMetadata. These properties could be written to the table entity properties in addition to the TableMetadata file so that we can look up some things from the entity directly.
Not all tests are passing yet (the catalog integration tests do). Just gauging interest.