]> source.dussan.org Git - sonarqube.git/blob
19fc8bbc43efa52b6bec316ba31c5f17c23c9366
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.application.cluster;
21
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;
33 import java.util.Map;
34 import java.util.Set;
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;
46
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;
53
54 public class AppNodesClusterHostsConsistencyTest {
55   @Rule
56   public ExpectedException expectedException = ExpectedException.none();
57
58   private TestAppSettings settings = new TestAppSettings();
59   private Consumer<String> logger = mock(Consumer.class);
60
61   @Before
62   public void setUp() {
63     AppNodesClusterHostsConsistency.clearInstance();
64   }
65
66   @Test
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);
72
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"));
75
76     settings.set(CLUSTER_HZ_HOSTS.getKey(), "1.1.1.1:1000,1.1.1.1:2000,1.1.1.2:1000");
77
78     TestHazelcastMember member = new TestHazelcastMember(hostsPerMember, m1);
79     AppNodesClusterHostsConsistency underTest = AppNodesClusterHostsConsistency.setInstance(member, settings, logger);
80     underTest.check();
81
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);
88   }
89
90   @Test
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);
96
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"));
99
100     settings.set(CLUSTER_HZ_HOSTS.getKey(), "1.1.1.1:1000,1.1.1.1:2000,1.1.1.2:1000");
101
102     TestHazelcastMember member = new TestHazelcastMember(hostsPerMember, m1);
103     AppNodesClusterHostsConsistency underTest = AppNodesClusterHostsConsistency.setInstance(member, settings, logger);
104     underTest.check();
105
106     verifyZeroInteractions(logger);
107   }
108
109   @Test
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));
112
113     AppNodesClusterHostsConsistency.setInstance(member, settings);
114
115     expectedException.expect(IllegalStateException.class);
116     expectedException.expectMessage("Instance is already set");
117
118     AppNodesClusterHostsConsistency.setInstance(member, settings);
119   }
120
121   @Test
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());
126
127     expectedException.expect(IllegalStateException.class);
128     expectedException.expectMessage("Instance is already set");
129
130     AppNodesClusterHostsConsistency.setInstance(member2, new TestAppSettings());
131   }
132
133   private Member newLocalHostMember(int port) throws UnknownHostException {
134     return newLocalHostMember(port, false);
135   }
136
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);
142     return member;
143   }
144
145   private class TestHazelcastMember implements HazelcastMember {
146     private final Map<Member, List<String>> hostsPerMember;
147     private final Cluster cluster = mock(Cluster.class);
148
149     private TestHazelcastMember(Map<Member, List<String>> hostsPerMember, Member localMember) {
150       this.hostsPerMember = hostsPerMember;
151       when(cluster.getLocalMember()).thenReturn(localMember);
152     }
153
154     @Override
155     public <E> IAtomicReference<E> getAtomicReference(String name) {
156       throw new IllegalStateException("not expected to be called");
157     }
158
159     @Override
160     public <K, V> Map<K, V> getReplicatedMap(String name) {
161       throw new IllegalStateException("not expected to be called");
162     }
163
164     @Override
165     public String getUuid() {
166       throw new IllegalStateException("not expected to be called");
167     }
168
169     @Override
170     public Set<String> getMemberUuids() {
171       throw new IllegalStateException("not expected to be called");
172     }
173
174     @Override
175     public Lock getLock(String name) {
176       throw new IllegalStateException("not expected to be called");
177     }
178
179     @Override
180     public long getClusterTime() {
181       throw new IllegalStateException("not expected to be called");
182     }
183
184     @Override
185     public Cluster getCluster() {
186       return cluster;
187     }
188
189     @Override
190     public <T> DistributedAnswer<T> call(DistributedCall<T> callable, MemberSelector memberSelector, long timeoutMs) {
191       throw new IllegalStateException("not expected to be called");
192     }
193
194     @Override
195     public <T> void callAsync(DistributedCall<T> callable, MemberSelector memberSelector, DistributedCallback<T> callback) {
196       callback.onComplete((Map<Member, T>) hostsPerMember);
197     }
198
199     @Override
200     public void close() {
201
202     }
203   }
204 }