ElasticsearchResourceMonitor extends ResourceMonitor { @Override public boolean isHealthy() { try { ByteSizeValue limit = settings.getSettingValue(Settings.Key.PPL_QUERY_MEMORY_LIMIT); Supplier<Boolean> booleanSupplier = Retry.decorateSupplier(retry, () -> memoryMonitor .isMemoryHealthy(limit.getBytes())); return booleanSupplier.get(); } catch (Exception e) { return false; } } ElasticsearchResourceMonitor(
      Settings settings,
      ElasticsearchMemoryHealthy memoryMonitor); @Override boolean isHealthy();  }
@Test void isHealthy() { when(memoryMonitor.isMemoryHealthy(anyLong())).thenReturn(true); ElasticsearchResourceMonitor resourceMonitor = new ElasticsearchResourceMonitor(settings, memoryMonitor); assertTrue(resourceMonitor.isHealthy()); }
@Test void notHealthyFastFailure() { when(memoryMonitor.isMemoryHealthy(anyLong())).thenThrow( ElasticsearchMemoryHealthy.MemoryUsageExceedFastFailureException.class); ElasticsearchResourceMonitor resourceMonitor = new ElasticsearchResourceMonitor(settings, memoryMonitor); assertFalse(resourceMonitor.isHealthy()); verify(memoryMonitor, times(1)).isMemoryHealthy(anyLong()); }
@Test void notHealthyWithRetry() { when(memoryMonitor.isMemoryHealthy(anyLong())).thenThrow( ElasticsearchMemoryHealthy.MemoryUsageExceedException.class); ElasticsearchResourceMonitor resourceMonitor = new ElasticsearchResourceMonitor(settings, memoryMonitor); assertFalse(resourceMonitor.isHealthy()); verify(memoryMonitor, times(3)).isMemoryHealthy(anyLong()); }
@Test void healthyWithRetry() { when(memoryMonitor.isMemoryHealthy(anyLong())).thenThrow( ElasticsearchMemoryHealthy.MemoryUsageExceedException.class).thenReturn(true); ElasticsearchResourceMonitor resourceMonitor = new ElasticsearchResourceMonitor(settings, memoryMonitor); assertTrue(resourceMonitor.isHealthy()); verify(memoryMonitor, times(2)).isMemoryHealthy(anyLong()); }