ElasticsearchClient { public static String modifyIndexTemplate(String templatePath, int moveToColdNodesAfterDays, Integer numberOfReplicas, Integer numberOfShards) { final JsonNode json; try { json = JsonUtils.getMapper().readTree(IOUtils.getResourceAsStream(templatePath)); ObjectNode indexSettings = (ObjectNode) json.get("settings").get("index"); if (moveToColdNodesAfterDays > 0) { indexSettings.put("routing.allocation.require.box_type", "hot"); } if (numberOfReplicas != null && numberOfReplicas >= 0) { indexSettings.put("number_of_replicas", numberOfReplicas); } if (numberOfShards != null && numberOfShards > 0) { indexSettings.put("number_of_shards", numberOfShards); } } catch (Exception e) { throw new RuntimeException(e); } return json.toString(); } ElasticsearchClient(final CorePlugin corePlugin, final HttpClient httpClient, int esAvailabilityCheckIntervalSec, List<ElasticsearchAvailabilityObserver> elasticsearchAvailabilityObservers); boolean isElasticsearch7Compatible(); boolean isElasticsearch6Compatible(); String getElasticSearchTemplateResourcePath(); String getKibanaResourcePath(); JsonNode getJson(final String path); JsonNode getJson(final String path, final boolean suppressWarning); T getObject(final String path, Class<T> type); Collection<T> getAll(String path, int limit, Class<T> clazz); int delete(final String path); int sendAsJson(final String method, final String path, final Object requestBody); void updateKibanaIndexPattern(final String indexName, final String indexPatternLocation); void sendMappingTemplate(String mappingJson, String templateName); static String modifyIndexTemplate(String templatePath, int moveToColdNodesAfterDays, Integer numberOfReplicas, Integer numberOfShards); void sendMetricDashboardBulkAsync(final String resource); void sendSpanDashboardBulkAsync(final String resource, boolean logBulkErrors); void deleteIndices(String indexPattern); void forceMergeIndices(String indexPattern); void updateIndexSettings(String indexPattern, final Map<String, ?> settings); void waitForCompletion(); void close(); void scheduleIndexManagement(String indexPrefix, int optimizeAndMoveIndicesToColdNodesOlderThanDays, int deleteIndicesOlderThanDays); static String getBulkHeader(String action, String index, String type); boolean isElasticsearchAvailable(); HttpClient getHttpClient(); URL getElasticsearchUrl(); void createIndexAndSendMapping(final String index, final String type, final InputStream mapping); void createEmptyIndex(String indexName); void checkEsAvailability(); static final Map<String, String> CONTENT_TYPE_NDJSON; }
@Test public void testRequireBoxTypeHotWhenHotColdActive() throws Exception { final String indexTemplate = modifyIndexTemplate("es/6/stagemonitor-elasticsearch-metrics-index-template.json", 2, null, 0); assertTrue(indexTemplate.contains("hot")); assertFalse(indexTemplate.contains("number_of_shards")); assertFalse(indexTemplate.contains("number_of_replicas")); }
@Test public void testSetReplicas() throws Exception { final String indexTemplate = modifyIndexTemplate("es/6/stagemonitor-elasticsearch-metrics-index-template.json", 0, 0, 0); assertFalse(indexTemplate.contains("hot")); assertEquals(0, JsonUtils.getMapper().readTree(indexTemplate).get("settings").get("index").get("number_of_replicas").asInt()); assertFalse(indexTemplate.contains("number_of_shards")); }
@Test public void testSetShards() throws Exception { final String indexTemplate = modifyIndexTemplate("es/6/stagemonitor-elasticsearch-metrics-index-template.json", 0, -1, 1); assertFalse(indexTemplate.contains("hot")); assertEquals(1, JsonUtils.getMapper().readTree(indexTemplate).get("settings").get("index").get("number_of_shards").asInt()); assertFalse(indexTemplate.contains("number_of_replicas")); }
@Test public void testDontRequireBoxTypeHotWhenHotColdInactive() throws Exception { assertFalse(modifyIndexTemplate("es/6/stagemonitor-elasticsearch-metrics-index-template.json", 0, 0, 0).contains("hot")); assertFalse(modifyIndexTemplate("es/6/stagemonitor-elasticsearch-metrics-index-template.json", -1, 0, 0).contains("hot")); }