Skip to content

Commit

Permalink
Merge pull request #39 from abyswang/master
Browse files Browse the repository at this point in the history
add user agent version matching
  • Loading branch information
vintmd authored Nov 23, 2021
2 parents 8ba1c28 + ac688f0 commit 978368b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
</distributionManagement>

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>hadoopCosPluginVersionInfo.properties</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -146,5 +156,6 @@
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1 change: 1 addition & 0 deletions src/main/java/hadoopCosPluginVersionInfo.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin_version=${project.version}
29 changes: 26 additions & 3 deletions src/main/java/org/apache/hadoop/fs/CosNativeFileSystemStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.utils.Base64;
import com.qcloud.cos.utils.IOUtils;
import com.qcloud.cos.utils.Jackson;
import com.qcloud.cos.utils.StringUtils;
import org.apache.commons.codec.binary.Hex;
Expand Down Expand Up @@ -138,9 +139,9 @@ private void initCOSClient(URI uri, Configuration conf) throws IOException {
config.setProxyPassword(proxyPassword);
}

String userAgent = conf.get(
CosNConfigKeys.USER_AGENT,
CosNConfigKeys.DEFAULT_USER_AGENT);
String versionNum = getPluginVersionInfo();
String versionInfo = versionNum.equals("unknown") ? CosNConfigKeys.DEFAULT_USER_AGENT : "cos-hadoop-plugin-v" + versionNum;
String userAgent = conf.get(CosNConfigKeys.USER_AGENT,versionInfo);
String emrVersion = conf.get(CosNConfigKeys.TENCENT_EMR_VERSION_KEY);
if (null != emrVersion && !emrVersion.isEmpty()) {
userAgent = String.format("%s on %s", userAgent, emrVersion);
Expand Down Expand Up @@ -1343,4 +1344,26 @@ private <X> Object callCOSClientWithRetry(X request) throws CosServiceException,
private static String ensureValidAttributeName(String attributeName) {
return attributeName.replace('.', '-').toLowerCase();
}

private String getPluginVersionInfo() {
Properties versionProperties = new Properties();
InputStream inputStream= null;
String versionStr = "unknown";
final String versionFile = "hadoopCosPluginVersionInfo.properties";
try {
inputStream = this.getClass().getClassLoader().getResourceAsStream(versionFile);
if (inputStream != null) {
versionProperties.load(inputStream);
versionStr = versionProperties.getProperty("plugin_version");
}else {
LOG.error("load versionInfo properties failed, propName: {} ", versionFile);
}
} catch (IOException e) {
LOG.error("load versionInfo properties exception, propName: {} ", versionFile);
} finally {
IOUtils.closeQuietly(inputStream,LOG);
}
return versionStr;
}

}

0 comments on commit 978368b

Please sign in to comment.