IndexShuffleBlockResolver

IndexShuffleBlockResolver is a concrete ShuffleBlockResolver that manages shuffle block data and uses shuffle index files for faster shuffle data access.

IndexShuffleBlockResolver can write, look up and remove shuffle block index and data files (given shuffle and map IDs).

IndexShuffleBlockResolver is created and managed exclusively by SortShuffleManager (so it can access shuffle block data).

spark IndexShuffleBlockResolver SortShuffleManager
Figure 1. SortShuffleManager creates IndexShuffleBlockResolver

IndexShuffleBlockResolver is later used to create the ShuffleWriter given a ShuffleHandle.

Enable ALL logging level for org.apache.spark.shuffle.IndexShuffleBlockResolver logger to see what happens inside.

Add the following line to conf/log4j.properties:

log4j.logger.org.apache.spark.shuffle.IndexShuffleBlockResolver=ALL

Refer to Logging.

Creating IndexShuffleBlockResolver Instance

IndexShuffleBlockResolver takes the following to be created:

IndexShuffleBlockResolver initializes the internal properties.

Writing Shuffle Index and Data Files — writeIndexFileAndCommit Method

writeIndexFileAndCommit(
  shuffleId: Int,
  mapId: Int,
  lengths: Array[Long],
  dataTmp: File): Unit

Internally, writeIndexFileAndCommit first finds the index file for the input shuffleId and mapId.

writeIndexFileAndCommit creates a temporary file for the index file (in the same directory) and writes offsets (as the moving sum of the input lengths starting from 0 to the final offset at the end for the end of the output file).

The offsets are the sizes in the input lengths exactly.
spark IndexShuffleBlockResolver writeIndexFileAndCommit
Figure 2. writeIndexFileAndCommit and offsets in a shuffle index file

writeIndexFileAndCommit requests a shuffle block data file for the input shuffleId and mapId.

writeIndexFileAndCommit checks if the given index and data files match each other (aka consistency check).

If the consistency check fails, it means that another attempt for the same task has already written the map outputs successfully and so the input dataTmp and temporary index files are deleted (as no longer correct).

If the consistency check succeeds, the existing index and data files are deleted (if they exist) and the temporary index and data files become "official", i.e. renamed to their final names.

In case of any IO-related exception, writeIndexFileAndCommit throws a IOException with the messages:

fail to rename file [indexTmp] to [indexFile]

or

fail to rename file [dataTmp] to [dataFile]
writeIndexFileAndCommit is used when ShuffleWriters are requested to write records to a shuffle system, i.e. SortShuffleWriter, BypassMergeSortShuffleWriter, and UnsafeShuffleWriter.

Creating ManagedBuffer to Read Shuffle Block Data File — getBlockData Method

getBlockData(blockId: ShuffleBlockId): ManagedBuffer
getBlockData is part of ShuffleBlockResolver contract.

Internally, getBlockData finds the index file for the input shuffle blockId.

ShuffleBlockId knows shuffleId and mapId.

getBlockData discards blockId.reduceId bytes of data from the index file.

getBlockData uses Guava’s com.google.common.io.ByteStreams to skip the bytes.

getBlockData reads the start and end offsets from the index file and then creates a FileSegmentManagedBuffer to read the data file for the offsets (using transportConf internal property).

The start and end offsets are the offset and the length of the file segment for the block data.

In the end, getBlockData closes the index file.

Checking Consistency of Shuffle Index and Data Files and Returning Block Lengths —  checkIndexAndDataFile Internal Method

checkIndexAndDataFile(
  index: File,
  data: File,
  blocks: Int): Array[Long]

checkIndexAndDataFile first checks if the size of the input index file is exactly the input blocks multiplied by 8.

checkIndexAndDataFile returns null when the numbers, and hence the shuffle index and data files, don’t match.

checkIndexAndDataFile reads the shuffle index file and converts the offsets into lengths of each block.

checkIndexAndDataFile makes sure that the size of the input shuffle data file is exactly the sum of the block lengths.

checkIndexAndDataFile returns the block lengths if the numbers match, and null otherwise.

checkIndexAndDataFile is used exclusively when IndexShuffleBlockResolver is requested to write the shuffle index and data files (for shuffle and map IDs).

Requesting Shuffle Block Index File (from DiskBlockManager) — getIndexFile Internal Method

getIndexFile(shuffleId: Int, mapId: Int): File

getIndexFile then requests DiskBlockManager for the shuffle index file given the input shuffleId and mapId (as ShuffleIndexBlockId)

Requesting Shuffle Block Data File — getDataFile Method

getDataFile(
  shuffleId: Int,
  mapId: Int): File

getDataFile requests the BlockManager for the DiskBlockManager that is in turn requested for the shuffle block data file (for a ShuffleDataBlockId)

getDataFile is used when:

Removing Shuffle Index and Data Files (For Shuffle and Map IDs) — removeDataByMap Method

removeDataByMap(shuffleId: Int, mapId: Int): Unit

removeDataByMap finds and deletes the shuffle data for the input shuffleId and mapId first followed by finding and deleting the shuffle data index file.

When removeDataByMap fails deleting the files, removeDataByMap prints out the following WARN message to the logs.

Error deleting data [path]

or

Error deleting index [path]
removeDataByMap is used exclusively when SortShuffleManager is requested to unregister a shuffle (remove a shuffle from a shuffle system).

Stopping IndexShuffleBlockResolver — stop Method

stop(): Unit

stop is a noop operation, i.e. does nothing when called.

Internal Properties

Table 1. IndexShuffleBlockResolver’s Internal Properties (e.g. Registries, Counters and Flags)
Name Description

transportConf

TransportConf for shuffle module

Created immediately when IndexShuffleBlockResolver is created by requesting SparkTransportConf object to create one from SparkConf