|
21 | 21 | import com.alipay.oceanbase.rpc.exception.*; |
22 | 22 | import com.alipay.oceanbase.rpc.location.model.TableEntry; |
23 | 23 | import com.alipay.oceanbase.rpc.location.model.partition.ObPair; |
| 24 | +import com.alipay.oceanbase.rpc.location.model.partition.ObPartitionLevel; |
24 | 25 | import com.alipay.oceanbase.rpc.protocol.payload.Constants; |
25 | 26 | import com.alipay.oceanbase.rpc.protocol.payload.ObPayload; |
26 | 27 | import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.query.*; |
@@ -176,6 +177,103 @@ protected void closeLastStreamResult(ObPair<Long, ObTableParam> partIdWithObTabl |
176 | 177 | } |
177 | 178 | } |
178 | 179 |
|
| 180 | + public boolean queryLastStreamResultInNext() throws Exception { |
| 181 | + Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet() |
| 182 | + .iterator(); |
| 183 | + Map.Entry<Long, ObPair<Long, ObTableParam>> lastEntry = it.next(); |
| 184 | + try { |
| 185 | + // try access new partition, async will not remove useless expectant |
| 186 | + referToLastStreamResult(lastEntry.getValue()); |
| 187 | + } catch (Exception e) { |
| 188 | + if (shouldRetry(e)) { |
| 189 | + String realTableName = client.getPhyTableNameFromTableGroup(entityType, |
| 190 | + tableName); |
| 191 | + TableEntry entry = client.getOrRefreshTableEntry(realTableName, false); |
| 192 | + // Calculate the next partition only when the range partition is affected by a split, based on the keys already scanned. |
| 193 | + if (entry.isPartitionTable() |
| 194 | + && entry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_ONE |
| 195 | + && entry.getPartitionInfo().getFirstPartDesc().getPartFuncType() |
| 196 | + .isRangePart()) { |
| 197 | + this.asyncRequest.getObTableQueryRequest().getTableQuery() |
| 198 | + .adjustStartKey(currentStartKey); |
| 199 | + setExpectant(refreshPartition(this.asyncRequest |
| 200 | + .getObTableQueryRequest().getTableQuery(), realTableName)); |
| 201 | + setEnd(true); |
| 202 | + } else { |
| 203 | + // non one-level-range partitioned table does not retry, inform user to rescan |
| 204 | + throw e; |
| 205 | + } |
| 206 | + } else { |
| 207 | + throw e; |
| 208 | + } |
| 209 | + } |
| 210 | + // remove useless expectant if it is end |
| 211 | + if (isEnd()) { |
| 212 | + it.remove(); |
| 213 | + } |
| 214 | + if (!cacheRows.isEmpty()) { |
| 215 | + nextRow(); |
| 216 | + return true; |
| 217 | + } |
| 218 | + return false; |
| 219 | + } |
| 220 | + |
| 221 | + public boolean queryNewStreamResultInNext() throws Exception { |
| 222 | + boolean hasNext = false; |
| 223 | + Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet() |
| 224 | + .iterator(); |
| 225 | + int retryTimes = 0; |
| 226 | + long startExecute = System.currentTimeMillis(); |
| 227 | + while (it.hasNext()) { |
| 228 | + Map.Entry<Long, ObPair<Long, ObTableParam>> entry = it.next(); |
| 229 | + try { |
| 230 | + // try access new partition, async will not remove useless expectant |
| 231 | + referToNewPartition(entry.getValue()); |
| 232 | + } catch (Exception e) { |
| 233 | + if (shouldRetry(e)) { |
| 234 | + String realTableName = client.getPhyTableNameFromTableGroup(entityType, |
| 235 | + tableName); |
| 236 | + TableEntry tableEntry = client.getOrRefreshTableEntry(realTableName, false); |
| 237 | + if (tableEntry.isPartitionTable() |
| 238 | + && tableEntry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_ONE |
| 239 | + && tableEntry.getPartitionInfo().getFirstPartDesc().getPartFuncType() |
| 240 | + .isRangePart()) { |
| 241 | + this.asyncRequest.getObTableQueryRequest().getTableQuery() |
| 242 | + .adjustStartKey(currentStartKey); |
| 243 | + setExpectant(refreshPartition(this.asyncRequest |
| 244 | + .getObTableQueryRequest().getTableQuery(), realTableName)); |
| 245 | + } else { |
| 246 | + // non one-level-range partitioned table does not retry, inform user to rescan |
| 247 | + throw e; |
| 248 | + } |
| 249 | + it = expectant.entrySet().iterator(); |
| 250 | + retryTimes++; |
| 251 | + long costMillis = System.currentTimeMillis() - startExecute; |
| 252 | + if (costMillis > client.getRuntimeMaxWait()) { |
| 253 | + RUNTIME.error("Fail to get refresh table entry response after {}", |
| 254 | + retryTimes); |
| 255 | + throw new ObTableTimeoutExcetion( |
| 256 | + "Fail to get refresh table entry response after " + retryTimes); |
| 257 | + } |
| 258 | + continue; |
| 259 | + } else { |
| 260 | + throw e; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + // remove useless expectant if it is end |
| 265 | + if (isEnd()) { |
| 266 | + it.remove(); |
| 267 | + } |
| 268 | + if (!cacheRows.isEmpty()) { |
| 269 | + hasNext = true; |
| 270 | + nextRow(); |
| 271 | + break; |
| 272 | + } |
| 273 | + } |
| 274 | + return hasNext; |
| 275 | + } |
| 276 | + |
179 | 277 | @Override |
180 | 278 | protected Map<Long, ObPair<Long, ObTableParam>> refreshPartition(ObTableQuery tableQuery, |
181 | 279 | String tableName) |
@@ -222,94 +320,26 @@ public boolean next() throws Exception { |
222 | 320 | return true; |
223 | 321 | } |
224 | 322 |
|
| 323 | + boolean hasNext = false; |
225 | 324 | // secondly, refer to the last stream result |
226 | 325 | if (!isEnd() && !expectant.isEmpty()) { |
227 | | - Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet() |
228 | | - .iterator(); |
229 | | - |
230 | | - Map.Entry<Long, ObPair<Long, ObTableParam>> lastEntry = it.next(); |
231 | 326 | try { |
232 | | - // try access new partition, async will not remove useless expectant |
233 | | - referToLastStreamResult(lastEntry.getValue()); |
234 | | - } catch (Exception e) { |
235 | | - if (shouldRetry(e)) { |
236 | | - String realTableName = client.getPhyTableNameFromTableGroup(entityType, |
237 | | - tableName); |
238 | | - TableEntry entry = client.getOrRefreshTableEntry(realTableName, false); |
239 | | - // Calculate the next partition only when the range partition is affected by a split, based on the keys already scanned. |
240 | | - if (entry.isPartitionTable() |
241 | | - && entry.getPartitionInfo().getFirstPartDesc().getPartFuncType() |
242 | | - .isRangePart()) { |
243 | | - this.asyncRequest.getObTableQueryRequest().getTableQuery() |
244 | | - .adjustStartKey(currentStartKey); |
245 | | - setExpectant(refreshPartition(this.asyncRequest |
246 | | - .getObTableQueryRequest().getTableQuery(), realTableName)); |
247 | | - setEnd(true); |
248 | | - } |
249 | | - } else { |
250 | | - throw e; |
251 | | - } |
252 | | - } |
253 | | - // remove useless expectant if it is end |
254 | | - if (isEnd()) |
255 | | - it.remove(); |
256 | | - |
257 | | - if (!cacheRows.isEmpty()) { |
258 | | - nextRow(); |
259 | | - return true; |
| 327 | + hasNext = queryLastStreamResultInNext(); |
| 328 | + } catch (ObTableSessionNotExistException e) { |
| 329 | + // session_id missing because the tablet has been transferred to new server |
| 330 | + // new server does not store the current session_id |
| 331 | + // only support range-partitioned table, check in server |
| 332 | + this.asyncRequest.getObTableQueryRequest().getTableQuery() |
| 333 | + .adjustStartKey(currentStartKey); |
| 334 | + // just need to asjust startKey to anchor the correct position |
| 335 | + // no need to refresh partition id for session_id missing |
| 336 | + hasNext = queryNewStreamResultInNext(); |
260 | 337 | } |
261 | 338 | } |
262 | | - |
263 | 339 | // lastly, refer to the new partition |
264 | | - boolean hasNext = false; |
265 | | - Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet() |
266 | | - .iterator(); |
267 | | - int retryTimes = 0; |
268 | | - long startExecute = System.currentTimeMillis(); |
269 | | - while (it.hasNext()) { |
270 | | - Map.Entry<Long, ObPair<Long, ObTableParam>> entry = it.next(); |
271 | | - try { |
272 | | - // try access new partition, async will not remove useless expectant |
273 | | - referToNewPartition(entry.getValue()); |
274 | | - } catch (Exception e) { |
275 | | - if (shouldRetry(e)) { |
276 | | - String realTableName = client.getPhyTableNameFromTableGroup(entityType, |
277 | | - tableName); |
278 | | - TableEntry tableEntry = client.getOrRefreshTableEntry(realTableName, false); |
279 | | - if (tableEntry.isPartitionTable() |
280 | | - && tableEntry.getPartitionInfo().getFirstPartDesc().getPartFuncType() |
281 | | - .isRangePart()) { |
282 | | - this.asyncRequest.getObTableQueryRequest().getTableQuery() |
283 | | - .adjustStartKey(currentStartKey); |
284 | | - setExpectant(refreshPartition(this.asyncRequest |
285 | | - .getObTableQueryRequest().getTableQuery(), realTableName)); |
286 | | - } |
287 | | - it = expectant.entrySet().iterator(); |
288 | | - retryTimes++; |
289 | | - long costMillis = System.currentTimeMillis() - startExecute; |
290 | | - if (costMillis > client.getRuntimeMaxWait()) { |
291 | | - RUNTIME.error("Fail to get refresh table entry response after {}", |
292 | | - retryTimes); |
293 | | - throw new ObTableTimeoutExcetion( |
294 | | - "Fail to get refresh table entry response after " + retryTimes); |
295 | | - } |
296 | | - continue; |
297 | | - } else { |
298 | | - throw e; |
299 | | - } |
300 | | - } |
301 | | - |
302 | | - // remove useless expectant if it is end |
303 | | - if (isEnd()) |
304 | | - it.remove(); |
305 | | - |
306 | | - if (!cacheRows.isEmpty()) { |
307 | | - hasNext = true; |
308 | | - nextRow(); |
309 | | - break; |
310 | | - } |
| 340 | + if (!hasNext) { |
| 341 | + hasNext = queryNewStreamResultInNext(); |
311 | 342 | } |
312 | | - |
313 | 343 | return hasNext; |
314 | 344 | } finally { |
315 | 345 | lock.unlock(); |
|
0 commit comments