2022-02-16 02:13:55 +08:00
# DAG
2022-10-31 01:47:12 +08:00
`hyperf/dag` is a lightweight directed acyclic graph (**D**irected **A**cyclic **G**raph) task orchestration library.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Scenes
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Suppose we have a series of tasks to perform.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
- If there is a dependency between them, they can be executed sequentially.
- If they do not depend on each other, then we can choose to execute concurrently to speed up the execution.
- There is also an intermediate state between the two: some tasks have dependencies, while others can be executed concurrently.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
We can solve the third complex scenario by abstracting it into a `DAG` .
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Install
2022-02-16 02:13:55 +08:00
```bash
composer require hyperf/dag
```
2022-10-31 01:47:12 +08:00
## Example
2022-02-16 02:13:55 +08:00
[![ ](https://mermaid.ink/img/eyJjb2RlIjoic3RhdGVEaWFncmFtLXYyXG4gICAgWypdIC0tPiBBXG4gICAgQSAtLT4gQlxuICAgIEEgLS0-IENcbiAgICBBIC0tPiBEXG4gICAgRCAtLT4gR1xuICAgIEMgLS0-IEdcbiAgICBDIC0tPiBGXG4gICAgQiAtLT4gRlxuICAgIEIgLS0-IEVcbiAgICBCIC0tPiBIXG4gICAgSCAtLT4gSVxuICAgIEUgLS0-IElcbiAgICBGIC0tPiBJXG4gICAgRyAtLT4gSVxuICAgIEkgLS0-IFsqXVxuICAgICAgICAgICAgIiwibWVybWFpZCI6eyJ0aGVtZSI6ImRlZmF1bHQiLCJ0aGVtZVZhcmlhYmxlcyI6eyJiYWNrZ3JvdW5kIjoid2hpdGUiLCJwcmltYXJ5Q29sb3IiOiIjRUNFQ0ZGIiwic2Vjb25kYXJ5Q29sb3IiOiIjZmZmZmRlIiwidGVydGlhcnlDb2xvciI6ImhzbCg4MCwgMTAwJSwgOTYuMjc0NTA5ODAzOSUpIiw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iBJXG4gICAgRyAtLT4gSVxuICAgIEkgLS0-IFsqXVxuICAgICAgICAgICAgIiwibWVybWFpZCI6eyJ0aGVtZSI6ImRlZmF1bHQiLCJ0aGVtZVZhcmlhYmxlcyI6eyJiYWNrZ3JvdW5kIjoid2hpdGUiLCJwcmltYXJ5Q29sb3IiOiIjRUNFQ0ZGIiwic2Vjb25kYXJ5Q29sb3IiOiIjZmZmZmRlIiwidGVydGlhcnlDb2xvciI6ImhzbCg4MCwgMTAwJSwgOTYuMjc0NTA5ODAzOSUpIiwicHJpbWFyeUJvcmRlckNvbG9yIjoiaHN
2022-10-31 01:47:12 +08:00
Suppose we have a series of tasks, the topology is as shown above, vertices represent tasks, and edges represent dependencies. (B, C, D can only be completed after A is completed, and H, E, F...) can only be completed after B is completed.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
With `hyperf/dag` , `DAG` can be constructed and executed as follows.
2022-02-16 02:13:55 +08:00
```php
< ?php
$dag = new \Hyperf\Dag\Dag();
$a = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "A\n";});
$b = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "B\n";});
$c = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "C\n";});
$d = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "D\n";});
$e = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "E\n";});
$f = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "F\n";});
$g = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "G\n";});
$h = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "H\n";});
$i = \Hyperf\Dag\Vertex::make(function() {sleep(1); echo "I\n";});
$dag->addVertex($a)
->addVertex($b)
->addVertex($c)
->addVertex($d)
->addVertex($e)
->addVertex($f)
->addVertex($g)
->addVertex($h)
->addVertex($i)
->addEdge($a, $b)
->addEdge($a, $c)
->addEdge($a, $d)
->addEdge($b, $h)
->addEdge($b, $e)
->addEdge($b, $f)
->addEdge($c, $f)
->addEdge($c, $g)
->addEdge($d, $g)
->addEdge($h, $i)
->addEdge($e, $i)
->addEdge($f, $i)
->addEdge($g, $i);
2022-10-31 01:47:12 +08:00
// need to be executed in a coroutine environment
2022-02-16 02:13:55 +08:00
$dag->run();
```
输出:
```php
2022-10-31 01:47:12 +08:00
// 1s afterwards
2022-02-16 02:13:55 +08:00
A
2022-10-31 01:47:12 +08:00
// 2s afterwards
2022-02-16 02:13:55 +08:00
D
C
B
2022-10-31 01:47:12 +08:00
// 3s afterwards
2022-02-16 02:13:55 +08:00
G
F
E
H
2022-10-31 01:47:12 +08:00
// 4s afterwards
2022-02-16 02:13:55 +08:00
I
```
2022-10-31 01:47:12 +08:00
> The DAG will schedule tasks on the earliest possible basis. Try adjusting the time at point B to 2 seconds, and you will find that B and G are completed together.
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
## Access previous step results
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
Each task can receive an array parameter containing the results of all pre-dependencies. After `DAG` is executed, it will also return an array of the same structure, including the execution result of each step.
2022-02-16 02:13:55 +08:00
```php
< ?php
$dag = new \Hyperf\Dag\Dag();
$a = \Hyperf\Dag\Vertex::make(function() {return 1;});
$b = \Hyperf\Dag\Vertex::make(function($results) use ($a) {
return $results[$a->key] + 1;
});
$results = $dag->addVertex($a)->addVertex($b)->addEdge($a, $b)->run();
assert($results[$a->key] === 1);
assert($results[$b->key] === 2);
```
2022-10-31 01:47:12 +08:00
## Define a task
2022-02-16 02:13:55 +08:00
2022-10-31 01:47:12 +08:00
In the above document, we used a closure to define a task. The format is as follows.
2022-02-16 02:13:55 +08:00
```php
2022-10-31 01:47:12 +08:00
// The second parameter of Vertex::make is an optional parameter, which is the key of vertex, that is, the key value of the result array.
2022-02-16 02:13:55 +08:00
\Hyperf\Dag\Vertex::make(function() { return 'hello'; }, "greeting");
```
2022-10-31 01:47:12 +08:00
In addition to using closure functions to define tasks, you can also use a class that implements the `\Hyperf\Dag\Runner` interface and convert it to a vertex via `Vertex::of` .
2022-02-16 02:13:55 +08:00
```php
class MyJob implements \Hyperf\Dag\Runner {
public function run($results = []) {
return 'hello';
}
}
\Hyperf\Dag\Vertex::of(new MyJob(), "greeting");
```
2022-10-31 01:47:12 +08:00
`\Hyperf\Dag\Dag` itself also implements the `\Hyperf\Dag\Runner` interface, so it can be nested.
2022-02-16 02:13:55 +08:00
```php
< ?php
2022-10-31 01:47:12 +08:00
// namespace omitted
2022-02-16 02:13:55 +08:00
$a = Vertex::make(function () { return 1;});
$b = Vertex::make(function () { return 2;});
$c = Vertex::make(function () { return 3;});
$nestedDag = new Dag();
$nestedDag->addVertex($a)->addVertex($b)->addEdge($a, $b);
$d = Vertex::of($nestedDag);
$superDag = new Dag();
$superDag->addVertex($c)->addVertex($d)->addEdge($c, $d);
$superDag->run();
```
2022-10-31 01:47:12 +08:00
## Control the number of concurrency
The `\Hyperf\Dag\Dag` class provides the `setConcurrency(int n)` method to control the maximum number of concurrency. Default is 10.