Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs/design: Support Spilling Unparalleled HashAgg #25792

Merged
merged 8 commits into from
Jul 12, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/design/2021-06-23-spilled-unparallel-hashagg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Proposal: Support Spilling Unparalleled HashAgg

- Author(s): [@wshwsh12](https://github.com/wshwsh12)
- Discussion PR: https://github.com/pingcap/tidb/pull/25792
- Tracking Issue: https://github.com/pingcap/tidb/issues/25882

## Table of Contents

* [Introduction](#introduction)
* [Motivation or Background](#motivation-or-background)
* [Detailed Design](#detailed-design)
* [Test Design](#test-design)
* [Functional Tests](#functional-tests)
* [Scenario Tests](#scenario-tests)
* [Compatibility Tests](#compatibility-tests)
* [Benchmark Tests](#benchmark-tests)
* [Impacts & Risks](#impacts--risks)
* [Investigation & Alternatives](#investigation--alternatives)
* [Future Work](#future-work)

## Introduction

This proposal describes the implementation of Unparalleled HashAgg that can spill intermediate data when memory usage is higher than memory quota.
Spilling for paralleled HashAgg will be supported later.

## Motivation or Background

Currently, the calculation logic of the aggregate executors in the TiDB is divided into two types, parallel and unparallel. However, when SQL memory usage exceeds the memory quota, neither implementation can use external memory to control memory usage and can only kill the SQL that is executing. In order to enable SQL to execute normally in the case of insufficient memory, we introduce the spilling algorithm for the unparallel aggregate executor.

## Detailed Design

In aggregate processing, memory increases when tuples are inserted into the hash table. So we can use the following algorithm to control the memory increasing:

1. When the memory usage is higher than the mem-quota, switch the HashAgg executor to spill-mode.
2. When HashAgg is in spill-mode, keep the tuple in the hashMap no longer growing.
a. If the processing key exists in the Map, aggregate the result.
b. If the processing key doesn't exist in the Map, spill the data to disk.
3. After all data have been processed, output the aggregate result in the Map, clear the Map. Then read the spilling data from disk, repeat the Step1-Step3 until all data have been aggregated.

## Test Design

### Functional Tests

* Querying using aggregate functions should give correct result.

### Scenario Tests

* When the unparallel-agg exceeds the memory quota, this feature helps reduce memory usage and run the sql successfully.
* When the parallel-agg exceeds the memory quota, the SQL will be canceled before. After the agg-concurrency args are set to 1, the SQL can run successfully.
* When the ndv of the data is low, the SQL contains distinct function will be canceled before. After the agg-concurrency args are set to 1, the SQL can run successfully.
* When the ndv of the data is high, the SQL contains distinct function will be canceled before. After the agg-concurrency args are set to 1, the SQL can be canceled successfully if there is insufficient memory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set the concurrency-related args when there exists an aggregation function with the keyword distinct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed


### Compatibility Tests

* N/A

### Benchmark Tests

* The feature shouldn't cause any obvious performance regression (< 2%) on non-spilling scenario.

## Impacts & Risks

* Memory will still grow without increasing the number of new tuples in HashMap for distinct aggregate function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. It's seem that there is a contradiction between memory still grow and without increasing the number of new tuples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, it is not contradict. For the distinct agg function, it is necessary to record those values that have appeared. We are using a set to record this information. This set will still grow during the aggregation process without increasing the number of new tuples in aggPartialResultMapper.
For example,

type partialResult4CountDistinctInt struct {
	valSet set.Int64SetWithMemoryUsage
}


## Investigation & Alternatives

## Future Work
1. Support friendly spilling implementation for the distinct aggregate functions.
2. Support spilling for paralleled HashAgg.