Skip to content

Commit c252365

Browse files
mehakmeetsnvijaya
authored andcommitted
HADOOP-17194. Adding Context class for AbfsClient in ABFS (apache#2216)
Contributed by Mehakmeet Singh.
1 parent 8e91196 commit c252365

File tree

5 files changed

+161
-18
lines changed

5 files changed

+161
-18
lines changed

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystemStore.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import org.apache.hadoop.fs.azurebfs.oauth2.IdentityTransformer;
7979
import org.apache.hadoop.fs.azurebfs.services.AbfsAclHelper;
8080
import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
81+
import org.apache.hadoop.fs.azurebfs.services.AbfsClientContext;
82+
import org.apache.hadoop.fs.azurebfs.services.AbfsClientContextBuilder;
8183
import org.apache.hadoop.fs.azurebfs.services.AbfsCounters;
8284
import org.apache.hadoop.fs.azurebfs.services.AbfsHttpOperation;
8385
import org.apache.hadoop.fs.azurebfs.services.AbfsInputStream;
@@ -138,6 +140,7 @@ public class AzureBlobFileSystemStore implements Closeable {
138140
private final UserGroupInformation userGroupInformation;
139141
private final IdentityTransformer identityTransformer;
140142
private final AbfsPerfTracker abfsPerfTracker;
143+
private final AbfsCounters abfsCounters;
141144

142145
public AzureBlobFileSystemStore(URI uri, boolean isSecureScheme,
143146
Configuration configuration,
@@ -177,7 +180,8 @@ public AzureBlobFileSystemStore(URI uri, boolean isSecureScheme,
177180
boolean usingOauth = (authType == AuthType.OAuth);
178181
boolean useHttps = (usingOauth || abfsConfiguration.isHttpsAlwaysUsed()) ? true : isSecureScheme;
179182
this.abfsPerfTracker = new AbfsPerfTracker(fileSystemName, accountName, this.abfsConfiguration);
180-
initializeClient(uri, fileSystemName, accountName, useHttps, abfsCounters);
183+
this.abfsCounters = abfsCounters;
184+
initializeClient(uri, fileSystemName, accountName, useHttps);
181185
this.identityTransformer = new IdentityTransformer(abfsConfiguration.getRawConfiguration());
182186
LOG.trace("IdentityTransformer init complete");
183187
}
@@ -1128,8 +1132,19 @@ public boolean isAtomicRenameKey(String key) {
11281132
return isKeyForDirectorySet(key, azureAtomicRenameDirSet);
11291133
}
11301134

1135+
/**
1136+
* A on-off operation to initialize AbfsClient for AzureBlobFileSystem
1137+
* Operations.
1138+
*
1139+
* @param uri Uniform resource identifier for Abfs.
1140+
* @param fileSystemName Name of the fileSystem being used.
1141+
* @param accountName Name of the account being used to access Azure
1142+
* data store.
1143+
* @param isSecure Tells if https is being used or http.
1144+
* @throws IOException
1145+
*/
11311146
private void initializeClient(URI uri, String fileSystemName,
1132-
String accountName, boolean isSecure, AbfsCounters abfsCounters)
1147+
String accountName, boolean isSecure)
11331148
throws IOException {
11341149
if (this.client != null) {
11351150
return;
@@ -1165,11 +1180,25 @@ private void initializeClient(URI uri, String fileSystemName,
11651180

11661181
LOG.trace("Initializing AbfsClient for {}", baseUrl);
11671182
this.client = new AbfsClient(baseUrl, creds, abfsConfiguration,
1168-
new ExponentialRetryPolicy(abfsConfiguration.getMaxIoRetries()),
1169-
tokenProvider, abfsPerfTracker, abfsCounters);
1183+
tokenProvider,
1184+
populateAbfsClientContext());
11701185
LOG.trace("AbfsClient init complete");
11711186
}
11721187

1188+
/**
1189+
* Populate a new AbfsClientContext instance with the desired properties.
1190+
*
1191+
* @return an instance of AbfsClientContext.
1192+
*/
1193+
private AbfsClientContext populateAbfsClientContext() {
1194+
return new AbfsClientContextBuilder()
1195+
.withExponentialRetryPolicy(
1196+
new ExponentialRetryPolicy(abfsConfiguration.getMaxIoRetries()))
1197+
.withAbfsCounters(abfsCounters)
1198+
.withAbfsPerfTracker(abfsPerfTracker)
1199+
.build();
1200+
}
1201+
11731202
private String getOctalNotation(FsPermission fsPermission) {
11741203
Preconditions.checkNotNull(fsPermission, "fsPermission");
11751204
return String.format(AbfsHttpConstants.PERMISSION_FORMAT, fsPermission.toOctal());

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,23 @@ public class AbfsClient implements Closeable {
6262
private final String userAgent;
6363
private final AbfsPerfTracker abfsPerfTracker;
6464

65+
private final String accountName;
66+
private final AuthType authType;
6567
private AccessTokenProvider tokenProvider;
6668
private final AbfsCounters abfsCounters;
6769

6870

6971
public AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredentials,
7072
final AbfsConfiguration abfsConfiguration,
71-
final ExponentialRetryPolicy exponentialRetryPolicy,
72-
final AbfsPerfTracker abfsPerfTracker,
73-
final AbfsCounters abfsCounters) {
73+
final AbfsClientContext abfsClientContext) {
7474
this.baseUrl = baseUrl;
7575
this.sharedKeyCredentials = sharedKeyCredentials;
7676
String baseUrlString = baseUrl.toString();
7777
this.filesystem = baseUrlString.substring(baseUrlString.lastIndexOf(FORWARD_SLASH) + 1);
7878
this.abfsConfiguration = abfsConfiguration;
79-
this.retryPolicy = exponentialRetryPolicy;
79+
this.retryPolicy = abfsClientContext.getExponentialRetryPolicy();
80+
this.accountName = abfsConfiguration.getAccountName().substring(0, abfsConfiguration.getAccountName().indexOf(AbfsHttpConstants.DOT));
81+
this.authType = abfsConfiguration.getAuthType(accountName);
8082

8183
String sslProviderName = null;
8284

@@ -95,18 +97,15 @@ public AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredent
9597

9698
this.userAgent = initializeUserAgent(abfsConfiguration, sslProviderName);
9799
this.tokenProvider = tokenProvider;
98-
this.abfsPerfTracker = abfsPerfTracker;
99-
this.abfsCounters = abfsCounters;
100+
this.abfsPerfTracker = abfsClientContext.getAbfsPerfTracker();
101+
this.abfsCounters = abfsClientContext.getAbfsCounters();
100102
}
101103

102104
public AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredentials,
103105
final AbfsConfiguration abfsConfiguration,
104-
final ExponentialRetryPolicy exponentialRetryPolicy,
105106
final AccessTokenProvider tokenProvider,
106-
final AbfsPerfTracker abfsPerfTracker,
107-
final AbfsCounters abfsCounters) {
108-
this(baseUrl, sharedKeyCredentials, abfsConfiguration,
109-
exponentialRetryPolicy, abfsPerfTracker, abfsCounters);
107+
final AbfsClientContext abfsClientContext) {
108+
this(baseUrl, sharedKeyCredentials, abfsConfiguration, abfsClientContext);
110109
this.tokenProvider = tokenProvider;
111110
}
112111

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.fs.azurebfs.services;
20+
21+
/**
22+
* Class to hold extra configurations for AbfsClient and further classes
23+
* inside AbfsClient.
24+
*/
25+
public class AbfsClientContext {
26+
27+
private final ExponentialRetryPolicy exponentialRetryPolicy;
28+
private final AbfsPerfTracker abfsPerfTracker;
29+
private final AbfsCounters abfsCounters;
30+
31+
AbfsClientContext(
32+
ExponentialRetryPolicy exponentialRetryPolicy,
33+
AbfsPerfTracker abfsPerfTracker,
34+
AbfsCounters abfsCounters) {
35+
this.exponentialRetryPolicy = exponentialRetryPolicy;
36+
this.abfsPerfTracker = abfsPerfTracker;
37+
this.abfsCounters = abfsCounters;
38+
}
39+
40+
public ExponentialRetryPolicy getExponentialRetryPolicy() {
41+
return exponentialRetryPolicy;
42+
}
43+
44+
public AbfsPerfTracker getAbfsPerfTracker() {
45+
return abfsPerfTracker;
46+
}
47+
48+
public AbfsCounters getAbfsCounters() {
49+
return abfsCounters;
50+
}
51+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.fs.azurebfs.services;
20+
21+
/**
22+
* A builder for AbfsClientContext class with different options to select and
23+
* build from.
24+
*/
25+
public class AbfsClientContextBuilder {
26+
27+
private ExponentialRetryPolicy exponentialRetryPolicy;
28+
private AbfsPerfTracker abfsPerfTracker;
29+
private AbfsCounters abfsCounters;
30+
31+
public AbfsClientContextBuilder withExponentialRetryPolicy(
32+
final ExponentialRetryPolicy exponentialRetryPolicy) {
33+
this.exponentialRetryPolicy = exponentialRetryPolicy;
34+
return this;
35+
}
36+
37+
public AbfsClientContextBuilder withAbfsPerfTracker(
38+
final AbfsPerfTracker abfsPerfTracker) {
39+
this.abfsPerfTracker = abfsPerfTracker;
40+
return this;
41+
}
42+
43+
public AbfsClientContextBuilder withAbfsCounters(final AbfsCounters abfsCounters) {
44+
this.abfsCounters = abfsCounters;
45+
return this;
46+
}
47+
48+
/**
49+
* Build the context and get the instance with the properties selected.
50+
*
51+
* @return an instance of AbfsClientContext.
52+
*/
53+
public AbfsClientContext build() {
54+
//validate the values
55+
return new AbfsClientContext(exponentialRetryPolicy, abfsPerfTracker,
56+
abfsCounters);
57+
}
58+
}

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/services/TestAbfsClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ private void validateUserAgent(String expectedPattern,
4545
URL baseUrl,
4646
AbfsConfiguration config,
4747
boolean includeSSLProvider) {
48+
AbfsClientContext abfsClientContext = new AbfsClientContextBuilder().build();
4849
AbfsClient client = new AbfsClient(baseUrl, null,
49-
config, null, null, null, null);
50+
config, null, abfsClientContext);
5051
String sslProviderName = null;
5152
if (includeSSLProvider) {
5253
sslProviderName = SSLSocketFactoryEx.getDefaultFactory().getProviderName();
@@ -106,6 +107,12 @@ public static AbfsClient createTestClientFromCurrentContext(
106107
abfsConfig.getAccountName(),
107108
abfsConfig);
108109

110+
AbfsClientContext abfsClientContext =
111+
new AbfsClientContextBuilder().withAbfsPerfTracker(tracker)
112+
.withExponentialRetryPolicy(
113+
new ExponentialRetryPolicy(abfsConfig.getMaxIoRetries()))
114+
.build();
115+
109116
// Create test AbfsClient
110117
AbfsClient testClient = new AbfsClient(
111118
baseAbfsClientInstance.getBaseUrl(),
@@ -116,11 +123,10 @@ public static AbfsClient createTestClientFromCurrentContext(
116123
abfsConfig.getStorageAccountKey())
117124
: null),
118125
abfsConfig,
119-
new ExponentialRetryPolicy(abfsConfig.getMaxIoRetries()),
120126
(currentAuthType == AuthType.OAuth
121127
? abfsConfig.getTokenProvider()
122128
: null),
123-
tracker, null);
129+
abfsClientContext);
124130

125131
return testClient;
126132
}

0 commit comments

Comments
 (0)