Skip to content

Commit 73cc92d

Browse files
committed
Fix a mappings update test (#33146)
This commit fixes a mappings update test. The test is broken in the sense that it passes, but for the wrong reason. The test here is testing that if we make a mapping update but do not commit that mapping update then the mapper service still maintains the previous document mapper. This was not the case long, long ago when a mapping update would update the in-memory state before the cluster state update was committed. This test was passing, but it was passing because the mapping update was never even updated. It was never even updated because it was encountering a null pointer exception. Of course the in-memory state is not going to be updated in that case, we are simply going to end up with a failed cluster state update. Fixing that leads to another issue which is that the mapping source does not even parse so again we would, of course, end up with the in-memory state not being modified. We fix these issues, assert that the result cluster state task completed successfully, and finally that the in-memory state was not updated since we never committed the resulting cluster state.
1 parent db75566 commit 73cc92d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataMappingServiceTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.elasticsearch.cluster.metadata;
2021

2122
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest;
2324
import org.elasticsearch.cluster.ClusterState;
25+
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
2426
import org.elasticsearch.cluster.service.ClusterService;
2527
import org.elasticsearch.common.compress.CompressedXContent;
2628
import org.elasticsearch.common.settings.Settings;
2729
import org.elasticsearch.common.xcontent.XContentType;
30+
import org.elasticsearch.index.Index;
2831
import org.elasticsearch.index.IndexService;
2932
import org.elasticsearch.index.mapper.DocumentMapper;
3033
import org.elasticsearch.index.mapper.MapperParsingException;
@@ -37,6 +40,7 @@
3740

3841
import static org.hamcrest.Matchers.equalTo;
3942
import static org.hamcrest.Matchers.is;
43+
import static org.hamcrest.Matchers.not;
4044

4145
public class MetaDataMappingServiceTests extends ESSingleNodeTestCase {
4246

@@ -98,8 +102,18 @@ public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Ex
98102
final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
99103
// TODO - it will be nice to get a random mapping generator
100104
final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest().type("type");
101-
request.source("{ \"properties\" { \"field\": { \"type\": \"text\" }}}");
102-
mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));
105+
request.indices(new Index[] {indexService.index()});
106+
request.source("{ \"properties\": { \"field\": { \"type\": \"text\" }}}");
107+
final ClusterStateTaskExecutor.ClusterTasksResult<PutMappingClusterStateUpdateRequest> result =
108+
mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));
109+
// the task completed successfully
110+
assertThat(result.executionResults.size(), equalTo(1));
111+
assertTrue(result.executionResults.values().iterator().next().isSuccess());
112+
// the task really was a mapping update
113+
assertThat(
114+
indexService.mapperService().documentMapper("type").mappingSource(),
115+
not(equalTo(result.resultingState.metaData().index("test").mapping("type").source())));
116+
// since we never committed the cluster state update, the in-memory state is unchanged
103117
assertThat(indexService.mapperService().documentMapper("type").mappingSource(), equalTo(currentMapping));
104118
}
105119

@@ -120,4 +134,5 @@ public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception
120134

121135
assertSame(result, result2);
122136
}
137+
123138
}

0 commit comments

Comments
 (0)