Algorithm Search⌘K

Techniquecore

Exchange operator

Also called Volcano exchange, Graefe exchange operator.

An operator that splits a plan into parallel streams by partitioning, broadcasting or merging tuples, so other operators stay serial.

The exchange operator puts parallelism into a query plan as one more operator. Below it, several copies of a subplan run in their own processes or threads; above it, one or more consumers read the tuples. In between, exchange moves each tuple to a consumer by a rule: a hash of a key, a key range, round robin, a copy to all, or a merge that keeps a sort order. To its parent and to its child it offers the same open, next and close calls as any operator of the iterator model, so scans, joins and aggregations stay serial code and need no change for parallel execution. Graefe introduced it in the Volcano system in 1990.

In plain words

A post office sorts letters by city. You do not teach every clerk how to work in a team. You give each clerk a pile and the same old job, and you put a row of bins between the clerks and the delivery workers, one bin per worker. A clerk drops each letter into the bin of the worker for its city. Each delivery worker takes letters only from his bin and does his old job. The bins are the exchange. Because the city decides the bin, all letters for one city meet in the same place, even though many clerks worked on them.

When it applies

  • A plan must run on several cores or several machines, and the operators are written for one thread, as in Volcano-style engines.
  • The work can be split by data: rows that must meet, such as the rows of one join key or one group, can be sent to the same consumer by a function of the row.
  • A pipeline should run in two processes at the same time, the producer and the consumer, with a buffer between them.
  • The degree of parallelism can be chosen when the plan is built. If it must change while the query runs, see morsel-driven parallelism.

The template

An exchange has nn producers below and mm consumers above. Each producer runs its own copy of the child subplan, and each consumer jj reads from its own queue Q[j]Q[j]. The rule ROUTE is the hole.

  1. OPEN(x) ▷ x is an exchange with n producers and m consumers
  2. 1create the queues Q[0 .. m - 1]
  3. 2start n workers; worker w runs PRODUCE(x, w)
  4. NEXT(x, j) ▷ called by consumer j
  5. 3while Q[j] is empty and some producer has not ended do wait
  6. 4if Q[j] is empty then return EOF
  7. 5return the next tuple from Q[j]
  8. PRODUCE(x, w)
  9. 6OPEN(child of x, copy w)
  10. 7while (t ← NEXT(child of x, copy w)) ≠ EOF do
  11. 8for each j in ROUTE(t) do add t to the packet for Q[j] ▷ hole: hash, range, round robin or all
  12. 9send each full packet; wait while its queue holds too many packets ▷ flow control
  13. 10send the last packets, CLOSE(child of x, copy w), and mark worker w as ended

Tuples move in packets of many tuples, so the cost of the queue and of the wake-up of a consumer is paid once per packet. Flow control stops a fast producer from filling the memory when its consumer is slow. A merging exchange uses one queue per producer at each consumer and returns the smallest head with a k-way merge, so sorted producer streams give a sorted consumer stream.

The rules for ROUTE:

  • Hash on a key kk: ROUTE(t)={h(t.k) mod m}\text{ROUTE}(t) = \{h(t.k) \bmod m\}, for joins and aggregations on kk. See hash partitioning.
  • Range on kk: consumer jj gets the keys in the jj-th range, for sorting. See range partitioning.
  • Round robin: the tuples go to the consumers in turn, which gives equal counts when no key matters.
  • Broadcast: ROUTE(t)={0,…,m−1}\text{ROUTE}(t) = \{0, \ldots, m - 1\}, for a small input that every consumer needs.
  • Gather: m=1m = 1, to bring parallel streams back to one consumer, for example at the root of the plan.

Why a hash exchange is correct for an equijoin. Exchange both inputs of R⋈R.k=S.kSR \bowtie_{R.k = S.k} S by the same hh on kk. If r.k=s.kr.k = s.k, then h(r.k)=h(s.k)h(r.k) = h(s.k), so rr and ss reach the same consumer, and the serial join there finds the pair. Each tuple goes to exactly one consumer, so no pair is found twice. The union of the mm local joins is therefore the full join. The same argument holds for grouping on kk: all rows of a group meet at one consumer. A null key never matches in a join, so its consumer does not matter, but for grouping all nulls must go to the same consumer, which a hash of the null value does.

