Skip to content

[SPARK-3411] Improve load-balancing of concurrently-submitted drivers across workers - #1106

Closed
WangTaoTheTonic wants to merge 7 commits into
apache:masterfrom
WangTaoTheTonic:fixBalanceDrivers
Closed

WangTaoTheTonic wants to merge 7 commits into
apache:masterfrom
WangTaoTheTonic:fixBalanceDrivers

Conversation

@WangTaoTheTonic

Copy link
Copy Markdown
Contributor

If the waiting driver array is too big, the drivers in it will be dispatched to the first worker we get(if it has enough resources), with or without the Randomization.

We should do randomization every time we dispatch a driver, in order to better balance drivers.

@AmplabJenkins

Copy link
Copy Markdown

Can one of the admins verify this patch?

@CodingCat

Copy link
Copy Markdown
Contributor

did you see any performance impact on the current strategy, "randomization at the start of every schedule point" is used not only at Master but also TaskSchedulerImpl...

until so far, it works fine......

@WangTaoTheTonic

Copy link
Copy Markdown
Contributor Author

You mean the increased shuffles may lead to a bad performance?

@CodingCat

Copy link
Copy Markdown
Contributor

yes, potentially...

and considering the long-term running of the cluster, eventually, the load is well balanced with the current strategy.....i.e. this commit only contributes to the case that the user submits a lot of drivers in a single batch and then waits for the result...

@WangTaoTheTonic

Copy link
Copy Markdown
Contributor Author

Another situation is that the works lists changes frequently, which will make drivers relaunching a lot.

Even when resources in all workers is not enough at same time, the drivers array could become larger as users keep submitting.

@CodingCat

Copy link
Copy Markdown
Contributor

I'm not sure about efficiency of changing to another mode for the extreme case that some worker (which is exactly the one running a lot of drivers) joins and leaves constantly....

for the second case, when the resources in all workers is not enough at same time, the driver cannot be scheduled.....

@WangTaoTheTonic

Copy link
Copy Markdown
Contributor Author

Make it short, the commit will better balance the load strategy when there comes a lot of drivers, while not result in bad performance when drivers is few as the randomization frequency depends on drivers number.

@andrewor14

Copy link
Copy Markdown
Contributor

@WangTaoTheTonic Can you up merge this with master? Also could you file a JIRA associated with this PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This creates multiple copies of all the workers and could be slow for large clusters. Maybe it's a better idea to keep track of a list of alive workers instead

@WangTaoTheTonic

Copy link
Copy Markdown
Contributor Author

The PR is: https://issues.apache.org/jira/browse/SPARK-3411.
Cause the filter will create copy of worker, so I change the way of filtering.
The shuffle will create copies too, could we change its way ?
@andrewor14 Please check and offer some advice.

@WangTaoTheTonic WangTaoTheTonic changed the title Optimize the schedule procedure in Master [SPARK-3411]Optimize the schedule procedure in Master Sep 5, 2014
@JoshRosen

Copy link
Copy Markdown
Contributor

Can you give this PR a more descriptive title? "Optimize the schedule procedure in Master" sounds like it could describe many different changes, so it's kind of hard to figure out what this PR might do by reading the current title.

I'd go with something like "[SPARK-3411] Improve load-balancing of concurrently-submitted drivers across workers."

@JoshRosen

Copy link
Copy Markdown
Contributor

I agree that this seems like a bit of a rare corner-case.

@andrewor14

Copy link
Copy Markdown
Contributor

@WangTaoTheTonic I looked at this more and I think it will actually be slower with the new changes. Before this patch we shuffle all the workers only once, but here we shuffle it for each waiting driver. If we have thousands of workers, shuffling can become fairly expensive.

As for the original problem you set out to solve, it seems to me that the existing code is already doing the randomization correctly. This is because we draw the first worker from a list of shuffled workers, so it will already be random by the time we draw from it.

@JoshRosen

Copy link
Copy Markdown
Contributor

If the problem is all of the drivers landing on the same randomly-chosen worker, I suppose you could treat the randomized list as a circular buffer and go through it in a round-robin order when picking which worker to launch the next driver on.

@andrewor14

Copy link
Copy Markdown
Contributor

Oh I see. Wouldn't it be sufficient to just pop the head of shuffledWorkers after allocating each driver then? Or just keep an iterator for shuffledWorkers to never reuse the same worker.

@JoshRosen

Copy link
Copy Markdown
Contributor

