|
| 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.ratis.grpc.server; |
| 19 | + |
| 20 | +import org.apache.ratis.grpc.GrpcTlsConfig; |
| 21 | +import org.apache.ratis.grpc.GrpcUtil; |
| 22 | +import org.apache.ratis.protocol.RaftPeer; |
| 23 | +import org.apache.ratis.thirdparty.io.grpc.ManagedChannel; |
| 24 | +import org.apache.ratis.thirdparty.io.grpc.netty.GrpcSslContexts; |
| 25 | +import org.apache.ratis.thirdparty.io.grpc.netty.NegotiationType; |
| 26 | +import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder; |
| 27 | +import org.apache.ratis.thirdparty.io.grpc.stub.AbstractStub; |
| 28 | +import org.apache.ratis.thirdparty.io.netty.channel.ChannelOption; |
| 29 | +import org.apache.ratis.thirdparty.io.netty.channel.WriteBufferWaterMark; |
| 30 | +import org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup; |
| 31 | +import org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel; |
| 32 | +import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.List; |
| 39 | +import java.util.concurrent.Semaphore; |
| 40 | +import java.util.concurrent.ThreadLocalRandom; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.concurrent.atomic.AtomicInteger; |
| 43 | +import java.util.function.Function; |
| 44 | + |
| 45 | +final class GrpcStubPool<S extends AbstractStub<S>> { |
| 46 | + public static final Logger LOG = LoggerFactory.getLogger(GrpcStubPool.class); |
| 47 | + |
| 48 | + static final class PooledStub<S extends AbstractStub<S>> { |
| 49 | + private final ManagedChannel ch; |
| 50 | + private final S stub; |
| 51 | + private final Semaphore permits; |
| 52 | + |
| 53 | + PooledStub(ManagedChannel ch, S stub, int maxInflight) { |
| 54 | + this.ch = ch; |
| 55 | + this.stub = stub; |
| 56 | + this.permits = new Semaphore(maxInflight); |
| 57 | + } |
| 58 | + |
| 59 | + S getStub() { |
| 60 | + return stub; |
| 61 | + } |
| 62 | + |
| 63 | + void release() { |
| 64 | + permits.release(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private final List<PooledStub<S>> pool; |
| 69 | + private final AtomicInteger rr = new AtomicInteger(); |
| 70 | + private final NioEventLoopGroup elg; |
| 71 | + private final int size; |
| 72 | + |
| 73 | + GrpcStubPool(RaftPeer target, int n, Function<ManagedChannel, S> stubFactory, GrpcTlsConfig tlsConfig) { |
| 74 | + this(target, n, stubFactory, tlsConfig, Math.max(2, Runtime.getRuntime().availableProcessors() / 2), 16); |
| 75 | + } |
| 76 | + |
| 77 | + GrpcStubPool(RaftPeer target, int n, |
| 78 | + Function<ManagedChannel, S> stubFactory, GrpcTlsConfig tlsConf, |
| 79 | + int elgThreads, int maxInflightPerConn) { |
| 80 | + this.elg = new NioEventLoopGroup(elgThreads); |
| 81 | + ArrayList<PooledStub<S>> tmp = new ArrayList<>(n); |
| 82 | + for (int i = 0; i < n; i++) { |
| 83 | + NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(target.getAddress()) |
| 84 | + .eventLoopGroup(elg) |
| 85 | + .channelType(NioSocketChannel.class) |
| 86 | + .keepAliveTime(30, TimeUnit.SECONDS) |
| 87 | + .keepAliveWithoutCalls(true) |
| 88 | + .idleTimeout(24, TimeUnit.HOURS) |
| 89 | + .withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(64 << 10, 128 << 10)); |
| 90 | + if (tlsConf != null) { |
| 91 | + LOG.debug("Setting TLS for {}", target.getAddress()); |
| 92 | + SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient(); |
| 93 | + GrpcUtil.setTrustManager(sslContextBuilder, tlsConf.getTrustManager()); |
| 94 | + if (tlsConf.getMtlsEnabled()) { |
| 95 | + GrpcUtil.setKeyManager(sslContextBuilder, tlsConf.getKeyManager()); |
| 96 | + } |
| 97 | + try { |
| 98 | + channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build()); |
| 99 | + } catch (Exception ex) { |
| 100 | + throw new RuntimeException(ex); |
| 101 | + } |
| 102 | + } else { |
| 103 | + channelBuilder.negotiationType(NegotiationType.PLAINTEXT); |
| 104 | + } |
| 105 | + ManagedChannel ch = channelBuilder.build(); |
| 106 | + tmp.add(new PooledStub<>(ch, stubFactory.apply(ch), maxInflightPerConn)); |
| 107 | + ch.getState(true); |
| 108 | + } |
| 109 | + this.pool = Collections.unmodifiableList(tmp); |
| 110 | + this.size = n; |
| 111 | + } |
| 112 | + |
| 113 | + PooledStub<S> acquire() throws InterruptedException { |
| 114 | + final int start = ThreadLocalRandom.current().nextInt(size); |
| 115 | + for (int k = 0; k < size; k++) { |
| 116 | + PooledStub<S> p = pool.get((start + k) % size); |
| 117 | + if (p.permits.tryAcquire()) { |
| 118 | + return p; |
| 119 | + } |
| 120 | + } |
| 121 | + final PooledStub<S> p = pool.get(start); |
| 122 | + p.permits.acquire(); |
| 123 | + return p; |
| 124 | + } |
| 125 | + |
| 126 | + public void close() { |
| 127 | + for (PooledStub p : pool) { |
| 128 | + p.ch.shutdown(); |
| 129 | + } |
| 130 | + elg.shutdownGracefully(); |
| 131 | + } |
| 132 | +} |
0 commit comments