3535import static org .apache .hadoop .hbase .util .ConcurrentMapUtils .computeIfAbsent ;
3636
3737import java .io .IOException ;
38- import java .util .ArrayList ;
3938import java .util .Arrays ;
4039import java .util .HashSet ;
4140import java .util .Iterator ;
4241import java .util .LinkedHashMap ;
43- import java .util .List ;
4442import java .util .Map ;
4543import java .util .Optional ;
4644import java .util .Set ;
@@ -124,26 +122,6 @@ public boolean equals(Object obj) {
124122 }
125123 }
126124
127- private static final class RegionLocationsFutureResult {
128- private final CompletableFuture <RegionLocations > future ;
129- private final RegionLocations result ;
130- private final Throwable e ;
131-
132- public RegionLocationsFutureResult (CompletableFuture <RegionLocations > future ,
133- RegionLocations result , Throwable e ) {
134- this .future = future ;
135- this .result = result ;
136- this .e = e ;
137- }
138-
139- public void complete () {
140- if (e != null ) {
141- future .completeExceptionally (e );
142- }
143- future .complete (result );
144- }
145- }
146-
147125 private static final class TableCache {
148126
149127 private final ConcurrentNavigableMap <byte [], RegionLocations > cache =
@@ -170,20 +148,18 @@ public Optional<LocateRequest> getCandidate() {
170148 return allRequests .keySet ().stream ().filter (r -> !isPending (r )).findFirst ();
171149 }
172150
173- public List <RegionLocationsFutureResult > clearCompletedRequests (RegionLocations locations ) {
174- List <RegionLocationsFutureResult > futureResultList = new ArrayList <>();
151+ public void clearCompletedRequests (RegionLocations locations ) {
175152 for (Iterator <Map .Entry <LocateRequest , CompletableFuture <RegionLocations >>> iter =
176153 allRequests .entrySet ().iterator (); iter .hasNext ();) {
177154 Map .Entry <LocateRequest , CompletableFuture <RegionLocations >> entry = iter .next ();
178- if (tryComplete (entry .getKey (), entry .getValue (), locations , futureResultList )) {
155+ if (tryComplete (entry .getKey (), entry .getValue (), locations )) {
179156 iter .remove ();
180157 }
181158 }
182- return futureResultList ;
183159 }
184160
185161 private boolean tryComplete (LocateRequest req , CompletableFuture <RegionLocations > future ,
186- RegionLocations locations , List < RegionLocationsFutureResult > futureResultList ) {
162+ RegionLocations locations ) {
187163 if (future .isDone ()) {
188164 return true ;
189165 }
@@ -209,7 +185,7 @@ private boolean tryComplete(LocateRequest req, CompletableFuture<RegionLocations
209185 completed = loc .getRegion ().containsRow (req .row );
210186 }
211187 if (completed ) {
212- futureResultList . add ( new RegionLocationsFutureResult ( future , locations , null ) );
188+ future . complete ( locations );
213189 return true ;
214190 } else {
215191 return false ;
@@ -343,36 +319,32 @@ private void complete(TableName tableName, LocateRequest req, RegionLocations lo
343319 TableCache tableCache = getTableCache (tableName );
344320 if (locs != null ) {
345321 RegionLocations addedLocs = addToCache (tableCache , locs );
346- List <RegionLocationsFutureResult > futureResultList = new ArrayList <>();
347322 synchronized (tableCache ) {
348323 tableCache .pendingRequests .remove (req );
349- futureResultList . addAll ( tableCache .clearCompletedRequests (addedLocs ) );
324+ tableCache .clearCompletedRequests (addedLocs );
350325 // Remove a complete locate request in a synchronized block, so the table cache must have
351326 // quota to send a candidate request.
352327 toSend = tableCache .getCandidate ();
353328 toSend .ifPresent (r -> tableCache .send (r ));
354329 }
355- futureResultList .forEach (RegionLocationsFutureResult ::complete );
356330 toSend .ifPresent (r -> locateInMeta (tableName , r ));
357331 } else {
358332 // we meet an error
359333 assert error != null ;
360- List <RegionLocationsFutureResult > futureResultList = new ArrayList <>();
361334 synchronized (tableCache ) {
362335 tableCache .pendingRequests .remove (req );
363336 // fail the request itself, no matter whether it is a DoNotRetryIOException, as we have
364337 // already retried several times
365- CompletableFuture <RegionLocations > future = tableCache .allRequests .remove (req );
338+ CompletableFuture <? > future = tableCache .allRequests .remove (req );
366339 if (future != null ) {
367- futureResultList . add ( new RegionLocationsFutureResult ( future , null , error ) );
340+ future . completeExceptionally ( error );
368341 }
369- futureResultList . addAll ( tableCache .clearCompletedRequests (null ) );
342+ tableCache .clearCompletedRequests (null );
370343 // Remove a complete locate request in a synchronized block, so the table cache must have
371344 // quota to send a candidate request.
372345 toSend = tableCache .getCandidate ();
373346 toSend .ifPresent (r -> tableCache .send (r ));
374347 }
375- futureResultList .forEach (RegionLocationsFutureResult ::complete );
376348 toSend .ifPresent (r -> locateInMeta (tableName , r ));
377349 }
378350 }
@@ -570,11 +542,9 @@ public void onNext(Result[] results, ScanController controller) {
570542 continue ;
571543 }
572544 RegionLocations addedLocs = addToCache (tableCache , locs );
573- List <RegionLocationsFutureResult > futureResultList = new ArrayList <>();
574545 synchronized (tableCache ) {
575- futureResultList . addAll ( tableCache .clearCompletedRequests (addedLocs ) );
546+ tableCache .clearCompletedRequests (addedLocs );
576547 }
577- futureResultList .forEach (RegionLocationsFutureResult ::complete );
578548 }
579549 }
580550 }
@@ -706,16 +676,12 @@ void clearCache(TableName tableName) {
706676 if (tableCache == null ) {
707677 return ;
708678 }
709- List <RegionLocationsFutureResult > futureResultList = new ArrayList <>();
710679 synchronized (tableCache ) {
711680 if (!tableCache .allRequests .isEmpty ()) {
712681 IOException error = new IOException ("Cache cleared" );
713- tableCache .allRequests .values ().forEach (f -> {
714- futureResultList .add (new RegionLocationsFutureResult (f , null , error ));
715- });
682+ tableCache .allRequests .values ().forEach (f -> f .completeExceptionally (error ));
716683 }
717684 }
718- futureResultList .forEach (RegionLocationsFutureResult ::complete );
719685 conn .getConnectionMetrics ()
720686 .ifPresent (metrics -> metrics .incrMetaCacheNumClearRegion (tableCache .cache .size ()));
721687 }
0 commit comments