|
30 | 30 | import java.util.HashMap; |
31 | 31 | import java.util.Iterator; |
32 | 32 | import java.util.List; |
| 33 | +import java.util.ListIterator; |
33 | 34 | import java.util.Map; |
34 | 35 | import java.util.NoSuchElementException; |
35 | 36 | import java.util.Spliterator; |
36 | 37 | import java.util.Spliterators; |
| 38 | +import java.util.concurrent.ConcurrentHashMap; |
37 | 39 | import java.util.stream.Stream; |
38 | 40 | import java.util.stream.StreamSupport; |
39 | 41 | import javax.annotation.Nonnull; |
|
72 | 74 | final class FlightSqlClient implements AutoCloseable { |
73 | 75 |
|
74 | 76 | private static final Logger LOG = LoggerFactory.getLogger(FlightSqlClient.class); |
| 77 | + private static int AUTOCLOSEABLE_CHECK_LIMIT = 10; |
| 78 | + private static Map<AutoCloseable, Boolean> CLOSEABLE_CLOSED_LEDGER = new ConcurrentHashMap<>(); |
75 | 79 |
|
76 | 80 | private final FlightClient client; |
77 | 81 |
|
@@ -134,12 +138,34 @@ Stream<VectorSchemaRoot> execute(@Nonnull final String query, |
134 | 138 | Ticket ticket = new Ticket(json.getBytes(StandardCharsets.UTF_8)); |
135 | 139 | FlightStream stream = client.getStream(ticket, callOptionArray); |
136 | 140 | FlightSqlIterator iterator = new FlightSqlIterator(stream); |
137 | | - autoCloseables.add(stream); |
| 141 | + addToAutoCloseable(stream); |
138 | 142 |
|
139 | 143 | Spliterator<VectorSchemaRoot> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL); |
140 | 144 | return StreamSupport.stream(spliterator, false).onClose(iterator::close); |
141 | 145 | } |
142 | 146 |
|
| 147 | + private void addToAutoCloseable(AutoCloseable closeable) { |
| 148 | + // need to occasionally clean up references to closed streams |
| 149 | + // in order to ensure memory can get freed. |
| 150 | + if(autoCloseables.size() > AUTOCLOSEABLE_CHECK_LIMIT) { |
| 151 | + LOG.info("checking to cleanup stale flight streams from {} known streams", autoCloseables.size()); |
| 152 | + |
| 153 | + ListIterator<AutoCloseable> iter = autoCloseables.listIterator(); |
| 154 | + while(iter.hasNext()){ |
| 155 | + AutoCloseable autoCloseable = iter.next(); |
| 156 | + if(CLOSEABLE_CLOSED_LEDGER.get(autoCloseable)){ |
| 157 | + LOG.info("removing closed stream {}", autoCloseable); |
| 158 | + CLOSEABLE_CLOSED_LEDGER.keySet().remove(autoCloseable); |
| 159 | + iter.remove(); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + autoCloseables.add(closeable); |
| 165 | + CLOSEABLE_CLOSED_LEDGER.put(closeable, false); |
| 166 | + LOG.debug("autoCloseables count {}, LEDGER count {}", autoCloseables.size(), CLOSEABLE_CLOSED_LEDGER.size()); |
| 167 | + } |
| 168 | + |
143 | 169 | @Override |
144 | 170 | public void close() throws Exception { |
145 | 171 | autoCloseables.add(client); |
@@ -262,6 +288,7 @@ public boolean hasNext() { |
262 | 288 | // Nothing left to read - close the stream |
263 | 289 | try { |
264 | 290 | flightStream.close(); |
| 291 | + CLOSEABLE_CLOSED_LEDGER.replace(flightStream, true); |
265 | 292 | } catch (Exception e) { |
266 | 293 | LOG.error("Error while closing FlightStream: ", e); |
267 | 294 | } |
|
0 commit comments