Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-42365

Implement parallel scan on partition engine

    XMLWordPrintable

Details

    • Task
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 13.2
    • None
    • Optimizer
    • None

    Description

      The partitioning engine (ha_partition)

      ha_partition is a handler that wraps one ordinary handler per partition (m_file[0..m_tot_parts)), all of the same engine. Every partition is a separate table in that engine; for InnoDB, each has its own clustered index and secondary indexes. The SQL layer sees one table, and ha_partition sends each call to the right partitions.

      • Pruning. Before the plan is costed, the optimizer sets part_info->read_partitions to the partitions the WHERE condition can reach. Every read loops over only those, and since MDEV-11084 only those partitions are opened and locked.
      • Unordered reads (rnd_init/rnd_next, unordered index scans) read one partition to its end, then move to the next set bit in read_partitions (ha_partition.cc:5361).
      • Ordered index scans open the same index on every partition and merge their outputs through a priority queue (handle_ordered_index_scan), so rows come out in key order.
      • Row positions are 2 bytes of partition id followed by the partition's own ref (position(), rnd_pos()). cmp_ref compares the inner ref first, then the partition id.
      • Statistics such as records_in_range and scan_time are sums over the partitions read.
      • Existing "parallel search" isn't thread parallelism. check_parallel_search() and handle_pre_scan() drive the pre_*() calls, which let Spider send its remote requests to all partitions before reading from any
        of them. It's a Spider hook, and it doesn't overlap with our worker scan.

      Our gate currently refuses partitioned driving tables outright: table_can_be_parallel_scanned() has !table->part_info (sql_parallel_costs.cc:307).

      How our chunking API maps onto it

      Our API has a coordinator side on the driving table's handler. That side initialises the scan for n workers over an index and its ranges, and hands out one opaque context per worker. Each worker, on its own handler, calls parallel_init_worker(ctx) and then reads rows until end-of-file. InnoDB keeps one coordinator per B-tree: a queue of chunks that every worker takes from, re-splitting chunks when workers run
      short.

      ha_partition would implement the API by building one coordinator per partition and gathering them into one scan:

        ┌──────────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────────┐
        │               API call               │                                                   ha_partition implementation                                                   │               Fit                │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_scan_support()              │ the underlying engine's bits (all partitions use one engine)                                                                    │ trivial                          │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_scan_pieces()               │ sum over read_partitions of each partition's pieces. Pruning has already run when the costing asks                              │ good: costing counts only        │
        │                                      │                                                                                                                                 │ partitions it will read          │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_init_coordinator(n, keynr,  │ call m_file[p]->parallel_init_coordinator() for each read partition. Indexes are local to each partition, so the same keynr and │ good, with a cost noted below    │
        │ ranges)                              │  ranges apply to all. If any partition declines, end the ones already started and decline the scan                              │                                  │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_get_worker_context(w)       │ a composite context holding worker w's context from each partition's coordinator, plus a cursor over the partitions             │ fits the opaque                  │
        │                                      │                                                                                                                                 │ Parallel_worker_ctx              │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_init_worker /               │ start in partition w mod P. On HA_ERR_END_OF_FILE from the inner partition (its chunks are used up), end that partition's       │ good: two-level load balancing   │
        │ parallel_get_next_row                │ worker and move on to the next partition that still has chunks. Keep m_last_part set, so position() stays correct               │ for free                         │
        ├──────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────┤
        │ parallel_get_chunk_stats             │ sums                                                                                                                            │ trivial                          │
        └──────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┘
      

      The API fits well; nothing in it assumes a single B-tree. Three properties make the delegation clean:

      1. The end-of-work signal already exists. parallel_init_worker() returns HA_ERR_END_OF_FILE when no chunk is left, which is exactly the "this partition is used up, move on" signal the partition loop needs.
      2. Load balancing works at both levels. Workers spread over partitions, and within a partition InnoDB's shared queue and re-splitting smooth out uneven chunks. A large partition is shared among whichever workers are still busy.
      3. A query pruned to one partition still runs in parallel, because that partition's chunks are divided among all the workers. Splitting only at partition boundaries would lose all parallelism there.

      Where it doesn't fit, or costs something

      1. Every partition's coordinator must start on the manager thread, before any worker. InnoDB's coordinator is bound to the manager's handler: it uses its m_prebuilt, its transaction, and the read view opened there. Building coordinators lazily from worker threads would touch the manager's trx concurrently, so that's out.

      • Startup cost therefore grows with the number of partitions read: each coordinator descends its B-tree and divides it at the level below the root.
      • With hundreds of partitions that's hundreds of page reads before the first row.
      • Mitigation: a partition with one leaf page gets no coordinator and is handed out whole as a single chunk.
        2. Ranges aren't pruned per partition. The ranges go to every read partition, even when the partitioning column is in the key and a given range can only hit some of them. ha_partition's MRR already avoids this with m_mrr_used_partitions and the per-interval partition iterators. The parallel path should reuse that, or it pays a B-tree descent per range per partition.
        3. The worker's copy of the table loses pruning.
      • open_worker_tables() opens each worker's copy with open_table_from_share(), which builds a fresh part_info with every partition selected.
      • The manager's read_partitions and lock_partitions must be copied into it before ha_external_lock(). Otherwise every worker opens and locks every partition.
      • Even with pruning copied, opening is W × P' handlers, where P' is the number of read partitions, each with its own InnoDB m_prebuilt and record buffers.
        4. Inner partitioned tables probably already lose pruning. The gate checks part_info only on the driving table, so an inner partitioned table is opened in the workers through the same path. The result stays correct because the WHERE condition is still evaluated, but the inner table may be read in all its partitions. I haven't verified this.
        5. Locking and the snapshot need checking, not redesign. The InnoDB checks (select_lock_type == LOCK_NONE, no REDUNDANT row format) are made per partition by the delegated init, so one declining partition makes the whole scan decline. ha_clone_consistent_snapshot() works on the transaction's engines. With InnoDB under ha_partition that should be InnoDB, but I haven't checked it.
        6. Partition-granular fallback for other engines is possible: ha_partition hands out whole partitions to workers, each scanning m_file[p] with rnd_next, for engines with no chunking of their own (MyISAM, Aria).
      • The number of pieces is the number of read partitions.
      • Uneven partitions (RANGE by date is typically very uneven) limit the speed-up.
      • Each worker needs its own handler on the partition, which our per-worker TABLE copies already give.
      • It isn't free: the snapshot code assumes a transactional engine. For non-transactional engines the manager's table lock would have to be what keeps the data stable.
        7. BLOB columns, fulltext search and temporary tables are refused by the same check. Lifting the part_info test removes only the partition restriction.

      Verdict

      The API fits well. ha_partition can implement it entirely inside the handler, by gathering one InnoDB coordinator per partition, with no change to the API and only small changes in the SQL layer:

      • remove the !table->part_info check;
      • copy the pruning bitmaps into the worker tables.

      Attachments

        Activity

          People

            Unassigned Unassigned
            Johnston Rex Johnston
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 5d
                5d
                Remaining:
                Remaining Estimate - 5d
                5d
                Logged:
                Time Spent - Not Specified
                Not Specified

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.