Skip to content

Commit f6e1ed4

Browse files
mehakmeetsteveloughran
authored andcommitted
HADOOP-17194. Adding Context class for AbfsClient in ABFS (#2216)
Contributed by Mehakmeet Singh. Change-Id: I120c9a068d758d8e5d071c878a3b7fbeb95e4de6
1 parent fcb80c1 commit f6e1ed4

File tree

5 files changed

+161
-25
lines changed

5 files changed

+161
-25
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
import org.apache.hadoop.fs.azurebfs.oauth2.IdentityTransformerInterface;
8585
import org.apache.hadoop.fs.azurebfs.services.AbfsAclHelper;
8686
import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
87+
import org.apache.hadoop.fs.azurebfs.services.AbfsClientContext;
88+
import org.apache.hadoop.fs.azurebfs.services.AbfsClientContextBuilder;
8789
import org.apache.hadoop.fs.azurebfs.services.AbfsCounters;
8890
import org.apache.hadoop.fs.azurebfs.services.AbfsHttpOperation;
8991
import org.apache.hadoop.fs.azurebfs.services.AbfsInputStream;
@@ -146,6 +148,7 @@ public class AzureBlobFileSystemStore implements Closeable {
146148
private final UserGroupInformation userGroupInformation;
147149
private final IdentityTransformerInterface identityTransformer;
148150
private final AbfsPerfTracker abfsPerfTracker;
151+
private final AbfsCounters abfsCounters;
149152

150153
/**
151154
* The set of directories where we should store files as append blobs.
@@ -192,7 +195,8 @@ public AzureBlobFileSystemStore(URI uri, boolean isSecureScheme,
192195
boolean usingOauth = (authType == AuthType.OAuth);
193196
boolean useHttps = (usingOauth || abfsConfiguration.isHttpsAlwaysUsed()) ? true : isSecureScheme;
194197
this.abfsPerfTracker = new AbfsPerfTracker(fileSystemName, accountName, this.abfsConfiguration);
195-
initializeClient(uri, fileSystemName, accountName, useHttps, abfsCounters);
198+
this.abfsCounters = abfsCounters;
199+
initializeClient(uri, fileSystemName, accountName, useHttps);
196200
final Class<? extends IdentityTransformerInterface> identityTransformerClass =
197201
configuration.getClass(FS_AZURE_IDENTITY_TRANSFORM_CLASS, IdentityTransformer.class,
198202
IdentityTransformerInterface.class);
@@ -1213,8 +1217,19 @@ public boolean isAtomicRenameKey(String key) {
12131217
return isKeyForDirectorySet(key, azureAtomicRenameDirSet);
12141218
}
12151219

1220+
/**
1221+
* A on-off operation to initialize AbfsClient for AzureBlobFileSystem
1222+
* Operations.
1223+
*
1224+
* @param uri Uniform resource identifier for Abfs.
1225+
* @param fileSystemName Name of the fileSystem being used.
1226+
* @param accountName Name of the account being used to access Azure
1227+
* data store.
1228+
* @param isSecure Tells if https is being used or http.
1229+
* @throws IOException
1230+
*/
12161231
private void initializeClient(URI uri, String fileSystemName,
1217-
String accountName, boolean isSecure, AbfsCounters abfsCounters)
1232+
String accountName, boolean isSecure)
12181233
throws IOException {
12191234
if (this.client != null) {
12201235
return;
@@ -1261,16 +1276,30 @@ private void initializeClient(URI uri, String fileSystemName,
12611276
LOG.trace("Initializing AbfsClient for {}", baseUrl);
12621277
if (tokenProvider != null) {
12631278
this.client = new AbfsClient(baseUrl, creds, abfsConfiguration,
1264-
new ExponentialRetryPolicy(abfsConfiguration.getMaxIoRetries()),
1265-
tokenProvider, abfsPerfTracker, abfsCounters);
1279+
tokenProvider,
1280+
populateAbfsClientContext());
12661281
} else {
12671282
this.client = new AbfsClient(baseUrl, creds, abfsConfiguration,
1268-
new ExponentialRetryPolicy(abfsConfiguration.getMaxIoRetries()),
1269-
sasTokenProvider, abfsPerfTracker, abfsCounters);
1283+
sasTokenProvider,
1284+
populateAbfsClientContext());
12701285
}
12711286
LOG.trace("AbfsClient init complete");
12721287
}
12731288

1289+
/**
1290+
* Populate a new AbfsClientContext instance with the desired properties.
1291+
*
1292+
* @return an instance of AbfsClientContext.
1293+
*/
1294+
private AbfsClientContext populateAbfsClientContext() {
1295+
return new AbfsClientContextBuilder()
1296+
.withExponentialRetryPolicy(
1297+
new ExponentialRetryPolicy(abfsConfiguration.getMaxIoRetries()))
1298+
.withAbfsCounters(abfsCounters)
1299+
.withAbfsPerfTracker(abfsPerfTracker)
1300+
.build();
1301+
}
1302+
12741303
private String getOctalNotation(FsPermission fsPermission) {
12751304
Preconditions.checkNotNull(fsPermission, "fsPermission");
12761305
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: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,13 @@ public class AbfsClient implements Closeable {
7777

7878
private AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredentials,
7979
final AbfsConfiguration abfsConfiguration,
80-
final ExponentialRetryPolicy exponentialRetryPolicy,
81-
final AbfsPerfTracker abfsPerfTracker,
82-
final AbfsCounters abfsCounters) {
80+
final AbfsClientContext abfsClientContext) {
8381
this.baseUrl = baseUrl;
8482
this.sharedKeyCredentials = sharedKeyCredentials;
8583
String baseUrlString = baseUrl.toString();
8684
this.filesystem = baseUrlString.substring(baseUrlString.lastIndexOf(FORWARD_SLASH) + 1);
8785
this.abfsConfiguration = abfsConfiguration;
88-
this.retryPolicy = exponentialRetryPolicy;
86+
this.retryPolicy = abfsClientContext.getExponentialRetryPolicy();
8987
this.accountName = abfsConfiguration.getAccountName().substring(0, abfsConfiguration.getAccountName().indexOf(AbfsHttpConstants.DOT));
9088
this.authType = abfsConfiguration.getAuthType(accountName);
9189

@@ -105,29 +103,23 @@ private AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCreden
105103
}
106104

107105
this.userAgent = initializeUserAgent(abfsConfiguration, sslProviderName);
108-
this.abfsPerfTracker = abfsPerfTracker;
109-
this.abfsCounters = abfsCounters;
106+
this.abfsPerfTracker = abfsClientContext.getAbfsPerfTracker();
107+
this.abfsCounters = abfsClientContext.getAbfsCounters();
110108
}
111109

112110
public AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredentials,
113111
final AbfsConfiguration abfsConfiguration,
114-
final ExponentialRetryPolicy exponentialRetryPolicy,
115112
final AccessTokenProvider tokenProvider,
116-
final AbfsPerfTracker abfsPerfTracker,
117-
final AbfsCounters abfsCounters) {
118-
this(baseUrl, sharedKeyCredentials, abfsConfiguration,
119-
exponentialRetryPolicy, abfsPerfTracker, abfsCounters);
113+
final AbfsClientContext abfsClientContext) {
114+
this(baseUrl, sharedKeyCredentials, abfsConfiguration, abfsClientContext);
120115
this.tokenProvider = tokenProvider;
121116
}
122117

