|
41 | 41 | import java.util.function.Supplier;
|
42 | 42 |
|
43 | 43 | import org.apache.accumulo.core.rpc.ThriftUtil;
|
| 44 | +import org.apache.accumulo.core.rpc.clients.ThriftClientTypes; |
44 | 45 | import org.apache.accumulo.core.util.Pair;
|
45 | 46 | import org.apache.accumulo.core.util.threads.Threads;
|
46 | 47 | import org.apache.thrift.TConfiguration;
|
|
49 | 50 | import org.slf4j.Logger;
|
50 | 51 | import org.slf4j.LoggerFactory;
|
51 | 52 |
|
52 |
| -import com.google.common.annotations.VisibleForTesting; |
53 | 53 | import com.google.common.base.Preconditions;
|
54 | 54 | import com.google.common.net.HostAndPort;
|
55 | 55 |
|
@@ -109,71 +109,40 @@ static ThriftTransportPool startNew(LongSupplier maxAgeMillis) {
|
109 | 109 | return pool;
|
110 | 110 | }
|
111 | 111 |
|
112 |
| - public TTransport getTransport(HostAndPort location, long milliseconds, ClientContext context) |
113 |
| - throws TTransportException { |
114 |
| - ThriftTransportKey cacheKey = new ThriftTransportKey(location, milliseconds, context); |
| 112 | + public TTransport getTransport(ThriftClientTypes<?> type, HostAndPort location, long milliseconds, |
| 113 | + ClientContext context, boolean preferCached) throws TTransportException { |
115 | 114 |
|
116 |
| - CachedConnection connection = connectionPool.reserveAny(cacheKey); |
117 |
| - |
118 |
| - if (connection != null) { |
119 |
| - log.trace("Using existing connection to {}", cacheKey.getServer()); |
120 |
| - return connection.transport; |
121 |
| - } else { |
122 |
| - return createNewTransport(cacheKey); |
| 115 | + ThriftTransportKey cacheKey = new ThriftTransportKey(type, location, milliseconds, context); |
| 116 | + if (preferCached) { |
| 117 | + CachedConnection connection = connectionPool.reserveAny(cacheKey); |
| 118 | + if (connection != null) { |
| 119 | + log.trace("Using existing connection to {}", cacheKey.getServer()); |
| 120 | + return connection.transport; |
| 121 | + } |
123 | 122 | }
|
| 123 | + return createNewTransport(cacheKey); |
124 | 124 | }
|
125 | 125 |
|
126 |
| - @VisibleForTesting |
127 |
| - public Pair<String,TTransport> getAnyTransport(List<ThriftTransportKey> servers, |
128 |
| - boolean preferCachedConnection) throws TTransportException { |
129 |
| - |
130 |
| - servers = new ArrayList<>(servers); |
131 |
| - |
132 |
| - if (preferCachedConnection) { |
133 |
| - HashSet<ThriftTransportKey> serversSet = new HashSet<>(servers); |
134 |
| - |
135 |
| - // randomly pick a server from the connection cache |
136 |
| - serversSet.retainAll(connectionPool.getThriftTransportKeys()); |
137 |
| - |
138 |
| - if (!serversSet.isEmpty()) { |
139 |
| - ArrayList<ThriftTransportKey> cachedServers = new ArrayList<>(serversSet); |
140 |
| - Collections.shuffle(cachedServers, RANDOM.get()); |
141 |
| - |
142 |
| - for (ThriftTransportKey ttk : cachedServers) { |
143 |
| - CachedConnection connection = connectionPool.reserveAny(ttk); |
144 |
| - if (connection != null) { |
145 |
| - final String serverAddr = ttk.getServer().toString(); |
146 |
| - log.trace("Using existing connection to {}", serverAddr); |
147 |
| - return new Pair<>(serverAddr, connection.transport); |
148 |
| - } |
149 |
| - |
150 |
| - } |
| 126 | + public Pair<String,TTransport> getAnyCachedTransport(ThriftClientTypes<?> type) { |
| 127 | + final List<ThriftTransportKey> serversSet = new ArrayList<>(); |
| 128 | + for (ThriftTransportKey ttk : connectionPool.getThriftTransportKeys()) { |
| 129 | + if (ttk.getType().equals(type)) { |
| 130 | + serversSet.add(ttk); |
151 | 131 | }
|
152 | 132 | }
|
153 |
| - |
154 |
| - int retryCount = 0; |
155 |
| - while (!servers.isEmpty() && retryCount < 10) { |
156 |
| - |
157 |
| - int index = RANDOM.get().nextInt(servers.size()); |
158 |
| - ThriftTransportKey ttk = servers.get(index); |
159 |
| - |
160 |
| - if (preferCachedConnection) { |
161 |
| - CachedConnection connection = connectionPool.reserveAnyIfPresent(ttk); |
162 |
| - if (connection != null) { |
163 |
| - return new Pair<>(ttk.getServer().toString(), connection.transport); |
164 |
| - } |
165 |
| - } |
166 |
| - |
167 |
| - try { |
168 |
| - return new Pair<>(ttk.getServer().toString(), createNewTransport(ttk)); |
169 |
| - } catch (TTransportException tte) { |
170 |
| - log.debug("Failed to connect to {}", servers.get(index), tte); |
171 |
| - servers.remove(index); |
172 |
| - retryCount++; |
| 133 | + if (serversSet.isEmpty()) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + Collections.shuffle(serversSet, RANDOM.get()); |
| 137 | + for (ThriftTransportKey ttk : serversSet) { |
| 138 | + CachedConnection connection = connectionPool.reserveAny(ttk); |
| 139 | + if (connection != null) { |
| 140 | + final String serverAddr = ttk.getServer().toString(); |
| 141 | + log.trace("Using existing connection to {}", serverAddr); |
| 142 | + return new Pair<>(serverAddr, connection.transport); |
173 | 143 | }
|
174 | 144 | }
|
175 |
| - |
176 |
| - throw new TTransportException("Failed to connect to a server"); |
| 145 | + return null; |
177 | 146 | }
|
178 | 147 |
|
179 | 148 | private TTransport createNewTransport(ThriftTransportKey cacheKey) throws TTransportException {
|
@@ -389,27 +358,6 @@ CachedConnection reserveAny(final ThriftTransportKey key) {
|
389 | 358 | return executeWithinLock(key, connections::reserveAny);
|
390 | 359 | }
|
391 | 360 |
|
392 |
| - /** |
393 |
| - * Reserve and return a new {@link CachedConnection} from the {@link CachedConnections} mapped |
394 |
| - * to the specified transport key. If a {@link CachedConnections} is not found, null will be |
395 |
| - * returned. |
396 |
| - * |
397 |
| - * <p> |
398 |
| - * |
399 |
| - * This operation locks access to the mapping for the key in {@link ConnectionPool#connections} |
400 |
| - * until the operation completes. |
401 |
| - * |
402 |
| - * @param key the transport key |
403 |
| - * @return the reserved {@link CachedConnection}, or null if none were available. |
404 |
| - */ |
405 |
| - CachedConnection reserveAnyIfPresent(final ThriftTransportKey key) { |
406 |
| - // It's possible that multiple locks from executeWithinLock will overlap with a single lock |
407 |
| - // inside the ConcurrentHashMap which can unnecessarily block threads. Access the |
408 |
| - // ConcurrentHashMap outside of executeWithinLock to prevent this. |
409 |
| - var connections = getCachedConnections(key); |
410 |
| - return connections == null ? null : executeWithinLock(key, connections::reserveAny); |
411 |
| - } |
412 |
| - |
413 | 361 | /**
|
414 | 362 | * Puts the specified connection into the reserved map of the {@link CachedConnections} for the
|
415 | 363 | * specified transport key. If a {@link CachedConnections} is not found, one will be created.
|
@@ -515,10 +463,6 @@ Lock getLock(final ThriftTransportKey key) {
|
515 | 463 | return lock;
|
516 | 464 | }
|
517 | 465 |
|
518 |
| - CachedConnections getCachedConnections(final ThriftTransportKey key) { |
519 |
| - return connections.get(key); |
520 |
| - } |
521 |
| - |
522 | 466 | CachedConnections getOrCreateCachedConnections(final ThriftTransportKey key) {
|
523 | 467 | return connections.computeIfAbsent(key, k -> new CachedConnections());
|
524 | 468 | }
|
|
0 commit comments