@Test public void isOperational_should_return_false_if_Elasticsearch_is_RED() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()).thenReturn(ClusterHealthStatus.RED); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isFalse(); }
@Override public boolean isOperational() { if (nodeOperational.get()) { return true; } boolean flag = false; try { flag = checkOperational(); } catch (InterruptedException e) { LOG.trace("Interrupted while checking ES node is operational", e); Thread.currentThread().interrupt(); } finally { if (flag) { esConnector.stop(); nodeOperational.set(true); } } 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 == YELLOW || status == GREEN; }
checkArgument(esInstallation != null, "Incorrect configuration EsInstallation is null"); EsConnectorImpl esConnector = new EsConnectorImpl(esInstallation.getClusterName(), singleton(HostAndPort.fromParts(esInstallation.getHost(), esInstallation.getPort()))); return new EsProcessMonitor(process, processId, esConnector); } else { ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex());
private TransportClient buildTransportClient() { Settings.Builder esSettings = Settings.builder(); // mandatory property defined by bootstrap process esSettings.put("cluster.name", esConfig.getClusterName()); TransportClient nativeClient = new MinimalTransportClient(esSettings.build()); HostAndPort host = HostAndPort.fromParts(esConfig.getHost(), esConfig.getPort()); addHostToClient(host, nativeClient); if (LOG.isDebugEnabled()) { LOG.debug("Connected to Elasticsearch node: [{}]", displayedAddresses(nativeClient)); } return nativeClient; }
private TransportClient getTransportClient() { TransportClient res = this.transportClient.get(); if (res == null) { res = buildTransportClient(); if (this.transportClient.compareAndSet(null, res)) { return res; } return this.transportClient.get(); } return res; }
private Status checkStatus() { try { switch (esConnector.getClusterHealthStatus(getTransportClient())) { case GREEN: return GREEN; case YELLOW: return YELLOW; case RED: return RED; default: return KO; } } catch (NoNodeAvailableException e) { return CONNECTION_REFUSED; } catch (MasterNotDiscoveredException e) { if (firstMasterNotDiscoveredLog.getAndSet(false)) { LOG.info("Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?"); } return KO; } catch (Exception e) { LOG.error("Failed to check status", e); return KO; } }
public ProcessMonitor launch(AbstractCommand command) { EsInstallation fileSystem = command.getEsInstallation(); if (fileSystem != null) { cleanupOutdatedEsData(fileSystem); writeConfFiles(fileSystem); } Process process; if (command instanceof EsScriptCommand) { process = launchExternal((EsScriptCommand) command); } else if (command instanceof JavaCommand) { process = launchJava((JavaCommand) command); } else { throw new IllegalStateException("Unexpected type of command: " + command.getClass()); } ProcessId processId = command.getProcessId(); try { if (processId == ProcessId.ELASTICSEARCH) { return new EsProcessMonitor(process, processId, command.getEsInstallation(), new EsConnectorImpl()); } else { ProcessCommands commands = allProcessesCommands.createAfterClean(processId.getIpcIndex()); return new ProcessCommandsProcessMonitor(process, processId, commands); } } catch (Exception e) { // just in case if (process != null) { process.destroyForcibly(); } throw new IllegalStateException(format("Fail to launch monitor of process [%s]", processId.getKey()), e); } }
@Test public void isOperational_should_return_true_if_Elasticsearch_is_YELLOW() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()).thenReturn(ClusterHealthStatus.YELLOW); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isTrue(); }
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 == YELLOW || status == GREEN; }
@Override public boolean isOperational() { if (nodeOperational.get()) { return true; } boolean flag = false; try { flag = checkOperational(); } catch (InterruptedException e) { LOG.trace("Interrupted while checking ES node is operational", e); Thread.currentThread().interrupt(); } finally { if (flag) { transportClient.set(null); nodeOperational.set(true); } } return nodeOperational.get(); }
@Test public void isOperational_should_return_true_if_Elasticsearch_is_GREEN() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()).thenReturn(ClusterHealthStatus.GREEN); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isTrue(); }
@Test public void isOperational_should_return_true_if_Elasticsearch_was_GREEN_once() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()).thenReturn(ClusterHealthStatus.GREEN); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isTrue(); when(esConnector.getClusterHealthStatus()).thenReturn(ClusterHealthStatus.RED); assertThat(underTest.isOperational()).isTrue(); }
@Test public void isOperational_should_return_false_if_Elasticsearch_status_cannot_be_evaluated() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()) .thenThrow(new RuntimeException("test")); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isFalse(); }
@Test public void isOperational_should_retry_if_Elasticsearch_is_unreachable() { EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()) .thenThrow(new NoNodeAvailableException("test")) .thenReturn(ClusterHealthStatus.GREEN); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isTrue(); }
@Test public void isOperational_must_log_once_when_master_is_not_elected() { MemoryAppender<ILoggingEvent> memoryAppender = new MemoryAppender<>(); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); lc.reset(); memoryAppender.setContext(lc); memoryAppender.start(); lc.getLogger(EsProcessMonitor.class).addAppender(memoryAppender); EsConnector esConnector = mock(EsConnector.class); when(esConnector.getClusterHealthStatus()) .thenThrow(new MasterNotDiscoveredException("Master not elected -test-")); EsProcessMonitor underTest = new EsProcessMonitor(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); assertThat(underTest.isOperational()).isFalse(); assertThat(memoryAppender.events).isNotEmpty(); assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") ); // Second call must not log another message assertThat(underTest.isOperational()).isFalse(); assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") ); }