Skip to content

Commit 75e3099

Browse files
authored
clients(php): cache host state only on change (#7202)
1 parent f312d66 commit 75e3099

4 files changed

Lines changed: 237 additions & 6 deletions

File tree

‎clients/algoliasearch-client-php/lib/RetryStrategy/ClusterHosts.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ public function timedOut($host)
118118

119119
public function resetHost($host)
120120
{
121-
$this->read->resetHost($host);
122-
$this->write->resetHost($host);
123-
$this->updateCache();
121+
$readChanged = $this->read->resetHost($host);
122+
$writeChanged = $this->write->resetHost($host);
123+
124+
// Called after every successful request: only write when a host actually changed
125+
if ($readChanged || $writeChanged) {
126+
$this->updateCache();
127+
}
124128

125129
return $this;
126130
}

‎clients/algoliasearch-client-php/lib/RetryStrategy/Host.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public function markAsDown()
4949
$this->lastCheck = time();
5050
}
5151

52+
/**
53+
* @return bool whether the host was down or had timed out before the reset
54+
*/
5255
public function reset()
5356
{
57+
$changed = !$this->up || 0 !== $this->retryCount;
58+
5459
$this->up = true;
5560
$this->lastCheck = null;
5661
$this->retryCount = 0;
62+
63+
return $changed;
5764
}
5865

5966
public function getRetryCount(): int

‎clients/algoliasearch-client-php/lib/RetryStrategy/HostCollection.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,22 @@ public function timedOut($hostKey)
100100
}, $this->hosts);
101101
}
102102

103+
/**
104+
* @param mixed $hostKey
105+
*
106+
* @return bool whether the host state changed
107+
*/
103108
public function resetHost($hostKey)
104109
{
105-
array_map(function (Host $host) use ($hostKey) {
110+
$changed = false;
111+
112+
foreach ($this->hosts as $host) {
106113
if ($host->getUrl() === $hostKey) {
107-
$host->reset();
114+
$changed = $host->reset() || $changed;
108115
}
109-
}, $this->hosts);
116+
}
117+
118+
return $changed;
110119
}
111120

