Skip to content

Commit afab540

Browse files
committed
Add support for multiple Elasticsearch hosts
1 parent 0e079ce commit afab540

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

app/Http/Controllers/WikiController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ public function __construct(DomainValidator $domainValidator, ProfileValidator $
3333
}
3434

3535
public function create(Request $request): \Illuminate\Http\Response {
36-
$sharedIndexHost = Config::get('wbstack.elasticsearch_shared_index_host');
3736
$sharedIndexPrefix = Config::get('wbstack.elasticsearch_shared_index_prefix');
37+
$esHosts = Config::get('wbstack.elasticsearch_hosts');
38+
$isSearchConfigValid = $esHosts && $sharedIndexPrefix;
3839

39-
if (Config::get('wbstack.elasticsearch_enabled_by_default')) {
40-
if (!($sharedIndexHost && $sharedIndexPrefix)) {
41-
abort(503, 'Search enabled, but its configuration is invalid');
42-
}
40+
if (Config::get('wbstack.elasticsearch_enabled_by_default') && !$isSearchConfigValid) {
41+
abort(503, 'Search enabled, but its configuration is invalid');
4342
}
4443
$user = $request->user();
4544

@@ -169,9 +168,9 @@ public function create(Request $request): \Illuminate\Http\Response {
169168
}
170169

171170
// dispatch elasticsearch init job to enable the feature
172-
if (Config::get('wbstack.elasticsearch_enabled_by_default')) {
173-
if ($sharedIndexHost && $sharedIndexPrefix) {
174-
dispatch(new ElasticSearchAliasInit($wiki->id));
171+
if (Config::get('wbstack.elasticsearch_enabled_by_default') && $isSearchConfigValid) {
172+
foreach ($esHosts as $esHost) {
173+
dispatch(new ElasticSearchAliasInit($wiki->id, $esHost));
175174
}
176175
}
177176

app/Jobs/ElasticSearchAliasInit.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
class ElasticSearchAliasInit extends Job {
1010
private $wikiId;
1111

12+
private $esHost;
13+
1214
private $dbName;
1315

1416
private $sharedPrefix;
1517

1618
/**
1719
* @param string $dbName
1820
*/
19-
public function __construct(int $wikiId, ?string $sharedPrefix = null) {
21+
public function __construct(int $wikiId, string $esHost, ?string $sharedPrefix = null) {
2022
$this->wikiId = $wikiId;
23+
$this->esHost = $esHost;
2124
$this->sharedPrefix = $sharedPrefix ?? getenv('ELASTICSEARCH_SHARED_INDEX_PREFIX');
2225
}
2326

@@ -68,7 +71,7 @@ public function handle(HttpRequest $request) {
6871
}
6972

7073
$request->setOptions([
71-
CURLOPT_URL => getenv('ELASTICSEARCH_SHARED_INDEX_HOST') . '/_aliases',
74+
CURLOPT_URL => $this->esHost . '/_aliases',
7275
CURLOPT_RETURNTRANSFER => true,
7376
CURLOPT_TIMEOUT => 60 * 15,
7477
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

config/wbstack.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
'elasticsearch_hosts' => array_filter(explode(',', env('ELASTICSEARCH_HOST', ''))),
2323
'elasticsearch_enabled_by_default' => env('WBSTACK_ELASTICSEARCH_ENABLED_BY_DEFAULT', false),
24-
'elasticsearch_shared_index_host' => env('ELASTICSEARCH_SHARED_INDEX_HOST', null),
2524
'elasticsearch_shared_index_prefix' => env('ELASTICSEARCH_SHARED_INDEX_PREFIX', null),
2625

2726
'signup_throttling_limit' => env('WBSTACK_SIGNUP_THROTTLING_LIMIT', ''),

tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
class ElasticSearchAliasInitTest extends TestCase {
1313
private $wikiId;
1414

15+
private $esHost;
16+
1517
private $prefix;
1618

1719
private $dbName;
1820

1921
protected function setUp(): void {
2022
parent::setUp();
2123
$this->wikiId = Wiki::factory()->create()->id;
24+
$this->esHost = 'elasticsearch-1.localhost';
2225
$this->dbName = WikiDb::factory()->create(['wiki_id' => $this->wikiId])->name;
2326
$this->prefix = 'testing_1';
2427
putenv('ELASTICSEARCH_SHARED_INDEX_PREFIX');
@@ -48,7 +51,7 @@ private function getMockRequest() {
4851
->method('setOptions')
4952
->with(
5053
[
51-
CURLOPT_URL => getenv('ELASTICSEARCH_SHARED_INDEX_HOST') . '/_aliases',
54+
CURLOPT_URL => $this->esHost . '/_aliases',
5255
CURLOPT_RETURNTRANSFER => true,
5356
CURLOPT_TIMEOUT => 60 * 15,
5457
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
@@ -80,7 +83,7 @@ public function testSuccess() {
8083
->method('fail')
8184
->withAnyParameters();
8285

83-
$job = new ElasticSearchAliasInit($this->wikiId, $this->prefix);
86+
$job = new ElasticSearchAliasInit($this->wikiId, $this->esHost, $this->prefix);
8487
$job->setJob($mockJob);
8588
$job->handle($request);
8689
}
@@ -95,7 +98,7 @@ public function testFailure() {
9598
->method('fail')
9699
->with(new \RuntimeException("Updating Elasticsearch aliases failed for $this->wikiId with {\"acknowledged\":false}"));
97100

98-
$job = new ElasticSearchAliasInit($this->wikiId, $this->prefix);
101+
$job = new ElasticSearchAliasInit($this->wikiId, $this->esHost, $this->prefix);
99102
$job->setJob($mockJob);
100103
$job->handle($request);
101104
}
@@ -109,7 +112,7 @@ public function testMissingDatabaseFailure() {
109112
->method('fail')
110113
->with(new \RuntimeException("Failed to get database name for $this->wikiId"));
111114

112-
$job = new ElasticSearchAliasInit($this->wikiId, $this->prefix);
115+
$job = new ElasticSearchAliasInit($this->wikiId, $this->esHost, $this->prefix);
113116
$job->setJob($mockJob);
114117
$job->handle($request);
115118
}
@@ -127,7 +130,7 @@ public function testSuccessWithPrefixEnv() {
127130
->method('fail')
128131
->withAnyParameters();
129132

130-
$job = new ElasticSearchAliasInit($this->wikiId);
133+
$job = new ElasticSearchAliasInit($this->wikiId, $this->esHost);
131134
$job->setJob($mockJob);
132135
$job->handle($request);
133136
}
@@ -140,7 +143,7 @@ public function testMissingPrefixFailure() {
140143
->method('fail')
141144
->with(new \RuntimeException("Missing shared index prefix for $this->wikiId"));
142145

143-
$job = new ElasticSearchAliasInit($this->wikiId);
146+
$job = new ElasticSearchAliasInit($this->wikiId, $this->esHost);
144147
$job->setJob($mockJob);
145148
$job->handle($request);
146149
}

tests/Routes/Wiki/CreateTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class CreateTest extends TestCase {
4040
*/
4141
public function testWikiCreateDispatchesSomeJobs($elasticSearchConfig) {
4242
$enabledForNewWikis = $elasticSearchConfig['enabledForNewWikis'];
43-
$sharedIndexHost = $elasticSearchConfig['sharedIndexHost'] ?? null;
4443
$sharedIndexPrefix = $elasticSearchConfig['sharedIndexPrefix'] ?? null;
44+
$esHosts = $elasticSearchConfig['esHosts'] ?? null;
4545

4646
Config::set('wbstack.elasticsearch_enabled_by_default', $enabledForNewWikis);
47-
Config::set('wbstack.elasticsearch_shared_index_host', $sharedIndexHost);
4847
Config::set('wbstack.elasticsearch_shared_index_prefix', $sharedIndexPrefix);
48+
Config::set('wbstack.elasticsearch_hosts', $esHosts);
4949

5050
$this->createSQLandQSDBs();
5151

@@ -70,13 +70,13 @@ public function testWikiCreateDispatchesSomeJobs($elasticSearchConfig) {
7070
]
7171
);
7272

73-
if ($enabledForNewWikis && $sharedIndexHost && $sharedIndexPrefix) {
74-
Queue::assertPushed(ElasticSearchAliasInit::class, 1);
73+
if ($enabledForNewWikis && $esHosts && $sharedIndexPrefix) {
74+
Queue::assertPushed(ElasticSearchAliasInit::class, count($esHosts));
7575
} else {
7676
Queue::assertNotPushed(ElasticSearchAliasInit::class);
7777
}
7878

79-
if ($enabledForNewWikis && !($sharedIndexHost && $sharedIndexPrefix)) {
79+
if ($enabledForNewWikis && !($esHosts && $sharedIndexPrefix)) {
8080
$response->assertStatus(503)
8181
->assertJsonPath('message', 'Search enabled, but its configuration is invalid');
8282

@@ -108,8 +108,26 @@ public function testWikiCreateDispatchesSomeJobs($elasticSearchConfig) {
108108
public static function createDispatchesSomeJobsProvider() {
109109
yield [[
110110
'enabledForNewWikis' => true,
111-
'sharedIndexHost' => 'somehost',
112111
'sharedIndexPrefix' => 'testing_1',
112+
'esHosts' => ['elasticsearch-1.localhost'],
113+
]];
114+
115+
yield [[
116+
'enabledForNewWikis' => true,
117+
'sharedIndexPrefix' => 'testing_1',
118+
'esHosts' => ['elasticsearch-1.localhost', 'elasticsearch-2.localhost'],
119+
]];
120+
121+
yield [[
122+
'enabledForNewWikis' => false,
123+
'sharedIndexPrefix' => 'testing_1',
124+
'esHosts' => ['elasticsearch-1.localhost'],
125+
]];
126+
127+
yield [[
128+
'enabledForNewWikis' => true,
129+
'sharedIndexPrefix' => 'testing_1',
130+
'esHosts' => [],
113131
]];
114132

115133
yield [[

0 commit comments

Comments
 (0)