diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-05-01 10:06:59 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-06-03 20:21:18 +0200 |
commit | 0067320ec65664c3cf296a8f46074deba85437a7 (patch) | |
tree | 11a07ef3cf3d36b04cd344c0992cf9b2bb73eb93 /server/sonar-main/src/main/java | |
parent | 662103a952f5d6804f43ff6cb1ee84181cad747c (diff) | |
download | sonarqube-0067320ec65664c3cf296a8f46074deba85437a7.tar.gz sonarqube-0067320ec65664c3cf296a8f46074deba85437a7.zip |
Minor fixes to main app
Diffstat (limited to 'server/sonar-main/src/main/java')
7 files changed, 31 insertions, 66 deletions
diff --git a/server/sonar-main/src/main/java/org/sonar/application/AppStateImpl.java b/server/sonar-main/src/main/java/org/sonar/application/AppStateImpl.java index eb9c8b6e125..29dd2852860 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/AppStateImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/AppStateImpl.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; -import javax.annotation.Nonnull; import org.sonar.process.NetworkUtilsImpl; import org.sonar.process.ProcessId; @@ -36,7 +35,7 @@ public class AppStateImpl implements AppState { private final AtomicBoolean webLeaderLocked = new AtomicBoolean(false); @Override - public void addListener(@Nonnull AppStateListener listener) { + public void addListener(AppStateListener listener) { this.listeners.add(listener); } diff --git a/server/sonar-main/src/main/java/org/sonar/application/NodeLifecycle.java b/server/sonar-main/src/main/java/org/sonar/application/NodeLifecycle.java index d6a8fc9cb10..e91212a17b5 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/NodeLifecycle.java +++ b/server/sonar-main/src/main/java/org/sonar/application/NodeLifecycle.java @@ -69,7 +69,7 @@ class NodeLifecycle { res.put(OPERATIONAL, toSet(STOPPING, STOPPED)); res.put(STOPPING, toSet(STOPPED)); res.put(STOPPED, toSet(STARTING)); - return res; + return Collections.unmodifiableMap(res); } private static Set<State> toSet(State... states) { diff --git a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java index 532f8eda90c..8a01020fd7d 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java @@ -51,7 +51,7 @@ public class SchedulerImpl implements Scheduler, ProcessEventListener, ProcessLi private final AppState appState; private final NodeLifecycle nodeLifecycle = new NodeLifecycle(); - private final CountDownLatch keepAlive = new CountDownLatch(1); + private final CountDownLatch awaitTermination = new CountDownLatch(1); private final AtomicBoolean firstWaitingEsLog = new AtomicBoolean(true); private final AtomicBoolean restartRequested = new AtomicBoolean(false); private final AtomicBoolean restartDisabled = new AtomicBoolean(false); @@ -63,8 +63,7 @@ public class SchedulerImpl implements Scheduler, ProcessEventListener, ProcessLi private long processWatcherDelayMs = SQProcess.DEFAULT_WATCHER_DELAY_MS; public SchedulerImpl(AppSettings settings, AppReloader appReloader, CommandFactory commandFactory, - ProcessLauncher processLauncher, - AppState appState) { + ProcessLauncher processLauncher, AppState appState) { this.settings = settings; this.appReloader = appReloader; this.commandFactory = commandFactory; @@ -203,13 +202,13 @@ public class SchedulerImpl implements Scheduler, ProcessEventListener, ProcessLi if (restarterThread != null) { restarterThread.interrupt(); } - keepAlive.countDown(); + awaitTermination.countDown(); } @Override public void awaitTermination() { try { - keepAlive.await(); + awaitTermination.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java index fa5bf20abda..efc2eafaf1d 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterAppStateImpl.java @@ -34,7 +34,6 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.locks.Lock; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -122,21 +121,7 @@ public class ClusterAppStateImpl implements ClusterAppState { @Override public boolean tryToLockWebLeader() { IAtomicReference<String> leader = hzMember.getAtomicReference(LEADER); - if (leader.get() == null) { - Lock lock = hzMember.getLock(LEADER); - lock.lock(); - try { - if (leader.get() == null) { - leader.set(hzMember.getUuid()); - return true; - } - return false; - } finally { - lock.unlock(); - } - } else { - return false; - } + return leader.compareAndSet(null, hzMember.getUuid()); } @Override @@ -147,44 +132,28 @@ public class ClusterAppStateImpl implements ClusterAppState { @Override public void registerSonarQubeVersion(String sonarqubeVersion) { IAtomicReference<String> sqVersion = hzMember.getAtomicReference(SONARQUBE_VERSION); - if (sqVersion.get() == null) { - Lock lock = hzMember.getLock(SONARQUBE_VERSION); - lock.lock(); - try { - if (sqVersion.get() == null) { - sqVersion.set(sonarqubeVersion); - } - } finally { - lock.unlock(); - } - } + boolean wasSet = sqVersion.compareAndSet(null, sonarqubeVersion); - String clusterVersion = sqVersion.get(); - if (!sqVersion.get().equals(sonarqubeVersion)) { - throw new IllegalStateException( - format("The local version %s is not the same as the cluster %s", sonarqubeVersion, clusterVersion)); + if (!wasSet) { + String clusterVersion = sqVersion.get(); + if (!sqVersion.get().equals(sonarqubeVersion)) { + throw new IllegalStateException( + format("The local version %s is not the same as the cluster %s", sonarqubeVersion, clusterVersion)); + } } } @Override public void registerClusterName(String clusterName) { IAtomicReference<String> property = hzMember.getAtomicReference(CLUSTER_NAME); - if (property.get() == null) { - Lock lock = hzMember.getLock(CLUSTER_NAME); - lock.lock(); - try { - if (property.get() == null) { - property.set(clusterName); - } - } finally { - lock.unlock(); - } - } + boolean wasSet = property.compareAndSet(null, clusterName); - String clusterValue = property.get(); - if (!property.get().equals(clusterName)) { - throw new MessageException( - format("This node has a cluster name [%s], which does not match [%s] from the cluster", clusterName, clusterValue)); + if (!wasSet) { + String clusterValue = property.get(); + if (!property.get().equals(clusterName)) { + throw new MessageException( + format("This node has a cluster name [%s], which does not match [%s] from the cluster", clusterName, clusterValue)); + } } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java index 06de587c28c..9c2bff55f8c 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java +++ b/server/sonar-main/src/main/java/org/sonar/application/cluster/ClusterProcess.java @@ -20,6 +20,7 @@ package org.sonar.application.cluster; import java.io.Serializable; +import java.util.Objects; import org.sonar.process.ProcessId; import static java.util.Objects.requireNonNull; @@ -58,8 +59,6 @@ public class ClusterProcess implements Serializable { @Override public int hashCode() { - int result = processId.hashCode(); - result = 31 * result + nodeUuid.hashCode(); - return result; + return Objects.hash(processId, nodeUuid); } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/Lifecycle.java b/server/sonar-main/src/main/java/org/sonar/application/process/Lifecycle.java index 216983464b8..45854b8ef00 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/Lifecycle.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/Lifecycle.java @@ -66,7 +66,7 @@ public class Lifecycle { res.put(STARTED, toSet(STOPPING, STOPPED)); res.put(STOPPING, toSet(STOPPED)); res.put(STOPPED, toSet()); - return res; + return Collections.unmodifiableMap(res); } private static Set<State> toSet(State... states) { diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/StopRequestWatcherImpl.java b/server/sonar-main/src/main/java/org/sonar/application/process/StopRequestWatcherImpl.java index a7cc16e40d8..22dc51718a2 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/StopRequestWatcherImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/StopRequestWatcherImpl.java @@ -20,7 +20,6 @@ package org.sonar.application.process; import org.sonar.application.FileSystem; -import org.sonar.application.Scheduler; import org.sonar.application.config.AppSettings; import org.sonar.process.ProcessId; import org.sonar.process.sharedmemoryfile.DefaultProcessCommands; @@ -33,24 +32,24 @@ public class StopRequestWatcherImpl extends Thread implements StopRequestWatcher private static final long DEFAULT_WATCHER_DELAY_MS = 500L; private final ProcessCommands commands; - private final Scheduler scheduler; + private final Runnable listener; private final AppSettings settings; private long delayMs = DEFAULT_WATCHER_DELAY_MS; - StopRequestWatcherImpl(AppSettings settings, Scheduler scheduler, ProcessCommands commands) { + StopRequestWatcherImpl(AppSettings settings, Runnable listener, ProcessCommands commands) { super("StopRequestWatcherImpl"); this.settings = settings; this.commands = commands; - this.scheduler = scheduler; + this.listener = listener; // safeguard, do not block the JVM if thread is not interrupted // (method stopWatching() never called). setDaemon(true); } - public static StopRequestWatcherImpl create(AppSettings settings, Scheduler scheduler, FileSystem fs) { + public static StopRequestWatcherImpl create(AppSettings settings, Runnable listener, FileSystem fs) { DefaultProcessCommands commands = DefaultProcessCommands.secondary(fs.getTempDir(), ProcessId.APP.getIpcIndex()); - return new StopRequestWatcherImpl(settings, scheduler, commands); + return new StopRequestWatcherImpl(settings, listener, commands); } long getDelayMs() { @@ -66,7 +65,7 @@ public class StopRequestWatcherImpl extends Thread implements StopRequestWatcher try { while (true) { if (commands.askedForStop()) { - scheduler.terminate(); + listener.run(); return; } Thread.sleep(delayMs); @@ -86,7 +85,7 @@ public class StopRequestWatcherImpl extends Thread implements StopRequestWatcher @Override public void stopWatching() { - // does nothing is not started + // does nothing if not started interrupt(); } } |