Skip to content

Commit c4ebd6f

Browse files
Renames and refactoring for reloadable plugins (#30992)
1 parent 6bca143 commit c4ebd6f

File tree

35 files changed

+420
-323
lines changed

35 files changed

+420
-323
lines changed

plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2Service.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import org.elasticsearch.common.settings.Setting;
2323
import org.elasticsearch.common.settings.Setting.Property;
2424
import org.elasticsearch.common.unit.TimeValue;
25+
26+
import java.io.Closeable;
2527
import java.util.ArrayList;
2628
import java.util.Collections;
2729
import java.util.List;
2830
import java.util.function.Function;
2931

30-
interface AwsEc2Service {
32+
interface AwsEc2Service extends Closeable {
3133
Setting<Boolean> AUTO_ATTRIBUTE_SETTING = Setting.boolSetting("cloud.node.auto_attributes", false, Property.NodeScope);
3234

3335
class HostType {
@@ -79,25 +81,20 @@ class HostType {
7981
key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.NodeScope));
8082

8183
/**
82-
* Creates then caches an {@code AmazonEC2} client using the current client
83-
* settings.
84+
* Builds then caches an {@code AmazonEC2} client using the current client
85+
* settings. Returns an {@code AmazonEc2Reference} wrapper which should be
86+
* released as soon as it is not required anymore.
8487
*/
8588
AmazonEc2Reference client();
8689

8790
/**
88-
* Updates settings for building the client. Future client requests will use the
89-
* new settings. Implementations SHOULD drop the client cache to prevent reusing
90-
* the client with old settings from cache.
91+
* Updates the settings for building the client and releases the cached one.
92+
* Future client requests will use the new settings to lazily built the new
93+
* client.
9194
*
92-
* @param clientSettings
93-
* the new settings
94-
* @return the old settings
95+
* @param clientSettings the new refreshed settings
96+
* @return the old stale settings
9597
*/
96-
Ec2ClientSettings updateClientSettings(Ec2ClientSettings clientSettings);
98+
Ec2ClientSettings refreshAndClearCache(Ec2ClientSettings clientSettings);
9799

98-
/**
99-
* Releases the cached client. Subsequent client requests will recreate the
100-
* client instance. Does not touch the client settings.
101-
*/
102-
void releaseCachedClient();
103100
}

plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.discovery.ec2;
2121

22+
import java.io.IOException;
2223
import java.util.Random;
2324

2425
import com.amazonaws.ClientConfiguration;
@@ -127,12 +128,12 @@ public AmazonEc2Reference client() {
127128

128129

129130
/**
130-
* Reloads the settings for the AmazonEC2 client. New clients will be build
131-
* using these. Old client is usable until released. On release it will be
132-
* destroyed instead of being returned to the cache.
131+
* Refreshes the settings for the AmazonEC2 client. New clients will be build
132+
* using these new settings. Old client is usable until released. On release it
133+
* will be destroyed instead of being returned to the cache.
133134
*/
134135
@Override
135-
public synchronized Ec2ClientSettings updateClientSettings(Ec2ClientSettings clientSettings) {
136+
public synchronized Ec2ClientSettings refreshAndClearCache(Ec2ClientSettings clientSettings) {
136137
// shutdown all unused clients
137138
// others will shutdown on their respective release
138139
releaseCachedClient();
@@ -142,7 +143,11 @@ public synchronized Ec2ClientSettings updateClientSettings(Ec2ClientSettings cli
142143
}
143144

144145
@Override
145-
public synchronized void releaseCachedClient() {
146+
public void close() {
147+
releaseCachedClient();
148+
}
149+
150+
private synchronized void releaseCachedClient() {
146151
if (this.clientReference == null) {
147152
return;
148153
}
@@ -154,4 +159,5 @@ public synchronized void releaseCachedClient() {
154159
// it will be restarted on new client usage
155160
IdleConnectionReaper.shutdown();
156161
}
162+
157163
}

plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPlugin.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.elasticsearch.node.Node;
3232
import org.elasticsearch.plugins.DiscoveryPlugin;
3333
import org.elasticsearch.plugins.Plugin;
34-
import org.elasticsearch.plugins.ReInitializablePlugin;
34+
import org.elasticsearch.plugins.ReloadablePlugin;
3535
import org.elasticsearch.transport.TransportService;
3636

3737
import java.io.BufferedReader;
@@ -50,7 +50,7 @@
5050
import java.util.Map;
5151
import java.util.function.Supplier;
5252

53-
public class Ec2DiscoveryPlugin extends Plugin implements DiscoveryPlugin, ReInitializablePlugin {
53+
public class Ec2DiscoveryPlugin extends Plugin implements DiscoveryPlugin, ReloadablePlugin {
5454

5555
private static Logger logger = Loggers.getLogger(Ec2DiscoveryPlugin.class);
5656
public static final String EC2 = "ec2";
@@ -85,7 +85,7 @@ protected Ec2DiscoveryPlugin(Settings settings, AwsEc2ServiceImpl ec2Service) {
8585
this.settings = settings;
8686
this.ec2Service = ec2Service;
8787
// eagerly load client settings when secure settings are accessible
88-
reinit(settings);
88+
reload(settings);
8989
}
9090

9191
@Override
@@ -172,14 +172,13 @@ static Settings getAvailabilityZoneNodeAttributes(Settings settings, String azMe
172172

173173
@Override
174174
public void close() throws IOException {
175-
ec2Service.releaseCachedClient();
175+
ec2Service.close();
176176
}
177177

178178
@Override
179-
public boolean reinit(Settings settings) {
179+
public void reload(Settings settings) {
180180
// secure settings should be readable
181181
final Ec2ClientSettings clientSettings = Ec2ClientSettings.getClientSettings(settings);
182-
ec2Service.updateClientSettings(clientSettings);
183-
return true;
182+
ec2Service.refreshAndClearCache(clientSettings);
184183
}
185184
}

plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPluginTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void testClientSettingsReInit() throws IOException {
135135
assertThat(((AmazonEc2Mock) clientReference.client()).configuration.getProxyPort(), is(881));
136136
assertThat(((AmazonEc2Mock) clientReference.client()).endpoint, is("ec2_endpoint_1"));
137137
// reload secure settings2
138-
plugin.reinit(settings2);
138+
plugin.reload(settings2);
139139
// client is not released, it is still using the old settings
140140
assertThat(((AmazonEc2Mock) clientReference.client()).credentials.getCredentials().getAWSAccessKeyId(), is("ec2_access_1"));
141141
assertThat(((AmazonEc2Mock) clientReference.client()).credentials.getCredentials().getAWSSecretKey(), is("ec2_secret_1"));

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public AzureBlobStore(RepositoryMetaData metadata, Settings settings, AzureStora
5454
this.service = service;
5555
// locationMode is set per repository, not per client
5656
this.locationMode = Repository.LOCATION_MODE_SETTING.get(metadata.settings());
57-
final Map<String, AzureStorageSettings> prevSettings = this.service.updateClientsSettings(emptyMap());
57+
final Map<String, AzureStorageSettings> prevSettings = this.service.refreshAndClearCache(emptyMap());
5858
final Map<String, AzureStorageSettings> newSettings = AzureStorageSettings.overrideLocationMode(prevSettings, this.locationMode);
59-
this.service.updateClientsSettings(newSettings);
59+
this.service.refreshAndClearCache(newSettings);
6060
}
6161

6262
@Override

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepositoryPlugin.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2525
import org.elasticsearch.env.Environment;
2626
import org.elasticsearch.plugins.Plugin;
27-
import org.elasticsearch.plugins.ReInitializablePlugin;
27+
import org.elasticsearch.plugins.ReloadablePlugin;
2828
import org.elasticsearch.plugins.RepositoryPlugin;
2929
import org.elasticsearch.repositories.Repository;
3030
import java.util.Arrays;
@@ -35,7 +35,7 @@
3535
/**
3636
* A plugin to add a repository type that writes to and from the Azure cloud storage service.
3737
*/
38-
public class AzureRepositoryPlugin extends Plugin implements RepositoryPlugin, ReInitializablePlugin {
38+
public class AzureRepositoryPlugin extends Plugin implements RepositoryPlugin, ReloadablePlugin {
3939

4040
// protected for testing
4141
final AzureStorageService azureStoreService;
@@ -65,10 +65,9 @@ public List<Setting<?>> getSettings() {
6565
}
6666

6767
@Override
68-
public boolean reinit(Settings settings) {
68+
public void reload(Settings settings) {
6969
// secure settings should be readable
7070
final Map<String, AzureStorageSettings> clientsSettings = AzureStorageSettings.load(settings);
71-
azureStoreService.updateClientsSettings(clientsSettings);
72-
return true;
71+
azureStoreService.refreshAndClearCache(clientsSettings);
7372
}
7473
}

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ public interface AzureStorageService {
5050
Tuple<CloudBlobClient, Supplier<OperationContext>> client(String clientName);
5151

5252
/**
53-
* Updates settings for building clients. Future client requests will use the
54-
* new settings.
53+
* Updates settings for building clients. Any client cache is cleared. Future
54+
* client requests will use the new refreshed settings.
5555
*
56-
* @param clientsSettings
57-
* the new settings
56+
* @param clientsSettings the settings for new clients
5857
* @return the old settings
5958
*/
60-
Map<String, AzureStorageSettings> updateClientsSettings(Map<String, AzureStorageSettings> clientsSettings);
59+
Map<String, AzureStorageSettings> refreshAndClearCache(Map<String, AzureStorageSettings> clientsSettings);
6160

6261
ByteSizeValue MIN_CHUNK_SIZE = new ByteSizeValue(1, ByteSizeUnit.BYTES);
6362
ByteSizeValue MAX_CHUNK_SIZE = new ByteSizeValue(64, ByteSizeUnit.MB);

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public AzureStorageServiceImpl(Settings settings) {
6161
super(settings);
6262
// eagerly load client settings so that secure settings are read
6363
final Map<String, AzureStorageSettings> clientsSettings = AzureStorageSettings.load(settings);
64-
updateClientsSettings(clientsSettings);
64+
refreshAndClearCache(clientsSettings);
6565
}
6666

6767
@Override
@@ -107,7 +107,7 @@ protected OperationContext buildOperationContext(AzureStorageSettings azureStora
107107
}
108108

109109
@Override
110-
public Map<String, AzureStorageSettings> updateClientsSettings(Map<String, AzureStorageSettings> clientsSettings) {
110+
public Map<String, AzureStorageSettings> refreshAndClearCache(Map<String, AzureStorageSettings> clientsSettings) {
111111
final Map<String, AzureStorageSettings> prevSettings = this.storageSettings;
112112
this.storageSettings = MapBuilder.newMapBuilder(clientsSettings).immutableMap();
113113
// clients are built lazily by {@link client(String)}

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public String toString() {
185185
}
186186

187187
/**
188-
* Parses settings and read all settings available under azure.client.*
188+
* Parse and read all settings available under the azure.client.* namespace
189189
* @param settings settings to parse
190190
* @return All the named configurations
191191
*/

plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceMock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public Tuple<CloudBlobClient, Supplier<OperationContext>> client(String clientNa
212212
}
213213

214214
@Override
215-
public Map<String, AzureStorageSettings> updateClientsSettings(Map<String, AzureStorageSettings> clientsSettings) {
215+
public Map<String, AzureStorageSettings> refreshAndClearCache(Map<String, AzureStorageSettings> clientsSettings) {
216216
return emptyMap();
217217
}
218218
}

0 commit comments

Comments
 (0)