|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.action.get; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.action.ActionListener; |
| 24 | +import org.elasticsearch.action.IndicesRequest; |
| 25 | +import org.elasticsearch.action.RoutingMissingException; |
| 26 | +import org.elasticsearch.action.support.ActionFilters; |
| 27 | +import org.elasticsearch.client.node.NodeClient; |
| 28 | +import org.elasticsearch.cluster.ClusterName; |
| 29 | +import org.elasticsearch.cluster.ClusterState; |
| 30 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 31 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 32 | +import org.elasticsearch.cluster.metadata.MetaData; |
| 33 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 34 | +import org.elasticsearch.cluster.routing.OperationRouting; |
| 35 | +import org.elasticsearch.cluster.routing.ShardIterator; |
| 36 | +import org.elasticsearch.cluster.service.ClusterService; |
| 37 | +import org.elasticsearch.common.bytes.BytesReference; |
| 38 | +import org.elasticsearch.common.settings.Settings; |
| 39 | +import org.elasticsearch.common.util.concurrent.AtomicArray; |
| 40 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 41 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 42 | +import org.elasticsearch.common.xcontent.XContentType; |
| 43 | +import org.elasticsearch.index.Index; |
| 44 | +import org.elasticsearch.index.shard.ShardId; |
| 45 | +import org.elasticsearch.indices.IndicesService; |
| 46 | +import org.elasticsearch.tasks.Task; |
| 47 | +import org.elasticsearch.tasks.TaskId; |
| 48 | +import org.elasticsearch.tasks.TaskManager; |
| 49 | +import org.elasticsearch.test.ESTestCase; |
| 50 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 51 | +import org.elasticsearch.threadpool.ThreadPool; |
| 52 | +import org.elasticsearch.transport.Transport; |
| 53 | +import org.elasticsearch.transport.TransportService; |
| 54 | +import org.junit.AfterClass; |
| 55 | +import org.junit.BeforeClass; |
| 56 | + |
| 57 | +import java.util.Map; |
| 58 | +import java.util.concurrent.TimeUnit; |
| 59 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 60 | + |
| 61 | +import static java.util.Collections.emptyMap; |
| 62 | +import static java.util.Collections.emptySet; |
| 63 | +import static org.elasticsearch.common.UUIDs.randomBase64UUID; |
| 64 | +import static org.hamcrest.Matchers.equalTo; |
| 65 | +import static org.hamcrest.Matchers.instanceOf; |
| 66 | +import static org.mockito.Matchers.anyString; |
| 67 | +import static org.mockito.Matchers.eq; |
| 68 | +import static org.mockito.Mockito.mock; |
| 69 | +import static org.mockito.Mockito.when; |
| 70 | + |
| 71 | +public class TransportMultiGetActionTests extends ESTestCase { |
| 72 | + |
| 73 | + private static ThreadPool threadPool; |
| 74 | + private static TransportService transportService; |
| 75 | + private static ClusterService clusterService; |
| 76 | + private static TransportMultiGetAction transportAction; |
| 77 | + private static TransportShardMultiGetAction shardAction; |
| 78 | + |
| 79 | + @BeforeClass |
| 80 | + public static void beforeClass() throws Exception { |
| 81 | + threadPool = new TestThreadPool(TransportMultiGetActionTests.class.getSimpleName()); |
| 82 | + |
| 83 | + transportService = new TransportService(Settings.EMPTY, mock(Transport.class), threadPool, |
| 84 | + TransportService.NOOP_TRANSPORT_INTERCEPTOR, |
| 85 | + boundAddress -> DiscoveryNode.createLocal(Settings.builder().put("node.name", "node1").build(), |
| 86 | + boundAddress.publishAddress(), randomBase64UUID()), null, emptySet()) { |
| 87 | + @Override |
| 88 | + public TaskManager getTaskManager() { |
| 89 | + return taskManager; |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + final Index index1 = new Index("index1", randomBase64UUID()); |
| 94 | + final ClusterState clusterState = ClusterState.builder(new ClusterName(TransportMultiGetActionTests.class.getSimpleName())) |
| 95 | + .metaData(new MetaData.Builder() |
| 96 | + .put(new IndexMetaData.Builder(index1.getName()) |
| 97 | + .settings(Settings.builder().put("index.version.created", Version.CURRENT) |
| 98 | + .put("index.number_of_shards", 1) |
| 99 | + .put("index.number_of_replicas", 1) |
| 100 | + .put(IndexMetaData.SETTING_INDEX_UUID, index1.getUUID())) |
| 101 | + .putMapping("type1", |
| 102 | + XContentHelper.convertToJson(BytesReference.bytes(XContentFactory.jsonBuilder() |
| 103 | + .startObject() |
| 104 | + .startObject("type1") |
| 105 | + .startObject("_routing") |
| 106 | + .field("required", false) |
| 107 | + .endObject() |
| 108 | + .endObject() |
| 109 | + .endObject()), true, XContentType.JSON)) |
| 110 | + .putMapping("type2", |
| 111 | + XContentHelper.convertToJson(BytesReference.bytes(XContentFactory.jsonBuilder() |
| 112 | + .startObject() |
| 113 | + .startObject("type2") |
| 114 | + .startObject("_routing") |
| 115 | + .field("required", true) |
| 116 | + .endObject() |
| 117 | + .endObject() |
| 118 | + .endObject()), true, XContentType.JSON)))).build(); |
| 119 | + |
| 120 | + final ShardIterator shardIterator = mock(ShardIterator.class); |
| 121 | + when(shardIterator.shardId()).thenReturn(new ShardId(index1, randomInt())); |
| 122 | + |
| 123 | + final OperationRouting operationRouting = mock(OperationRouting.class); |
| 124 | + when(operationRouting.getShards(eq(clusterState), eq(index1.getName()), anyString(), anyString(), anyString())) |
| 125 | + .thenReturn(shardIterator); |
| 126 | + when(operationRouting.shardId(eq(clusterState), eq(index1.getName()), anyString(), anyString())) |
| 127 | + .thenReturn(new ShardId(index1, randomInt())); |
| 128 | + |
| 129 | + clusterService = mock(ClusterService.class); |
| 130 | + when(clusterService.localNode()).thenReturn(transportService.getLocalNode()); |
| 131 | + when(clusterService.state()).thenReturn(clusterState); |
| 132 | + when(clusterService.operationRouting()).thenReturn(operationRouting); |
| 133 | + |
| 134 | + shardAction = new TransportShardMultiGetAction(clusterService, transportService, mock(IndicesService.class), threadPool, |
| 135 | + new ActionFilters(emptySet()), new Resolver()) { |
| 136 | + @Override |
| 137 | + protected void doExecute(Task task, MultiGetShardRequest request, ActionListener<MultiGetShardResponse> listener) { |
| 138 | + } |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + @AfterClass |
| 143 | + public static void afterClass() { |
| 144 | + ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS); |
| 145 | + threadPool = null; |
| 146 | + transportService = null; |
| 147 | + clusterService = null; |
| 148 | + transportAction = null; |
| 149 | + shardAction = null; |
| 150 | + } |
| 151 | + |
| 152 | + public void testTransportMultiGetAction() { |
| 153 | + final Task task = createTask(); |
| 154 | + final NodeClient client = new NodeClient(Settings.EMPTY, threadPool); |
| 155 | + final MultiGetRequestBuilder request = new MultiGetRequestBuilder(client, MultiGetAction.INSTANCE); |
| 156 | + request.add(new MultiGetRequest.Item("index1", "type1", "1")); |
| 157 | + request.add(new MultiGetRequest.Item("index1", "type1", "2")); |
| 158 | + |
| 159 | + final AtomicBoolean shardActionInvoked = new AtomicBoolean(false); |
| 160 | + transportAction = new TransportMultiGetAction(transportService, clusterService, shardAction, |
| 161 | + new ActionFilters(emptySet()), new Resolver()) { |
| 162 | + @Override |
| 163 | + protected void executeShardAction(final ActionListener<MultiGetResponse> listener, |
| 164 | + final AtomicArray<MultiGetItemResponse> responses, |
| 165 | + final Map<ShardId, MultiGetShardRequest> shardRequests) { |
| 166 | + shardActionInvoked.set(true); |
| 167 | + assertEquals(2, responses.length()); |
| 168 | + assertNull(responses.get(0)); |
| 169 | + assertNull(responses.get(1)); |
| 170 | + } |
| 171 | + }; |
| 172 | + |
| 173 | + transportAction.execute(task, request.request(), new ActionListenerAdapter()); |
| 174 | + assertTrue(shardActionInvoked.get()); |
| 175 | + } |
| 176 | + |
| 177 | + public void testTransportMultiGetAction_withMissingRouting() { |
| 178 | + final Task task = createTask(); |
| 179 | + final NodeClient client = new NodeClient(Settings.EMPTY, threadPool); |
| 180 | + final MultiGetRequestBuilder request = new MultiGetRequestBuilder(client, MultiGetAction.INSTANCE); |
| 181 | + request.add(new MultiGetRequest.Item("index1", "type2", "1").routing("1")); |
| 182 | + request.add(new MultiGetRequest.Item("index1", "type2", "2")); |
| 183 | + |
| 184 | + final AtomicBoolean shardActionInvoked = new AtomicBoolean(false); |
| 185 | + transportAction = new TransportMultiGetAction(transportService, clusterService, shardAction, |
| 186 | + new ActionFilters(emptySet()), new Resolver()) { |
| 187 | + @Override |
| 188 | + protected void executeShardAction(final ActionListener<MultiGetResponse> listener, |
| 189 | + final AtomicArray<MultiGetItemResponse> responses, |
| 190 | + final Map<ShardId, MultiGetShardRequest> shardRequests) { |
| 191 | + shardActionInvoked.set(true); |
| 192 | + assertEquals(2, responses.length()); |
| 193 | + assertNull(responses.get(0)); |
| 194 | + assertThat(responses.get(1).getFailure().getFailure(), instanceOf(RoutingMissingException.class)); |
| 195 | + assertThat(responses.get(1).getFailure().getFailure().getMessage(), |
| 196 | + equalTo("routing is required for [index1]/[type2]/[2]")); |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + transportAction.execute(task, request.request(), new ActionListenerAdapter()); |
| 201 | + assertTrue(shardActionInvoked.get()); |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + private static Task createTask() { |
| 206 | + return new Task(randomLong(), "transport", MultiGetAction.NAME, "description", |
| 207 | + new TaskId(randomLong() + ":" + randomLong()), emptyMap()); |
| 208 | + } |
| 209 | + |
| 210 | + static class Resolver extends IndexNameExpressionResolver { |
| 211 | + |
| 212 | + @Override |
| 213 | + public Index concreteSingleIndex(ClusterState state, IndicesRequest request) { |
| 214 | + return new Index("index1", randomBase64UUID()); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + static class ActionListenerAdapter implements ActionListener<MultiGetResponse> { |
| 219 | + |
| 220 | + @Override |
| 221 | + public void onResponse(MultiGetResponse response) { |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public void onFailure(Exception e) { |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments