Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-8798 start and monitor ES script from main process
- Loading branch information
1 parent
8e7d3d3
commit 070e44f
Showing
11 changed files
with
721 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
server/sonar-process-monitor/src/main/java/org/sonar/application/process/EsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,64 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2017 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.application.process; | |||
|
|||
import java.io.File; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import org.sonar.process.ProcessId; | |||
|
|||
public class EsCommand extends AbstractCommand<EsCommand> { | |||
private File executable; | |||
private String url; | |||
private List<String> esOptions = new ArrayList<>(); | |||
|
|||
public EsCommand(ProcessId id) { | |||
super(id); | |||
} | |||
|
|||
public File getExecutable() { | |||
return executable; | |||
} | |||
|
|||
public EsCommand setExecutable(File executable) { | |||
this.executable = executable; | |||
return this; | |||
} | |||
|
|||
public String getUrl() { | |||
return url; | |||
} | |||
|
|||
public EsCommand setUrl(String url) { | |||
this.url = url; | |||
return this; | |||
} | |||
|
|||
public List<String> getEsOptions() { | |||
return esOptions; | |||
} | |||
|
|||
public EsCommand addEsOption(String s) { | |||
if (!s.isEmpty()) { | |||
esOptions.add(s); | |||
} | |||
return this; | |||
} | |||
} |
119 changes: 119 additions & 0 deletions
119
...r/sonar-process-monitor/src/main/java/org/sonar/application/process/EsProcessMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,119 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2017 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.application.process; | |||
|
|||
import java.io.IOException; | |||
import java.net.ConnectException; | |||
import java.net.MalformedURLException; | |||
import java.net.URL; | |||
import java.net.URLConnection; | |||
import java.util.concurrent.atomic.AtomicBoolean; | |||
import org.apache.commons.io.IOUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
public class EsProcessMonitor extends AbstractProcessMonitor { | |||
private static final Logger LOG = LoggerFactory.getLogger(EsProcessMonitor.class); | |||
private static final int WAIT_FOR_UP_DELAY_IN_MILLIS = 100; | |||
private static final int WAIT_FOR_UP_TIMEOUT = 10 * 60; /* 1min */ | |||
|
|||
private final AtomicBoolean nodeUp = new AtomicBoolean(false); | |||
private final AtomicBoolean nodeOperational = new AtomicBoolean(false); | |||
private final URL healthCheckURL; | |||
|
|||
public EsProcessMonitor(Process process, String url) throws MalformedURLException { | |||
super(process); | |||
this.healthCheckURL = new URL(url + "/_cluster/health?wait_for_status=yellow&timeout=30s"); | |||
} | |||
|
|||
@Override | |||
public boolean isOperational() { | |||
if (nodeOperational.get()) { | |||
return true; | |||
} | |||
|
|||
try { | |||
boolean flag = checkOperational(); | |||
if (flag) { | |||
nodeOperational.set(true); | |||
} | |||
} catch (InterruptedException e) { | |||
LOG.trace("Interrupted while checking ES node is operational", e); | |||
Thread.currentThread().interrupt(); | |||
} | |||
return nodeOperational.get(); | |||
} | |||
|
|||
private boolean checkOperational() throws InterruptedException { | |||
int i = 0; | |||
Status status = checkStatus(); | |||
do { | |||
if (status != Status.CONNECTION_REFUSED) { | |||
nodeUp.set(true); | |||
} else { | |||
Thread.sleep(WAIT_FOR_UP_DELAY_IN_MILLIS); | |||
i++; | |||
status = checkStatus(); | |||
} | |||
} while (!nodeUp.get() && i < WAIT_FOR_UP_TIMEOUT); | |||
return status == Status.YELLOW || status == Status.GREEN; | |||
} | |||
|
|||
private Status checkStatus() { | |||
try { | |||
URLConnection urlConnection = healthCheckURL.openConnection(); | |||
urlConnection.connect(); | |||
String response = IOUtils.toString(urlConnection.getInputStream()); | |||
if (response.contains("\"status\":\"green\"")) { | |||
return Status.GREEN; | |||
} else if (response.contains("\"status\":\"yellow\"")) { | |||
return Status.YELLOW; | |||
} else if (response.contains("\"status\":\"red\"")) { | |||
return Status.RED; | |||
} | |||
return Status.KO; | |||
} catch (ConnectException e) { | |||
return Status.CONNECTION_REFUSED; | |||
} catch (IOException e) { | |||
LOG.error("Unexpected error occurred while checking ES node status using WebService API", e); | |||
return Status.KO; | |||
} | |||
} | |||
|
|||
enum Status { | |||
CONNECTION_REFUSED, KO, RED, YELLOW, GREEN | |||
} | |||
|
|||
@Override | |||
public void askForStop() { | |||
process.destroy(); | |||
} | |||
|
|||
@Override | |||
public boolean askedForRestart() { | |||
// ES does not support asking for restart | |||
return false; | |||
} | |||
|
|||
@Override | |||
public void acknowledgeAskForRestart() { | |||
// nothing to do | |||
} | |||
} |
Oops, something went wrong.