|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.concurrent.CompletableFuture; |
24 | 24 | import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.concurrent.Executors; |
25 | 27 |
|
26 | 28 | /** An IO reader implementation for JDBC. */ |
27 | 29 | public class HologresJDBCReader<T> extends HologresReader<T> { |
28 | 30 | private static final transient Logger LOG = LoggerFactory.getLogger(HologresJDBCReader.class); |
29 | 31 | private final HologresRecordConverter<T, Record> recordConverter; |
30 | 32 | private transient HologresJDBCClientProvider clientProvider; |
31 | 33 | protected final boolean insertIfNotExists; |
| 34 | + protected ExecutorService callbackPool; |
32 | 35 |
|
33 | 36 | public HologresJDBCReader( |
34 | 37 | String[] primaryKeys, |
@@ -71,6 +74,7 @@ public void open(Integer subtaskIdx, Integer numSubtasks) throws IOException { |
71 | 74 | if (insertIfNotExists) { |
72 | 75 | LOG.info("Hologres dim table will insert new record if primary key does not exist."); |
73 | 76 | } |
| 77 | + this.callbackPool = Executors.newFixedThreadPool(1); |
74 | 78 | this.clientProvider = new HologresJDBCClientProvider(param); |
75 | 79 | LOG.info( |
76 | 80 | "Successfully initiated connection to database [{}] / table[{}]", |
@@ -121,7 +125,8 @@ public CompletableFuture<T> asyncGet(T in) throws IOException { |
121 | 125 | result.completeExceptionally(e); |
122 | 126 | } |
123 | 127 | return null; |
124 | | - }); |
| 128 | + }, |
| 129 | + callbackPool); |
125 | 130 | } catch (HoloClientException e) { |
126 | 131 | result.completeExceptionally(e); |
127 | 132 | } |
@@ -154,22 +159,28 @@ public CompletableFuture<List<T>> asyncGetMany(T in) throws IOException { |
154 | 159 | .asyncScan(scanBuilder.build()) |
155 | 160 | .handleAsync( |
156 | 161 | (scanner, throwable) -> { |
157 | | - List<T> result = new ArrayList<>(); |
158 | | - while (true) { |
159 | | - try { |
160 | | - if (!scanner.next()) { |
| 162 | + // caught an error |
| 163 | + if (throwable != null) { |
| 164 | + scanResult.completeExceptionally(throwable); |
| 165 | + } else { |
| 166 | + List<T> result = new ArrayList<>(); |
| 167 | + while (true) { |
| 168 | + try { |
| 169 | + if (!scanner.next()) { |
| 170 | + break; |
| 171 | + } |
| 172 | + Record resultRecord = scanner.getRecord(); |
| 173 | + result.add(recordConverter.convertTo(resultRecord)); |
| 174 | + } catch (HoloClientException e) { |
| 175 | + scanResult.completeExceptionally(e); |
161 | 176 | break; |
162 | 177 | } |
163 | | - Record resultRecord = scanner.getRecord(); |
164 | | - result.add(recordConverter.convertTo(resultRecord)); |
165 | | - } catch (HoloClientException e) { |
166 | | - scanResult.completeExceptionally(e); |
167 | | - break; |
168 | 178 | } |
| 179 | + scanResult.complete(result); |
169 | 180 | } |
170 | | - scanResult.complete(result); |
171 | 181 | return null; |
172 | | - }); |
| 182 | + }, |
| 183 | + callbackPool); |
173 | 184 | } catch (HoloClientException e) { |
174 | 185 | throw new IOException(e); |
175 | 186 | } |
|
0 commit comments