|
| 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.thirdparty.io.grpc.ManagedChannel; |
| 21 | +import org.apache.ratis.thirdparty.io.grpc.netty.NegotiationType; |
| 22 | +import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder; |
| 23 | +import org.apache.ratis.thirdparty.io.grpc.stub.AbstractStub; |
| 24 | +import org.apache.ratis.thirdparty.io.netty.channel.ChannelOption; |
| 25 | +import org.apache.ratis.thirdparty.io.netty.channel.WriteBufferWaterMark; |
| 26 | +import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContext; |
| 27 | +import org.apache.ratis.util.MemoizedSupplier; |
| 28 | +import org.apache.ratis.util.Preconditions; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.Semaphore; |
| 36 | +import java.util.concurrent.ThreadLocalRandom; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.function.Function; |
| 39 | + |
| 40 | +final class GrpcStubPool<S extends AbstractStub<S>> { |
| 41 | + public static final Logger LOG = LoggerFactory.getLogger(GrpcStubPool.class); |
| 42 | + |
| 43 | + static ManagedChannel buildManagedChannel(String address, SslContext sslContext) { |
| 44 | + NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(address) |
| 45 | + .keepAliveTime(10, TimeUnit.MINUTES) |
| 46 | + .keepAliveWithoutCalls(false) |
| 47 | + .idleTimeout(30, TimeUnit.MINUTES) |
| 48 | + .withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(64 << 10, 128 << 10)); |
| 49 | + if (sslContext != null) { |
| 50 | + LOG.debug("Setting TLS for {}", address); |
| 51 | + channelBuilder.useTransportSecurity().sslContext(sslContext); |
| 52 | + } else { |
| 53 | + channelBuilder.negotiationType(NegotiationType.PLAINTEXT); |
| 54 | + } |
| 55 | + ManagedChannel ch = channelBuilder.build(); |
| 56 | + ch.getState(true); |
| 57 | + return ch; |
| 58 | + } |
| 59 | + |
| 60 | + static final class Stub<S extends AbstractStub<S>> { |
| 61 | + private final ManagedChannel ch; |
| 62 | + private final S stub; |
| 63 | + private final Semaphore permits; |
| 64 | + |
| 65 | + Stub(String address, SslContext sslContext, Function<ManagedChannel, S> stubFactory, int maxInflight) { |
| 66 | + this.ch = buildManagedChannel(address, sslContext); |
| 67 | + this.stub = stubFactory.apply(ch); |
| 68 | + this.permits = new Semaphore(maxInflight); |
| 69 | + } |
| 70 | + |
| 71 | + S getStub() { |
| 72 | + return stub; |
| 73 | + } |
| 74 | + |
| 75 | + void release() { |
| 76 | + permits.release(); |
| 77 | + } |
| 78 | + |
| 79 | + void shutdown() { |
| 80 | + ch.shutdown(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private final List<MemoizedSupplier<Stub<S>>> pool; |
| 85 | + |
| 86 | + GrpcStubPool(int connections, String address, SslContext sslContext, Function<ManagedChannel, S> stubFactory, |
| 87 | + int maxInflightPerConn) { |
| 88 | + Preconditions.assertTrue(connections > 1, "connections must be > 1"); |
| 89 | + final List<MemoizedSupplier<Stub<S>>> tmpPool = new ArrayList<>(connections); |
| 90 | + for (int i = 0; i < connections; i++) { |
| 91 | + tmpPool.add(MemoizedSupplier.valueOf(() -> new Stub<>(address, sslContext, stubFactory, maxInflightPerConn))); |
| 92 | + } |
| 93 | + this.pool = Collections.unmodifiableList(tmpPool); |
| 94 | + } |
| 95 | + |
| 96 | + Stub<S> getStub(int i) { |
| 97 | + return pool.get(i).get(); |
| 98 | + } |
| 99 | + |
| 100 | + Stub<S> acquire() throws InterruptedException { |
| 101 | + final int size = pool.size(); |
| 102 | + final int start = ThreadLocalRandom.current().nextInt(size); |
| 103 | + for (int k = 0; k < size; k++) { |
| 104 | + Stub<S> p = getStub((start + k) % size); |
| 105 | + if (p.permits.tryAcquire()) { |
| 106 | + return p; |
| 107 | + } |
| 108 | + } |
| 109 | + final Stub<S> p = getStub(start); |
| 110 | + p.permits.acquire(); |
| 111 | + return p; |
| 112 | + } |
| 113 | + |
| 114 | + public void close() { |
| 115 | + for (MemoizedSupplier<Stub<S>> p : pool) { |
| 116 | + if (p.isInitialized()) { |
| 117 | + p.get().shutdown(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments