-
Notifications
You must be signed in to change notification settings - Fork 475
支持多区域上传 #522
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
Merged
Merged
支持多区域上传 #522
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b5a5558
uc query api to v3
YangSen-qn 06fcf37
upload support switch region
YangSen-qn 4837522
upload performer support switch region
YangSen-qn 30b59f8
switch region case add form
YangSen-qn 7130d6c
check region valid
YangSen-qn 4ae3983
switch region check region valid
YangSen-qn 25b65a2
delete useless code
YangSen-qn e7dd746
static region id list
YangSen-qn c7dd361
base uploader check config
YangSen-qn 824f619
static before final
YangSen-qn 4a4c198
reload source
YangSen-qn 678bc3e
code style
YangSen-qn 984275d
optimize switch region logic
YangSen-qn e42bfe9
change get regionid logic & add switch region test
YangSen-qn 47ea1ab
code style
YangSen-qn 4f5f606
close private clound test
YangSen-qn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.qiniu.storage; | ||
|
||
import com.qiniu.common.QiniuException; | ||
import com.qiniu.http.Client; | ||
import com.qiniu.http.Response; | ||
|
||
public abstract class BaseUploader { | ||
|
||
protected final Client client; | ||
protected final String key; | ||
protected final String upToken; | ||
protected final ConfigHelper configHelper; | ||
protected final Configuration config; | ||
|
||
BaseUploader(Client client, String upToken, String key, Configuration config) { | ||
this.client = client; | ||
this.key = key; | ||
this.upToken = upToken; | ||
if (config == null) { | ||
this.config = new Configuration(); | ||
} else { | ||
this.config = config.clone(); | ||
} | ||
this.configHelper = new ConfigHelper(this.config); | ||
} | ||
|
||
public Response upload() throws QiniuException { | ||
if (this.config == null) { | ||
throw QiniuException.unrecoverable("config can't be empty"); | ||
} | ||
return uploadWithRegionRetry(); | ||
} | ||
|
||
private Response uploadWithRegionRetry() throws QiniuException { | ||
Response response = null; | ||
while (true) { | ||
try { | ||
response = uploadFlows(); | ||
if (!couldSwitchRegionAndRetry(response, null) | ||
|| !couldReloadSource() || !reloadSource() | ||
|| config.region == null || !config.region.switchRegion(new UploadToken(upToken))) { | ||
break; | ||
} | ||
} catch (QiniuException e) { | ||
if (!couldSwitchRegionAndRetry(null, e) | ||
|| !couldReloadSource() || !reloadSource() | ||
|| config.region == null || !config.region.switchRegion(new UploadToken(upToken))) { | ||
throw e; | ||
} | ||
} | ||
} | ||
return response; | ||
} | ||
|
||
abstract Response uploadFlows() throws QiniuException; | ||
|
||
abstract boolean couldReloadSource(); | ||
|
||
abstract boolean reloadSource(); | ||
|
||
private boolean couldSwitchRegionAndRetry(Response response, QiniuException exception) { | ||
Response checkResponse = response; | ||
if (checkResponse == null && exception != null) { | ||
checkResponse = exception.response; | ||
} | ||
|
||
if (checkResponse != null) { | ||
int statusCode = checkResponse.statusCode; | ||
return (statusCode > -2 && statusCode < 200) || (statusCode > 299 | ||
&& statusCode != 401 && statusCode != 413 && statusCode != 419 | ||
&& statusCode != 608 && statusCode != 614 && statusCode != 630); | ||
} | ||
|
||
return exception == null || !exception.isUnrecoverable(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.qiniu.storage; | ||
|
||
import com.qiniu.common.QiniuException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RegionGroup extends Region { | ||
|
||
private Region currentRegion = null; | ||
private int currentRegionIndex = 0; | ||
private final List<Region> regionList = new ArrayList<>(); | ||
|
||
|
||
public boolean addRegion(Region region) { | ||
if (region == null) { | ||
return false; | ||
} | ||
|
||
regionList.add(region); | ||
|
||
if (currentRegion == null) { | ||
updateCurrentRegion(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
boolean switchRegion(RegionReqInfo regionReqInfo) { | ||
if (currentRegion != null && currentRegion.isValid() && currentRegion.switchRegion(regionReqInfo)) { | ||
return true; | ||
} | ||
|
||
if ((currentRegionIndex + 1) < regionList.size()) { | ||
currentRegionIndex += 1; | ||
updateCurrentRegion(); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
String getRegion(RegionReqInfo regionReqInfo) { | ||
if (currentRegion == null) { | ||
return ""; | ||
} else { | ||
return currentRegion.getRegion(regionReqInfo); | ||
} | ||
} | ||
|
||
List<String> getSrcUpHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getSrcUpHost(regionReqInfo); | ||
} | ||
} | ||
|
||
List<String> getAccUpHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getAccUpHost(regionReqInfo); | ||
} | ||
} | ||
|
||
String getIovipHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getIovipHost(regionReqInfo); | ||
} | ||
} | ||
|
||
String getRsHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getRsHost(regionReqInfo); | ||
} | ||
} | ||
|
||
String getRsfHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getRsfHost(regionReqInfo); | ||
} | ||
} | ||
|
||
String getApiHost(RegionReqInfo regionReqInfo) throws QiniuException { | ||
if (currentRegion == null) { | ||
return null; | ||
} else { | ||
return currentRegion.getApiHost(regionReqInfo); | ||
} | ||
} | ||
|
||
Region getCurrentRegion(RegionReqInfo regionReqInfo) { | ||
if (currentRegion == null) { | ||
return null; | ||
} else if (currentRegion instanceof AutoRegion || currentRegion instanceof RegionGroup) { | ||
return currentRegion.getCurrentRegion(regionReqInfo); | ||
} else { | ||
return currentRegion; | ||
} | ||
} | ||
|
||
@Override | ||
boolean isValid() { | ||
if (currentRegion == null) { | ||
return false; | ||
} | ||
// 只判断当前的 | ||
return currentRegion.isValid(); | ||
} | ||
|
||
private void updateCurrentRegion() { | ||
if (regionList.size() == 0) { | ||
return; | ||
} | ||
|
||
if (currentRegionIndex < regionList.size()) { | ||
currentRegion = regionList.get(currentRegionIndex); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.