Skip to content

Commit 35cd05b

Browse files
committed
tweak
1 parent b48a32c commit 35cd05b

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class GcsPerProjectClientManager implements ClusterStateListener {
2525

2626
private final Settings settings;
2727
private final BiFunction<GoogleCloudStorageClientSettings, GcsRepositoryStatsCollector, MeteredStorage> clientBuilder;
28-
private final Map<ProjectId, ClientHolder> perProjectClientsCache;
28+
private final Map<ProjectId, ClientsHolder> perProjectClientsCache;
2929

3030
public GcsPerProjectClientManager(
3131
Settings settings,
@@ -40,7 +40,7 @@ public GcsPerProjectClientManager(
4040
public void clusterChanged(ClusterChangedEvent event) {
4141
final Map<ProjectId, ProjectMetadata> currentProjects = event.state().metadata().projects();
4242

43-
final var updatedPerProjectClients = new HashMap<ProjectId, ClientHolder>();
43+
final var updatedPerProjectClients = new HashMap<ProjectId, ClientsHolder>();
4444
for (var project : currentProjects.values()) {
4545
final ProjectSecrets projectSecrets = project.custom(ProjectSecrets.TYPE);
4646
if (projectSecrets == null) {
@@ -61,9 +61,10 @@ public void clusterChanged(ClusterChangedEvent event) {
6161
// TODO: Building and comparing the whole GoogleCloudStorageClientSettings may be insufficient, we could just compare the
6262
// relevant secrets
6363
if (newOrUpdated(project.id(), clientSettings)) {
64-
updatedPerProjectClients.put(project.id(), new ClientHolder(clientSettings));
64+
updatedPerProjectClients.put(project.id(), new ClientsHolder(clientSettings));
6565
}
6666
}
67+
6768
// Updated projects
6869
perProjectClientsCache.putAll(updatedPerProjectClients);
6970

@@ -81,39 +82,39 @@ public MeteredStorage client(
8182
String repositoryName,
8283
GcsRepositoryStatsCollector statsCollector
8384
) {
84-
final var clientHolder = perProjectClientsCache.get(projectId);
85-
if (clientHolder == null) {
85+
final var clientsHolder = perProjectClientsCache.get(projectId);
86+
if (clientsHolder == null) {
8687
throw new IllegalArgumentException("No project found for [" + projectId + "]");
8788
}
88-
return clientHolder.client(clientName, repositoryName, statsCollector);
89+
return clientsHolder.client(clientName, repositoryName, statsCollector);
8990
}
9091

9192
public void closeRepositoryClients(ProjectId projectId, String repositoryName) {
92-
final var clientHolder = perProjectClientsCache.get(projectId);
93-
if (clientHolder != null) {
94-
clientHolder.closeRepositoryClients(repositoryName);
93+
final var clientsHolder = perProjectClientsCache.get(projectId);
94+
if (clientsHolder != null) {
95+
clientsHolder.closeRepositoryClients(repositoryName);
9596
}
9697
}
9798

9899
private boolean newOrUpdated(ProjectId projectId, Map<String, GoogleCloudStorageClientSettings> currentClientSettings) {
99-
if (perProjectClientsCache.containsKey(projectId) == false) {
100+
final ClientsHolder old = perProjectClientsCache.get(projectId);
101+
if (old == null) {
100102
return true;
101103
}
102-
final var previousClientSettings = perProjectClientsCache.get(projectId).clientSettings();
103-
return currentClientSettings.equals(previousClientSettings) == false;
104+
return currentClientSettings.equals(old.clientSettings()) == false;
104105
}
105106

106-
private final class ClientHolder {
107+
private final class ClientsHolder {
107108
// clientName -> client settings
108109
private final Map<String, GoogleCloudStorageClientSettings> clientSettings;
109110
// repositoryName -> client
110111
private final Map<String, MeteredStorage> clientCache = new ConcurrentHashMap<>();
111112

112-
ClientHolder(Map<String, GoogleCloudStorageClientSettings> clientSettings) {
113+
ClientsHolder(Map<String, GoogleCloudStorageClientSettings> clientSettings) {
113114
this.clientSettings = clientSettings;
114115
}
115116

116-
public Map<String, GoogleCloudStorageClientSettings> clientSettings() {
117+
Map<String, GoogleCloudStorageClientSettings> clientSettings() {
117118
return clientSettings;
118119
}
119120

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3PerProjectClientManager.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232

3333
public class S3PerProjectClientManager implements ClusterStateListener {
3434

35-
// Original settings at node startup time
3635
private final Settings settings;
3736
private final Function<S3ClientSettings, AmazonS3> clientBuilder;
38-
3937
// A map of projectId to clients holder. Adding to and removing from the map happen only with the cluster state listener thread.
4038
private final Map<ProjectId, ClientsHolder> perProjectClientsCache;
4139

@@ -107,11 +105,11 @@ public void clearCacheForProject(ProjectId projectId) {
107105
final var old = perProjectClientsCache.get(projectId);
108106
if (old != null) {
109107
old.clearCache();
108+
// TODO: do we need this?
109+
// shutdown IdleConnectionReaper background thread
110+
// it will be restarted on new client usage
111+
IdleConnectionReaper.shutdown();
110112
}
111-
// TODO: do we need this?
112-
// shutdown IdleConnectionReaper background thread
113-
// it will be restarted on new client usage
114-
IdleConnectionReaper.shutdown();
115113
}
116114

117115
/**
@@ -126,8 +124,7 @@ private boolean newOrUpdated(ProjectId projectId, Map<String, S3ClientSettings>
126124
if (old == null) {
127125
return true;
128126
}
129-
final var oldClientSettings = old.clientSettings();
130-
return currentClientSettings.equals(oldClientSettings) == false;
127+
return currentClientSettings.equals(old.clientSettings()) == false;
131128
}
132129

133130
private final class ClientsHolder implements Closeable {
@@ -139,11 +136,11 @@ private final class ClientsHolder implements Closeable {
139136
this.clientSettings = clientSettings;
140137
}
141138

142-
public Map<String, S3ClientSettings> clientSettings() {
139+
Map<String, S3ClientSettings> clientSettings() {
143140
return clientSettings;
144141
}
145142

146-
public AmazonS3Reference client(String clientName) {
143+
AmazonS3Reference client(String clientName) {
147144
final var clientReference = clientsCache.get(clientName);
148145
// It is ok to retrieve an existing client when the cache is being cleared or the holder is closing.
149146
// As long as there are paired incRef/decRef calls, the client will be closed when the last reference is released
@@ -174,10 +171,10 @@ public AmazonS3Reference client(String clientName) {
174171
}
175172

176173
/**
177-
* Clear the cache by closing and clear out all clients. New {@link #client(String)} call will recreate
174+
* Clear the cache by closing and clear out all clients. Subsequent {@link #client(String)} calls will recreate
178175
* the clients and populate the cache again.
179176
*/
180-
public synchronized void clearCache() {
177+
synchronized void clearCache() {
181178
IOUtils.closeWhileHandlingException(clientsCache.values());
182179
clientsCache = Collections.emptyMap();
183180
}

0 commit comments

Comments
 (0)