Yeah, I suppose so, but there was one corner-case that I was concerned about (that is addressed by treating it as a circular buffer):

Let's say we have a scenario where we are trying to schedule three drivers but there are only two workers that they can run on, and let's also say that there's initially enough capacity to run all three drivers. In that case, I think we would pop the head of shuffleWorkers until it becomes empty and not schedule the third driver. This other driver would get scheduled on a subsequent call to Master.schedule(), but I guess that only happens when new apps join or when resource availability changes, so we might wait longer than is necessary to launch this driver.

The circular buffer brings its own problems, though: let's say that there are no valid locations where we can schedule the driver. In this case, we should stop looping through the buffer so that we don't go into an infinite loop.

@andrewor14

Copy link
Copy Markdown
Contributor

Hm, it looks like launchDriver is asynchronous, so there seems to be no easy way to identify workers that have already been scheduled to launch a driver. This means even with the outstanding changes we might schedule too many drivers on the same worker. Now, we could keep track of the worker's remaining memory and cores after scheduling them to launch drivers, but this adds some complexity:

(semi-pseudocode)

// ID -> scheduled resources as a 2-tuple (memory, cores)
val scheduledWorkerResources = new HashMap[Int, (Int, Int)]

val shuffledWorkers = Random.shuffle(workers).iterator.filter(_.state == ALIVE)
for each waiting driver {
  if (shuffledWorkers.hasNext) {
    val candidateWorker = shuffledWorkers.next()
    val (scheduledMemory, scheduledCores) =
      scheduledWorkerResources.get(candidateWorker.id).getOrElse(0, 0)
    val remainingMemory = candidateWorker.memory - scheduledMemory
    val remainingCores = candidateWorker.cores - scheduledCores
    // Compare remaining resources to account for workers that have already been scheduled
    if (remainingMemory > driver.mem && remainingCores > driver.cores) {
      launchDriver(candidateWorker)
      ...
      // update scheduledWorkerResources 
      // add back this used worker into the pool to iterate through
    }
  }
}

@WangTaoTheTonic WangTaoTheTonic changed the title [SPARK-3411]Optimize the schedule procedure in Master [SPARK-3411] Improve load-balancing of concurrently-submitted drivers across workers Sep 6, 2014
@WangTaoTheTonic

Copy link
Copy Markdown
Contributor Author

To @JoshRosen , the pr title is already modified, so is the jira.
@andrewor14 i think keeping track the workers' resource is too complex. So I choose worker to launch the driver with round robin to avoid doing shuffle every time.
If all workers have no enough mem and core, we just skip this worker to schedule the next one, which is consistent with before.
What do you think guys?

@SparkQA

SparkQA commented Sep 6, 2014

Copy link
Copy Markdown

QA tests have started for PR 1106 at commit bc91bb1.

  • This patch merges cleanly.

@SparkQA

SparkQA commented Sep 6, 2014

Copy link
Copy Markdown

QA tests have finished for PR 1106 at commit bc91bb1.

  • This patch fails unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA

SparkQA commented Sep 8, 2014

Copy link
Copy Markdown

QA tests have started for PR 1106 at commit 2ca3091.

  • This patch merges cleanly.

@SparkQA

SparkQA commented Sep 8, 2014

Copy link
Copy Markdown

QA tests have finished for PR 1106 at commit 2ca3091.

  • This patch fails unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA

SparkQA commented Sep 9, 2014

Copy link
Copy Markdown

QA tests have finished for PR 1106 at commit b6560cf.

  • This patch fails unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Somewhat minor, but why don't we just declare val startPos = curPos after we increment it? Then we don't need the -1 here

@andrewor14

Copy link
Copy Markdown
Contributor

@WangTaoTheTonic This mostly looks good. I left a few more minor comments. Anything else to add @markhamstra and @JoshRosen?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could be just:

for (driver <- waitingDrivers.toList)

@SparkQA

SparkQA commented Sep 10, 2014

Copy link
Copy Markdown

QA tests have started for PR 1106 at commit d1a928b.

  • This patch merges cleanly.

@SparkQA

SparkQA commented Sep 10, 2014

Copy link
Copy Markdown

QA tests have finished for PR 1106 at commit d1a928b.

  • This patch passes unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • case class Last(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression]
    • case class LastFunction(expr: Expression, base: AggregateExpression) extends AggregateFunction
    • case class Abs(child: Expression) extends UnaryExpression

