|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.bulk; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.node.stats.NodeStats; |
| 12 | +import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse; |
| 13 | +import org.opensearch.action.search.SearchResponse; |
| 14 | +import org.opensearch.client.Client; |
| 15 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.opensearch.common.settings.Settings; |
| 17 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 18 | +import org.opensearch.index.query.QueryBuilders; |
| 19 | +import org.opensearch.ingest.IngestTestPlugin; |
| 20 | +import org.opensearch.plugins.Plugin; |
| 21 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 22 | +import org.opensearch.test.transport.MockTransportService; |
| 23 | +import org.opensearch.transport.ConnectTransportException; |
| 24 | +import org.opensearch.transport.TransportService; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING; |
| 33 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING; |
| 34 | +import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; |
| 35 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 36 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 37 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 38 | +import static org.hamcrest.Matchers.containsString; |
| 39 | + |
| 40 | +public class AppendOnlyIndicesIT extends OpenSearchIntegTestCase { |
| 41 | + |
| 42 | + @Override |
| 43 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 44 | + return Arrays.asList(IngestTestPlugin.class, MockTransportService.TestPlugin.class); |
| 45 | + } |
| 46 | + |
| 47 | + public void testIndexDocumentWithACustomDocIdForAppendOnlyIndices() throws Exception { |
| 48 | + Client client = internalCluster().coordOnlyNodeClient(); |
| 49 | + assertAcked( |
| 50 | + client().admin() |
| 51 | + .indices() |
| 52 | + .prepareCreate("index") |
| 53 | + .setSettings( |
| 54 | + Settings.builder() |
| 55 | + .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) |
| 56 | + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) |
| 57 | + .put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), true) |
| 58 | + ) |
| 59 | + ); |
| 60 | + ensureGreen("index"); |
| 61 | + |
| 62 | + BulkRequestBuilder bulkBuilder = client.prepareBulk(); |
| 63 | + |
| 64 | + XContentBuilder doc = null; |
| 65 | + doc = jsonBuilder().startObject().field("foo", "bar").endObject(); |
| 66 | + bulkBuilder.add(client.prepareIndex("index").setId(Integer.toString(0)).setSource(doc)); |
| 67 | + |
| 68 | + BulkResponse response = bulkBuilder.get(); |
| 69 | + assertThat( |
| 70 | + response.getItems()[0].getFailureMessage(), |
| 71 | + containsString( |
| 72 | + "Operation [INDEX] is not allowed with a custom document id 0 as setting `" |
| 73 | + + IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey() |
| 74 | + + "` is enabled for this index: index;" |
| 75 | + ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + public void testUpdateDeleteDocumentForAppendOnlyIndices() throws Exception { |
| 80 | + Client client = internalCluster().coordOnlyNodeClient(); |
| 81 | + assertAcked( |
| 82 | + client().admin() |
| 83 | + .indices() |
| 84 | + .prepareCreate("index") |
| 85 | + .setSettings( |
| 86 | + Settings.builder() |
| 87 | + .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) |
| 88 | + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) |
| 89 | + .put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), true) |
| 90 | + ) |
| 91 | + ); |
| 92 | + ensureGreen("index"); |
| 93 | + |
| 94 | + BulkRequestBuilder bulkBuilder = client.prepareBulk(); |
| 95 | + |
| 96 | + XContentBuilder doc = null; |
| 97 | + doc = jsonBuilder().startObject().field("foo", "bar").endObject(); |
| 98 | + bulkBuilder.add(client.prepareIndex("index").setSource(doc)); |
| 99 | + |
| 100 | + bulkBuilder.get(); |
| 101 | + BulkResponse response = client().prepareBulk().add(client().prepareUpdate("index", "0").setDoc("foo", "updated")).get(); |
| 102 | + assertThat( |
| 103 | + response.getItems()[0].getFailureMessage(), |
| 104 | + containsString( |
| 105 | + "Operation [UPDATE] is not allowed as setting `" |
| 106 | + + IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey() |
| 107 | + + "` is enabled for this index" |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + response = client().prepareBulk().add(client().prepareDelete("index", "0")).get(); |
| 112 | + assertThat( |
| 113 | + response.getItems()[0].getFailureMessage(), |
| 114 | + containsString( |
| 115 | + "Operation [DELETE] is not allowed as setting `" |
| 116 | + + IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey() |
| 117 | + + "` is enabled for this index" |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + public void testRetryForAppendOnlyIndices() throws Exception { |
| 123 | + final AtomicBoolean exceptionThrown = new AtomicBoolean(false); |
| 124 | + int numDocs = scaledRandomIntBetween(100, 1000); |
| 125 | + Client client = internalCluster().coordOnlyNodeClient(); |
| 126 | + NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().get(); |
| 127 | + NodeStats unluckyNode = randomFrom( |
| 128 | + nodeStats.getNodes().stream().filter((s) -> s.getNode().isDataNode()).collect(Collectors.toList()) |
| 129 | + ); |
| 130 | + assertAcked( |
| 131 | + client().admin() |
| 132 | + .indices() |
| 133 | + .prepareCreate("index") |
| 134 | + .setSettings( |
| 135 | + Settings.builder() |
| 136 | + .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) |
| 137 | + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) |
| 138 | + .put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), true) |
| 139 | + ) |
| 140 | + ); |
| 141 | + ensureGreen("index"); |
| 142 | + logger.info("unlucky node: {}", unluckyNode.getNode()); |
| 143 | + // create a transport service that throws a ConnectTransportException for one bulk request and therefore triggers a retry. |
| 144 | + for (NodeStats dataNode : nodeStats.getNodes()) { |
| 145 | + if (exceptionThrown.get()) { |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + MockTransportService mockTransportService = ((MockTransportService) internalCluster().getInstance( |
| 150 | + TransportService.class, |
| 151 | + dataNode.getNode().getName() |
| 152 | + )); |
| 153 | + mockTransportService.addSendBehavior( |
| 154 | + internalCluster().getInstance(TransportService.class, unluckyNode.getNode().getName()), |
| 155 | + (connection, requestId, action, request, options) -> { |
| 156 | + connection.sendRequest(requestId, action, request, options); |
| 157 | + if (action.equals(TransportShardBulkAction.ACTION_NAME) && exceptionThrown.compareAndSet(false, true)) { |
| 158 | + logger.debug("Throw ConnectTransportException"); |
| 159 | + throw new ConnectTransportException(connection.getNode(), action); |
| 160 | + } |
| 161 | + } |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + BulkRequestBuilder bulkBuilder = client.prepareBulk(); |
| 166 | + |
| 167 | + for (int i = 0; i < numDocs; i++) { |
| 168 | + XContentBuilder doc = null; |
| 169 | + doc = jsonBuilder().startObject().field("foo", "bar").endObject(); |
| 170 | + bulkBuilder.add(client.prepareIndex("index").setSource(doc)); |
| 171 | + } |
| 172 | + |
| 173 | + BulkResponse response = bulkBuilder.get(); |
| 174 | + for (BulkItemResponse singleIndexResponse : response.getItems()) { |
| 175 | + // Retry will not create a new version. |
| 176 | + assertThat(singleIndexResponse.getVersion(), equalTo(1L)); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public void testNodeReboot() throws Exception { |
| 181 | + int numDocs = scaledRandomIntBetween(100, 1000); |
| 182 | + Client client = internalCluster().coordOnlyNodeClient(); |
| 183 | + assertAcked( |
| 184 | + client().admin() |
| 185 | + .indices() |
| 186 | + .prepareCreate("index") |
| 187 | + .setSettings( |
| 188 | + Settings.builder() |
| 189 | + .put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) |
| 190 | + .put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) |
| 191 | + .put(IndexMetadata.INDEX_APPEND_ONLY_ENABLED_SETTING.getKey(), true) |
| 192 | + ) |
| 193 | + ); |
| 194 | + |
| 195 | + ensureGreen("index"); |
| 196 | + |
| 197 | + BulkRequestBuilder bulkBuilder = client.prepareBulk(); |
| 198 | + |
| 199 | + for (int i = 0; i < numDocs; i++) { |
| 200 | + XContentBuilder doc = null; |
| 201 | + doc = jsonBuilder().startObject().field("foo", "bar").endObject(); |
| 202 | + bulkBuilder.add(client.prepareIndex("index").setSource(doc)); |
| 203 | + } |
| 204 | + |
| 205 | + BulkResponse response = bulkBuilder.get(); |
| 206 | + assertFalse(response.hasFailures()); |
| 207 | + internalCluster().restartRandomDataNode(); |
| 208 | + ensureGreen("index"); |
| 209 | + refresh(); |
| 210 | + SearchResponse searchResponse = client().prepareSearch() |
| 211 | + .setQuery(QueryBuilders.matchAllQuery()) |
| 212 | + .setIndices("index") |
| 213 | + .setSize(numDocs) |
| 214 | + .get(); |
| 215 | + |
| 216 | + assertBusy(() -> { assertHitCount(searchResponse, numDocs); }, 20L, TimeUnit.SECONDS); |
| 217 | + |
| 218 | + } |
| 219 | +} |
0 commit comments