Skip to content

Allow data output streams to be generated via custom mechanisms when given partitioning and file name - #73

Closed
mccheah wants to merge 9 commits into
apache:masterfrom
mccheah:custom-path-generation
Closed

mccheah wants to merge 9 commits into
apache:masterfrom
mccheah:custom-path-generation

Conversation

@mccheah

@mccheah mccheah commented Jan 11, 2019

Copy link
Copy Markdown
Contributor

No description provided.

@mccheah mccheah left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@rdblue @yifeih @vinooganesh

Some of the design decisions are not obvious. I've highlighted them below and we can discuss further.

* The partition values of the rows in this file may be used to derive the final location of
* the file.
*/
OutputFile newPartitionedDataFile(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about this a little bit more, and in contrast to what was discussed on the ticket, I propose creating the OutputFile instance directly instead of just returning the path alone.

This comes up in the case where the file doesn't exist yet, which is always going to be the case for Spark (Spark is always creating new data files). In such a case, it is desirable for the plugin to not necessarily only know what the path is, but also to perhaps create the path itself, oftentimes by asking for some new entry in an external system that is a reference to this file we're about to write to.

In other words, the semantics of this API are that we're not only asking for the location of the data file, but we're also asking to create the data file as part of this action. In practice most of the implementations should just derive this as newOutputFile(getPath(args)). Nevertheless I think this version of the API is more rich. Thoughts?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable to me, as long as this doesn't cause odd practices like generating an OutputFile, converting it to a location, then later getting an OutputFile for it. I don't think that would happen though, so this is probably saving a call.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We're often using the location of the OutputFile object, but we shouldn't be converting twice anywhere.

return new Path(new Path(tableLocation, "metadata"), filename);
}

public void updateProperties(Map<String, String> newTableProperties) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a concession to the fact that when table properties are updated, it's possible that the data path could change. It's not immediately clear if we can do better than this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The table's base location may change as well, at least for metastore tables that also use HadoopFileIO.

I think that this FileIO should use the TableMetadata instead of maintaining parts of it (location and properties). What about passing in a supplier?

public HadoopFileIO(Configuration conf, Supplier<TableMetadata> tableMetadata) { ... }

public Path metadataPath(String filename) {
  Path basePath = new Path(this.tableMetadata.get().location());
  ...
}

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The Supplier makes sense to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually Supplier isn't great because the Supplier itself isn't serializable. The Supplier would be tied to the HadoopTableOperations object.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense. I think the best option is what you originally had, but with TableMetadata, updateTableMetadata. That's Serializable right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could also make tableMetadataSupplier transient and cache the result. Then at least the FileIO pulls metadata when needed instead of requiring a push.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, TableMetadata isn't serializable as far as I can tell. So here are the fields:

  // stored metadata
  private final String location;
  private final long lastUpdatedMillis;
  private final int lastColumnId;
  private final Schema schema;
  private final int defaultSpecId;
  private final List<PartitionSpec> specs;
  private final Map<String, String> properties;
  private final long currentSnapshotId;
  private final List<Snapshot> snapshots;
  private final Map<Long, Snapshot> snapshotsById;
  private final Map<Integer, PartitionSpec> specsById;
  private final List<SnapshotLogEntry> snapshotLog;

The problem is Snapshot. The interface doesn't extend Serializable. Furthermore, BaseSnapshot which I presume the Snapshot fields are, contains a reference to TableOperations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah actually I also missed the fact that TableMetadata also contains a reference to TableOperations.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, so HadoopFileIO would need to extract paths from TableMetadata each time updateMetadata is called instead of storing a reference. I think it is still better to pass the whole TableMetadata in case anything else changes. Probably also note in javadoc that TableMetadata isn't serializable, so don't save a copy of it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updateMetadata is a fitting compromise here!

private final Configuration conf;
private final FileIO fileIo;

private HadoopFileIO fileIo;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this needs to be a HadoopFileIO, does it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now it does because we specifically refresh the metadata on it (this really should be defaultFileIo though). However, perhaps part of the question is if updateMetadata should be part of the FileIO API contract too.

