1+ /**
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+ package org .apache .hadoop .yarn .server .resourcemanager .federation ;
19+
20+ import org .apache .hadoop .yarn .exceptions .YarnException ;
21+ import org .apache .hadoop .yarn .server .federation .store .FederationStateStore ;
22+ import org .apache .hadoop .yarn .util .Clock ;
23+ import org .slf4j .Logger ;
24+ import org .slf4j .LoggerFactory ;
25+
26+ import java .lang .reflect .Method ;
27+ import java .util .Arrays ;
28+
29+ /**
30+ * Class to define client method,params and arguments.
31+ */
32+ public class FederationClientMethod <R > {
33+
34+ public static final Logger LOG =
35+ LoggerFactory .getLogger (FederationClientMethod .class );
36+
37+ /**
38+ * List of parameters: static and dynamic values, matchings types.
39+ */
40+ private final Object [] params ;
41+
42+ /**
43+ * List of method parameters types, matches parameters.
44+ */
45+ private final Class <?>[] types ;
46+
47+ /**
48+ * String name of the method.
49+ */
50+ private final String methodName ;
51+
52+ private FederationStateStore stateStoreClient = null ;
53+
54+ private Clock clock = null ;
55+
56+ private Class <R > clazz ;
57+
58+ public FederationClientMethod (String method , Class <?>[] pTypes , Object ... pParams )
59+ throws YarnException {
60+ if (pParams .length != pTypes .length ) {
61+ throw new YarnException ("Invalid parameters for method " + method );
62+ }
63+
64+ this .params = pParams ;
65+ this .types = Arrays .copyOf (pTypes , pTypes .length );
66+ this .methodName = method ;
67+ }
68+
69+ public FederationClientMethod (String method , Class pTypes , Object pParams )
70+ throws YarnException {
71+ this (method , new Class []{pTypes }, new Object []{pParams });
72+ }
73+
74+ public FederationClientMethod (String method , Class pTypes , Object pParams , Class <R > rTypes ,
75+ FederationStateStore fedStateStore , Clock fedClock ) throws YarnException {
76+ this (method , pTypes , pParams );
77+ this .stateStoreClient = fedStateStore ;
78+ this .clock = fedClock ;
79+ this .clazz = rTypes ;
80+ }
81+
82+ public Object [] getParams () {
83+ return Arrays .copyOf (this .params , this .params .length );
84+ }
85+
86+ public String getMethodName () {
87+ return methodName ;
88+ }
89+
90+ /**
91+ * Get the calling types for this method.
92+ *
93+ * @return An array of calling types.
94+ */
95+ public Class <?>[] getTypes () {
96+ return Arrays .copyOf (this .types , this .types .length );
97+ }
98+
99+ /**
100+ * We will use the invoke method to call the method in FederationStateStoreService.
101+ *
102+ * @return The result returned after calling the interface.
103+ * @throws YarnException yarn exception.
104+ */
105+ protected R invoke () throws YarnException {
106+ try {
107+ long startTime = clock .getTime ();
108+ Method method = FederationStateStore .class .getMethod (methodName , types );
109+ R result = clazz .cast (method .invoke (stateStoreClient , params ));
110+
111+ long stopTime = clock .getTime ();
112+ FederationStateStoreServiceMetrics .succeededStateStoreServiceCall (
113+ methodName , stopTime - startTime );
114+ return result ;
115+ } catch (Exception e ) {
116+ LOG .error ("stateStoreClient call method {} error." , methodName , e );
117+ FederationStateStoreServiceMetrics .failedStateStoreServiceCall (methodName );
118+ throw new YarnException (e );
119+ }
120+ }
121+ }
0 commit comments