Skip to content

Conversation

@XuQianJin-Stars
Copy link
Contributor

An implementation of Spark Structured Streaming Offset, to track the current processed files of Iceberg table, This PR is a split of the PR-796 of Structured streaming read for Iceberg.

@github-actions github-actions bot added the spark label Jan 14, 2021
int version = JsonUtil.getInt(VERSION, node);
if (version > CURR_VERSION) {
throw new IOException(String.format("Cannot deserialize a JSON offset from version %d. %d is not compatible " +
"with the version of Iceberg %d and cannot be used. Please use a compatible version of Iceberg " +
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit confusing because it's not the Iceberg version, but the Streaming Offset Version. Maybe just phrase it instead "This version of iceberg only supports version $curversion"

* snapshot.
* snapshot_fully_processed: Denote whether the current snapshot is fully processed, to avoid revisiting the processed
* snapshot.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Somewhat of a nit: Can you please make this a proper javadoc comment, such as using @param before the constructor parameters, listing out all of the constructor parameters in order, as well as formatting the constructor parameters the way that they are in the code (i.e. using camel case and not snake case)? I would say also that the line This StreamingOffset consists of: will be unnecessary if you do that.

@XuQianJin-Stars
Copy link
Contributor Author

Thanks @RussellSpitzer @kbendick review this PR.

@XuQianJin-Stars
Copy link
Contributor Author

hi @aokolnychyi @rdblue please take a look at this PR at your convenience.

}

static StreamingOffset fromJson(String json) {
Preconditions.checkNotNull(json, "The input JSON string is null");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: it might be best to have a more explanatory message in the Preconditions check. Something like The input JSON string representation of a StreamingOffset cannot be null. I'll leave that up to you / the others to decide as it's a minor nit.

// The version of StreamingOffset. The offset was created with a version number
// used to validate when deserializing from json string.
int version = JsonUtil.getInt(VERSION, node);
if (version > CURR_VERSION) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we not plan to support version 2 snapshot files yet? I suppose that makes sense given the need to process deletes etc with the version 2 snapshots, but looking to understand what other reasons might exist for not supporting them yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right now, I think we want to focus on v1 data. But I think this version is actually referring to the version of the offset JSON, in case we need to change it later.

private static final String SNAPSHOT_ID = "snapshot_id";
private static final String INDEX = "index";
private static final String SCAN_ALL_FILES = "scan_all_files";
private static final String SNAPSHOT_FULLY_PROCESSED = "snapshot_fully_processed";
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that data and delete files can report the row position within a manifest, I don't think that we need to use SNAPSHOT_FULLY_PROCESSED anymore. Even if manifests are filtered, the position will be reliable. So by using position for the index field in this offset, we can check whether a given manifest has been completely processed by comparing the index with the number of entries in the manifest.

I think that getting rid of this field and using a simpler offset is a good improvement.

* @param snapshotId The current processed snapshot id.
* @param index The index of last scanned file in snapshot.
* @param scanAllFiles Denote whether to scan all files in a snapshot, currently we only
* scan all files in the starting snapshot.
Copy link
Contributor

Choose a reason for hiding this comment

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

Docs should not refer to currently. If you want to give context for why this might be used, then use "for example". "Whether to scan all files in a snapshot; for example, to read all data when starting a stream"

* revisiting the processed snapshot.
*/
StreamingOffset(long snapshotId, int index, boolean scanAllFiles,
boolean snapshotFullyProcessed) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: looks like this newline is unnecessary.

}

static StreamingOffset fromJson(String json) {
Preconditions.checkNotNull(json, "The input JSON string representation of a StreamingOffset cannot be null");
Copy link
Contributor

Choose a reason for hiding this comment

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

Error messages typically follow the pattern "Invalid ..." or "Cannot (action): (reason) [suggestion]". Here, there is no suggestion for fixing it, so it would be something like "Cannot parse offset JSON: null".


return new StreamingOffset(snapshotId, index, shouldScanAllFiles, snapshotFullyProcessed);
} catch (IOException e) {
throw new IllegalStateException(String.format("Failed to parse StreamingOffset from JSON string %s", json), e);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be IllegalArgumentException instead. There is no state in this static method.


import static org.apache.iceberg.types.Types.NestedField.optional;

public abstract class TestStructuredStreamingRead {
Copy link
Contributor

Choose a reason for hiding this comment

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

This class has no tests? Can you remove it?

// used to validate when deserializing from json string.
int version = JsonUtil.getInt(VERSION, node);
if (version > CURR_VERSION) {
throw new IOException(String.format("This version of iceberg only supports version %s", CURR_VERSION));
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not throw IOException, I think it would be IllegalArgumentException, which means you could use a Precondition. Also, I'd prefer to have the error message follow conventions:

    Preconditions.check(version == CURR_VERSION,
        "Cannot parse offset JSON: offset version %s is not supported", version);

@XuQianJin-Stars
Copy link
Contributor Author

Thanks again to @rdblue and @kbendick review this PR. I have addressed it.

public ExpectedException exceptionRule = ExpectedException.none();

@BeforeClass
public static void startSpark() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

This PR introduces a simple class that can serialize/deserialize itself using JSON.

I don't see any value in having a test that creates a Spark session and does dataframe operations. I think what needs to be tested is that you can create an offset, get the expected values from the getter methods, serialize, and deserialize correctly. Those don't involve Spark, so I don't see a reason to slow down tests overall by adding Spark tests here.

Can you remove the Spark code and do some basic tests for serialization?

@rdblue
Copy link
Contributor

rdblue commented Jan 29, 2021

Thanks for updating this, @XuQianJin-Stars. I think the implementation looks good now. We just need to fix up the tests. Thanks!

@XuQianJin-Stars
Copy link
Contributor Author

hi @rdblueThanks again, I have updating it.

@XuQianJin-Stars
Copy link
Contributor Author

hi @rdblue would you please take another look at this PR at your convenience.

@rdblue
Copy link
Contributor

rdblue commented Feb 19, 2021

Will do. Thanks for pinging me.

@rdblue
Copy link
Contributor

rdblue commented Feb 21, 2021

Looks good now. I'll merge this. Thanks @XuQianJin-Stars!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants