Skip to content

Commit

Permalink
Uc query hosts order (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed Jul 25, 2023
1 parent abe1593 commit 7729932
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 26 deletions.
6 changes: 1 addition & 5 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.qiniu.util.StringUtils;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.BufferedSink;

Expand Down Expand Up @@ -117,10 +116,7 @@ protected com.qiniu.http.Response innerRequest(Request request) throws QiniuExce
MethodType method = request.getMethod();
String url = request.getUrl().toString();
StringMap header = request.getHeader();
RequestBody body = null;
if (method.hasContent()) {
body = request.getRequestBody();
}
RequestBody body = method.hasContent() ? request.getRequestBody() : null;
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder()
.url(url)
.method(method.toString(), body);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ApiInterceptorAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Api.Response intercept(Api.Request request, Api.Handler handler) throws QiniuExc
return handler.handle(request);
}

final static class Builder {
static final class Builder {
private Auth auth;

Builder setAuth(Auth auth) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ApiQueryRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ApiQueryRegion extends Api {
public ApiQueryRegion(Client client) {
this(client, new Api.Config.Builder()
.setHostRetryMax(DEFAULT_UC_BACKUP_HOSTS.length)
.setHostProvider(HostProvider.ArrayProvider(DEFAULT_UC_BACKUP_HOSTS))
.setHostProvider(HostProvider.arrayProvider(DEFAULT_UC_BACKUP_HOSTS))
.build());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/AutoRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private UCRet queryRegionInfoFromServerIfNeeded(RegionIndex index) throws QiniuE
.setRetryInterval(retryInterval)
.setHostFreezeDuration(hostFreezeDuration)
.setHostRetryMax(ucHosts.length)
.setHostProvider(HostProvider.ArrayProvider(ucHosts))
.setHostProvider(HostProvider.arrayProvider(ucHosts))
.build());
Response r = api.requestWithInterceptor(new Api.Request(address));
ret = r.jsonToObject(UCRet.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/BucketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ private Api.Interceptor[] ucInterceptors() throws QiniuException {
Api.Interceptor hostRetryInterceptor = new ApiInterceptorRetryHosts.Builder()
.setRetryMax(config.retryMax)
.setRetryInterval(Retry.staticInterval(config.retryInterval))
.setHostProvider(HostProvider.ArrayProvider(ucHosts))
.setHostProvider(HostProvider.arrayProvider(ucHosts))
.setHostFreezeDuration(config.hostFreezeDuration)
.build();
return new Api.Interceptor[]{authInterceptor, hostRetryInterceptor};
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/qiniu/storage/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.qiniu.util.UrlUtils;

import java.util.*;
import java.util.function.Function;

class ConfigHelper {
private Configuration config;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/qiniu/storage/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public final class Configuration implements Cloneable {
*/
public static String defaultRsHost = "rs.qiniu.com";
public static String defaultApiHost = "api.qiniu.com";
public static String defaultUcHost = "uc.qbox.me";
public static String defaultUcHost = "kodo-config.qiniuapi.com";

static final String ucBackUpHost0 = "kodo-config.qiniuapi.com";
static final String ucBackUpHost0 = "uc.qbox.me";
static final String[] defaultUcHosts = new String[]{defaultUcHost, ucBackUpHost0, defaultApiHost};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/FormUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Response uploadFlows() throws QiniuException {
.setHostRetryMax(config.retryMax)
.setRetryInterval(Retry.staticInterval(config.retryInterval))
.setHostFreezeDuration(config.hostFreezeDuration)
.setHostProvider(HostProvider.ArrayProvider(this.configHelper.upHostsWithoutScheme().toArray(new String[0])))
.setHostProvider(HostProvider.arrayProvider(this.configHelper.upHostsWithoutScheme().toArray(new String[0])))
.build());
Api.Request request = new Api.Request(urlPrefix);
request.setMethod(MethodType.POST);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/HostProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public abstract class HostProvider {

public static HostProvider ArrayProvider(String... hosts) {
public static HostProvider arrayProvider(String... hosts) {
if (hosts == null || hosts.length == 0) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/storage/ResumeUploadPerformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class ResumeUploadPerformer {
.setHostRetryMax(ipHosts.size())
.setRetryInterval(Retry.staticInterval(config.retryInterval))
.setHostFreezeDuration(config.hostFreezeDuration)
.setHostProvider(HostProvider.ArrayProvider(ipHosts.toArray(new String[0])))
.setHostProvider(HostProvider.arrayProvider(ipHosts.toArray(new String[0])))
.build();
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/qiniu/util/DefaultHeader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.qiniu.util;

import com.qiniu.http.Client;
import com.qiniu.http.Headers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand All @@ -11,6 +10,9 @@
public final class DefaultHeader {
public static final String DISABLE_TIMESTAMP_SIGNATURE_ENV_KEY = "DISABLE_QINIU_TIMESTAMP_SIGNATURE";

private DefaultHeader() {
}

public static void setDefaultHeader(HeadAdder adder) {
if (adder == null) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/qiniu/util/Timestamp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.qiniu.util;

public final class Timestamp {

private Timestamp() {
}

public static long second() {
return System.currentTimeMillis() / 1000;
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/qiniu/storage/ApiQueryRegionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.storage.*;
import com.qiniu.util.Timestamp;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void testQueryWithRetry() {
.setHostRetryMax(retryMax)
.setRetryInterval(1000)
.setSingleHostRetryMax(retryMax)
.setHostProvider(HostProvider.ArrayProvider("mock.uc.com", Configuration.defaultUcHost))
.setHostProvider(HostProvider.arrayProvider("mock.uc.com", Configuration.defaultUcHost))
.build());
ApiQueryRegion.Request request = new ApiQueryRegion.Request("https://mock.uc.com", accessKey, bucket);

Expand All @@ -89,7 +88,7 @@ public void testQueryWithRetry() {
System.out.println("query region data:" + response.getDataMap());
long en = Timestamp.second();

assertTrue(en - st >= retryMax * 2, "retry interval or retry max error");
assertTrue(en - st >= retryMax + 1, "retry interval or retry max error");

assertTrue(response.isOK(), response.getResponse() + "");

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/test/com/qiniu/storage/ApiUploadV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testUpload(boolean isUploadBytes) {
try {
Field hostsFiled = Region.class.getDeclaredField("srcUpHosts");
hostsFiled.setAccessible(true);
hosts = new ArrayList<String>((List<String>)hostsFiled.get(region));
hosts = new ArrayList<>((List<String>) hostsFiled.get(region));
} catch (Exception e) {
e.printStackTrace();
fail(e);
Expand All @@ -104,7 +104,7 @@ public void testUpload(boolean isUploadBytes) {
.setResponseDebugLevel(Api.Config.DebugLevelNormal)
.setResponseDebugLevel(1)
.setHostRetryMax(hosts.size())
.setHostProvider(HostProvider.ArrayProvider(hosts.toArray(new String[0])))
.setHostProvider(HostProvider.arrayProvider(hosts.toArray(new String[0])))
.build();
if (!isUploadBytes) {
// 非 bytes 不支持重试
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/test/com/qiniu/storage/ApiUploadV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private void testUpload(boolean isUploadBytes, boolean isSetContentLength) {
try {
Field hostsFiled = Region.class.getDeclaredField("srcUpHosts");
hostsFiled.setAccessible(true);
hosts = new ArrayList<String>((List<String>)hostsFiled.get(region));
List<String> srcHosts = (List<String>) hostsFiled.get(region);
hosts = new ArrayList<>(srcHosts);
} catch (Exception e) {
e.printStackTrace();
fail(e);
Expand All @@ -108,7 +109,7 @@ private void testUpload(boolean isUploadBytes, boolean isSetContentLength) {
.setRequestDebugLevel(Api.Config.DebugLevelNormal)
.setResponseDebugLevel(Api.Config.DebugLevelNormal)
.setHostRetryMax(hosts.size())
.setHostProvider(HostProvider.ArrayProvider(hosts.toArray(new String[0])))
.setHostProvider(HostProvider.arrayProvider(hosts.toArray(new String[0])))
.build();
if (!isUploadBytes) {
// 非 bytes 不支持重试
Expand Down Expand Up @@ -193,7 +194,7 @@ private void testUpload(boolean isUploadBytes, boolean isSetContentLength) {
ApiUploadV2ListParts listPartsApi = new ApiUploadV2ListParts(client, apiConfig);
ApiUploadV2ListParts.Request listPartsRequest = new ApiUploadV2ListParts.Request(urlPrefix, token,
uploadId).setKey(key).setMaxParts(2) // 此处仅为示例分页拉去,实际可不配置使用默认值1000
.setPartNumberMarker(partNumberMarker);
.setPartNumberMarker(partNumberMarker);
try {
ApiUploadV2ListParts.Response listPartsResponse = listPartsApi.request(listPartsRequest);
partNumberMarker = listPartsResponse.getPartNumberMarker();
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/test/com/qiniu/storage/BucketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.*;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import okhttp3.Call;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -75,8 +74,13 @@ public void testCreateBuckets() throws Exception {
testFileWithHandler(new TestFileHandler() {
@Override
public void testFile(TestConfig.TestFile file, BucketManager bucketManager) throws IOException {
String bucket = "java-sdk-test-create-bucket";
try {
bucketManager.deleteBucket(bucket);
} catch (QiniuException e) {
}

try {
String bucket = "java-sdk-test-create-bucket";
Response response = bucketManager.createBucket(bucket, file.getRegionId());
assertTrue(response.isOK());

Expand Down

0 comments on commit 7729932

Please sign in to comment.