Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

collado-mike
Copy link
Contributor

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.

Copy link
Contributor

@adutra adutra left a comment

Choose a reason for hiding this comment

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

Interesting improvement 👍

Comment on lines +550 to +552
.getConfiguration(
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA)
.equals(true)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
.getConfiguration(
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA)
.equals(true)) {
.getConfiguration(
polarisCallContext, FeatureConfiguration.ENABLE_STREAMING_TABLE_METADATA)) {

Comment on lines +573 to +626
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();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be a record:

Suggested change
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");
}
}

Comment on lines +557 to +563
(fileIO, tableEntity) -> {
return new StreamingLoadTableResponse(
tableEntity.getMetadataLocation(),
fileIO.newInputFile(tableEntity.getMetadataLocation()).newStream(),
Map.of(),
List.of());
},
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
(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> {
Copy link
Contributor

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) {
Copy link
Contributor

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.

Suggested change
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("");
Copy link
Contributor

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(
Copy link
Contributor

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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated?

Copy link
Contributor

@dimas-b dimas-b left a 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(),
Copy link
Contributor

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 =
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants