|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.engine.server.joiner; |
| 19 | + |
| 20 | +import com.hazelcast.cluster.Address; |
| 21 | +import com.hazelcast.cluster.impl.MemberImpl; |
| 22 | +import com.hazelcast.config.JoinConfig; |
| 23 | +import com.hazelcast.instance.EndpointQualifier; |
| 24 | +import com.hazelcast.instance.ProtocolType; |
| 25 | +import com.hazelcast.instance.impl.Node; |
| 26 | +import com.hazelcast.internal.config.AliasedDiscoveryConfigUtils; |
| 27 | +import com.hazelcast.internal.util.Preconditions; |
| 28 | +import com.hazelcast.internal.util.concurrent.BackoffIdleStrategy; |
| 29 | +import com.hazelcast.internal.util.concurrent.IdleStrategy; |
| 30 | +import com.hazelcast.spi.discovery.DiscoveryNode; |
| 31 | +import com.hazelcast.spi.discovery.integration.DiscoveryService; |
| 32 | +import com.hazelcast.spi.properties.ClusterProperty; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.Iterator; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static com.hazelcast.internal.config.AliasedDiscoveryConfigUtils.allUsePublicAddress; |
| 42 | +import static com.hazelcast.spi.properties.ClusterProperty.DISCOVERY_SPI_PUBLIC_IP_ENABLED; |
| 43 | + |
| 44 | +public class LiteNodeDropOutDiscoveryJoiner extends LiteNodeDropOutTcpIpJoiner { |
| 45 | + |
| 46 | + private final DiscoveryService discoveryService; |
| 47 | + private final boolean usePublicAddress; |
| 48 | + private final IdleStrategy idleStrategy; |
| 49 | + private final int maximumWaitingTimeBeforeJoinSeconds; |
| 50 | + |
| 51 | + public LiteNodeDropOutDiscoveryJoiner(Node node) { |
| 52 | + super(node); |
| 53 | + this.idleStrategy = |
| 54 | + new BackoffIdleStrategy( |
| 55 | + 0L, |
| 56 | + 0L, |
| 57 | + TimeUnit.MILLISECONDS.toNanos(10L), |
| 58 | + TimeUnit.MILLISECONDS.toNanos(500L)); |
| 59 | + this.maximumWaitingTimeBeforeJoinSeconds = |
| 60 | + node.getProperties().getInteger(ClusterProperty.WAIT_SECONDS_BEFORE_JOIN); |
| 61 | + this.discoveryService = node.discoveryService; |
| 62 | + this.usePublicAddress = usePublicAddress(node.getConfig().getNetworkConfig().getJoin()); |
| 63 | + } |
| 64 | + |
| 65 | + private boolean usePublicAddress(JoinConfig join) { |
| 66 | + return node.getProperties().getBoolean(DISCOVERY_SPI_PUBLIC_IP_ENABLED) |
| 67 | + || allUsePublicAddress( |
| 68 | + AliasedDiscoveryConfigUtils.aliasedDiscoveryConfigsFrom(join)); |
| 69 | + } |
| 70 | + |
| 71 | + protected Collection<Address> getPossibleAddressesForInitialJoin() { |
| 72 | + long deadLine = |
| 73 | + System.nanoTime() |
| 74 | + + TimeUnit.SECONDS.toNanos((long) this.maximumWaitingTimeBeforeJoinSeconds); |
| 75 | + |
| 76 | + for (int i = 0; System.nanoTime() < deadLine; ++i) { |
| 77 | + Collection<Address> possibleAddresses = this.getPossibleAddresses(); |
| 78 | + if (!possibleAddresses.isEmpty()) { |
| 79 | + return possibleAddresses; |
| 80 | + } |
| 81 | + |
| 82 | + this.idleStrategy.idle((long) i); |
| 83 | + } |
| 84 | + |
| 85 | + return Collections.emptyList(); |
| 86 | + } |
| 87 | + |
| 88 | + protected Collection<Address> getPossibleAddresses() { |
| 89 | + Iterable<DiscoveryNode> discoveredNodes = |
| 90 | + (Iterable) |
| 91 | + Preconditions.checkNotNull( |
| 92 | + this.discoveryService.discoverNodes(), |
| 93 | + "Discovered nodes cannot be null!"); |
| 94 | + MemberImpl localMember = this.node.nodeEngine.getLocalMember(); |
| 95 | + Set<Address> localAddresses = this.node.getLocalAddressRegistry().getLocalAddresses(); |
| 96 | + Collection<Address> possibleMembers = new ArrayList(); |
| 97 | + Iterator var5 = discoveredNodes.iterator(); |
| 98 | + |
| 99 | + while (var5.hasNext()) { |
| 100 | + DiscoveryNode discoveryNode = (DiscoveryNode) var5.next(); |
| 101 | + Address discoveredAddress = |
| 102 | + this.usePublicAddress |
| 103 | + ? discoveryNode.getPublicAddress() |
| 104 | + : discoveryNode.getPrivateAddress(); |
| 105 | + if (localAddresses.contains(discoveredAddress)) { |
| 106 | + if (!this.usePublicAddress && discoveryNode.getPublicAddress() != null) { |
| 107 | + localMember |
| 108 | + .getAddressMap() |
| 109 | + .put( |
| 110 | + EndpointQualifier.resolve(ProtocolType.CLIENT, "public"), |
| 111 | + this.publicAddress(localMember, discoveryNode)); |
| 112 | + } |
| 113 | + } else { |
| 114 | + possibleMembers.add(discoveredAddress); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return possibleMembers; |
| 119 | + } |
| 120 | + |
| 121 | + private Address publicAddress(MemberImpl localMember, DiscoveryNode discoveryNode) { |
| 122 | + if (localMember.getAddressMap().containsKey(EndpointQualifier.CLIENT)) { |
| 123 | + try { |
| 124 | + String publicHost = discoveryNode.getPublicAddress().getHost(); |
| 125 | + int clientPort = |
| 126 | + ((Address) localMember.getAddressMap().get(EndpointQualifier.CLIENT)) |
| 127 | + .getPort(); |
| 128 | + return new Address(publicHost, clientPort); |
| 129 | + } catch (Exception var5) { |
| 130 | + Exception e = var5; |
| 131 | + this.logger.fine(e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return discoveryNode.getPublicAddress(); |
| 136 | + } |
| 137 | +} |
0 commit comments