|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phloppy\Statistic; |
|
4
|
|
|
|
|
5
|
|
|
use Phloppy\Exception; |
|
6
|
|
|
use Phloppy\Job; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
use SebastianBergmann\RecursionContext\InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Collect Statistics of jobs/sec by node. |
|
13
|
|
|
*/ |
|
14
|
|
|
class JobOriginStatistic implements NodeStatistic |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Node Ids |
|
18
|
|
|
* |
|
19
|
|
|
* @var \DateTime[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $lastReceived; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 2-dimensional array storing rates for (queue, nodeId) pairs |
|
25
|
|
|
* @var string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
private $stats; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LoggerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $log; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var float |
|
36
|
|
|
*/ |
|
37
|
|
|
private $alpha; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param float $alpha Sensitivity factor. |
|
42
|
|
|
* @param LoggerInterface $log |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
5 |
|
function __construct($alpha = 0.9, LoggerInterface $log = null) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
5 |
|
if (!$log) { |
|
47
|
3 |
|
$log = new NullLogger(); |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
5 |
|
if ((float) $alpha < 0.5 || (float) $alpha > 1.0) { |
|
51
|
3 |
|
throw new InvalidArgumentException('0.5 < $alpha < 1.0'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
$this->alpha = (float) $alpha; |
|
55
|
2 |
|
$this->log = $log; |
|
|
|
|
|
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Update jobs by node/sec rt-statistic. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Job $job |
|
63
|
|
|
* |
|
64
|
|
|
* @param \DateTime $receivedAt |
|
|
|
|
|
|
65
|
|
|
* |
|
66
|
|
|
* @return float messages/sec from the given node |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function update(Job $job, \DateTime $receivedAt = null) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$nodeId = $job->getOriginNode(); |
|
71
|
2 |
|
$queue = $job->getQueue(); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
2 |
|
if (!$receivedAt) { |
|
74
|
|
|
$receivedAt = new \DateTime('now'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
if (!isset($this->lastReceived[$queue][$nodeId])) { |
|
78
|
2 |
|
$this->lastReceived[$queue][$nodeId] = clone $receivedAt; |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
if (!isset($this->stats[$queue])) { |
|
82
|
2 |
|
$this->stats[$queue] = []; |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
if (!isset($this->stats[$queue][$nodeId])) { |
|
86
|
2 |
|
$this->stats[$queue][$nodeId] = 0.; |
|
87
|
2 |
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$secs = $receivedAt->diff($this->lastReceived[$queue][$nodeId])->s; |
|
90
|
|
|
|
|
91
|
2 |
|
if ($secs < 0) { |
|
92
|
|
|
$secs = 0; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
$this->lastReceived[$queue][$nodeId] = clone $receivedAt; |
|
96
|
|
|
// exp moving average from the last message to now (per sec) |
|
97
|
2 |
|
$this->stats[$queue][$nodeId] = |
|
98
|
2 |
|
pow($this->alpha, $secs) * $this->stats[$queue][$nodeId] + |
|
99
|
2 |
|
(1 - $this->alpha); |
|
100
|
|
|
|
|
101
|
2 |
|
$this->log->debug('updated value', [ |
|
102
|
2 |
|
'id' => $nodeId, |
|
103
|
2 |
|
'queue' => $queue, |
|
104
|
2 |
|
'value' => $this->stats[$queue][$nodeId], |
|
105
|
|
|
'delta' => $secs |
|
106
|
2 |
|
]); |
|
107
|
|
|
|
|
108
|
2 |
|
return $this->stats[$queue][$nodeId]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return an ordered list of nodes. |
|
114
|
|
|
* |
|
115
|
|
|
* The list is ordered descending by the frequency of Jobs. |
|
116
|
|
|
* |
|
117
|
|
|
* @return float[] |
|
|
|
|
|
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function nodes($queue) |
|
120
|
|
|
{ |
|
121
|
1 |
|
assert(arsort($this->stats[$queue]), true); |
|
122
|
1 |
|
return $this->stats[$queue]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Return information on a specific (queue, node). |
|
128
|
|
|
* |
|
129
|
|
|
* If the queue and/or node is not known, no messages have been retrieved from it, so 0. will be returned. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $queue |
|
132
|
|
|
* @param string $nodeId |
|
133
|
|
|
* |
|
134
|
|
|
* @return float msg rate (per sec) |
|
|
|
|
|
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function node($queue, $nodeId) |
|
137
|
|
|
{ |
|
138
|
1 |
|
return isset($this->stats[$queue][$nodeId]) ? $this->stats[$queue][$nodeId] : 0.; |
|
139
|
|
|
} |
|
140
|
|
|
} |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.