title: Evaluating Spark DataFrame in loop slows down with every iteration, all work done by controller
body: <p>I am trying to use a Spark cluster (running on AWS EMR) to link groups of items that have common elements in them. Essentially, I have groups with some elements and if some of the elements are in multiple groups, I want to make one group that contains elements from all of those groups.</p>
  
  <p>I know about GraphX library and I tried to use <a href="http://graphframes.github.io/" rel="noreferrer">graphframes</a> package (<code>ConnectedComponents</code> algorithm) to resolve this task, but it seams that the <code>graphframes</code> package is not yet mature enough and is very wasteful with resources... Running it on my data set (cca 60GB) it just runs out of memory no matter how much I tune the Spark parameters, how I partition and re-partion my data or how big cluster I create (the graph IS huge).</p>
  
  <p>So I wrote my own code do accomplish the task. <strong>The code works and it solves my problem, but it slows down with every iteration. Since it can take sometimes around 10 iterations to finish, it can run very long and I could not figure out what the problem is.</strong></p>
  
  <p>I start with a table (DataFrame) <code>item_links</code> that has two columns: <code>item</code> and <code>group_name</code>. Items are unique within each group, but not within this table. One item can be in multiple groups. If two items each have a row with the same group name, they both belong to the same group.</p>
  
  <p>I first group by item and find for every item the smallest of all group names from all groups that it belongs to. I append this information as an extra column to the original DataFrame. Then I create a new DataFrame by groupping by the group name and finding the smallest value of this new column within every group. I join this DataFrame with my original table on the group name and replace the group name column with the minimum value from that new column. The idea is, that if a group contains an item that also belongs to some smaller group, this group will be merged it. In every iteration it links groups that were indirectly linked by more and more items in between.</p>
  
  <p>The code that I am running looks like this:</p>
  
  <pre><code>print(" Merging groups that have common items...")
  
  n_partitions = 32
  
  merge_level = 0
  
  min_new_group = "min_new_group_{}".format(merge_level)
  
  # For every item identify the (alphabetically) first group in which this item was found
  # and add a new column min_new_group with that information for every item.
  first_group = item_links \
                      .groupBy('item') \
                      .agg( min('group_name').alias(min_new_group) ) \
                      .withColumnRenamed('item', 'item_id') \
                      .coalesce(n_partitions) \
                      .cache()
  
  item_links = item_links \
                  .join( first_group,
                         item_links['item'] == first_group['item_id'] ) \
                  .drop(first_group['item_id']) \
                  .coalesce(n_partitions) \
                  .cache()
  
  first_group.unpersist()
  
  # In every group find the (alphabetically) smallest min_new_group value.
  # If the group contains a item that was in some other group,
  # this value will be different than the current group_name.
  merged_groups = item_links \
                      .groupBy('group_name') \
                      .agg(
                          min(col(min_new_group)).alias('merged_group')
                      ) \
                      .withColumnRenamed('group_name', 'group_to_merge') \
                      .coalesce(n_partitions) \
                      .cache()
  
  # Replace the group_name column with the lowest group that any of the item in the group had.
  item_links = item_links \
                  .join( merged_groups,
                         item_links['group_name'] == merged_groups['group_to_merge'] ) \
                  .drop(item_links['group_name']) \
                  .drop(merged_groups['group_to_merge']) \
                  .drop(item_links[min_new_group]) \
                  .withColumnRenamed('merged_group', 'group_name') \
                  .coalesce(n_partitions) \
                  .cache()
  
  # Count the number of common items found
  common_items_count = merged_groups.filter(col('merged_group') != col('group_to_merge')).count()
  
  merged_groups.unpersist()
  
  # just some debug output
  print("  level {}: found {} common items".format(merge_level, common_items_count))
  
  # As long as the number of groups keep decreasing (groups are merged together), repeat the operation.
  while (common_items_count &gt; 0):
      merge_level += 1
  
      min_new_group = "min_new_group_{}".format(merge_level)
  
      # for every item find new minimal group...
      first_group = item_links \
                          .groupBy('item') \
                          .agg(
                              min(col('group_name')).alias(min_new_group)
                          ) \
                          .withColumnRenamed('item', 'item_id') \
                          .coalesce(n_partitions) \
                          .cache() 
  
      item_links = item_links \
                      .join( first_group,
                             item_links['item'] == first_group['item_id'] ) \
                      .drop(first_group['item']) \
                      .coalesce(n_partitions) \
                      .cache()
  
      first_group.unpersist()
  
      # find groups that have items from other groups...
      merged_groups = item_links \
                          .groupBy(col('group_name')) \
                          .agg(
                              min(col(min_new_group)).alias('merged_group')
                          ) \
                          .withColumnRenamed('group_name', 'group_to_merge') \
                          .coalesce(n_partitions) \
                          .cache()
  
      # merge the groups with items from other groups...
      item_links = item_links \
                      .join( merged_groups,
                             item_links['group_name'] == merged_groups['group_to_merge'] ) \
                      .drop(item_links['group_name']) \
                      .drop(merged_groups['group_to_merge']) \
                      .drop(item_links[min_new_group]) \
                      .withColumnRenamed('merged_group', 'group_name') \
                      .coalesce(n_partitions) \
                      .cache()
  
      common_items_count = merged_groups.filter(col('merged_group') != col('group_to_merge')).count()
  
      merged_groups.unpersist()
  
      print("  level {}: found {} common items".format(merge_level, common_items_count))
  </code></pre>
  
  <p>As I said, it works, but the problem is, that it slows down with every iteration. The iterations 1-3 run just a few seconds or minutes. Iteration 5 runs around 20-40 minutes. Iteration 6 sometimes doesn't even finish, because controller runs out of memory (14 GB for controller, around 140 GB of RAM for the entire cluster with 20 CPU cores... the test data is around 30 GB).</p>
  
  <p>When I monitor the cluster in Ganglia, I see, that after every iteration the workers perform less and less work and the controller performs more and more. The network traffic also goes down to zero. Memory usage is rather stable after the initial phase.</p>
  
  <p>I read lot about re-partitioning, turning Spark parameters and background of shuffle operations and I did my best to optimize everything, but I have no idea what's going on here. Below is a load of my cluster nodes (yellow for controller node) over time as the code above is running.</p>
  
  <p><a href="https://i.stack.imgur.com/opd1a.png" rel="noreferrer"><img src="https://i.stack.imgur.com/opd1a.png" alt="load of cluster nodes, yellow is controller"></a></p>