112121
public function setRetryCount($hostKey, int $count)
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Tests;
4+
5+
use Algolia\AlgoliaSearch\Algolia;
6+
use Algolia\AlgoliaSearch\Api\SearchClient;
7+
use Algolia\AlgoliaSearch\Cache\NullCacheDriver;
8+
use Algolia\AlgoliaSearch\Configuration\SearchConfig;
9+
use Algolia\AlgoliaSearch\Exceptions\TimeoutException;
10+
use Algolia\AlgoliaSearch\Http\HttpClientInterface;
11+
use Algolia\AlgoliaSearch\Http\Psr7\Response;
12+
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
13+
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
14+
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;
15+
use PHPUnit\Framework\TestCase;
16+
use Psr\Http\Message\RequestInterface;
17+
use Psr\SimpleCache\CacheInterface;
18+
19+
/**
20+
* Contract test for the host state cache: `resetHost()` only writes to the cache when it
21+
* actually changes a host, so a successful request on a healthy host does not hit the cache.
22+
*
23+
* @internal
24+
*
25+
* @coversNothing
26+
*/
27+
class HostStateCacheTest extends TestCase
28+
{
29+
private const HOSTS = ['host-one.test', 'host-two.test'];
30+
31+
private $cache;
32+
33+
protected function setUp(): void
34+
{
35+
$this->cache = $this->countingCache();
36+
Algolia::setCache($this->cache);
37+
}
38+
39+
protected function tearDown(): void
40+
{
41+
Algolia::setCache(new NullCacheDriver());
42+
}
43+
44+
public function testResetOfAHealthyHostDoesNotWriteTheCache(): void
45+
{
46+
$clusterHosts = ClusterHosts::create(self::HOSTS)->setCacheKey('hosts');
47+
48+
$clusterHosts->resetHost('host-one.test');
49+
50+
$this->assertSame(0, $this->cache->sets);
51+
}
52+
53+
public function testResetOfAChangedHostWritesTheCache(): void
54+
{
55+
$clusterHosts = ClusterHosts::create(self::HOSTS)->setCacheKey('hosts');
56+
57+
$clusterHosts->failed('host-one.test');
58+
$this->assertSame(1, $this->cache->sets);
59+
60+
$clusterHosts->resetHost('host-one.test');
61+
$this->assertSame(2, $this->cache->sets);
62+
63+
$clusterHosts->timedOut('host-two.test');
64+
$this->assertSame(3, $this->cache->sets);
65+
66+
// a timed out host is still up, but its retry count has to be cleared
67+
$clusterHosts->resetHost('host-two.test');
68+
$this->assertSame(4, $this->cache->sets);
69+
}
70+
71+
public function testSuccessfulRequestsOnAHealthyHostDoNotWriteTheCache(): void
72+
{
73+
$client = $this->makeClient($this->recorder(function () {
74+
return new Response(200, [], '{}');
75+
}));
76+
77+
$client->customGet('1/test');
78+
$sets = $this->cache->sets;
79+
80+
$client->customGet('1/test');
81+
$client->customGet('1/test');
82+
83+
$this->assertSame($sets, $this->cache->sets);
84+
}
85+
86+
public function testSuccessAfterATimeoutWritesTheCacheOnce(): void
87+
{
88+
$client = $this->makeClient($this->recorder(function ($call) {
89+
if (1 === $call) {
90+
throw new TimeoutException('timed out');
91+
}
92+
93+
return new Response(200, [], '{}');
94+
}));
95+
96+
// first call: timeout on host one (write), success on host two (no change, no write)
97+
$client->customGet('1/test');
98+
$sets = $this->cache->sets;
99+
100+
// host one is tried first again: its retry count is cleared (write), then nothing changes
101+
$client->customGet('1/test');
102+
$client->customGet('1/test');
103+
104+
$this->assertSame($sets + 1, $this->cache->sets);
105+
}
106+
107+
private function countingCache(): CacheInterface
108+
{
109+
return new class implements CacheInterface {
110+
public $sets = 0;
111+
112+
private $values = [];
113+
114+
public function get($key, $default = null): mixed
115+
{
116+
return $this->values[$key] ?? $default;
117+
}
118+
119+
public function set($key, $value, $ttl = null): bool
120+
{
121+
++$this->sets;
122+
$this->values[$key] = $value;
123+
124+
return true;
125+
}
126+
127+
public function delete($key): bool
128+
{
129+
unset($this->values[$key]);
130+
131+
return true;
132+
}
133+
134+
public function clear(): bool
135+
{
136+
$this->values = [];
137+
138+
return true;
139+
}
140+
141+
public function getMultiple($keys, $default = null): iterable
142+
{
143+
$values = [];
144+
foreach ($keys as $key) {
145+
$values[$key] = $this->get($key, $default);
146+
}
147+
148+
return $values;
149+
}
150+
151+
public function setMultiple($values, $ttl = null): bool
152+
{
153+
foreach ($values as $key => $value) {
154+
$this->set($key, $value, $ttl);
155+
}
156+
157+
return true;
158+
}
159+
160+
public function deleteMultiple($keys): bool
161+
{
162+
foreach ($keys as $key) {
163+
$this->delete($key);
164+
}
165+
166+
return true;
167+
}
168+
169+
public function has($key): bool
170+
{
171+
return isset($this->values[$key]);
172+
}
173+
};
174+
}
175+
176+
private function recorder(callable $respond): HttpClientInterface
177+
{
178+
return new class($respond) implements HttpClientInterface {
179+
public $calls = 0;
180+
181+
private $respond;
182+
183+
public function __construct(callable $respond)
184+
{
185+
$this->respond = $respond;
186+
}
187+
188+
public function sendRequest(RequestInterface $request, $timeout, $connectTimeout)
189+
{
190+
++$this->calls;
191+
192+
return ($this->respond)($this->calls);
193+
}
194+
};
195+
}
196+
197+
private function makeClient(HttpClientInterface $mockHttp): SearchClient
198+
{
199+
$config = SearchConfig::create('test-app-id', 'test-api-key');
200+
201+
return new SearchClient(
202+
new ApiWrapper(
203+
$mockHttp,
204+
$config,
205+
ClusterHosts::create(self::HOSTS)->setCacheKey('hosts'),
206+
new RequestOptionsFactory($config)
207+
),
208+
$config
209+
);
210+
}
211+
}

0 commit comments

Comments
 (0)