@WangTaoTheTonic
WangTaoTheTonic force-pushed the fixBalanceDrivers branch 2 times, most recently from 76346bc to d1a928b Compare September 10, 2014 09:49
@SparkQA

SparkQA commented Sep 10, 2014

Copy link
Copy Markdown

QA tests have started for PR 1106 at commit d1a928b.

  • This patch merges cleanly.

@SparkQA

SparkQA commented Sep 10, 2014

Copy link
Copy Markdown

QA tests have finished for PR 1106 at commit d1a928b.

  • This patch passes unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • case class Last(child: Expression) extends PartialAggregate with trees.UnaryNode[Expression]
    • case class LastFunction(expr: Expression, base: AggregateExpression) extends AggregateFunction
    • case class Abs(child: Expression) extends UnaryExpression

@andrewor14

Copy link
Copy Markdown
Contributor

Thanks @WangTaoTheTonic I'm merging this

@asfgit asfgit closed this in 558962a Sep 10, 2014
asfgit pushed a commit that referenced this pull request Sep 17, 2014
I think, this issue is caused by #1106

Author: Kousuke Saruta <sarutak@oss.nttdata.co.jp>

Closes #2436 from sarutak/SPARK-3571 and squashes the following commits:

7a4deea [Kousuke Saruta] Modified Master.scala to use numWorkersVisited and numWorkersAlive instead of stopPos
4e51e35 [Kousuke Saruta] Modified Master to prevent from 0 divide
4817ecd [Kousuke Saruta] Brushed up previous change
71e84b6 [Kousuke Saruta] Modified Master to enable schedule normally
asfgit pushed a commit that referenced this pull request Mar 15, 2016
## What changes were proposed in this pull request?

This patch contains the functionality to balance the load of the cluster-mode drivers among workers

This patch restores the changes in #1106 which was erased due to the merging of #731

## How was this patch tested?

test with existing test cases

Author: CodingCat <zhunansjtu@gmail.com>

Closes #11702 from CodingCat/SPARK-13803.

(cherry picked from commit bd5365b)
Signed-off-by: Sean Owen <sowen@cloudera.com>
asfgit pushed a commit that referenced this pull request Mar 15, 2016
## What changes were proposed in this pull request?

This patch contains the functionality to balance the load of the cluster-mode drivers among workers

This patch restores the changes in #1106 which was erased due to the merging of #731

## How was this patch tested?

test with existing test cases

Author: CodingCat <zhunansjtu@gmail.com>

Closes #11702 from CodingCat/SPARK-13803.
asfgit pushed a commit that referenced this pull request Mar 15, 2016
## What changes were proposed in this pull request?

This patch contains the functionality to balance the load of the cluster-mode drivers among workers

This patch restores the changes in #1106 which was erased due to the merging of #731

## How was this patch tested?

test with existing test cases

Author: CodingCat <zhunansjtu@gmail.com>

Closes #11702 from CodingCat/SPARK-13803.

(cherry picked from commit bd5365b)
Signed-off-by: Sean Owen <sowen@cloudera.com>
asfgit pushed a commit that referenced this pull request Mar 15, 2016
## What changes were proposed in this pull request?

This patch contains the functionality to balance the load of the cluster-mode drivers among workers

This patch restores the changes in #1106 which was erased due to the merging of #731

## How was this patch tested?

test with existing test cases

Author: CodingCat <zhunansjtu@gmail.com>

Closes #11702 from CodingCat/SPARK-13803.

(cherry picked from commit bd5365b)
Signed-off-by: Sean Owen <sowen@cloudera.com>
roygao94 pushed a commit to roygao94/spark that referenced this pull request Mar 22, 2016
## What changes were proposed in this pull request?

This patch contains the functionality to balance the load of the cluster-mode drivers among workers

This patch restores the changes in apache#1106 which was erased due to the merging of apache#731

## How was this patch tested?

test with existing test cases

Author: CodingCat <zhunansjtu@gmail.com>

