# I wrote a Spark job. The stack trace was Hadoop.

> Two days after getting Spark running locally I pointed it at S3, and everything that broke was named org.apache.hadoop. What Hadoop actually is, which third of it Spark replaced, and why committing a write to object storage is still a problem.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-01-25
- Tags: Apache Spark, Apache Hadoop, AWS, Amazon S3, Amazon EMR, Python, Data Engineering
- URL: https://deyanpeev.com/posts/the-stack-trace-was-hadoop

[Sunday's post](https://deyanpeev.com/posts/the-cluster-went-away-the-shuffle-did-not) ended with a
Spark container on my laptop reading a folder of CSV files off local disk. The
obvious next step was to read the same data out of S3, because that's where
data lives and because it is what EMR Serverless will want when the preview
lets me in.

That took an evening, and not for any of the reasons I'd budgeted for. Every
single thing that went wrong was called `org.apache.hadoop` something, in a job
with no Hadoop cluster, no HDFS, and no MapReduce anywhere near it. Working out
why was the most useful thing I learned this month.

# "Hadoop" is three things, and Spark replaced one of them

I had been carrying around the sentence "Spark replaced Hadoop" without ever
asking what the words meant. Hadoop is a project more than a program, and it
has three separable pieces:

- **HDFS**, a distributed filesystem. The storage.
- **YARN**, a cluster resource manager, which decides which machine runs what.
- **MapReduce**, a compute engine. This is what processes the data.

Spark replaced one of those. It's a compute engine. It stores nothing, and by
itself it schedules nothing across a cluster.

So "we moved off Hadoop onto Spark" can mean at least four different things.
Usually it means the MapReduce jobs became Spark jobs and everything underneath
stayed where it was.

The difference between the two engines is narrower than the marketing suggested
and lines up neatly with Sunday's post. MapReduce materialises intermediate
results to disk between every map and reduce step. Spark builds a plan of stages
and keeps data in memory across the narrow transformations inside a stage,
writing out only at shuffle boundaries. That's where its advantage comes from,
so Spark's lead over MapReduce is largest on jobs with many chained steps and
smallest on a job that is basically one big shuffle.

# Storage and scheduling still come from somewhere

Storage has to come from somewhere. HDFS is one option, but on AWS hardly
anyone chooses it as the store of record. You put the data in S3 and let
compute be a separate, disposable thing. I think that separation is the big
architectural change of the last decade, bigger than the engine swap.

Scheduling has to come from somewhere too. Spark can run standalone, on YARN, on
Mesos, or on Kubernetes. On EMR it runs on YARN. On EMR Serverless you never see
a resource manager at all, and that was the subject of Sunday's post.

So a modern Spark-on-AWS job uses none of HDFS, none of MapReduce, and (on the
serverless option) none of YARN that you can observe. And the jars are still
everywhere.

# The fourth thing, which is the one that bit me

The piece that rarely gets listed is Hadoop Common, the shared libraries. The
important one in there is `org.apache.hadoop.fs.FileSystem`, the abstraction
that everything in this ecosystem reads and writes through.

Spark does not have an S3 client. It asks Hadoop's `FileSystem` for a path, and
the implementation for `s3a://` lives in a Hadoop module called `hadoop-aws`.
So that's why my stack traces looked the way they did.

The binary I had been using is `spark-3.1.2-bin-hadoop3.2`, which ships Hadoop
3.2.0 jars but not the cloud connectors. So the first attempt ends at:

```
java.lang.ClassNotFoundException: Class org.apache.hadoop.fs.s3a.S3AFileSystem not found
```

The fix is to add the module, and the version isn't a matter of taste:

```bash
pyspark \
  --packages org.apache.hadoop:hadoop-aws:3.2.0,com.amazonaws:aws-java-sdk-bundle:1.11.375
```

`3.2.0` because that is the version of the Hadoop jars already inside the
tarball, and `hadoop-aws` isn't versioned independently from the rest of
Hadoop. Put a newer 3.3.x `hadoop-aws` next to 3.2.0 `hadoop-common` and it
doesn't fail cleanly at startup, it fails later with a `NoSuchMethodError` from
somewhere unrelated-looking. `1.11.375` is fixed too. It's the AWS SDK version
that `hadoop-aws` 3.2.0 was compiled against and declares as its dependency.

That's two version numbers that must agree with a third one you didn't pick,
and nothing checks any of it for you. An hour went here.

Then the configuration, which is Hadoop configuration wearing a Spark prefix:

```python
hadoop_conf = spark.sparkContext._jsc.hadoopConfiguration()
hadoop_conf.set("fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
hadoop_conf.set(
    "fs.s3a.aws.credentials.provider",
    "com.amazonaws.auth.DefaultAWSCredentialsProviderChain",
)

events = spark.read.parquet("s3a://my-bucket/events/")
```

I'd set the credentials provider explicitly instead of putting an access key
into Spark configuration, where it will end up in the event log, the UI, and
eventually somebody's screenshot. The default chain picks up the environment,
the profile, or the instance role, and that covers all three of the places this
job might run.

# Reading was fine. Writing was slow.

Reading worked after that. Writing a partitioned Parquet output took far longer
than the computation that produced it, and sat there doing nothing visible at
the end of an otherwise finished job.

It wasn't a bug or a misconfiguration. It follows from the first half of this
post.

Hadoop's commit protocol was designed for HDFS, where renaming a directory is a
cheap atomic metadata operation. `FileOutputCommitter` uses that. Each task
writes its output into a temporary location, and committing means renaming it
into the final path. Rename is how you get the guarantee that either all of the
output appears or none of it does.

S3 has no rename. The S3A client emulates one by copying every object to the new
key and deleting the original. So the commit phase of the job is a full copy of
everything you just produced, performed after all the real work is done. It is
not atomic either, so it can fail halfway and leave a partial result behind, and
nothing prevents two processes attempting it at once.

There are answers, and both work the same way. Since Hadoop 3.1 there are the
S3A committers (directory, partitioned and magic), which use S3 multipart
uploads. The task streams its data to S3 as it works but doesn't complete the
upload, so committing is a cheap API call that makes already-uploaded bytes
visible, and there is no copy. On EMR, the EMRFS S3-optimized committer does the
same thing for `s3://` paths, and AWS's own comparison against
`FileOutputCommitter` v1 puts the difference in multiples, not percentages.

Turning the committers on in open-source Spark needs the binding classes from
Spark's `hadoop-cloud` module, and that isn't in the binary tarball either.
That is where I stopped for the evening, so this is a description of the fix
and not a measurement of it. I haven't yet run a job with a committer
configured, and I would want to see the numbers on my own data before repeating
anybody's.

# The consistency problem that already went away

A lot of what I read while digging conflated two separate problems.

S3 used to be eventually consistent for listings. A job could write files and
then not see them. That's why S3Guard and EMRFS Consistent View existed (a
DynamoDB table whose entire purpose was recording which objects really exist).
On 1 December 2020 S3 became strongly read-after-write consistent for all
requests, at no extra cost and with no change to performance, and that layer
became redundant.

The rename problem survived that, becuase it was never a consistency problem.
It's about cost. Rename is a metadata update on a filesystem and a full copy on
an object store, and strong consistency makes the copy correct but not cheap.
If you read older material about writing to S3 from Spark, half the advice has
expired and half of it has not, and this is the line that separates them.

# Read the package name first

The practical change is small and I suspect it'll save me a lot of time. Read
the package name before reading the message.

`org.apache.spark` in a stack trace is my logic, my plan, or my partitioning
(Sunday's post). `org.apache.hadoop.fs` is the storage layer, and the answer is
almost always a version number, a credentials provider, or a committer. I'd
been treating those as one undifferentiated category called "Spark is broken",
and they are not remotely the same problem.

The broader thing I got wrong is this. The three famous pieces of Hadoop are
the ones every migration story is about, and all three are gone from a job like
mine. The piece that doesn't get migrated off is the one in the middle, the
filesystem API, because it's the interface every connector in the ecosystem was
written against. EMR Serverless takes away the cluster and the resource
manager, and I do want that. It does not take away `hadoop-aws`, and that code
still assumes rename is cheap.