123118
public AbfsClient(final URL baseUrl, final SharedKeyCredentials sharedKeyCredentials,
124119
final AbfsConfiguration abfsConfiguration,
125-
final ExponentialRetryPolicy exponentialRetryPolicy,
126120
final SASTokenProvider sasTokenProvider,
127-
final AbfsPerfTracker abfsPerfTracker,
128-
final AbfsCounters abfsCounters) {
129-
this(baseUrl, sharedKeyCredentials, abfsConfiguration,
130-
exponentialRetryPolicy, abfsPerfTracker, abfsCounters);
121+
final AbfsClientContext abfsClientContext) {
122+
this(baseUrl, sharedKeyCredentials, abfsConfiguration, abfsClientContext);
131123
this.sasTokenProvider = sasTokenProvider;
132124
}
133125

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
@@ -103,8 +103,9 @@ public TestAbfsClient(){
103103

104104
private String getUserAgentString(AbfsConfiguration config,
105105
boolean includeSSLProvider) throws MalformedURLException {
106+
AbfsClientContext abfsClientContext = new AbfsClientContextBuilder().build();
106107
AbfsClient client = new AbfsClient(new URL("https://azure.com"), null,
107-
config, null, (AccessTokenProvider) null, null, null);
108+
config, (AccessTokenProvider) null, abfsClientContext);
108109
String sslProviderName = null;
109110
if (includeSSLProvider) {
110111
sslProviderName = DelegatingSSLSocketFactory.getDefaultFactory()
@@ -257,6 +258,12 @@ public static AbfsClient createTestClientFromCurrentContext(
257258
abfsConfig.getAccountName(),
258259
abfsConfig);
259260

261+
AbfsClientContext abfsClientContext =
262+
new AbfsClientContextBuilder().withAbfsPerfTracker(tracker)
263+
.withExponentialRetryPolicy(
264+
new ExponentialRetryPolicy(abfsConfig.getMaxIoRetries()))
265+
.build();
266+
260267
// Create test AbfsClient
261268
AbfsClient testClient = new AbfsClient(
262269
baseAbfsClientInstance.getBaseUrl(),
@@ -267,11 +274,10 @@ public static AbfsClient createTestClientFromCurrentContext(
267274
abfsConfig.getStorageAccountKey())
268275
: null),
269276
abfsConfig,
270-
new ExponentialRetryPolicy(abfsConfig.getMaxIoRetries()),
271277
(currentAuthType == AuthType.OAuth
272278
? abfsConfig.getTokenProvider()
273279
: null),
274-
tracker, null);
280+
abfsClientContext);
275281

276282
return testClient;
277283
}

0 commit comments

Comments
 (0)