-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NIFI-10152 Storage client caching in Azure ADLS processors #6158
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e9f41d7
NIFI-10152 Storage client caching in Azure ADLS processors
nandorsoma eba42d9
NIFI-10152 apply review comments
nandorsoma aee5908
NIFI-10152 adding a little clarification to the filesystem description
nandorsoma 5eb9ec8
NIFI-10152 log cache behavior when getting client
nandorsoma bf587ad
NIFI-10152 test cache mechanism in DataLakeServiceClientFactoryTest t…
nandorsoma f1040e7
NIFI-10152 address review comment
nandorsoma File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...ain/java/org/apache/nifi/processors/azure/storage/utils/DataLakeServiceClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.nifi.processors.azure.storage.utils; | ||
|
||
import com.azure.core.credential.AccessToken; | ||
import com.azure.core.credential.TokenCredential; | ||
import com.azure.core.http.HttpClient; | ||
import com.azure.core.http.ProxyOptions; | ||
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; | ||
import com.azure.identity.ClientSecretCredential; | ||
import com.azure.identity.ClientSecretCredentialBuilder; | ||
import com.azure.identity.ManagedIdentityCredential; | ||
import com.azure.identity.ManagedIdentityCredentialBuilder; | ||
import com.azure.storage.common.StorageSharedKeyCredential; | ||
import com.azure.storage.file.datalake.DataLakeServiceClient; | ||
import com.azure.storage.file.datalake.DataLakeServiceClientBuilder; | ||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.nifi.logging.ComponentLog; | ||
import org.apache.nifi.services.azure.storage.ADLSCredentialsDetails; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class DataLakeServiceClientFactory { | ||
|
||
private static final long STORAGE_CLIENT_CACHE_SIZE = 10; | ||
|
||
private final ComponentLog logger; | ||
|
||
private final Cache<ADLSCredentialsDetails, DataLakeServiceClient> clientCache; | ||
|
||
public DataLakeServiceClientFactory(ComponentLog logger) { | ||
this.logger = logger; | ||
this.clientCache = createCache(); | ||
} | ||
|
||
private Cache<ADLSCredentialsDetails, DataLakeServiceClient> createCache() { | ||
return Caffeine.newBuilder() | ||
.maximumSize(STORAGE_CLIENT_CACHE_SIZE) | ||
.build(); | ||
} | ||
|
||
public Cache<ADLSCredentialsDetails, DataLakeServiceClient> getCache() { | ||
return clientCache; | ||
} | ||
|
||
/** | ||
* Retrieves a {@link DataLakeServiceClient} | ||
* | ||
* @param credentialsDetails used for caching because it can contain properties that are results of an expression | ||
* @param proxyOptions not used for caching, because proxy parameters are set in the ProxyConfiguration service | ||
* @return DataLakeServiceClient | ||
*/ | ||
public DataLakeServiceClient getStorageClient(ADLSCredentialsDetails credentialsDetails, ProxyOptions proxyOptions) { | ||
return clientCache.get(credentialsDetails, __ -> { | ||
logger.debug("DataLakeServiceClient is not found in the cache with the given credentials. Creating it."); | ||
return createStorageClient(credentialsDetails, proxyOptions); | ||
}); | ||
} | ||
|
||
private static DataLakeServiceClient createStorageClient(ADLSCredentialsDetails credentialsDetails, ProxyOptions proxyOptions) { | ||
final String accountName = credentialsDetails.getAccountName(); | ||
final String accountKey = credentialsDetails.getAccountKey(); | ||
final String sasToken = credentialsDetails.getSasToken(); | ||
final AccessToken accessToken = credentialsDetails.getAccessToken(); | ||
final String endpointSuffix = credentialsDetails.getEndpointSuffix(); | ||
final boolean useManagedIdentity = credentialsDetails.getUseManagedIdentity(); | ||
final String managedIdentityClientId = credentialsDetails.getManagedIdentityClientId(); | ||
final String servicePrincipalTenantId = credentialsDetails.getServicePrincipalTenantId(); | ||
final String servicePrincipalClientId = credentialsDetails.getServicePrincipalClientId(); | ||
final String servicePrincipalClientSecret = credentialsDetails.getServicePrincipalClientSecret(); | ||
|
||
final String endpoint = String.format("https://%s.%s", accountName, endpointSuffix); | ||
|
||
final DataLakeServiceClientBuilder dataLakeServiceClientBuilder = new DataLakeServiceClientBuilder(); | ||
dataLakeServiceClientBuilder.endpoint(endpoint); | ||
|
||
if (StringUtils.isNotBlank(accountKey)) { | ||
final StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey); | ||
dataLakeServiceClientBuilder.credential(credential); | ||
} else if (StringUtils.isNotBlank(sasToken)) { | ||
dataLakeServiceClientBuilder.sasToken(sasToken); | ||
} else if (accessToken != null) { | ||
final TokenCredential credential = tokenRequestContext -> Mono.just(accessToken); | ||
dataLakeServiceClientBuilder.credential(credential); | ||
} else if (useManagedIdentity) { | ||
final ManagedIdentityCredential misCredential = new ManagedIdentityCredentialBuilder() | ||
.clientId(managedIdentityClientId) | ||
.build(); | ||
dataLakeServiceClientBuilder.credential(misCredential); | ||
} else if (StringUtils.isNoneBlank(servicePrincipalTenantId, servicePrincipalClientId, servicePrincipalClientSecret)) { | ||
final ClientSecretCredential credential = new ClientSecretCredentialBuilder() | ||
.tenantId(servicePrincipalTenantId) | ||
.clientId(servicePrincipalClientId) | ||
.clientSecret(servicePrincipalClientSecret) | ||
.build(); | ||
dataLakeServiceClientBuilder.credential(credential); | ||
} else { | ||
throw new IllegalArgumentException("No valid credentials were provided"); | ||
} | ||
|
||
final NettyAsyncHttpClientBuilder nettyClientBuilder = new NettyAsyncHttpClientBuilder(); | ||
nettyClientBuilder.proxy(proxyOptions); | ||
|
||
final HttpClient nettyClient = nettyClientBuilder.build(); | ||
dataLakeServiceClientBuilder.httpClient(nettyClient); | ||
|
||
return dataLakeServiceClientBuilder.buildClient(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nandorsoma I think it is a bit overkill to expose internal fields publicly for integration tests only. Furthermore, the IT does not really test we should check: it asserts the cache size at the end (1 client in the cache) but it is more important to check how many client instance creation have happened (only 1).
It could be tested with a unit test and in that case the public getter would not be needed.