Closes apache#11702 from CodingCat/SPARK-13803.
udaynpusa pushed a commit to mapr/spark that referenced this pull request Jan 30, 2024
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Oct 29, 2026
…ssion (apache#1107)

### What changes were proposed in this pull request?

The date, interval and integer kernels' answer checks now run without a Spark session, through the expression harness that the coverage and TIME suites already use. `VarkaDifferentialSuite` keeps the 27 tests that need a session.

**This PR sits on apache#1106, which sits on apache#1105.** Its own commits are the last two.

- **`VarkaDifferentialHarnessSuite`** holds 68 of the old suite's 94 tests, under their old names, over the same tables. The tables are now SQL `VALUES` with the old nullability: where the old table declared a column nullable that holds no null, an all-null row goes into the `VALUES` and is filtered out again. Without it, Spark would read the column as non-nullable, and the optimizer would rewrite some queries; `coalesce(d, d2)`, for example, would become `d`.
- **`VarkaHarnessCases`** now lets one test run its queries in parts, each with its own table, ANSI mode and emit options, as the old tests set them between queries. Each query states what the old test asserted of the plan and the node's metrics:

  | Expectation | Old assertion |
  |---|---|
  | something fused, and a kernel answered a batch | `checkDifferential(..., expectFused = true)` |
  | something fused | `assertFused` alone |
  | fused whole and served every batch | `numFallbackBatchesDeclined === 0` beside a kernel run |
  | fused whole, and a kernel declined a batch with its own status | `numFallbackBatchesDeclined > 0` |
  | nothing fused | `expectFused = false` |

  Where the old test read verbose `EXPLAIN`, the harness checks the same lines from `VarkaFusionReport`: `"far: residual (day range ["`, `"kernel 2 of 2"` and the others.
- **Two changes to how the harness runs a query:**
  - *Filters:* the filter evaluator is handed only the conjuncts the compiler fuses. That is what `VarkaColumnarRule` hands the Varka node, leaving the rest to a row filter above it. The evaluator compiles only a condition that fuses whole, so without this a filter with one residual conjunct fell back on every batch.
  - *The row consumer:* it now compares only the outputs `projectFused` returns. Projections with residual or forwarded entries therefore run through the row node's path too.
- **`VarkaDifferentialSuite`** keeps what needs a session:
  - the plan around the Varka nodes: a narrowing projection absorbed, a residual conjunct's row filter and the order Spark evaluates conjuncts in, a projection stacked on a filter's compacted batch, the common-expression hoist;
  - the fallback hooks and the metrics they move;
  - AQE, aggregation, the cache's batches, tasks sharing a kernel class, a non-Arrow source, the SQL UI's residual count, Metaspace;
  - the `guardDayProducers=false` test, which asserts only the metric, never the wrong values that configuration produces; the harness compares every answer;
  - the `ILLEGAL_DAY_OF_WEEK` error a user sees. No coverage row raises that condition, so no other session test shows it.
- **`VarkaSharedSessions`** drops the seven fixtures no suite reads any more.
- **`VarkaHarnessParitySuite`** plans every hand-written query of both harness suites in a session, under its part's ANSI mode, and requires each expression to equal the harness's.
- **`skips.tsv`:**
  - 11 lines follow their tests to the new suite.
  - The `arrowBatchSize=1` line for "null-as-false, and the boundary selectivities" is removed. Its reason was the in-memory scan's statistics pruning one-row batches, and the harness has no scan, so the test now passes there.
- **Docs:** `ADDING_AN_EXPRESSION.md` and `docs/sql-varka.md` now point to the harness suite.

### Why are the changes needed?

The suite spent its time on session work. It ran each query through two sessions, sorted the results, and ran the query a second time for the warm cache. It took 66 s at the defaults on the laptop, and 3.66 h of the option matrix's run 40658422887. The checks need only the expressions and the batches.

| Configuration | Before (old suite) | After (harness suite + session suite) |
|---|---|---|
| defaults | 66 s | 4.1 s + 36.7 s |
| `arrowBatchSize=1` | 70 s | 4.6 s + 36.4 s |

Results, with each of the 94 old tests mapped to where its check now lives: `sql/varka/plans/m8/results/VARKA-509-step3b-local.txt`.

### Does this PR introduce _any_ user-facing change?

No. Tests only.

### How was this patch tested?

- All 1,211 tests of the harness and session suites this work touches pass at the defaults, in one JVM. That includes the coverage suites and the 68 new tests.
- I ran the six configurations that have skip lines for these tests locally: `checkIntOverflow=false`, `guardDayProducers=false`, `guardUnderArm=false`, `severalKernels=false`, `arrowBatchSize=1` and `validityByWidth=false`. Each test cancels where its line says, and nothing else fails. The CI matrix covers the other configurations.
- `build/sbt 'sql/testOnly *VarkaDifferentialHarnessSuite'` runs the 68 tests, as `ADDING_AN_EXPRESSION.md` now says.
- The linters pass.

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Opus 5.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants