Skip to content

Commit 9b3c605

Browse files
committed
Create GCSService in createComponents
1 parent 8bd0bef commit 9b3c605

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

modules/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public Map<String, Repository.Factory> getRepositories(
280280
projectId,
281281
metadata,
282282
registry,
283-
this.storageService,
283+
this.storageService.get(),
284284
clusterService,
285285
bigArrays,
286286
recoverySettings,
@@ -292,7 +292,7 @@ protected GoogleCloudStorageBlobStore createBlobStore() {
292292
metadata.settings().get("bucket"),
293293
"test",
294294
metadata.name(),
295-
storageService,
295+
storageService.get(),
296296
bigArrays,
297297
randomIntBetween(1, 8) * 1024,
298298
BackoffPolicy.noBackoff(),

modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStoragePlugin.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.repositories.gcs;
1111

12+
import org.apache.lucene.util.SetOnce;
1213
import org.elasticsearch.cluster.node.DiscoveryNode;
1314
import org.elasticsearch.cluster.service.ClusterService;
1415
import org.elasticsearch.common.settings.Setting;
@@ -24,21 +25,20 @@
2425
import org.elasticsearch.xcontent.NamedXContentRegistry;
2526

2627
import java.util.Arrays;
28+
import java.util.Collection;
2729
import java.util.Collections;
2830
import java.util.List;
2931
import java.util.Map;
3032

3133
public class GoogleCloudStoragePlugin extends Plugin implements RepositoryPlugin, ReloadablePlugin {
3234

35+
private final Settings settings;
3336
// package-private for tests
34-
final GoogleCloudStorageService storageService;
37+
final SetOnce<GoogleCloudStorageService> storageService = new SetOnce<>();
3538

3639
@SuppressWarnings("this-escape")
3740
public GoogleCloudStoragePlugin(final Settings settings) {
38-
var isServerless = DiscoveryNode.isStateless(settings);
39-
this.storageService = createStorageService(isServerless);
40-
// eagerly load client settings so that secure settings are readable (not closed)
41-
reload(settings);
41+
this.settings = settings;
4242
}
4343

4444
// overridable for tests
@@ -61,7 +61,7 @@ public Map<String, Repository.Factory> getRepositories(
6161
projectId,
6262
metadata,
6363
namedXContentRegistry,
64-
this.storageService,
64+
this.storageService.get(),
6565
clusterService,
6666
bigArrays,
6767
recoverySettings,
@@ -70,6 +70,15 @@ public Map<String, Repository.Factory> getRepositories(
7070
);
7171
}
7272

73+
@Override
74+
public Collection<?> createComponents(PluginServices services) {
75+
var isServerless = DiscoveryNode.isStateless(settings);
76+
storageService.set(createStorageService(isServerless));
77+
// eagerly load client settings so that secure settings are readable (not closed)
78+
reload(settings);
79+
return List.of(storageService.get());
80+
}
81+
7382
@Override
7483
public List<Setting<?>> getSettings() {
7584
return Arrays.asList(
@@ -94,6 +103,6 @@ public void reload(Settings settings) {
94103
// `GoogleCloudStorageClientSettings` instance) instead of the `Settings`
95104
// instance.
96105
final Map<String, GoogleCloudStorageClientSettings> clientsSettings = GoogleCloudStorageClientSettings.load(settings);
97-
this.storageService.refreshAndClearCache(clientsSettings);
106+
this.storageService.get().refreshAndClearCache(clientsSettings);
98107
}
99108
}

modules/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.settings.Setting;
2323
import org.elasticsearch.common.settings.Settings;
2424
import org.elasticsearch.core.TimeValue;
25+
import org.elasticsearch.plugins.Plugin;
2526
import org.elasticsearch.test.ESTestCase;
2627
import org.elasticsearch.xcontent.XContentBuilder;
2728
import org.hamcrest.Matchers;
@@ -38,6 +39,7 @@
3839
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
3940
import static org.hamcrest.Matchers.containsString;
4041
import static org.hamcrest.Matchers.equalTo;
42+
import static org.mockito.Mockito.mock;
4143

4244
public class GoogleCloudStorageServiceTests extends ESTestCase {
4345

@@ -113,7 +115,8 @@ public void testReinitClientSettings() throws Exception {
113115
secureSettings2.setFile("gcs.client.gcs3.credentials_file", serviceAccountFileContent("project_gcs23"));
114116
final Settings settings2 = Settings.builder().setSecureSettings(secureSettings2).build();
115117
try (GoogleCloudStoragePlugin plugin = new GoogleCloudStoragePlugin(settings1)) {
116-
final GoogleCloudStorageService storageService = plugin.storageService;
118+
plugin.createComponents(mock(Plugin.PluginServices.class));
119+
final GoogleCloudStorageService storageService = plugin.storageService.get();
117120
var statsCollector = new GcsRepositoryStatsCollector();
118121
final var client11 = storageService.client("gcs1", "repo1", statsCollector);
119122
assertThat(client11.getOptions().getProjectId(), equalTo("project_gcs11"));
@@ -151,7 +154,8 @@ public void testClientsAreNotSharedAcrossRepositories() throws Exception {
151154
secureSettings1.setFile("gcs.client.gcs1.credentials_file", serviceAccountFileContent("test_project"));
152155
final Settings settings = Settings.builder().setSecureSettings(secureSettings1).build();
153156
try (GoogleCloudStoragePlugin plugin = new GoogleCloudStoragePlugin(settings)) {
154-
final GoogleCloudStorageService storageService = plugin.storageService;
157+
plugin.createComponents(mock(Plugin.PluginServices.class));
158+
final GoogleCloudStorageService storageService = plugin.storageService.get();
155159

156160
final MeteredStorage repo1Client = storageService.client("gcs1", "repo1", new GcsRepositoryStatsCollector());
157161
final MeteredStorage repo2Client = storageService.client("gcs1", "repo2", new GcsRepositoryStatsCollector());

0 commit comments

Comments
 (0)