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 producers below and consumers above. Each producer runs its own copy of the child subplan, and each consumer reads from its own queue . The rule ROUTE is the hole.
OPEN(x) ▷ x is an exchange with n producers and m consumers- 1
create the queues Q[0 .. m - 1] - 2
start n workers; worker w runs PRODUCE(x, w) NEXT(x, j) ▷ called by consumer j- 3
while Q[j] is empty and some producer has not ended do wait - 4
if Q[j] is empty then return EOF - 5
return the next tuple from Q[j] PRODUCE(x, w)- 6
OPEN(child of x, copy w) - 7
while (t ← NEXT(child of x, copy w)) ≠ EOF do - 8
for each j in ROUTE(t) do add t to the packet for Q[j] ▷ hole: hash, range, round robin or all - 9
send each full packet; wait while its queue holds too many packets ▷ flow control - 10
send 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 : , for joins and aggregations on . See hash partitioning.
- Range on : consumer gets the keys in the -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: , for a small input that every consumer needs.
- Gather: , 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 by the same on . If , then , so and 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 local joins is therefore the full join. The same argument holds for grouping on : 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 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 is small, broadcast to all consumers and leave where it is. With and , the exchange moves 4000 tuples of and none of . A hash exchange of both inputs moves about of each input when the data starts spread evenly. Broadcast is better when is smaller than the part of 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 producers and groups, at most 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 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 and 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 producers and consumers there are 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
- Write ROUTE for a range exchange with split points , and show that the outputs of the local sorts, taken in range order, form one sorted list.
- In the broadcast join example, find the size of above which a broadcast of to consumers moves fewer tuples than a hash exchange of both inputs, if both inputs start evenly spread over the machines.
- A query computes with an exchange between a local and a global aggregation. Which state must cross the exchange, and why is sending the local averages wrong?
- 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
- Goetz Graefe, Encapsulation of parallelism in the Volcano query processing system, Proceedings of ACM SIGMOD 1990, 102 to 111, the exchange operator
- 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
- Goetz Graefe, Query evaluation techniques for large databases, ACM Computing Surveys 25(2), 73 to 169 (1993), the section on parallel query execution
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag JAW8XW Edit this page History Report an error Markdown