forked from solariumphp/solarium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionReuseTest.php
298 lines (267 loc) · 10.5 KB
/
ConnectionReuseTest.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace Solarium\Tests\Integration;
use PHPUnit\Framework\TestCase;
use Solarium\Core\Client\ClientInterface;
use Solarium\Core\Client\Request;
/**
* Test connection reuse with PSR-18 adapter.
*
* @group integration
*/
class ConnectionReuseTest extends TestCase
{
/**
* @var ClientInterface
*/
protected static $client;
/**
* @var array
*/
protected static $config;
/**
* Are we running against the new v2 logging API that was tweaked for Solr 9.3 (SOLR-16458)?
*
* @var bool
*/
protected static $isNewLoggingApi;
/**
* Are we running against a v2 logging API with a known bug (SOLR-17176l)?
*
* @var bool
*/
protected static $isBuggyLoggingApi;
/**
* Original org.eclipse.jetty.io.AbstractConnection log level to restore after the testcase.
*
* @var string
*/
protected static $origLogLevel;
/**
* Original watcher threshold to restore after the testcase.
*
* @var string
*/
protected static $origThreshold;
/**
* Keep track of the last retrieved logging timestamp.
*
* @var int
*/
protected static $since = 0;
public static function setUpBeforeClass(): void
{
self::$config = [
'endpoint' => [
'localhost' => [
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/',
'username' => 'solr',
'password' => 'SolrRocks',
],
],
];
self::$client = TestClientFactory::createWithPsr18Adapter(self::$config);
// determine if we're running against a version of Solr that uses the tweaked v2 logging API (SOLR-16458)
// determine if we're running against a version of Solr with a known bug in the v2 logging API (SOLR-17176l)
$query = self::$client->createApi([
'version' => Request::API_V1,
'handler' => 'admin/info/system',
]);
$response = self::$client->execute($query);
$system = $response->getData();
$solrSpecVersion = $system['lucene']['solr-spec-version'];
self::$isNewLoggingApi = version_compare($solrSpecVersion, '9.3', '>=');
self::$isBuggyLoggingApi = version_compare($solrSpecVersion, '9.5', '>=');
// get the current log level to restore afterwards to avoid excessive logging in other testcases
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => self::$isNewLoggingApi ? 'node/logging/levels' : 'node/logging',
]);
$result = self::$client->execute($query);
$connectionLogger = array_values(array_filter(
$result->getData()['loggers'],
function (array $logger): bool {
return 'org.eclipse.jetty.io.AbstractConnection' === $logger['name'];
}
))[0];
self::$origLogLevel = $connectionLogger['level'];
// set the minimal log level that will provide us with enough information
if (self::$isNewLoggingApi) {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging/levels',
'method' => Request::METHOD_PUT,
'contenttype' => Request::CONTENT_TYPE_APPLICATION_JSON,
]);
$query->setRawData(json_encode(
[
[
'logger' => 'org.eclipse.jetty.io.AbstractConnection',
'level' => 'DEBUG',
],
]
));
$result = self::$client->execute($query);
self::assertTrue($result->getWasSuccessful());
} else {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging',
]);
$query->addParam('set', 'org.eclipse.jetty.io.AbstractConnection:DEBUG');
$result = self::$client->execute($query);
self::assertTrue($result->getWasSuccessful());
}
// get the current watcher threshold to restore afterwards
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => self::$isNewLoggingApi ? 'node/logging/messages' : 'node/logging',
]);
$query->addParam('since', self::$since);
$result = self::$client->execute($query);
self::$origThreshold = $result->getData()['info']['threshold'] ?? 'WARN';
// set the watcher threshold to match the log level we need
if (self::$isNewLoggingApi) {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging/messages/threshold',
'method' => Request::METHOD_PUT,
'contenttype' => Request::CONTENT_TYPE_APPLICATION_JSON,
]);
$query->setRawData(json_encode(
[
'level' => 'DEBUG',
]
));
self::$client->execute($query);
} else {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging',
]);
$query->addParam('since', self::$since);
$query->addParam('threshold', 'DEBUG');
self::$client->execute($query);
}
// get the initial timestamp to use for retrieving the logging history
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => self::$isNewLoggingApi ? 'node/logging/messages' : 'node/logging',
]);
$query->addParam('since', self::$since);
$result = self::$client->execute($query);
self::$since = $result->getData()['info']['last'];
}
public static function tearDownAfterClass(): void
{
// restore the original log level and watcher threshold
if (self::$isNewLoggingApi) {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging/levels',
'method' => Request::METHOD_PUT,
'contenttype' => Request::CONTENT_TYPE_APPLICATION_JSON,
]);
$query->setRawData(json_encode(
[
[
'logger' => 'org.eclipse.jetty.io.AbstractConnection',
'level' => self::$origLogLevel,
],
]
));
$result = self::$client->execute($query);
self::assertTrue($result->getWasSuccessful());
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging/messages/threshold',
'method' => Request::METHOD_PUT,
'contenttype' => Request::CONTENT_TYPE_APPLICATION_JSON,
]);
$query->setRawData(json_encode(
[
'level' => self::$origThreshold,
]
));
$result = self::$client->execute($query);
self::assertTrue($result->getWasSuccessful());
} else {
$query = self::$client->createApi([
'version' => Request::API_V2,
'handler' => 'node/logging',
]);
$query->addParam('set', sprintf('org.eclipse.jetty.io.AbstractConnection:%s', self::$origLogLevel));
$query->addParam('threshold', self::$origThreshold);
$result = self::$client->execute($query);
self::assertTrue($result->getWasSuccessful());
}
}
/**
* @dataProvider createAdapterProvider
*/
public function testConnectionReuse(string $createFunction, int $expectedCount)
{
// make sure the next logged timestamp is not on the same millisecond as self::$since
usleep(1000);
$client = TestClientFactory::$createFunction(self::$config);
// the logging for 5 requests fits within the default logging watcher buffer size
for ($i = 0; $i < 5; ++$i) {
$query = $client->createApi([
'version' => Request::API_V2,
'handler' => 'node/system',
]);
$client->execute($query);
}
if (self::$isBuggyLoggingApi) {
// fallback to v1 logging API to work around SOLR-17176
$apiOptions = [
'version' => Request::API_V1,
'handler' => 'admin/info/logging',
];
} else {
$apiOptions = [
'version' => Request::API_V2,
'handler' => self::$isNewLoggingApi ? 'node/logging/messages' : 'node/logging',
];
}
$query = $client->createApi($apiOptions);
$query->addParam('since', self::$since);
$result = $client->execute($query);
$data = $result->getData();
self::$since = $data['info']['last'];
$connections = $this->extractConnections(self::$isNewLoggingApi && !self::$isBuggyLoggingApi ? $data['history'] : $data['history']['docs']);
// The count is off-by-1 with an additional connection for one of the tests:
// - the request for node/logging without reuse is included in the count without reuse in most tests in a workflow;
// - if it isn't picked up the first time around, it shows up instead when we get the log with reuse.
// The request for node/logging with reuse doesn't cause another additional connection because of the reuse.
$this->assertContains(\count($connections), [$expectedCount, $expectedCount + 1]);
}
public function createAdapterProvider(): array
{
return [
'without reuse' => ['createWithCurlAdapter', 5],
'with reuse' => ['createWithPsr18Adapter', 1],
];
}
/**
* Extracts connections from the logging history docs.
*
* Connections are identified by the org.eclipse.jetty.io.AbstractConnection
* logger's message with format "onOpen {}".
*
* @param array $docs
*
* @return string[]
*/
protected function extractConnections(array $docs): array
{
$connections = [];
foreach ($docs as $doc) {
if ('org.eclipse.jetty.io.AbstractConnection' === $doc['logger'] && str_starts_with($doc['message'], 'onOpen ')) {
$connections[] = $doc['message'];
}
}
return $connections;
}
}