1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| @Override public boolean advance() { if (!isRunning()) { return false; }
URI nextUri = currentStatusInfo().getNextUri(); if (nextUri == null) { state.compareAndSet(State.RUNNING, State.FINISHED); return false; } Request request = prepareRequest(HttpUrl.get(nextUri)).build();
Exception cause = null; long start = System.nanoTime(); long attempts = 0;
while (true) { if (isClientAborted()) { return false; }
Duration sinceStart = Duration.nanosSince(start); if (attempts > 0 && sinceStart.compareTo(requestTimeoutNanos) > 0) { state.compareAndSet(State.RUNNING, State.CLIENT_ERROR); throw new RuntimeException(format("Error fetching next (attempts: %s, duration: %s)", attempts, sinceStart), cause); }
if (attempts > 0) { try { MILLISECONDS.sleep(attempts * 100); } catch (InterruptedException e) { try { close(); } finally { Thread.currentThread().interrupt(); } state.compareAndSet(State.RUNNING, State.CLIENT_ERROR); throw new RuntimeException("StatementClient thread was interrupted"); } } attempts++;
JsonResponse<QueryResults> response; try { response = JsonResponse.execute(QUERY_RESULTS_CODEC, httpClient, request); } catch (RuntimeException e) { cause = e; continue; }
if ((response.getStatusCode() == HTTP_OK) && response.hasValue()) { processResponse(response.getHeaders(), response.getValue()); return true; }
if (response.getStatusCode() != HTTP_UNAVAILABLE) { state.compareAndSet(State.RUNNING, State.CLIENT_ERROR); throw requestFailedException("fetching next", request, response); } } }
private void processResponse(Headers headers, QueryResults results) { setCatalog.set(headers.get(TRINO_HEADERS.responseSetCatalog())); setSchema.set(headers.get(TRINO_HEADERS.responseSetSchema())); setPath.set(headers.get(TRINO_HEADERS.responseSetPath())); for (String setSession : headers.values(TRINO_HEADERS.responseSetSession())) { List<String> keyValue = SESSION_HEADER_SPLITTER.splitToList(setSession); if (keyValue.size() != 2) { continue; } setSessionProperties.put(keyValue.get(0), urlDecode(keyValue.get(1))); } resetSessionProperties.addAll(headers.values(TRINO_HEADERS.responseClearSession()));
for (String setRole : headers.values(TRINO_HEADERS.responseSetRole())) { List<String> keyValue = SESSION_HEADER_SPLITTER.splitToList(setRole); if (keyValue.size() != 2) { continue; } setRoles.put(keyValue.get(0), ClientSelectedRole.valueOf(urlDecode(keyValue.get(1)))); }
for (String entry : headers.values(TRINO_HEADERS.responseAddedPrepare())) { List<String> keyValue = SESSION_HEADER_SPLITTER.splitToList(entry); if (keyValue.size() != 2) { continue; } addedPreparedStatements.put(urlDecode(keyValue.get(0)), urlDecode(keyValue.get(1))); } for (String entry : headers.values(TRINO_HEADERS.responseDeallocatedPrepare())) { deallocatedPreparedStatements.add(urlDecode(entry)); }
String startedTransactionId = headers.get(TRINO_HEADERS.responseStartedTransactionId()); if (startedTransactionId != null) { this.startedTransactionId.set(startedTransactionId); } if (headers.get(TRINO_HEADERS.responseClearTransactionId()) != null) { clearTransactionId.set(true); } currentResults.set(results); }
}
@JsonCreator public QueryResults( @JsonProperty("id") String id, @JsonProperty("infoUri") URI infoUri, @JsonProperty("partialCancelUri") URI partialCancelUri, @JsonProperty("nextUri") URI nextUri, @JsonProperty("columns") List<Column> columns, @JsonProperty("data") List<List<Object>> data, @JsonProperty("stats") StatementStats stats, @JsonProperty("error") QueryError error, @JsonProperty("warnings") List<Warning> warnings, @JsonProperty("updateType") String updateType, @JsonProperty("updateCount") Long updateCount) { this( id, infoUri, partialCancelUri, nextUri, columns, fixData(columns, data), stats, error, firstNonNull(warnings, ImmutableList.of()), updateType, updateCount); }
|