3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.application.cluster;
22 import com.hazelcast.core.Cluster;
23 import com.hazelcast.core.IAtomicReference;
24 import com.hazelcast.core.Member;
25 import com.hazelcast.core.MemberSelector;
26 import com.hazelcast.nio.Address;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.LinkedHashMap;
32 import java.util.List;
35 import java.util.concurrent.locks.Lock;
36 import java.util.function.Consumer;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.sonar.application.config.TestAppSettings;
42 import org.sonar.process.cluster.hz.DistributedAnswer;
43 import org.sonar.process.cluster.hz.DistributedCall;
44 import org.sonar.process.cluster.hz.DistributedCallback;
45 import org.sonar.process.cluster.hz.HazelcastMember;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.verifyNoMoreInteractions;
50 import static org.mockito.Mockito.verifyZeroInteractions;
51 import static org.mockito.Mockito.when;
52 import static org.sonar.process.ProcessProperties.Property.CLUSTER_HZ_HOSTS;
54 public class AppNodesClusterHostsConsistencyTest {
56 public ExpectedException expectedException = ExpectedException.none();
58 private TestAppSettings settings = new TestAppSettings();
59 private Consumer<String> logger = mock(Consumer.class);
63 AppNodesClusterHostsConsistency.clearInstance();
67 public void log_warning_if_configured_hosts_are_not_consistent() throws UnknownHostException {
68 Map<Member, List<String>> hostsPerMember = new LinkedHashMap<>();
69 Member m1 = newLocalHostMember(1, true);
70 Member m2 = newLocalHostMember(2);
71 Member m3 = newLocalHostMember(3);
73 hostsPerMember.put(m2, Arrays.asList("1.1.1.1:1000", "1.1.1.1:2000"));
74 hostsPerMember.put(m3, Arrays.asList("1.1.1.1:1000", "1.1.1.2:1000"));
76 settings.set(CLUSTER_HZ_HOSTS.getKey(), "1.1.1.1:1000,1.1.1.1:2000,1.1.1.2:1000");
78 TestHazelcastMember member = new TestHazelcastMember(hostsPerMember, m1);
79 AppNodesClusterHostsConsistency underTest = AppNodesClusterHostsConsistency.setInstance(member, settings, logger);
82 verify(logger).accept("The configuration of the current node doesn't match the list of hosts configured in the application nodes that have already joined the cluster:\n" +
83 m1.getAddress().getHost() + ":" + m1.getAddress().getPort() + " : [1.1.1.1:1000, 1.1.1.1:2000, 1.1.1.2:1000] (current)\n" +
84 m2.getAddress().getHost() + ":" + m2.getAddress().getPort() + " : [1.1.1.1:1000, 1.1.1.1:2000]\n" +
85 m3.getAddress().getHost() + ":" + m3.getAddress().getPort() + " : [1.1.1.1:1000, 1.1.1.2:1000]\n" +
86 "Make sure the configuration is consistent among all application nodes before you restart any node");
87 verifyNoMoreInteractions(logger);
91 public void dont_log_if_configured_hosts_are_consistent() throws UnknownHostException {
92 Map<Member, List<String>> hostsPerMember = new LinkedHashMap<>();
93 Member m1 = newLocalHostMember(1, true);
94 Member m2 = newLocalHostMember(2);
95 Member m3 = newLocalHostMember(3);
97 hostsPerMember.put(m2, Arrays.asList("1.1.1.1:1000", "1.1.1.1:2000", "1.1.1.2:1000"));
98 hostsPerMember.put(m3, Arrays.asList("1.1.1.1:1000", "1.1.1.1:2000", "1.1.1.2:1000"));
100 settings.set(CLUSTER_HZ_HOSTS.getKey(), "1.1.1.1:1000,1.1.1.1:2000,1.1.1.2:1000");
102 TestHazelcastMember member = new TestHazelcastMember(hostsPerMember, m1);
103 AppNodesClusterHostsConsistency underTest = AppNodesClusterHostsConsistency.setInstance(member, settings, logger);
106 verifyZeroInteractions(logger);
110 public void setInstance_fails_with_ISE_when_called_twice_with_same_arguments() throws UnknownHostException {
111 TestHazelcastMember member = new TestHazelcastMember(Collections.emptyMap(), newLocalHostMember(1, true));
113 AppNodesClusterHostsConsistency.setInstance(member, settings);
115 expectedException.expect(IllegalStateException.class);
116 expectedException.expectMessage("Instance is already set");
118 AppNodesClusterHostsConsistency.setInstance(member, settings);
122 public void setInstance_fails_with_ISE_when_called_twice_with_other_arguments() throws UnknownHostException {
123 TestHazelcastMember member1 = new TestHazelcastMember(Collections.emptyMap(), newLocalHostMember(1, true));
124 TestHazelcastMember member2 = new TestHazelcastMember(Collections.emptyMap(), newLocalHostMember(2, true));
125 AppNodesClusterHostsConsistency.setInstance(member1, new TestAppSettings());
127 expectedException.expect(IllegalStateException.class);
128 expectedException.expectMessage("Instance is already set");
130 AppNodesClusterHostsConsistency.setInstance(member2, new TestAppSettings());
133 private Member newLocalHostMember(int port) throws UnknownHostException {
134 return newLocalHostMember(port, false);
137 private Member newLocalHostMember(int port, boolean localMember) throws UnknownHostException {
138 Member member = mock(Member.class);
139 when(member.localMember()).thenReturn(localMember);
140 Address address1 = new Address(InetAddress.getLocalHost(), port);
141 when(member.getAddress()).thenReturn(address1);
145 private class TestHazelcastMember implements HazelcastMember {
146 private final Map<Member, List<String>> hostsPerMember;
147 private final Cluster cluster = mock(Cluster.class);
149 private TestHazelcastMember(Map<Member, List<String>> hostsPerMember, Member localMember) {
150 this.hostsPerMember = hostsPerMember;
151 when(cluster.getLocalMember()).thenReturn(localMember);
155 public <E> IAtomicReference<E> getAtomicReference(String name) {
156 throw new IllegalStateException("not expected to be called");
160 public <K, V> Map<K, V> getReplicatedMap(String name) {
161 throw new IllegalStateException("not expected to be called");
165 public String getUuid() {
166 throw new IllegalStateException("not expected to be called");
170 public Set<String> getMemberUuids() {
171 throw new IllegalStateException("not expected to be called");
175 public Lock getLock(String name) {
176 throw new IllegalStateException("not expected to be called");
180 public long getClusterTime() {
181 throw new IllegalStateException("not expected to be called");
185 public Cluster getCluster() {
190 public <T> DistributedAnswer<T> call(DistributedCall<T> callable, MemberSelector memberSelector, long timeoutMs) {
191 throw new IllegalStateException("not expected to be called");
195 public <T> void callAsync(DistributedCall<T> callable, MemberSelector memberSelector, DistributedCallback<T> callback) {
196 callback.onComplete((Map<Member, T>) hostsPerMember);
200 public void close() {