Skip to content
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

uc query result support disk cache #592

Merged
merged 23 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qiniu.common;

import java.io.File;
import java.nio.charset.Charset;

/**
Expand Down Expand Up @@ -47,7 +48,28 @@ public final class Constants {
*/
public static final int CONNECTION_POOL_MAX_IDLE_MINUTES = 5;

public static final String CACHE_DIR = getCacheDir();

private Constants() {
}

private static String getCacheDir() {
String tmpDir = System.getProperty("java.io.tmpdir");
if (tmpDir == null || tmpDir.isEmpty()) {
return null;
}

String qiniuDir = tmpDir + "com.qiniu.java-sdk";
File dir = new File(qiniuDir);
if (!dir.exists()) {
return dir.mkdirs() ? qiniuDir : null;
}

if (dir.isDirectory()) {
return qiniuDir;
}

return null;
}
}

11 changes: 11 additions & 0 deletions src/main/java/com/qiniu/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,18 @@ public static Response create(okhttp3.Response response, String address, double
error = e.getMessage();
}
}
} else if (response.body() != null) {
// 处理其他 body 非预期情况
if (response.code() >= 300) {
try {
body = response.body().bytes();
error = new String(body, Constants.UTF_8);
} catch (Exception e) {
error = e.getMessage();
}
}
}

return new Response(response, code, reqId, response.header("X-Log"), via(response),
address, duration, error, body);
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,12 @@ public StringMap getHeader() throws QiniuException {
header.put(key, this.header.get(key));
}

if (header.keySet().contains("Content-Type")) {
if (body == null || body.contentType == null
|| header.keySet().contains("Content-Type")) {
return header;
}

if (body != null) {
header.put("Content-Type", body.contentType.toString());
}
header.put("Content-Type", body.contentType.toString());

return header;
}
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/com/qiniu/storage/AutoRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.StringUtils;
import com.qiniu.util.UrlUtils;
import com.qiniu.util.*;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -27,7 +26,9 @@ class AutoRegion extends Region {
/**
* 全局空间信息缓存,此缓存绑定了 token、bucket,全局有效。
*/
private static Map<String, UCRet> globalRegionCache = new ConcurrentHashMap<>();
private static final Cache<UCRet> globalRegionCache = new Cache.Builder<>(UCRet.class)
.setVersion("v1")
.builder();

/**
* 定义HTTP请求管理相关方法
Expand Down Expand Up @@ -75,8 +76,8 @@ private AutoRegion() {
* 通过 API 接口查询上传域名
*/
private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuException {
String cacheKey = index.accessKey + index.bucket;
UCRet ret = globalRegionCache.get(cacheKey);
String cacheKey = getCacheId(index);
UCRet ret = globalRegionCache.cacheForKey(cacheKey);
if (ret != null && ret.isValid()) {
return ret;
}
Expand All @@ -94,7 +95,7 @@ private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuE
ret = r.jsonToObject(UCRet.class);
if (ret != null) {
ret.setupDeadline();
globalRegionCache.put(cacheKey, ret);
globalRegionCache.cache(cacheKey, ret);
}
return ret;
}
Expand Down Expand Up @@ -122,7 +123,7 @@ static Region regionGroup(UCRet ret) {
*/
private Region queryRegionInfo(String accessKey, String bucket) throws QiniuException {
RegionIndex index = new RegionIndex(accessKey, bucket);
String cacheKey = index.accessKey + "::" + index.bucket;
String cacheKey = getCacheId(index);
Region region = regions.get(cacheKey);

Exception ex = null;
Expand Down Expand Up @@ -311,6 +312,22 @@ String[] getUcHostArray() throws QiniuException {
return getUcHosts(null).toArray(new String[0]);
}

private String getCacheId(RegionIndex index) {
StringBuilder builder = new StringBuilder()
.append(index.accessKey)
.append("-")
.append(index.bucket);

if (ucServers != null && !ucServers.isEmpty()) {
for (String host : ucServers) {
if (host != null && !host.isEmpty()) {
builder.append(host);
}
}
}

return UrlSafeBase64.encodeToString(builder.toString());
}

public Object clone() {
AutoRegion newRegion = new AutoRegion();
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/com/qiniu/storage/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,44 @@ public String tryChangeUpHost(String upToken, String lastUsedHost) throws QiniuE

private String upHost(String upToken, String lastUsedHost, boolean changeHost, boolean mustReturnUpHost)
throws QiniuException {
return getScheme()
+ getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
String host = getHelper().upHost(config.region, upToken, UrlUtils.removeHostScheme(lastUsedHost), changeHost, mustReturnUpHost);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String ioHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getIovipHost(regionReqInfo);
String host = config.region.getIovipHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String ioSrcHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return config.region.getIoSrcHost(regionReqInfo);
String host = config.region.getIoSrcHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String apiHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getApiHost(regionReqInfo);
String host = config.region.getApiHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getRsHost(regionReqInfo);
String host = config.region.getRsHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsfHost(String ak, String bucket) throws QiniuException {
RegionReqInfo regionReqInfo = new RegionReqInfo(ak, bucket);
return getScheme() + config.region.getRsfHost(regionReqInfo);
String host = config.region.getRsfHost(regionReqInfo);
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String rsHost() {
Expand All @@ -78,7 +89,8 @@ public String rsHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultRsHost;
}
return getScheme() + host;
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String apiHost() {
Expand All @@ -91,7 +103,8 @@ public String apiHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultApiHost;
}
return getScheme() + host;
host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

public String ucHost() {
Expand All @@ -104,7 +117,9 @@ public String ucHost() {
if (host == null || host.length() == 0) {
host = Configuration.defaultUcHost;
}
return getScheme() + host;

host = UrlUtils.setHostScheme(host, config.useHttpsDomains);
return host;
}

List<String> ucHostsWithoutScheme() {
Expand Down Expand Up @@ -151,10 +166,6 @@ List<String> upHostsWithoutScheme() {
return new ArrayList<>(hosts);
}

private String getScheme() {
return config.useHttpsDomains ? "https://" : "http://";
}

private void makeSureRegion() {
if (config.region == null) {
if (config.zone != null) {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/qiniu/storage/DownloadUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ protected void didBuildUrl() throws QiniuException {
}

private String getUrlPrefix() throws QiniuException {
if (useHttps) {
return "https://" + domain;
} else {
return "http://" + domain;
}
return UrlUtils.setHostScheme(domain, useHttps);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/qiniu/storage/UpHostHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
regionHostsLRU.put(regionKey, regionHost);
}

String host = regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
return host;
return regionHost.upHost(accHosts, srcHosts, lastUsedHost, changeHost);
}

private String failedUpHost(String regionKey) {
Expand Down
Loading
Loading