58
58
import java .util .concurrent .Callable ;
59
59
import java .util .concurrent .CompletableFuture ;
60
60
61
- import static org .apache .hadoop .hdfs .server .federation .fairness .RouterRpcFairnessConstants .CONCURRENT_NS ;
62
61
import static org .apache .hadoop .hdfs .server .federation .router .async .utils .Async .warpCompletionException ;
63
62
import static org .apache .hadoop .hdfs .server .federation .router .async .utils .AsyncUtil .asyncApply ;
64
63
import static org .apache .hadoop .hdfs .server .federation .router .async .utils .AsyncUtil .asyncApplyUseExecutor ;
@@ -178,8 +177,14 @@ public Object invokeMethod(
178
177
namenodes .toString (), params );
179
178
}
180
179
threadLocalContext .transfer ();
180
+ RouterRpcFairnessPolicyController controller = getRouterRpcFairnessPolicyController ();
181
+ acquirePermit (nsid , ugi , method .getName (), controller );
181
182
invokeMethodAsync (ugi , (List <FederationNamenodeContext >) namenodes ,
182
183
useObserver , protocol , method , params );
184
+ asyncFinally (object -> {
185
+ releasePermit (nsid , ugi , method , controller );
186
+ return object ;
187
+ });
183
188
}, router .getRpcServer ().getAsyncRouterHandlerExecutors ().getOrDefault (nsid ,
184
189
router .getRpcServer ().getRouterAsyncHandlerDefaultExecutor ()));
185
190
return null ;
@@ -227,7 +232,7 @@ private void invokeMethodAsync(
227
232
connection [0 ] = getConnection (ugi , nsId , rpcAddress , protocol );
228
233
NameNodeProxiesClient .ProxyAndInfo <?> client = connection [0 ].getClient ();
229
234
invoke (namenode , status .isShouldUseObserver (), 0 , method ,
230
- client .getProxy (), params );
235
+ client .getProxy (), params );
231
236
asyncApply (res -> {
232
237
status .setComplete (true );
233
238
postProcessResult (method , status , namenode , nsId , client );
@@ -363,7 +368,6 @@ public <R extends RemoteLocationContext, T> RemoteResult invokeSequential(
363
368
Class <T > expectedResultClass , Object expectedResultValue )
364
369
throws IOException {
365
370
366
- RouterRpcFairnessPolicyController controller = getRouterRpcFairnessPolicyController ();
367
371
final UserGroupInformation ugi = RouterRpcServer .getRemoteUser ();
368
372
final Method m = remoteMethod .getMethod ();
369
373
List <IOException > thrownExceptions = new ArrayList <>();
@@ -378,7 +382,6 @@ public <R extends RemoteLocationContext, T> RemoteResult invokeSequential(
378
382
boolean isObserverRead = isObserverReadEligible (ns , m );
379
383
List <? extends FederationNamenodeContext > namenodes =
380
384
getOrderedNamenodes (ns , isObserverRead );
381
- acquirePermit (ns , ugi , remoteMethod , controller );
382
385
asyncTry (() -> {
383
386
Class <?> proto = remoteMethod .getProtocol ();
384
387
Object [] params = remoteMethod .getParams (loc );
@@ -419,10 +422,6 @@ public <R extends RemoteLocationContext, T> RemoteResult invokeSequential(
419
422
}
420
423
return ret ;
421
424
}, Exception .class );
422
- asyncFinally (ret -> {
423
- releasePermit (ns , ugi , remoteMethod , controller );
424
- return ret ;
425
- });
426
425
});
427
426
asyncApply (result -> {
428
427
if (status .isComplete ()) {
@@ -498,7 +497,6 @@ public <T extends RemoteLocationContext, R> Map<T, R> invokeConcurrent(
498
497
protected <T extends RemoteLocationContext , R > List <RemoteResult <T , R >> getRemoteResults (
499
498
RemoteMethod method , long timeOutMs , RouterRpcFairnessPolicyController controller ,
500
499
List <T > orderedLocations , List <Callable <Object >> callables ) throws IOException {
501
- final UserGroupInformation ugi = RouterRpcServer .getRemoteUser ();
502
500
final Method m = method .getMethod ();
503
501
final CompletableFuture <Object >[] futures =
504
502
new CompletableFuture [callables .size ()];
@@ -523,8 +521,6 @@ protected <T extends RemoteLocationContext, R> List<RemoteResult<T, R>> getRemot
523
521
LOG .error ("Unexpected error while invoking API: {}" , e .getMessage ());
524
522
throw warpCompletionException (new IOException (
525
523
"Unexpected error while invoking API " + e .getMessage (), e ));
526
- } finally {
527
- releasePermit (CONCURRENT_NS , ugi , method , controller );
528
524
}
529
525
}));
530
526
return asyncReturn (List .class );
@@ -553,8 +549,6 @@ public <T extends RemoteLocationContext, R> List<RemoteResult<T, R>> invokeSingl
553
549
boolean isObserverRead = isObserverReadEligible (ns , m );
554
550
final List <? extends FederationNamenodeContext > namenodes =
555
551
getOrderedNamenodes (ns , isObserverRead );
556
- RouterRpcFairnessPolicyController controller = getRouterRpcFairnessPolicyController ();
557
- acquirePermit (ns , ugi , method , controller );
558
552
asyncTry (() -> {
559
553
Class <?> proto = method .getProtocol ();
560
554
Object [] paramList = method .getParams (location );
@@ -567,10 +561,6 @@ public <T extends RemoteLocationContext, R> List<RemoteResult<T, R>> invokeSingl
567
561
asyncCatch ((o , ioe ) -> {
568
562
throw processException (ioe , location );
569
563
}, IOException .class );
570
- asyncFinally (o -> {
571
- releasePermit (ns , ugi , method , controller );
572
- return o ;
573
- });
574
564
return asyncReturn (List .class );
575
565
}
576
566
@@ -589,21 +579,13 @@ public <T extends RemoteLocationContext, R> List<RemoteResult<T, R>> invokeSingl
589
579
public Object invokeSingle (final String nsId , RemoteMethod method )
590
580
throws IOException {
591
581
UserGroupInformation ugi = RouterRpcServer .getRemoteUser ();
592
- RouterRpcFairnessPolicyController controller = getRouterRpcFairnessPolicyController ();
593
- acquirePermit (nsId , ugi , method , controller );
594
- asyncTry (() -> {
595
- boolean isObserverRead = isObserverReadEligible (nsId , method .getMethod ());
596
- List <? extends FederationNamenodeContext > nns = getOrderedNamenodes (nsId , isObserverRead );
597
- RemoteLocationContext loc = new RemoteLocation (nsId , "/" , "/" );
598
- Class <?> proto = method .getProtocol ();
599
- Method m = method .getMethod ();
600
- Object [] params = method .getParams (loc );
601
- invokeMethod (ugi , nns , isObserverRead , proto , m , params );
602
- });
603
- asyncFinally (o -> {
604
- releasePermit (nsId , ugi , method , controller );
605
- return o ;
606
- });
582
+ boolean isObserverRead = isObserverReadEligible (nsId , method .getMethod ());
583
+ List <? extends FederationNamenodeContext > nns = getOrderedNamenodes (nsId , isObserverRead );
584
+ RemoteLocationContext loc = new RemoteLocation (nsId , "/" , "/" );
585
+ Class <?> proto = method .getProtocol ();
586
+ Method m = method .getMethod ();
587
+ Object [] params = method .getParams (loc );
588
+ invokeMethod (ugi , nns , isObserverRead , proto , m , params );
607
589
return null ;
608
590
}
609
591
@@ -627,4 +609,21 @@ public <T> T invokeSingle(
627
609
invokeSequential (locations , remoteMethod );
628
610
return asyncReturn (clazz );
629
611
}
612
+
613
+ /**
614
+ * Release permit for specific nsId after processing against downstream
615
+ * nsId is completed.
616
+ * @param nsId Identifier of the block pool.
617
+ * @param ugi UserGroupIdentifier associated with the user.
618
+ * @param m Remote method that needs to be invoked.
619
+ * @param controller fairness policy controller to release permit from
620
+ */
621
+ protected void releasePermit (final String nsId , final UserGroupInformation ugi ,
622
+ final Method m , RouterRpcFairnessPolicyController controller ) {
623
+ if (controller != null ) {
624
+ controller .releasePermit (nsId );
625
+ LOG .trace ("Permit released for ugi: {} for method: {}" , ugi ,
626
+ m .getName ());
627
+ }
628
+ }
630
629
}
0 commit comments