YARN-5659. getPathFromYarnURL should use standard methods. Contributed by Sergey Shelukhin.

(cherry picked from commit 459a4833a9)
This commit is contained in:
Junping Du 2016-10-07 07:46:08 -07:00
parent a6bb21eec4
commit 8bee319130
2 changed files with 130 additions and 27 deletions

View File

@ -18,11 +18,15 @@
package org.apache.hadoop.yarn.api.records; package org.apache.hadoop.yarn.api.records;
import com.google.common.annotations.VisibleForTesting;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Stable; import org.apache.hadoop.classification.InterfaceStability.Stable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider; import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
import org.apache.hadoop.yarn.util.Records; import org.apache.hadoop.yarn.util.Records;
@ -128,28 +132,16 @@ public static URL newInstance(String scheme, String host, int port, String file)
@Public @Public
@Stable @Stable
public Path toPath() throws URISyntaxException { public Path toPath() throws URISyntaxException {
String scheme = getScheme() == null ? "" : getScheme(); return new Path(new URI(getScheme(), getUserInfo(),
getHost(), getPort(), getFile(), null, null));
String authority = "";
if (getHost() != null) {
authority = getHost();
if (getUserInfo() != null) {
authority = getUserInfo() + "@" + authority;
}
if (getPort() > 0) {
authority += ":" + getPort();
}
} }
return new Path(
(new URI(scheme, authority, getFile(), null, null)).normalize());
}
@Public @Private
@Stable @VisibleForTesting
public static URL fromURI(URI uri) { public static URL fromURI(URI uri, Configuration conf) {
URL url = URL url =
RecordFactoryProvider.getRecordFactory(null).newRecordInstance( RecordFactoryProvider.getRecordFactory(conf).newRecordInstance(
URL.class); URL.class);
if (uri.getHost() != null) { if (uri.getHost() != null) {
url.setHost(uri.getHost()); url.setHost(uri.getHost());
@ -163,6 +155,18 @@ public static URL fromURI(URI uri) {
return url; return url;
} }
@Public
@Stable
public static URL fromURI(URI uri) {
return fromURI(uri, null);
}
@Private
@VisibleForTesting
public static URL fromPath(Path path, Configuration conf) {
return fromURI(path.toUri(), conf);
}
@Public @Public
@Stable @Stable
public static URL fromPath(Path path) { public static URL fromPath(Path path) {

View File

@ -0,0 +1,99 @@
/**
* 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.hadoop.yarn.api.records;
import static org.junit.Assert.assertEquals;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.factories.RecordFactory;
import org.junit.Test;
/** Test for the URL class. */
public class TestURL {
@Test
public void testConversion() throws Exception {
Configuration conf = new Configuration();
conf.set(YarnConfiguration.IPC_RECORD_FACTORY_CLASS,
RecordFactoryForTest.class.getName());
String[] pathStrs = new String[] {"/", ".", "foo/bar", "foo",
"/foo/bar/baz", "moo://bar/baz", "moo://bar:123/baz", "moo:///foo",
"moo://foo@bar:123/baz/foo", "moo://foo@bar/baz/foo", "moo://foo@bar",
"moo://foo:123"};
for (String s : pathStrs) {
Path path = new Path(s);
assertEquals(path, URL.fromPath(path, conf).toPath());
}
Path p = new Path("/foo/bar#baz");
assertEquals(p, URL.fromPath(p, conf).toPath());
}
/** Record factory that instantiates URLs for this test. */
public static class RecordFactoryForTest implements RecordFactory {
private static final RecordFactoryForTest SELF =
new RecordFactoryForTest();
@SuppressWarnings("unchecked")
@Override
public <T> T newRecordInstance(Class<T> clazz) {
return (T) new URLForTest();
}
public static RecordFactory get() {
return SELF;
}
}
/** URL fake for this test; sidesteps proto-URL dependency. */
public static class URLForTest extends URL {
private String scheme, userInfo, host, file;
private int port;
public String getScheme() {
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
}