|
73 | 73 | final class FlightSqlClient implements AutoCloseable { |
74 | 74 |
|
75 | 75 | private static final Logger LOG = LoggerFactory.getLogger(FlightSqlClient.class); |
76 | | - static final int AUTOCLOSEABLE_CHECK_LIMIT = 10; |
77 | 76 |
|
78 | 77 | private final FlightClient client; |
79 | 78 |
|
80 | 79 | private final Map<String, String> defaultHeaders = new HashMap<>(); |
81 | 80 | private final ObjectMapper objectMapper = new ObjectMapper(); |
82 | | - final List<AutoCloseable> autoCloseables = new ArrayList<>(); |
83 | 81 |
|
84 | 82 | FlightSqlClient(@Nonnull final ClientConfig config) { |
85 | 83 | this(config, null); |
@@ -134,44 +132,16 @@ Stream<VectorSchemaRoot> execute(@Nonnull final String query, |
134 | 132 | CallOption[] callOptionArray = GrpcCallOptions.mergeCallOptions(callOptions, headerCallOption); |
135 | 133 |
|
136 | 134 | Ticket ticket = new Ticket(json.getBytes(StandardCharsets.UTF_8)); |
137 | | - StatefulFlightStream stream = new StatefulFlightStream(client.getStream(ticket, callOptionArray)); |
| 135 | + FlightStream stream = client.getStream(ticket, callOptionArray); |
138 | 136 | FlightSqlIterator iterator = new FlightSqlIterator(stream); |
139 | | - addToAutoCloseable(stream); |
140 | 137 |
|
141 | 138 | Spliterator<VectorSchemaRoot> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL); |
142 | 139 | return StreamSupport.stream(spliterator, false).onClose(iterator::close); |
143 | 140 | } |
144 | 141 |
|
145 | | - private synchronized void addToAutoCloseable(@Nonnull final AutoCloseable closeable) { |
146 | | - // need to occasionally clean up references to closed streams |
147 | | - // in order to ensure memory can get freed. |
148 | | - if (autoCloseables.size() > AUTOCLOSEABLE_CHECK_LIMIT) { |
149 | | - LOG.debug("checking to cleanup stale flight streams from {} known streams", autoCloseables.size()); |
150 | | - |
151 | | - cleanAutoCloseables(); |
152 | | - } |
153 | | - |
154 | | - autoCloseables.add(closeable); |
155 | | - LOG.debug("autoCloseables count {}", autoCloseables.size()); |
156 | | - } |
157 | | - |
158 | | - public void cleanAutoCloseables() { |
159 | | - ListIterator<AutoCloseable> iter = autoCloseables.listIterator(); |
160 | | - while (iter.hasNext()) { |
161 | | - AutoCloseable autoCloseable = iter.next(); |
162 | | - if (autoCloseable.getClass() == FlightSqlClient.StatefulFlightStream.class) { |
163 | | - if (((FlightSqlClient.StatefulFlightStream) autoCloseable).closed) { |
164 | | - iter.remove(); |
165 | | - } |
166 | | - } |
167 | | - } |
168 | | - } |
169 | | - |
170 | 142 | @Override |
171 | 143 | public void close() throws Exception { |
172 | | - autoCloseables.add(client); |
173 | | - AutoCloseables.close(autoCloseables); |
174 | | - cleanAutoCloseables(); |
| 144 | + client.close(); |
175 | 145 | } |
176 | 146 |
|
177 | 147 | @Nonnull |
@@ -273,60 +243,36 @@ ProxyDetector createProxyDetector(@Nonnull final String targetUrl, @Nonnull fina |
273 | 243 | }; |
274 | 244 | } |
275 | 245 |
|
276 | | - private static final class StatefulFlightStream implements AutoCloseable { |
277 | | - FlightStream flightStream; |
278 | | - Boolean closed; |
279 | | - |
280 | | - public StatefulFlightStream(@Nonnull final FlightStream flightStream) { |
281 | | - this.flightStream = flightStream; |
282 | | - this.closed = false; |
283 | | - } |
284 | | - |
285 | | - @Override |
286 | | - public void close() throws Exception { |
287 | | - this.flightStream.close(); |
288 | | - this.closed = true; |
289 | | - } |
290 | | - } |
291 | | - |
292 | 246 | private static final class FlightSqlIterator implements Iterator<VectorSchemaRoot>, AutoCloseable { |
293 | 247 |
|
294 | 248 | private final List<AutoCloseable> autoCloseable = new ArrayList<>(); |
295 | 249 |
|
296 | | - private final StatefulFlightStream sFlightStream; |
| 250 | + private final FlightStream flightStream; |
297 | 251 |
|
298 | | - private FlightSqlIterator(@Nonnull final StatefulFlightStream sFlightStream) { |
299 | | - this.sFlightStream = sFlightStream; |
| 252 | + private FlightSqlIterator(@Nonnull final FlightStream flightStream) { |
| 253 | + this.flightStream = flightStream; |
300 | 254 | } |
301 | 255 |
|
302 | 256 | @Override |
303 | 257 | public boolean hasNext() { |
304 | | - boolean nextable = sFlightStream.flightStream.next(); |
305 | | - if (!nextable) { |
306 | | - // Nothing left to read - close the stream |
307 | | - try { |
308 | | - sFlightStream.close(); |
309 | | - } catch (Exception e) { |
310 | | - LOG.error("Error while closing FlightStream: ", e); |
311 | | - } |
312 | | - } |
313 | | - return nextable; |
| 258 | + return flightStream.next(); |
314 | 259 | } |
315 | 260 |
|
316 | 261 | @Override |
317 | 262 | public VectorSchemaRoot next() { |
318 | | - if (sFlightStream.flightStream.getRoot() == null) { |
| 263 | + if (flightStream.getRoot() == null) { |
319 | 264 | throw new NoSuchElementException(); |
320 | 265 | } |
321 | 266 |
|
322 | | - autoCloseable.add(sFlightStream.flightStream.getRoot()); |
| 267 | + autoCloseable.add(flightStream.getRoot()); |
323 | 268 |
|
324 | | - return sFlightStream.flightStream.getRoot(); |
| 269 | + return flightStream.getRoot(); |
325 | 270 | } |
326 | 271 |
|
327 | 272 | @Override |
328 | 273 | public void close() { |
329 | 274 | try { |
| 275 | + flightStream.close(); |
330 | 276 | AutoCloseables.close(autoCloseable); |
331 | 277 | } catch (Exception e) { |
332 | 278 | throw new RuntimeException(e); |
|
0 commit comments