| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 |
1
1
1
1
1
1
1
1
773
773
773
773
48
725
725
4
721
1
300
300
300
300
300
300
300
300
300
1
1
1
1
762
762
762
4
4
3
3
1
1
1
759
1
1
722
722
1
33
33
33
31
1
3
3
3
3
3
3
3
3
3
1
1
1
1
20
20
20
7
7
20
6
14
14
14
1
7
7
1
7
7
7
1
1
| // Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var inherits = require('util').inherits;
var format = require('util').format;
var errors = require('./errors');
var TChannelPeerState = require('./peer_state');
module.exports.TChannelPeerHealthyState = TChannelPeerHealthyState;
module.exports.TChannelPeerUnhealthyState = TChannelPeerUnhealthyState;
/*
* Collectively, the health states receive additional options through the peer
* options:
* - maxErrorRate (error rate to go from healthy to unhealthy)
* - minResponseCount (response count to go from unhealthy to healthy)
* - TODO
*
* They also inherit:
* - channel.timers
* - channel.random
*/
var symptoms = {
'BadRequest': false, // not an indicator of bad health
'Cancelled': false, // not an indicator of bad health
'Timeout': true, // TODO throttle
'Busy': true, // TODO throttle
'Declined': true,
'UnexpectedError': true,
'NetworkError': true,
'FatalProtocolError': true
};
function healthyScore(state/* , req, options */) {
// space:
// [0.1, 0.2) unconnected peers
// [0.2, 0.3) incoming connections
// [0.3, 0.4) new outgoing connections
// [0.4, 1.0) identified outgoing connections
var inconn = state.peer.getInConnection();
var outconn = state.peer.getOutConnection();
var random = state.peer.outPendingWeightedRandom();
if (!inconn && !outconn) {
return 0.1 + random * 0.1;
} else Iif (!outconn || outconn.direction !== 'out') {
return 0.2 + random * 0.1;
} else if (outconn.remoteName === null) {
return 0.3 + random * 0.1;
} else {
return 0.4 + random * 0.6;
}
}
// ## Healthy
function TChannelPeerHealthyState(channel, peer) {
var self = this;
TChannelPeerState.call(self, channel, peer);
self.timers = channel.timers;
self.random = channel.random;
self.period = peer.options.period || 1000; // ms
self.start = self.timers.now();
self.maxErrorRate = peer.options.maxErrorRate || 0.5;
self.okCount = 0;
self.notOkCount = 0;
}
inherits(TChannelPeerHealthyState, TChannelPeerState);
TChannelPeerHealthyState.prototype.type = 'tchannel.healthy';
TChannelPeerHealthyState.prototype.toString = function healthyToString() {
var self = this;
return format('[HealthyPeer %s ok %s err]', self.okCount, self.notOkCount);
};
TChannelPeerHealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
var self = this;
var now = self.timers.now();
// At the conclusion of a period
if (now - self.start >= self.period) {
var totalCount = self.okCount + self.notOkCount;
// Transition to unhealthy state if the success rate dips below the
// acceptable threshold.
if (self.notOkCount / totalCount > self.maxErrorRate) {
self.peer.setState(TChannelPeerUnhealthyState);
return 0;
}
// Alternatley, start a new monitoring period.
self.start = self.timers.now();
self.okCount = 0;
self.notOkCount = 0;
}
// TODO throttle
return healthyScore(self, req, options);
};
TChannelPeerHealthyState.prototype.onRequest = function onRequest(/* req */) {
};
TChannelPeerHealthyState.prototype.onRequestResponse = function onRequestResponse(/* req */) {
var self = this;
self.okCount++;
};
TChannelPeerHealthyState.prototype.onRequestError = function onRequestError(err) {
var self = this;
var codeString = errors.classify(err);
if (symptoms[codeString]) {
self.notOkCount++;
}
};
// ## Unhealthy
function TChannelPeerUnhealthyState(channel, peer) {
var self = this;
TChannelPeerState.call(self, channel, peer);
self.timers = channel.timers;
self.random = channel.random;
self.minResponseCount = peer.options.probation || 5;
self.period = peer.options.period || 1000;
self.start = self.timers.now();
self.successCount = 0;
self.triedThisPeriod = true;
}
inherits(TChannelPeerUnhealthyState, TChannelPeerState);
TChannelPeerUnhealthyState.prototype.type = 'tchannel.unhealthy';
TChannelPeerUnhealthyState.prototype.toString = function healthyToString() {
var self = this;
return format('[UnhealthyPeer %s ok]', self.successCount);
};
TChannelPeerUnhealthyState.prototype.shouldRequest = function shouldRequest(req, options) {
var self = this;
// Start a new period if the previous has concluded
var now = self.timers.now();
if (now - self.start >= self.period) {
self.start = self.timers.now();
self.triedThisPeriod = false;
}
// Allow one trial per period
if (self.triedThisPeriod) {
return 0;
}
var score = healthyScore(self, req, options);
Iif (score >= 0.1 && score < 0.3) { // TODO predicate
// TODO: add channel / peers level support for "want more connections?"
self.peer.connect();
}
return score;
};
TChannelPeerUnhealthyState.prototype.onRequest = function onRequest(/* req */) {
var self = this;
self.triedThisPeriod = true;
};
TChannelPeerUnhealthyState.prototype.onRequestResponse = function onRequestResponse(/* req */) {
var self = this;
self.successCount++;
if (self.successCount > self.minResponseCount) {
self.peer.setState(TChannelPeerHealthyState);
}
};
TChannelPeerUnhealthyState.prototype.onRequestError = function onRequestError(err) {
var self = this;
var codeString = errors.classify(err);
if (symptoms[codeString]) {
self.successCount = 0;
}
};
|