3232import org .apache .ratis .server .leader .FollowerInfo ;
3333import org .apache .ratis .server .leader .LeaderState ;
3434import org .apache .ratis .thirdparty .io .netty .buffer .PooledByteBufAllocator ;
35+ import org .apache .ratis .thirdparty .io .netty .handler .ssl .SslContext ;
3536import org .apache .ratis .util .JavaUtils ;
37+ import org .apache .ratis .util .MemoizedSupplier ;
3638import org .slf4j .Logger ;
3739import org .slf4j .LoggerFactory ;
3840
41+ import java .util .function .BiFunction ;
3942import java .util .function .Consumer ;
43+ import java .util .function .Supplier ;
4044
4145public class GrpcFactory implements ServerFactory , ClientFactory {
4246
@@ -65,19 +69,32 @@ static boolean checkPooledByteBufAllocatorUseCacheForAllThreads(Consumer<String>
6569 return value ;
6670 }
6771
68- private final GrpcServices .Customizer servicesCustomizer ;
72+ static final BiFunction <GrpcTlsConfig , SslContext , SslContext > BUILD_SSL_CONTEXT_FOR_SERVER
73+ = (tlsConf , defaultContext ) -> tlsConf == null ? defaultContext : GrpcUtil .buildSslContextForServer (tlsConf );
74+
75+ static final BiFunction <GrpcTlsConfig , SslContext , SslContext > BUILD_SSL_CONTEXT_FOR_CLIENT
76+ = (tlsConf , defaultContext ) -> tlsConf == null ? defaultContext : GrpcUtil .buildSslContextForClient (tlsConf );
6977
70- private final GrpcTlsConfig tlsConfig ;
71- private final GrpcTlsConfig adminTlsConfig ;
72- private final GrpcTlsConfig clientTlsConfig ;
73- private final GrpcTlsConfig serverTlsConfig ;
78+ static final class SslContexts {
79+ private final SslContext adminSslContext ;
80+ private final SslContext clientSslContext ;
81+ private final SslContext serverSslContext ;
7482
75- public static Parameters newRaftParameters (GrpcTlsConfig conf ) {
76- final Parameters p = new Parameters ();
77- GrpcConfigKeys .TLS .setConf (p , conf );
78- return p ;
83+ private SslContexts (GrpcTlsConfig tlsConfig , GrpcTlsConfig adminTlsConfig ,
84+ GrpcTlsConfig clientTlsConfig , GrpcTlsConfig serverTlsConfig ,
85+ BiFunction <GrpcTlsConfig , SslContext , SslContext > buildMethod ) {
86+ final SslContext defaultSslContext = buildMethod .apply (tlsConfig , null );
87+ this .adminSslContext = buildMethod .apply (adminTlsConfig , defaultSslContext );
88+ this .clientSslContext = buildMethod .apply (clientTlsConfig , defaultSslContext );
89+ this .serverSslContext = buildMethod .apply (serverTlsConfig , defaultSslContext );
90+ }
7991 }
8092
93+ private final GrpcServices .Customizer servicesCustomizer ;
94+
95+ private final Supplier <SslContexts > forServerSupplier ;
96+ private final Supplier <SslContexts > forClientSupplier ;
97+
8198 public GrpcFactory (Parameters parameters ) {
8299 this (GrpcConfigKeys .Server .servicesCustomizer (parameters ),
83100 GrpcConfigKeys .TLS .conf (parameters ),
@@ -87,35 +104,15 @@ public GrpcFactory(Parameters parameters) {
87104 );
88105 }
89106
90- public GrpcFactory (GrpcTlsConfig tlsConfig ) {
91- this (null , tlsConfig , null , null , null );
92- }
93-
94107 private GrpcFactory (GrpcServices .Customizer servicesCustomizer ,
95108 GrpcTlsConfig tlsConfig , GrpcTlsConfig adminTlsConfig ,
96109 GrpcTlsConfig clientTlsConfig , GrpcTlsConfig serverTlsConfig ) {
97110 this .servicesCustomizer = servicesCustomizer ;
98111
99- this .tlsConfig = tlsConfig ;
100- this .adminTlsConfig = adminTlsConfig ;
101- this .clientTlsConfig = clientTlsConfig ;
102- this .serverTlsConfig = serverTlsConfig ;
103- }
104-
105- public GrpcTlsConfig getTlsConfig () {
106- return tlsConfig ;
107- }
108-
109- public GrpcTlsConfig getAdminTlsConfig () {
110- return adminTlsConfig != null ? adminTlsConfig : tlsConfig ;
111- }
112-
113- public GrpcTlsConfig getClientTlsConfig () {
114- return clientTlsConfig != null ? clientTlsConfig : tlsConfig ;
115- }
116-
117- public GrpcTlsConfig getServerTlsConfig () {
118- return serverTlsConfig != null ? serverTlsConfig : tlsConfig ;
112+ this .forServerSupplier = MemoizedSupplier .valueOf (() -> new SslContexts (
113+ tlsConfig , adminTlsConfig , clientTlsConfig , serverTlsConfig , BUILD_SSL_CONTEXT_FOR_SERVER ));
114+ this .forClientSupplier = MemoizedSupplier .valueOf (() -> new SslContexts (
115+ tlsConfig , adminTlsConfig , clientTlsConfig , serverTlsConfig , BUILD_SSL_CONTEXT_FOR_CLIENT ));
119116 }
120117
121118 @ Override
@@ -131,19 +128,24 @@ public LogAppender newLogAppender(RaftServer.Division server, LeaderState state,
131128 @ Override
132129 public GrpcServices newRaftServerRpc (RaftServer server ) {
133130 checkPooledByteBufAllocatorUseCacheForAllThreads (LOG ::info );
131+
132+ final SslContexts forServer = forServerSupplier .get ();
133+ final SslContexts forClient = forClientSupplier .get ();
134134 return GrpcServicesImpl .newBuilder ()
135135 .setServer (server )
136136 .setCustomizer (servicesCustomizer )
137- .setAdminTlsConfig (getAdminTlsConfig ())
138- .setServerTlsConfig (getServerTlsConfig ())
139- .setClientTlsConfig (getClientTlsConfig ())
137+ .setAdminSslContext (forServer .adminSslContext )
138+ .setServerSslContextForServer (forServer .serverSslContext )
139+ .setServerSslContextForClient (forClient .serverSslContext )
140+ .setClientSslContext (forServer .clientSslContext )
140141 .build ();
141142 }
142143
143144 @ Override
144145 public GrpcClientRpc newRaftClientRpc (ClientId clientId , RaftProperties properties ) {
145146 checkPooledByteBufAllocatorUseCacheForAllThreads (LOG ::debug );
146- return new GrpcClientRpc (clientId , properties ,
147- getAdminTlsConfig (), getClientTlsConfig ());
147+
148+ final SslContexts forClient = forClientSupplier .get ();
149+ return new GrpcClientRpc (clientId , properties , forClient .adminSslContext , forClient .clientSslContext );
148150 }
149151}
0 commit comments