|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.transport; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.LogManager; |
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 25 | +import org.elasticsearch.Version; |
| 26 | +import org.elasticsearch.action.ActionListener; |
| 27 | +import org.elasticsearch.cluster.ClusterName; |
| 28 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 29 | +import org.elasticsearch.common.transport.TransportAddress; |
| 30 | +import org.elasticsearch.common.util.concurrent.CountDown; |
| 31 | + |
| 32 | +import java.util.Iterator; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.atomic.AtomicInteger; |
| 35 | +import java.util.concurrent.atomic.AtomicLong; |
| 36 | +import java.util.concurrent.atomic.AtomicReference; |
| 37 | +import java.util.function.Supplier; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +public class SimpleConnectionStrategy extends RemoteConnectionStrategy { |
| 41 | + |
| 42 | + private static final int MAX_CONNECT_ATTEMPTS_PER_RUN = 3; |
| 43 | + private static final Logger logger = LogManager.getLogger(SimpleConnectionStrategy.class); |
| 44 | + |
| 45 | + private final int maxNumRemoteConnections; |
| 46 | + private final AtomicLong counter = new AtomicLong(0); |
| 47 | + private final List<Supplier<TransportAddress>> addresses; |
| 48 | + private final AtomicReference<ClusterName> remoteClusterName = new AtomicReference<>(); |
| 49 | + private final ConnectionProfile profile; |
| 50 | + private final ConnectionManager.ConnectionValidator clusterNameValidator; |
| 51 | + |
| 52 | + SimpleConnectionStrategy(String clusterAlias, TransportService transportService, RemoteConnectionManager connectionManager, |
| 53 | + int maxNumRemoteConnections, List<Supplier<TransportAddress>> addresses) { |
| 54 | + super(clusterAlias, transportService, connectionManager); |
| 55 | + this.maxNumRemoteConnections = maxNumRemoteConnections; |
| 56 | + assert addresses.isEmpty() == false : "Cannot use simple connection strategy with no configured addresses"; |
| 57 | + this.addresses = addresses; |
| 58 | + // TODO: Move into the ConnectionManager |
| 59 | + this.profile = new ConnectionProfile.Builder() |
| 60 | + .addConnections(1, TransportRequestOptions.Type.REG, TransportRequestOptions.Type.PING) |
| 61 | + .addConnections(0, TransportRequestOptions.Type.BULK, TransportRequestOptions.Type.STATE, TransportRequestOptions.Type.RECOVERY) |
| 62 | + .build(); |
| 63 | + this.clusterNameValidator = (newConnection, actualProfile, listener) -> |
| 64 | + transportService.handshake(newConnection, actualProfile.getHandshakeTimeout().millis(), cn -> true, |
| 65 | + ActionListener.map(listener, resp -> { |
| 66 | + ClusterName remote = resp.getClusterName(); |
| 67 | + if (remoteClusterName.compareAndSet(null, remote)) { |
| 68 | + return null; |
| 69 | + } else { |
| 70 | + if (remoteClusterName.get().equals(remote) == false) { |
| 71 | + DiscoveryNode node = newConnection.getNode(); |
| 72 | + throw new ConnectTransportException(node, "handshake failed. unexpected remote cluster name " + remote); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + })); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected boolean shouldOpenMoreConnections() { |
| 81 | + return connectionManager.size() < maxNumRemoteConnections; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void connectImpl(ActionListener<Void> listener) { |
| 86 | + performSimpleConnectionProcess(addresses.iterator(), listener); |
| 87 | + } |
| 88 | + |
| 89 | + private void performSimpleConnectionProcess(Iterator<Supplier<TransportAddress>> addressIter, ActionListener<Void> listener) { |
| 90 | + openConnections(listener, 1); |
| 91 | + } |
| 92 | + |
| 93 | + private void openConnections(ActionListener<Void> finished, int attemptNumber) { |
| 94 | + if (attemptNumber <= MAX_CONNECT_ATTEMPTS_PER_RUN) { |
| 95 | + List<TransportAddress> resolved = addresses.stream().map(Supplier::get).collect(Collectors.toList()); |
| 96 | + |
| 97 | + int remaining = maxNumRemoteConnections - connectionManager.size(); |
| 98 | + ActionListener<Void> compositeListener = new ActionListener<>() { |
| 99 | + |
| 100 | + private final AtomicInteger successfulConnections = new AtomicInteger(0); |
| 101 | + private final CountDown countDown = new CountDown(remaining); |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onResponse(Void v) { |
| 105 | + successfulConnections.incrementAndGet(); |
| 106 | + if (countDown.countDown()) { |
| 107 | + if (shouldOpenMoreConnections()) { |
| 108 | + openConnections(finished, attemptNumber + 1); |
| 109 | + } else { |
| 110 | + finished.onResponse(v); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onFailure(Exception e) { |
| 117 | + if (countDown.countDown()) { |
| 118 | + openConnections(finished, attemptNumber + 1); |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + |
| 124 | + for (int i = 0; i < remaining; ++i) { |
| 125 | + TransportAddress address = nextAddress(resolved); |
| 126 | + String id = clusterAlias + "#" + address; |
| 127 | + DiscoveryNode node = new DiscoveryNode(id, address, Version.CURRENT.minimumCompatibilityVersion()); |
| 128 | + |
| 129 | + connectionManager.connectToNode(node, profile, clusterNameValidator, new ActionListener<>() { |
| 130 | + @Override |
| 131 | + public void onResponse(Void v) { |
| 132 | + compositeListener.onResponse(v); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onFailure(Exception e) { |
| 137 | + logger.debug(new ParameterizedMessage("failed to open remote connection [remote cluster: {}, address: {}]", |
| 138 | + clusterAlias, address), e); |
| 139 | + compositeListener.onFailure(e); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + } else { |
| 144 | + int openConnections = connectionManager.size(); |
| 145 | + if (openConnections == 0) { |
| 146 | + finished.onFailure(new IllegalStateException("Unable to open any simple connections to remote cluster [" + clusterAlias |
| 147 | + + "]")); |
| 148 | + } else { |
| 149 | + logger.debug("unable to open maximum number of connections [remote cluster: {}, opened: {}, maximum: {}]", clusterAlias, |
| 150 | + openConnections, maxNumRemoteConnections); |
| 151 | + finished.onResponse(null); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private TransportAddress nextAddress(List<TransportAddress> resolvedAddresses) { |
| 157 | + long curr; |
| 158 | + while ((curr = counter.getAndIncrement()) == Long.MIN_VALUE) ; |
| 159 | + return resolvedAddresses.get(Math.floorMod(curr, resolvedAddresses.size())); |
| 160 | + } |
| 161 | +} |
0 commit comments