3030
3131import org .apache .hadoop .conf .Configuration ;
3232import org .apache .hadoop .io .Text ;
33+ import org .apache .hadoop .ipc .protobuf .MiniRPCBenchmarkProtos .MiniDelegationTokenProto ;
34+ import org .apache .hadoop .ipc .protobuf .MiniRPCBenchmarkProtos .MiniGetDelegationTokenRequestProto ;
35+ import org .apache .hadoop .ipc .protobuf .MiniRPCBenchmarkProtos .MiniGetDelegationTokenResponseProto ;
36+ import org .apache .hadoop .ipc .protobuf .MiniRPCBenchmarkProtos .MiniProtocolService ;
3337import org .apache .hadoop .net .NetUtils ;
3438import org .apache .hadoop .security .KerberosInfo ;
3539import org .apache .hadoop .security .SecurityUtil ;
4044import org .apache .hadoop .security .token .delegation .AbstractDelegationTokenSelector ;
4145import org .apache .hadoop .security .token .delegation .TestDelegationToken .TestDelegationTokenIdentifier ;
4246import org .apache .hadoop .security .token .delegation .TestDelegationToken .TestDelegationTokenSecretManager ;
47+ import org .apache .hadoop .thirdparty .protobuf .BlockingService ;
48+ import org .apache .hadoop .thirdparty .protobuf .ByteString ;
49+ import org .apache .hadoop .thirdparty .protobuf .RpcController ;
50+ import org .apache .hadoop .thirdparty .protobuf .ServiceException ;
4351import org .apache .hadoop .util .Time ;
4452import org .slf4j .event .Level ;
4553
@@ -101,18 +109,19 @@ protected TestDelegationTokenSelector() {
101109 super (new Text ("MY KIND" ));
102110 }
103111 }
104-
105- @ KerberosInfo (
106- serverPrincipal =USER_NAME_KEY )
107- @ TokenInfo (TestDelegationTokenSelector .class )
108- public static interface MiniProtocol extends VersionedProtocol {
109- public static final long versionID = 1L ;
110112
111- /**
112- * Get a Delegation Token.
113- */
114- public Token <TestDelegationTokenIdentifier > getDelegationToken (Text renewer )
115- throws IOException ;
113+ /**
114+ * Protobuf based MiniProtocol used by {@link MiniRPCBenchmark}.
115+ * Replaces the legacy Writable based protocol now that
116+ * {@code WritableRpcEngine} has been removed.
117+ */
118+ @ KerberosInfo (serverPrincipal = USER_NAME_KEY )
119+ @ TokenInfo (TestDelegationTokenSelector .class )
120+ @ ProtocolInfo (
121+ protocolName = "org.apache.hadoop.ipc.MiniRPCBenchmark$MiniProtocol" ,
122+ protocolVersion = 1 )
123+ public interface MiniProtocol extends MiniProtocolService .BlockingInterface {
124+ long versionID = 1L ;
116125 }
117126
118127 /**
@@ -125,34 +134,32 @@ static class MiniServer implements MiniProtocol {
125134 private TestDelegationTokenSecretManager secretManager ;
126135 private Server rpcServer ;
127136
128- @ Override // VersionedProtocol
129- public long getProtocolVersion (String protocol ,
130- long clientVersion ) throws IOException {
131- if (protocol .equals (MiniProtocol .class .getName ()))
132- return versionID ;
133- throw new IOException ("Unknown protocol: " + protocol );
134- }
135-
136- @ Override // VersionedProtocol
137- public ProtocolSignature getProtocolSignature (String protocol ,
138- long clientVersion ,
139- int clientMethodsHashCode ) throws IOException {
140- if (protocol .equals (MiniProtocol .class .getName ()))
141- return new ProtocolSignature (versionID , null );
142- throw new IOException ("Unknown protocol: " + protocol );
143- }
144-
145137 @ Override // MiniProtocol
146- public Token <TestDelegationTokenIdentifier > getDelegationToken (Text renewer )
147- throws IOException {
148- String owner = UserGroupInformation .getCurrentUser ().getUserName ();
149- String realUser =
150- UserGroupInformation .getCurrentUser ().getRealUser () == null ? "" :
151- UserGroupInformation .getCurrentUser ().getRealUser ().getUserName ();
152- TestDelegationTokenIdentifier tokenId =
153- new TestDelegationTokenIdentifier (
154- new Text (owner ), renewer , new Text (realUser ));
155- return new Token <TestDelegationTokenIdentifier >(tokenId , secretManager );
138+ public MiniGetDelegationTokenResponseProto getDelegationToken (
139+ RpcController controller ,
140+ MiniGetDelegationTokenRequestProto request ) throws ServiceException {
141+ try {
142+ Text renewer = new Text (request .getRenewer ());
143+ String owner = UserGroupInformation .getCurrentUser ().getUserName ();
144+ String realUser =
145+ UserGroupInformation .getCurrentUser ().getRealUser () == null ? "" :
146+ UserGroupInformation .getCurrentUser ().getRealUser ().getUserName ();
147+ TestDelegationTokenIdentifier tokenId =
148+ new TestDelegationTokenIdentifier (
149+ new Text (owner ), renewer , new Text (realUser ));
150+ Token <TestDelegationTokenIdentifier > token =
151+ new Token <>(tokenId , secretManager );
152+ return MiniGetDelegationTokenResponseProto .newBuilder ()
153+ .setToken (MiniDelegationTokenProto .newBuilder ()
154+ .setIdentifier (ByteString .copyFrom (token .getIdentifier ()))
155+ .setPassword (ByteString .copyFrom (token .getPassword ()))
156+ .setKind (token .getKind ().toString ())
157+ .setService (token .getService ().toString ())
158+ .build ())
159+ .build ();
160+ } catch (IOException ioe ) {
161+ throw new ServiceException (ioe );
162+ }
156163 }
157164
158165 /** Start RPC server */
@@ -164,8 +171,11 @@ public Token<TestDelegationTokenIdentifier> getDelegationToken(Text renewer)
164171 new TestDelegationTokenSecretManager (24 *60 *60 *1000 ,
165172 7 *24 *60 *60 *1000 ,24 *60 *60 *1000 ,3600000 );
166173 secretManager .startThreads ();
174+ RPC .setProtocolEngine (conf , MiniProtocol .class , ProtobufRpcEngine2 .class );
175+ BlockingService service =
176+ MiniProtocolService .newReflectiveBlockingService (this );
167177 rpcServer = new RPC .Builder (conf ).setProtocol (MiniProtocol .class )
168- .setInstance (this ).setBindAddress (DEFAULT_SERVER_ADDRESS ).setPort (0 )
178+ .setInstance (service ).setBindAddress (DEFAULT_SERVER_ADDRESS ).setPort (0 )
169179 .setNumHandlers (1 ).setVerbose (false ).setSecretManager (secretManager )
170180 .build ();
171181 rpcServer .start ();
@@ -189,8 +199,8 @@ long connectToServer(Configuration conf, InetSocketAddress addr)
189199 MiniProtocol client = null ;
190200 try {
191201 long start = Time .now ();
192- client = RPC .getProxy ( MiniProtocol .class ,
193- MiniProtocol .versionID , addr , conf );
202+ RPC .setProtocolEngine ( conf , MiniProtocol .class , ProtobufRpcEngine2 . class );
203+ client = RPC . getProxy ( MiniProtocol . class , MiniProtocol .versionID , addr , conf );
194204 long end = Time .now ();
195205 return end - start ;
196206 } finally {
@@ -211,14 +221,28 @@ void connectToServerAndGetDelegationToken(
211221 client = proxyUserUgi .doAs (new PrivilegedExceptionAction <MiniProtocol >() {
212222 @ Override
213223 public MiniProtocol run () throws IOException {
224+ RPC .setProtocolEngine (conf , MiniProtocol .class ,
225+ ProtobufRpcEngine2 .class );
214226 MiniProtocol p = RPC .getProxy (MiniProtocol .class ,
215227 MiniProtocol .versionID , addr , conf );
216- Token <TestDelegationTokenIdentifier > token ;
217- token = p .getDelegationToken (new Text (RENEWER ));
218- currentUgi = UserGroupInformation .createUserForTesting (MINI_USER ,
219- GROUP_NAMES );
220- SecurityUtil .setTokenService (token , addr );
221- currentUgi .addToken (token );
228+ try {
229+ MiniGetDelegationTokenResponseProto response =
230+ p .getDelegationToken (null ,
231+ MiniGetDelegationTokenRequestProto .newBuilder ()
232+ .setRenewer (RENEWER ).build ());
233+ MiniDelegationTokenProto tokenProto = response .getToken ();
234+ Token <TestDelegationTokenIdentifier > token = new Token <>(
235+ tokenProto .getIdentifier ().toByteArray (),
236+ tokenProto .getPassword ().toByteArray (),
237+ new Text (tokenProto .getKind ()),
238+ new Text (tokenProto .getService ()));
239+ currentUgi = UserGroupInformation .createUserForTesting (MINI_USER ,
240+ GROUP_NAMES );
241+ SecurityUtil .setTokenService (token , addr );
242+ currentUgi .addToken (token );
243+ } catch (ServiceException se ) {
244+ throw new IOException (se );
245+ }
222246 return p ;
223247 }
224248 });
@@ -239,6 +263,7 @@ long connectToServerUsingDelegationToken(
239263 client = currentUgi .doAs (new PrivilegedExceptionAction <MiniProtocol >() {
240264 @ Override
241265 public MiniProtocol run () throws IOException {
266+ RPC .setProtocolEngine (conf , MiniProtocol .class , ProtobufRpcEngine2 .class );
242267 return RPC .getProxy (MiniProtocol .class ,
243268 MiniProtocol .versionID , addr , conf );
244269 }
0 commit comments