|
8 | 8 |
|
9 | 9 | package org.opensearch.indices.replication; |
10 | 10 |
|
11 | | -import org.opensearch.action.admin.indices.replication.SegmentReplicationStatsResponse; |
| 11 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsRequest; |
| 12 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse; |
12 | 13 | import org.opensearch.cluster.metadata.IndexMetadata; |
13 | 14 | import org.opensearch.common.settings.Settings; |
14 | 15 | import org.opensearch.common.util.FeatureFlags; |
| 16 | +import org.opensearch.index.Index; |
15 | 17 | import org.opensearch.index.IndexModule; |
| 18 | +import org.opensearch.indices.IndicesService; |
16 | 19 | import org.opensearch.indices.SystemIndexDescriptor; |
17 | 20 | import org.opensearch.indices.replication.common.ReplicationType; |
18 | 21 | import org.opensearch.plugins.Plugin; |
|
24 | 27 | import java.util.Collections; |
25 | 28 | import java.util.Arrays; |
26 | 29 |
|
| 30 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE; |
27 | 31 | import static org.opensearch.indices.IndicesService.CLUSTER_SETTING_REPLICATION_TYPE; |
28 | | -import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
29 | 32 |
|
30 | 33 | @OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
31 | 34 | public class SegmentReplicationClusterSettingIT extends OpenSearchIntegTestCase { |
@@ -77,71 +80,119 @@ protected Collection<Class<? extends Plugin>> nodePlugins() { |
77 | 80 | return Arrays.asList(SegmentReplicationClusterSettingIT.TestPlugin.class, MockTransportService.TestPlugin.class); |
78 | 81 | } |
79 | 82 |
|
80 | | - public void testReplicationWithSegmentReplicationClusterSetting() throws Exception { |
81 | | - |
82 | | - boolean isSystemIndex = randomBoolean(); |
83 | | - String indexName = isSystemIndex ? SYSTEM_INDEX_NAME : INDEX_NAME; |
| 83 | + public void testSystemIndexWithSegmentReplicationClusterSetting() throws Exception { |
84 | 84 |
|
85 | 85 | // Starting two nodes with primary and replica shards respectively. |
86 | 86 | final String primaryNode = internalCluster().startNode(); |
87 | | - createIndex(indexName); |
88 | | - ensureYellowAndNoInitializingShards(indexName); |
| 87 | + createIndex(SYSTEM_INDEX_NAME); |
| 88 | + ensureYellowAndNoInitializingShards(SYSTEM_INDEX_NAME); |
89 | 89 | final String replicaNode = internalCluster().startNode(); |
90 | | - ensureGreen(indexName); |
91 | | - |
92 | | - final int initialDocCount = scaledRandomIntBetween(20, 30); |
93 | | - for (int i = 0; i < initialDocCount; i++) { |
94 | | - client().prepareIndex(indexName).setId(Integer.toString(i)).setSource("field", "value" + i).execute().actionGet(); |
95 | | - } |
96 | | - |
97 | | - refresh(indexName); |
98 | | - assertBusy(() -> { |
99 | | - assertHitCount(client(replicaNode).prepareSearch(indexName).setSize(0).setPreference("_only_local").get(), initialDocCount); |
100 | | - }); |
101 | | - |
102 | | - SegmentReplicationStatsResponse segmentReplicationStatsResponse = client().admin() |
| 90 | + ensureGreen(SYSTEM_INDEX_NAME); |
| 91 | + final GetSettingsResponse response = client().admin() |
103 | 92 | .indices() |
104 | | - .prepareSegmentReplicationStats(indexName) |
105 | | - .execute() |
| 93 | + .getSettings(new GetSettingsRequest().indices(SYSTEM_INDEX_NAME).includeDefaults(true)) |
106 | 94 | .actionGet(); |
107 | | - if (isSystemIndex) { |
108 | | - // Verify that Segment Replication did not happen on the replica shard. |
109 | | - assertNull(segmentReplicationStatsResponse.getReplicationStats().get(indexName)); |
110 | | - } else { |
111 | | - // Verify that Segment Replication happened on the replica shard. |
112 | | - assertFalse(segmentReplicationStatsResponse.getReplicationStats().get(indexName).get(0).getReplicaStats().isEmpty()); |
113 | | - } |
| 95 | + assertEquals(response.getSetting(SYSTEM_INDEX_NAME, SETTING_REPLICATION_TYPE), ReplicationType.DOCUMENT.toString()); |
| 96 | + |
| 97 | + // Verify index setting isSegRepEnabled is false. |
| 98 | + Index index = resolveIndex(SYSTEM_INDEX_NAME); |
| 99 | + IndicesService indicesService = internalCluster().getInstance(IndicesService.class, primaryNode); |
| 100 | + assertEquals(indicesService.indexService(index).getIndexSettings().isSegRepEnabled(), false); |
114 | 101 | } |
115 | 102 |
|
116 | | - public void testIndexReplicationSettingOverridesClusterSetting() throws Exception { |
| 103 | + public void testIndexReplicationSettingOverridesSegRepClusterSetting() throws Exception { |
| 104 | + Settings settings = Settings.builder().put(CLUSTER_SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT).build(); |
| 105 | + final String ANOTHER_INDEX = "test-index"; |
| 106 | + |
117 | 107 | // Starting two nodes with primary and replica shards respectively. |
118 | | - final String primaryNode = internalCluster().startNode(); |
| 108 | + final String primaryNode = internalCluster().startNode(settings); |
119 | 109 | prepareCreate( |
120 | 110 | INDEX_NAME, |
121 | 111 | Settings.builder() |
122 | 112 | // we want to override cluster replication setting by passing a index replication setting |
123 | 113 | .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) |
124 | 114 | ).get(); |
125 | | - ensureYellowAndNoInitializingShards(INDEX_NAME); |
126 | | - final String replicaNode = internalCluster().startNode(); |
127 | | - ensureGreen(INDEX_NAME); |
| 115 | + createIndex(ANOTHER_INDEX); |
| 116 | + ensureYellowAndNoInitializingShards(INDEX_NAME, ANOTHER_INDEX); |
| 117 | + final String replicaNode = internalCluster().startNode(settings); |
| 118 | + |
| 119 | + // Randomly close and open index. |
| 120 | + if (randomBoolean()) { |
| 121 | + logger.info("--> Closing the index "); |
| 122 | + client().admin().indices().prepareClose(INDEX_NAME).get(); |
128 | 123 |
|
129 | | - final int initialDocCount = scaledRandomIntBetween(20, 30); |
130 | | - for (int i = 0; i < initialDocCount; i++) { |
131 | | - client().prepareIndex(INDEX_NAME).setId(Integer.toString(i)).setSource("field", "value" + i).execute().actionGet(); |
| 124 | + logger.info("--> Opening the index"); |
| 125 | + client().admin().indices().prepareOpen(INDEX_NAME).get(); |
132 | 126 | } |
| 127 | + ensureGreen(INDEX_NAME, ANOTHER_INDEX); |
133 | 128 |
|
134 | | - refresh(INDEX_NAME); |
135 | | - assertBusy(() -> { |
136 | | - assertHitCount(client(replicaNode).prepareSearch(INDEX_NAME).setSize(0).setPreference("_only_local").get(), initialDocCount); |
137 | | - }); |
| 129 | + final GetSettingsResponse response = client().admin() |
| 130 | + .indices() |
| 131 | + .getSettings(new GetSettingsRequest().indices(INDEX_NAME, ANOTHER_INDEX).includeDefaults(true)) |
| 132 | + .actionGet(); |
| 133 | + assertEquals(response.getSetting(INDEX_NAME, SETTING_REPLICATION_TYPE), ReplicationType.DOCUMENT.toString()); |
| 134 | + assertEquals(response.getSetting(ANOTHER_INDEX, SETTING_REPLICATION_TYPE), ReplicationType.SEGMENT.toString()); |
| 135 | + |
| 136 | + // Verify index setting isSegRepEnabled. |
| 137 | + Index index = resolveIndex(INDEX_NAME); |
| 138 | + Index anotherIndex = resolveIndex(ANOTHER_INDEX); |
| 139 | + IndicesService indicesService = internalCluster().getInstance(IndicesService.class, primaryNode); |
| 140 | + assertEquals(indicesService.indexService(index).getIndexSettings().isSegRepEnabled(), false); |
| 141 | + assertEquals(indicesService.indexService(anotherIndex).getIndexSettings().isSegRepEnabled(), true); |
| 142 | + } |
138 | 143 |
|
139 | | - SegmentReplicationStatsResponse segmentReplicationStatsResponse = client().admin() |
| 144 | + public void testIndexReplicationSettingOverridesDocRepClusterSetting() throws Exception { |
| 145 | + Settings settings = Settings.builder().put(CLUSTER_SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build(); |
| 146 | + final String ANOTHER_INDEX = "test-index"; |
| 147 | + final String primaryNode = internalCluster().startNode(settings); |
| 148 | + prepareCreate( |
| 149 | + INDEX_NAME, |
| 150 | + Settings.builder() |
| 151 | + // we want to override cluster replication setting by passing a index replication setting |
| 152 | + .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) |
| 153 | + ).get(); |
| 154 | + createIndex(ANOTHER_INDEX); |
| 155 | + ensureYellowAndNoInitializingShards(INDEX_NAME, ANOTHER_INDEX); |
| 156 | + final String replicaNode = internalCluster().startNode(settings); |
| 157 | + ensureGreen(INDEX_NAME, ANOTHER_INDEX); |
| 158 | + |
| 159 | + final GetSettingsResponse response = client().admin() |
140 | 160 | .indices() |
141 | | - .prepareSegmentReplicationStats(INDEX_NAME) |
142 | | - .execute() |
| 161 | + .getSettings(new GetSettingsRequest().indices(INDEX_NAME, ANOTHER_INDEX).includeDefaults(true)) |
143 | 162 | .actionGet(); |
144 | | - // Verify that Segment Replication did not happen on the replica shard. |
145 | | - assertNull(segmentReplicationStatsResponse.getReplicationStats().get(INDEX_NAME)); |
| 163 | + assertEquals(response.getSetting(INDEX_NAME, SETTING_REPLICATION_TYPE), ReplicationType.SEGMENT.toString()); |
| 164 | + assertEquals(response.getSetting(ANOTHER_INDEX, SETTING_REPLICATION_TYPE), ReplicationType.DOCUMENT.toString()); |
| 165 | + |
| 166 | + // Verify index setting isSegRepEnabled. |
| 167 | + Index index = resolveIndex(INDEX_NAME); |
| 168 | + Index anotherIndex = resolveIndex(ANOTHER_INDEX); |
| 169 | + IndicesService indicesService = internalCluster().getInstance(IndicesService.class, primaryNode); |
| 170 | + assertEquals(indicesService.indexService(index).getIndexSettings().isSegRepEnabled(), true); |
| 171 | + assertEquals(indicesService.indexService(anotherIndex).getIndexSettings().isSegRepEnabled(), false); |
146 | 172 | } |
| 173 | + |
| 174 | + public void testHiddenIndicesWithReplicationStrategyClusterSetting() throws Exception { |
| 175 | + final String primaryNode = internalCluster().startNode(); |
| 176 | + final String replicaNode = internalCluster().startNode(); |
| 177 | + prepareCreate( |
| 178 | + INDEX_NAME, |
| 179 | + Settings.builder() |
| 180 | + // we want to set index as hidden |
| 181 | + .put("index.hidden", true) |
| 182 | + ).get(); |
| 183 | + ensureGreen(INDEX_NAME); |
| 184 | + |
| 185 | + // Verify that document replication strategy is used for hidden indices. |
| 186 | + final GetSettingsResponse response = client().admin() |
| 187 | + .indices() |
| 188 | + .getSettings(new GetSettingsRequest().indices(INDEX_NAME).includeDefaults(true)) |
| 189 | + .actionGet(); |
| 190 | + assertEquals(response.getSetting(INDEX_NAME, SETTING_REPLICATION_TYPE), ReplicationType.DOCUMENT.toString()); |
| 191 | + |
| 192 | + // Verify index setting isSegRepEnabled. |
| 193 | + Index index = resolveIndex(INDEX_NAME); |
| 194 | + IndicesService indicesService = internalCluster().getInstance(IndicesService.class, primaryNode); |
| 195 | + assertEquals(indicesService.indexService(index).getIndexSettings().isSegRepEnabled(), false); |
| 196 | + } |
| 197 | + |
147 | 198 | } |
0 commit comments