try {
// don't check if the file exists if version is non-null because it was already checked
if (version == null && !fs.exists(metadataFile)) {
if (version == null && !fs.exists(metadataFilePath)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should InputFile define an exists method?

I'm reluctant to add another method to that interface because it is supposed to be light-weight, but it would be simple to implement in most cases and would avoid this problem of getting the location of an input file to check existence here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I also understand not wanting to add exists. I'm also hesitant because we don't want to blow up these APIs to become a full-fledged file system. Where is the boundary with regards to what File System-like operations we will require implementations to provide?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We want to keep it as light-weight as possible to make it easy to avoid using the Hadoop API in the future. There's no reason to force downstream apps to include it. After that goal is met, then it's really a matter of trying to do as much as we can cleanly through the API. I think that adding exists would be okay. Then the only non-standard part of the Hadoop implementation would be the rename call.

/**
* Get an {@link InputFile} to get the bytes for this table's metadata file with the given name.
*/
InputFile readMetadataFile(String fileName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Metastore tables always use full paths to metadata files, so this is only used by file system tables in HadoopTables. I think the need for this method is caused by not separating the concern of building a metadata path from getting an InputFile or OutputFile instance. It seems odd to me to add a method to this interface just for Hadoop tables. Another minor issue is that it isn't obvious why this takes a filename and not a full path: the name readMetadataFile doesn't indicate that to me. If a caller has a full path to a metadata file, why indicates that readMetadataFile is not the right choice?

On the other hand, if this were replaced by metadataFileLocation, it would still only be used by Hadoop tables because it is built into newMetadataFile. I like the way newMetadataFile and newPartitionedDataFile work, so I'd rather not change those.

What about removing readMetadataFile and instead using newMetadataFile(filename).toInputFile()? I think that it is clear that newMetadataFile adds the metadata path because it is distinct from newOutputFile and how to construct the full file path is a concern. I think this is a reasonable way to "guess" the location of metadata files for Hadoop tables.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

+1 - sounds like a good way to resolve this.

/**
* Get an {@link OutputFile} to write bytes for a new table metadata file with the given name.
*/
OutputFile newMetadataFile(String fileName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about renaming this to newMetadataOutputFile to be clear that this is producing an OutputFile and to fit the existing convention where newInputFile and newOutputFile are distinguished from one another?

@mccheah

mccheah commented Jan 15, 2019

Copy link
Copy Markdown
Contributor Author

@rdblue addressed most of the comments. Think there's a few things we can continue to think about. Also worth highlighting is that the last commit is a conflict resolution where I also moved the metadata location configuration into HadoopFileIO but it's not clear if I did that correctly, though tests pass.

FileIO initializedFileIo = io();
// This would be false if the subclass overrides io() and returns a different kind.
if (initializedFileIo instanceof HadoopFileIO) {
((HadoopFileIO) initializedFileIo).updateTableMetadata(metadata);

@rdblue rdblue Jan 15, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Alternatively, we could pass TableMetadata to newMetadataOutputFile and newDataOutputFile. Then we wouldn't need this notification, or special handling for HadoopFileIO.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The more I think about this, the more I like it. Then FileIO wouldn't need to keep mutable state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problem is that TableMetadata is in iceberg-core but this module is in iceberg-api. But FileIO is returned by Table, which is in iceberg-api. Thus to make this API take TableMetadata as input we'd likely have to move TableMetadata into iceberg-api. The modules aren't set up correctly right now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moving TableMetadata into iceberg-api runs into the problem of TableOperations not being in iceberg-api.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we would expose the classes themselves. We'd add an interface to the API and implement that. That gets around the TableOperations problem.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think better would be that instead of caching the FileIO object and trying to maintain its state, we should just make a new one with the most up to date metadata every time it's needed to write something.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we would expose the classes themselves. We'd add an interface to the API and implement that. That gets around the TableOperations problem.

Meaning, add an interface on top of TableMetadata?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think better would be that instead of caching the FileIO object and trying to maintain its state, we should just make a new one with the most up to date metadata every time it's needed to write something.

Yes, that sounds reasonable.

Meaning, add an interface on top of TableMetadata?

Yeah. That's what we've done with the rest of the API to avoid exposing implementation details. I'd prefer to avoid adding one, though. I like the idea of refreshing FileIO better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I built something similar to the proposed solution. The main difference is that now TableOperations has a new io(TableMetadata) method, which allows custom file IO implementations to be returned both in the case when the current table metadata is used and when some alternative table metadata is used. We now don't save the FileIO object as discussed earlier.

* Metadata for a table.
*/
public class TableMetadata {
public class TableMetadata implements Serializable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This isn't Serializable, right? I think this may have been left in from other changes.

@mccheah

mccheah commented Jan 15, 2019

Copy link
Copy Markdown
Contributor Author

Addressed comments.

@mccheah

mccheah commented Jan 16, 2019

Copy link
Copy Markdown
Contributor Author

@rdblue any further comments on this?

@Override
public FileIO io() {
return fileIo;
public FileIO io(TableMetadata tableMetadata) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to make this private? It is odd for a TableOperations class that provides TableMetadata instances to have a public method that accepts TableMetadata instead of using current.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm now looking at the javadoc for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, looks like the trade-off here is that BaseTransaction needs to provide a FileIO object and its view of TableMetadata may be different from the table's actual metadata state because we stack up multiple changes and then commit just the final version in the transaction.

So unless I'm missing something again, the only thing that adding this method accomplishes is to be able to update some location and have the change take effect for a write in the same atomic commit. I'm okay with that limitation to make this API cleaner, unless you need to be able to change settings and write in an atomic commit for your use case.

@mccheah mccheah Jan 17, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So unless I'm missing something again, the only thing that adding this method accomplishes is to be able to update some location and have the change take effect for a write in the same atomic commit. I'm okay with that limitation to make this API cleaner, unless you need to be able to change settings and write in an atomic commit for your use case.

So I accidentally stumbled upon having to do it this way because a lot of tests just failed if we used the old location to commit when the new metadata specifies committing elsewhere. Thus I had thought that the current behavior is part of public API and we don't intend on changing it. I suppose we ought to document this specific behavior somewhere (if you change the new metadata location table property on a commit, the change is / is not reflected in that current commit). However it's unclear where the right place to document that is.

Map<String, String> tableProperties = tableMetadata.properties();
this.newDataFileDir = stripTrailingSlash(
tableProperties
.getOrDefault(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: different indentation than the next statement? (Not a big deal if you aren't making further changes)

return;
}

FileIO newMetadataIO = io(metadata);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why move this before the validation checks? If those fail, this file would need to be cleaned up.

}

InputFile inFile = fileIo.newInputFile(file);
InputFile inFile = fileIo.newInputFile(file.location());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about file.toInputFile()?

currentAppender.close();
this.currentAppender = null;
fileIo.deleteFile(currentPath);
fileIo.deleteFile(currentFile.location());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe it would make sense to add deleteFile(InputFile file) with a default implementation that calls file.location().

@rdblue

rdblue commented Jan 17, 2019

Copy link
Copy Markdown
Contributor

@mccheah, thanks for working on this. It's looking close, but I have doubts about adding TableOperations#io(TableMetadata). I'd rather add FileIO#updateMetadata(TableMetadata) if you think it is necessary to have the behavior that requires io(TableMetadata).

Otherwise, I think this is about ready to get in.

But pushes down the idea of "metadata-aware" file IO down to BaseMetastoreTableOperations.
@mccheah

mccheah commented Jan 24, 2019

Copy link
Copy Markdown
Contributor Author

Ok I adjusted this such that TableOperations only has the single io() method. We now offer a metadata-aware IO method in BaseMetastoreTableOperations though, to maintain existing behavior.

@mccheah

mccheah commented Jan 24, 2019

Copy link
Copy Markdown
Contributor Author

To override FileIO generation for subclasses of BaseMetastoreTableOperations though, devs should override BaseMetastoreTableOperations#metadataAwareFileIO.

@mccheah

mccheah commented Feb 14, 2019

Copy link
Copy Markdown
Contributor Author

Superceded by #87

@mccheah mccheah closed this Feb 14, 2019
@mccheah
mccheah deleted the custom-path-generation branch February 14, 2019 02:07
jun-ma-0 pushed a commit to jun-ma-0/incubator-iceberg that referenced this pull request May 11, 2020
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.

2 participants