Classic examples

  • Parallel hash join. Hash exchanges on both inputs, then mm serial hash joins, one per consumer. This is the plan of a parallel hash join on a shared-nothing system, where the exchange sends tuples over the network.
  • Broadcast join. If SS is small, broadcast SS to all consumers and leave RR where it is. With m=4m = 4 and ∣S∣=1000|S| = 1000, the exchange moves 4000 tuples of SS and none of RR. A hash exchange of both inputs moves about (1−1/m)(1 - 1/m) of each input when the data starts spread evenly. Broadcast is better when m∣S∣m |S| is smaller than the part of RR that would move.
  • Two-phase aggregation. Each producer aggregates its own rows, a hash exchange on the grouping key sends the partial states, and each consumer merges them. With nn producers and dd groups, at most ndn d partial states cross the exchange, in place of all the rows. This is the combiner of MapReduce.
  • Parallel sort. A range exchange on the sort key, then a local sort at each consumer. The outputs of the consumers, taken in range order, are the sorted result. The other way is a local sort at each producer and a merging exchange.
  • Pipelined parallelism. An exchange with n=m=1n = m = 1 only puts a queue between two parts of a pipeline, so that they run on two cores at the same time.

Variations

  • Kinds of parallelism. A hash or range exchange gives partitioned parallelism inside one operator. An exchange with one producer and one consumer gives pipelined parallelism between operators. Two subtrees under separate exchanges can run at the same time, which gives parallelism between independent parts of a bushy plan.
  • Inside one machine and across machines. In one machine the queues are in shared memory. Across machines the same operator sends packets over the network; many distributed engines call the same operator a shuffle.
  • Repartition only when needed. If an input is already partitioned on the key, for example a table stored by hash partitioning on the join key, the optimizer leaves out the exchange. Partitioning is a property of a stream, like a sort order.
  • Fixed against elastic parallelism. The classic exchange fixes nn and mm in the plan. Morsel-driven engines let threads take small pieces of input at run time, which balances the load and lets the number of threads change during the query.

Pitfalls

  • Skew. A hash exchange sends all rows of one key to one consumer. If one key has a large share of the rows, that consumer does most of the work, and the query runs at its speed. See data skew. Splitting hot keys or broadcasting their partners helps.
  • Memory of the queues. With nn producers and mm consumers there are nmn m streams, and each needs packet buffers. On many cores or many machines this grows fast.
  • Deadlock with merging and flow control. A merging consumer must wait for a packet from one given producer. If that producer is blocked because the queue of another consumer is full, and that consumer waits for this one, no one moves. Larger buffers or spooling to disk break the cycle.
  • Order is lost. A hash or round robin exchange mixes the streams, so an order from below is lost unless the exchange merges.
  • Startup cost. Starting processes and queues costs time. For a small input a serial plan is faster, and Amdahl's law limits the gain when part of the plan stays serial, for example a gather at the root.

Exercises

  1. Write ROUTE for a range exchange with split points s1<⋯<sm−1s_1 < \cdots < s_{m-1}, and show that the outputs of the local sorts, taken in range order, form one sorted list.
  2. In the broadcast join example, find the size of RR above which a broadcast of SS to mm consumers moves fewer tuples than a hash exchange of both inputs, if both inputs start evenly spread over the mm machines.
  3. A query computes γg; AVG(v)(R)\gamma_{g;\,\text{AVG}(v)}(R) with an exchange between a local and a global aggregation. Which state must cross the exchange, and why is sending the local averages wrong?
  4. Give a plan with two merging exchanges and bounded queues that can deadlock, and say how spooling one input to disk removes the deadlock.

References

  1. Goetz Graefe, Encapsulation of parallelism in the Volcano query processing system, Proceedings of ACM SIGMOD 1990, 102 to 111, the exchange operator
  2. Goetz Graefe, Volcano, an extensible and parallel query evaluation system, IEEE Transactions on Knowledge and Data Engineering 6(1), 120 to 135 (1994), the exchange operator and its use for parallel query evaluation
  3. Goetz Graefe, Query evaluation techniques for large databases, ACM Computing Surveys 25(2), 73 to 169 (1993), the section on parallel query execution
  4. David DeWitt and Jim Gray, Parallel database systems, the future of high performance database systems, Communications of the ACM 35(6), 85 to 98 (1992), pipelined and partitioned parallelism, and the split and merge of data streams

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises