-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from qiniu/ys_dev
支持多区域上传
- Loading branch information
Showing
14 changed files
with
1,012 additions
and
285 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 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.