|
20 | 20 | import org.elasticsearch.common.util.Maps; |
21 | 21 | import org.elasticsearch.common.util.concurrent.AbstractRunnable; |
22 | 22 | import org.elasticsearch.core.IOUtils; |
| 23 | +import org.elasticsearch.core.Nullable; |
23 | 24 | import org.elasticsearch.logging.LogManager; |
24 | 25 | import org.elasticsearch.logging.Logger; |
25 | 26 |
|
@@ -183,48 +184,42 @@ void refreshAndClearCacheForClusterClients(Map<String, S3ClientSettings> clients |
183 | 184 | } |
184 | 185 |
|
185 | 186 | S3ClientSettings settingsForClient(ProjectId projectId, RepositoryMetadata repositoryMetadata) { |
186 | | - if (ProjectId.DEFAULT.equals(Objects.requireNonNull(projectId))) { |
187 | | - return clusterClientsHolder.singleClientSettings(repositoryMetadata); |
188 | | - } |
189 | | - |
190 | | - assert perProjectClientsHolders != null : "expect per-project clients holders to be non-null"; |
191 | | - final var clientsHolder = perProjectClientsHolders.get(projectId); |
192 | | - if (clientsHolder == null) { |
193 | | - throw new IllegalArgumentException("no s3 client is configured for project [" + projectId + "]"); |
194 | | - } |
195 | | - return clientsHolder.singleClientSettings(repositoryMetadata); |
| 187 | + return getClientsHolderSafe(projectId).singleClientSettings(repositoryMetadata); |
196 | 188 | } |
197 | 189 |
|
198 | 190 | AmazonS3Reference client(ProjectId projectId, RepositoryMetadata repositoryMetadata) { |
199 | | - if (ProjectId.DEFAULT.equals(Objects.requireNonNull(projectId))) { |
200 | | - return clusterClientsHolder.client(repositoryMetadata); |
201 | | - } |
202 | | - |
203 | | - assert perProjectClientsHolders != null : "expect per-project clients holders to be non-null"; |
204 | | - final var clientsHolder = perProjectClientsHolders.get(projectId); |
205 | | - if (clientsHolder == null) { |
206 | | - throw new IllegalArgumentException("no s3 client is configured for project [" + projectId + "]"); |
207 | | - } |
208 | | - return clientsHolder.client(repositoryMetadata); |
| 191 | + return getClientsHolderSafe(projectId).client(repositoryMetadata); |
209 | 192 | } |
210 | 193 |
|
211 | 194 | /** |
212 | 195 | * Clears the cache for the given project (default project-id is for the cluster level clients). |
213 | 196 | * All clients for the project are closed and will be recreated on next access. |
214 | 197 | */ |
215 | 198 | void releaseCachedClients(ProjectId projectId) { |
216 | | - if (ProjectId.DEFAULT.equals(Objects.requireNonNull(projectId))) { |
217 | | - clusterClientsHolder.clearCache(); |
218 | | - return; |
219 | | - } |
220 | | - |
221 | | - assert perProjectClientsHolders != null : "expect per-project clients holders to be non-null"; |
222 | | - final var old = perProjectClientsHolders.get(projectId); |
| 199 | + final var old = getClientsHolder(projectId); |
223 | 200 | if (old != null) { |
224 | 201 | old.clearCache(); |
225 | 202 | } |
226 | 203 | } |
227 | 204 |
|
| 205 | + private ClientsHolder<?> getClientsHolderSafe(ProjectId projectId) { |
| 206 | + final var clientsHolder = getClientsHolder(projectId); |
| 207 | + if (clientsHolder == null) { |
| 208 | + throw new IllegalArgumentException("no s3 client is configured for project [" + projectId + "]"); |
| 209 | + } |
| 210 | + return clientsHolder; |
| 211 | + } |
| 212 | + |
| 213 | + @Nullable |
| 214 | + private ClientsHolder<?> getClientsHolder(ProjectId projectId) { |
| 215 | + if (ProjectId.DEFAULT.equals(Objects.requireNonNull(projectId))) { |
| 216 | + return clusterClientsHolder; |
| 217 | + } else { |
| 218 | + assert perProjectClientsHolders != null : "expect per-project clients holders to be non-null"; |
| 219 | + return perProjectClientsHolders.get(projectId); |
| 220 | + } |
| 221 | + } |
| 222 | + |
228 | 223 | /** |
229 | 224 | * Shutdown the manager by closing all clients holders. This is called when the node is shutting down. |
230 | 225 | */ |
|
0 commit comments