1
1
package com .qiniu .storage ;
2
2
3
3
import com .qiniu .common .QiniuException ;
4
+ import com .qiniu .util .StringUtils ;
4
5
5
6
import java .util .*;
6
7
@@ -32,11 +33,15 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
32
33
try {
33
34
accHosts = region .getAccUpHost (regionReqInfo );
34
35
srcHosts = region .getSrcUpHost (regionReqInfo );
35
- } catch (QiniuException e ) { // it will success soon.
36
+ } catch (Exception e ) { // it will success soon.
36
37
if (mustReturnUpHost && conf .useDefaultUpHostIfNone ) {
37
38
return failedUpHost ("failed_get_region" );
38
39
} else {
39
- throw e ;
40
+ if (e instanceof QiniuException ) {
41
+ throw e ;
42
+ } else {
43
+ throw QiniuException .unrecoverable (e );
44
+ }
40
45
}
41
46
}
42
47
@@ -50,7 +55,7 @@ String upHost(Region region, String upToken, String lastUsedHost, boolean change
50
55
return regionHost .upHost (accHosts , srcHosts , lastUsedHost , changeHost );
51
56
}
52
57
53
- private String failedUpHost (String regionKey ) {
58
+ private String failedUpHost (String regionKey ) throws QiniuException {
54
59
List <String > srcHosts ;
55
60
List <String > accHosts ;
56
61
@@ -75,42 +80,44 @@ private String failedUpHost(String regionKey) {
75
80
srcHosts = regionHost .lastSrcHosts ;
76
81
accHosts = regionHost .lastAccHosts ;
77
82
}
78
- String host = regionHost .upHost (accHosts , srcHosts , null , false );
79
- return host ;
83
+ return regionHost .upHost (accHosts , srcHosts , null , false );
80
84
}
81
85
82
86
83
87
String getRegionKey (List <String > srcHosts , List <String > accHosts ) {
84
88
String host = null ;
85
89
String lhost = null ;
86
- for (String a : srcHosts ) {
87
- if (host == null ) {
88
- host = a ;
89
- lhost = a ;
90
- }
91
- if (a .length () < host .length ()) {
92
- host = a ;
93
- }
94
- if (a .length () > lhost .length ()) {
95
- lhost = a ;
90
+
91
+ if (srcHosts != null ) {
92
+ for (String a : srcHosts ) {
93
+ if (host == null ) {
94
+ host = a ;
95
+ lhost = a ;
96
+ }
97
+ if (a .length () < host .length ()) {
98
+ host = a ;
99
+ }
100
+ if (a .length () > lhost .length ()) {
101
+ lhost = a ;
102
+ }
96
103
}
97
104
}
98
- if (host != null ) {
99
- return host + ";+=-_" + lhost ;
100
- }
101
105
102
- for (String a : accHosts ) {
103
- if (host == null ) {
104
- host = a ;
105
- lhost = a ;
106
- }
107
- if (a .length () < host .length ()) {
108
- host = a ;
109
- }
110
- if (a .length () > lhost .length ()) {
111
- lhost = a ;
106
+ if (host == null && accHosts != null ) {
107
+ for (String a : accHosts ) {
108
+ if (host == null ) {
109
+ host = a ;
110
+ lhost = a ;
111
+ }
112
+ if (a .length () < host .length ()) {
113
+ host = a ;
114
+ }
115
+ if (a .length () > lhost .length ()) {
116
+ lhost = a ;
117
+ }
112
118
}
113
119
}
120
+
114
121
return host + ";+=-_" + lhost ;
115
122
}
116
123
@@ -126,6 +133,14 @@ class RegionUpHost {
126
133
volatile String lastHost ;
127
134
128
135
private void initHostMark (List <String > f , List <String > s ) {
136
+ if (f == null ) {
137
+ f = new ArrayList <>();
138
+ }
139
+
140
+ if (s == null ) {
141
+ s = new ArrayList <>();
142
+ }
143
+
129
144
ArrayList <String > _lastHosts = new ArrayList <>();
130
145
int _mainHostCount = 0 ;
131
146
@@ -181,7 +196,12 @@ private void randomAdd(ArrayList<String> _lastHosts, ArrayList<Integer> iidx, Ra
181
196
}
182
197
}
183
198
184
- String upHost (List <String > accHosts , List <String > srcHosts , String lastUsedHost , boolean changeHost ) {
199
+ String upHost (List <String > accHosts , List <String > srcHosts , String lastUsedHost , boolean changeHost ) throws QiniuException {
200
+ if ((accHosts == null || accHosts .isEmpty ()) &&
201
+ (srcHosts == null || srcHosts .isEmpty ())) {
202
+ throw QiniuException .unrecoverable ("no up host found" );
203
+ }
204
+
185
205
if (lastAccHosts != accHosts || lastSrcHosts != srcHosts ) {
186
206
lastAccHosts = accHosts ;
187
207
lastSrcHosts = srcHosts ;
0 commit comments