Skip to content

Use broadcast variables in IcebergSource - #569

Merged
rdblue merged 1 commit into
apache:masterfrom
aokolnychyi:serialization-fix
Dec 9, 2019
Merged

rdblue merged 1 commit into
apache:masterfrom
aokolnychyi:serialization-fix

Conversation

@aokolnychyi

@aokolnychyi aokolnychyi commented Oct 21, 2019 •

Copy link
Copy Markdown
Contributor

This PR fixes #553.

In some Spark jobs, we see a substantial scheduler delay, which happens in TaskSetManager when Spark serializes Iceberg ReadTask. The latter contains a reference to FileIO (which can contain a full Hadoop conf).

The current idea is to broadcast EncryptionManager and FileIO in IcebergSource. Then Reader and ReadTask can store references to the broadcasted values and fetch actual ones in createPartitionReader while creating TaskDataReader.

All broadcasted values are automatically registered for clean-up in SparkContext$broadcast.

Note! Using this approach with Kryo requires providing a custom registrator to instruct Spark to apply Java serialization for SerializableConfiguration. We should extend #549 for this.

@rdblue

rdblue commented Oct 22, 2019

Copy link
Copy Markdown
Contributor

Can we use Spark's internal SerializableConfiguration to avoid the Kryo problem?

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

@rdblue, Hadoop conf is hidden in FileIO, which belongs to the Table API. FileIO can be arbitrary, so the logic to wrap/unwrap it in IcebergSource will be cumbersome.

@rdblue

rdblue commented Oct 22, 2019

Copy link
Copy Markdown
Contributor

Yes, I think that this is something we will want to add a special case for. If the FileIO is a Hadoop FileIO, then hijack it.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

Will it be worth the complexity assuming we will have a custom Kryo registrator anyway?

@rdblue

rdblue commented Oct 22, 2019

Copy link
Copy Markdown
Contributor

I'm not confident that we will want to have a custom Kryo registrator. Requiring users to register extra classes makes it significantly harder because Iceberg doesn't work out of the box. We might just build some collection wrappers that can handle this. I think the SerializableByteBufferMap is working just fine, so maybe we can customize like that.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

Applying the proxy pattern looks promising, let me try that.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

Wait, writeReplace/readResolve won't have any effect on Kryo. SerializableByteBufferMap works because Spark registers ByteBuffer separately.

@rdblue

rdblue commented Oct 22, 2019

Copy link
Copy Markdown
Contributor

Yes, but it also appears that Kryo handles maps by serializing the map type and the entries, then calling put to reconstruct the map. We can possibly use this to build a proxy class.

@rdblue

rdblue commented Oct 30, 2019

Copy link
Copy Markdown
Contributor

This looks good to me. Is the remaining concern that it uses Iceberg's SerializableConfiguration that isn't correctly handled by Kryo?

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

Yes, I've decided to wait until we arrive to a solution in #549 and then update this PR accordingly. I'd want to avoid hijacking depending on FileIO implementation, but don't see an easy way without a custom registrator.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

@rdblue, it just occurred to me that SerializableConfiguration in Spark is not public and we cannot really use it.

@rdblue

rdblue commented Nov 12, 2019

Copy link
Copy Markdown
Contributor

@aokolnychyi, SerializableConfiguration is public now and annotated with @DeveloperApi: apache/spark@bd05339

I think it should be fine to rely on it. It's a little ugly, but better than needing special registration for Kryo.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

Then we can do smth like below.

  • Introduce BaseHadoopFileIO:
public abstract class BaseHadoopFileIO implements FileIO {

  public abstract Configuration hadoopConf();

  // all existing logic from HadoopFileIO
}

  • Make HadoopFileIO extend BaseHadoopFileIO and use Iceberg's SerializableConfiguration:
public class HadoopFileIO implements BaseHadoopFileIO {

  private final SerializableConfiguration hadoopConf;

  public HadoopFileIO(Configuration hadoopConf) {
    this.hadoopConf = new SerializableConfiguration(hadoopConf);
  }

  public Configuration hadoopConf() {
    return hadoopConf.get();
  }
}
  • Introduce SparkHadoopFileIO that extends BaseHadoopFileIO and uses Spark's SerializableConfiguration.

  • Replace HadoopFileIO with SparkHadoopFileIO in IcebergSource.

Still ugly and won't work for arbitrary FileIO implementations but I don't see a clean solution without extra configs.

@rdblue

rdblue commented Dec 4, 2019 •

Copy link
Copy Markdown
Contributor

Here's a slightly cleaner way. I updated HadoopFileIO to use a Supplier<Configuration> instead of a SerializableConfiguration:

public class HadoopFileIO implements FileIO {

  private final Supplier<Configuration> hadoopConf;

  public HadoopFileIO(Configuration hadoopConf) {
    this(new SerializableConfiguration(hadoopConf)::get);
  }

  public HadoopFileIO(Supplier<Configuration> hadoopConf) {
    this.hadoopConf = hadoopConf;
  }

  public Configuration conf() {
    return hadoopConf.get();
  }

  ...
}

Then we just need to pass Spark's SerializableConfiguration in Spark:

    if (table.io() instanceof HadoopFileIO) {
      this.fileIo = new HadoopFileIO(new SerializableConfiguration(((HadoopFileIO) table.io()).conf())::value);
    } else {
      this.fileIo = table.io();
    }

Here's a branch: rdblue@84aa3ba

It shouldn't be too difficult to add the broadcast to that.

@aokolnychyi

aokolnychyi commented Dec 5, 2019 •

Copy link
Copy Markdown
Contributor Author

@rdblue, I like the idea and it can work if we make Supplier serializable. There is one more point we should be careful about. Iceberg's ReadTask can reference ManifestReadTask that holds a reference to FileIO. We don't broadcast that and it should work assuming that Spark will continue to use Java serialization for tasks.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

This one is ready for another review round.

@rdblue

rdblue commented Dec 9, 2019

Copy link
Copy Markdown
Contributor

+1

@rdblue
rdblue merged commit 6048e5a into apache:master Dec 9, 2019
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.

Spark ReadTask is expensive to serialize

2 participants