|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.monitoring.collector.ccr; |
| 7 | + |
| 8 | +import org.elasticsearch.ElasticsearchException; |
| 9 | +import org.elasticsearch.common.Strings; |
| 10 | +import org.elasticsearch.common.bytes.BytesReference; |
| 11 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 12 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 13 | +import org.elasticsearch.common.xcontent.XContentType; |
| 14 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 15 | +import org.elasticsearch.xpack.core.ccr.AutoFollowStats; |
| 16 | +import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; |
| 17 | +import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; |
| 18 | +import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; |
| 19 | +import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; |
| 20 | +import org.joda.time.DateTime; |
| 21 | +import org.joda.time.DateTimeZone; |
| 22 | +import org.junit.Before; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.NavigableMap; |
| 28 | +import java.util.TreeMap; |
| 29 | + |
| 30 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 31 | +import static org.hamcrest.Matchers.anyOf; |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | +import static org.hamcrest.Matchers.is; |
| 34 | +import static org.hamcrest.Matchers.notNullValue; |
| 35 | +import static org.hamcrest.Matchers.nullValue; |
| 36 | + |
| 37 | +public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase<AutoFollowStatsMonitoringDoc> { |
| 38 | + |
| 39 | + private AutoFollowStats autoFollowStats; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void instantiateAutoFollowStats() { |
| 43 | + autoFollowStats = new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), |
| 44 | + Collections.emptyNavigableMap()); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected AutoFollowStatsMonitoringDoc createMonitoringDoc(String cluster, |
| 49 | + long timestamp, |
| 50 | + long interval, |
| 51 | + MonitoringDoc.Node node, |
| 52 | + MonitoredSystem system, |
| 53 | + String type, |
| 54 | + String id) { |
| 55 | + return new AutoFollowStatsMonitoringDoc(cluster, timestamp, interval, node, autoFollowStats); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void assertMonitoringDoc(AutoFollowStatsMonitoringDoc document) { |
| 60 | + assertThat(document.getSystem(), is(MonitoredSystem.ES)); |
| 61 | + assertThat(document.getType(), is(AutoFollowStatsMonitoringDoc.TYPE)); |
| 62 | + assertThat(document.getId(), nullValue()); |
| 63 | + assertThat(document.stats(), is(autoFollowStats)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void testToXContent() throws IOException { |
| 68 | + final long timestamp = System.currentTimeMillis(); |
| 69 | + final long intervalMillis = System.currentTimeMillis(); |
| 70 | + final long nodeTimestamp = System.currentTimeMillis(); |
| 71 | + final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", nodeTimestamp); |
| 72 | + |
| 73 | + final NavigableMap<String, ElasticsearchException> recentAutoFollowExceptions = |
| 74 | + new TreeMap<>(Collections.singletonMap( |
| 75 | + randomAlphaOfLength(4), |
| 76 | + new ElasticsearchException("cannot follow index"))); |
| 77 | + final AutoFollowStats autoFollowStats = |
| 78 | + new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), recentAutoFollowExceptions); |
| 79 | + |
| 80 | + final AutoFollowStatsMonitoringDoc document = |
| 81 | + new AutoFollowStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, autoFollowStats); |
| 82 | + final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); |
| 83 | + assertThat( |
| 84 | + xContent.utf8ToString(), |
| 85 | + equalTo( |
| 86 | + "{" |
| 87 | + + "\"cluster_uuid\":\"_cluster\"," |
| 88 | + + "\"timestamp\":\"" + new DateTime(timestamp, DateTimeZone.UTC).toString() + "\"," |
| 89 | + + "\"interval_ms\":" + intervalMillis + "," |
| 90 | + + "\"type\":\"ccr_auto_follow_stats\"," |
| 91 | + + "\"source_node\":{" |
| 92 | + + "\"uuid\":\"_uuid\"," |
| 93 | + + "\"host\":\"_host\"," |
| 94 | + + "\"transport_address\":\"_addr\"," |
| 95 | + + "\"ip\":\"_ip\"," |
| 96 | + + "\"name\":\"_name\"," |
| 97 | + + "\"timestamp\":\"" + new DateTime(nodeTimestamp, DateTimeZone.UTC).toString() + "\"" |
| 98 | + + "}," |
| 99 | + + "\"ccr_auto_follow_stats\":{" |
| 100 | + + "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + "," |
| 101 | + + "\"number_of_failed_remote_cluster_state_requests\":" + |
| 102 | + autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + "," |
| 103 | + + "\"number_of_successful_follow_indices\":" + autoFollowStats.getNumberOfSuccessfulFollowIndices() + "," |
| 104 | + + "\"recent_auto_follow_errors\":[" |
| 105 | + + "{" |
| 106 | + + "\"leader_index\":\"" + recentAutoFollowExceptions.keySet().iterator().next() + "\"," |
| 107 | + + "\"auto_follow_exception\":{" |
| 108 | + + "\"type\":\"exception\"," |
| 109 | + + "\"reason\":\"cannot follow index\"" |
| 110 | + + "}" |
| 111 | + + "}" |
| 112 | + + "]" |
| 113 | + + "}" |
| 114 | + + "}")); |
| 115 | + } |
| 116 | + |
| 117 | + public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { |
| 118 | + final NavigableMap<String, ElasticsearchException> fetchExceptions = |
| 119 | + new TreeMap<>(Collections.singletonMap("leader_index", new ElasticsearchException("cannot follow index"))); |
| 120 | + final AutoFollowStats status = new AutoFollowStats(1, 0, 2, fetchExceptions); |
| 121 | + XContentBuilder builder = jsonBuilder(); |
| 122 | + builder.value(status); |
| 123 | + Map<String, Object> serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false); |
| 124 | + |
| 125 | + Map<String, Object> template = |
| 126 | + XContentHelper.convertToMap(XContentType.JSON.xContent(), MonitoringTemplateUtils.loadTemplate("es"), false); |
| 127 | + Map<?, ?> autoFollowStatsMapping = |
| 128 | + (Map<?, ?>) XContentMapValues.extractValue("mappings.doc.properties.ccr_auto_follow_stats.properties", template); |
| 129 | + |
| 130 | + assertThat(serializedStatus.size(), equalTo(autoFollowStatsMapping.size())); |
| 131 | + for (Map.Entry<String, Object> entry : serializedStatus.entrySet()) { |
| 132 | + String fieldName = entry.getKey(); |
| 133 | + Map<?, ?> fieldMapping = (Map<?, ?>) autoFollowStatsMapping.get(fieldName); |
| 134 | + assertThat(fieldMapping, notNullValue()); |
| 135 | + |
| 136 | + Object fieldValue = entry.getValue(); |
| 137 | + String fieldType = (String) fieldMapping.get("type"); |
| 138 | + if (fieldValue instanceof Long || fieldValue instanceof Integer) { |
| 139 | + assertThat("expected long field type for field [" + fieldName + "]", fieldType, |
| 140 | + anyOf(equalTo("long"), equalTo("integer"))); |
| 141 | + } else if (fieldValue instanceof String) { |
| 142 | + assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, |
| 143 | + anyOf(equalTo("keyword"), equalTo("text"))); |
| 144 | + } else { |
| 145 | + // Manual test specific object fields and if not just fail: |
| 146 | + if (fieldName.equals("recent_auto_follow_errors")) { |
| 147 | + assertThat(fieldType, equalTo("nested")); |
| 148 | + assertThat(((Map<?, ?>) fieldMapping.get("properties")).size(), equalTo(2)); |
| 149 | + assertThat(XContentMapValues.extractValue("properties.leader_index.type", fieldMapping), equalTo("keyword")); |
| 150 | + assertThat(XContentMapValues.extractValue("properties.auto_follow_exception.type", fieldMapping), equalTo("object")); |
| 151 | + |
| 152 | + Map<?, ?> exceptionFieldMapping = |
| 153 | + (Map<?, ?>) XContentMapValues.extractValue("properties.auto_follow_exception.properties", fieldMapping); |
| 154 | + assertThat(exceptionFieldMapping.size(), equalTo(2)); |
| 155 | + assertThat(XContentMapValues.extractValue("type.type", exceptionFieldMapping), equalTo("keyword")); |
| 156 | + assertThat(XContentMapValues.extractValue("reason.type", exceptionFieldMapping), equalTo("text")); |
| 157 | + } else { |
| 158 | + fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments