Skip to content

Commit 71ce156

Browse files
committed
improve refreshing logic to resync mappings on upgrade, reduce the amount of cluster events processing requires if the even if fired from several nodes / sources
1 parent 577285d commit 71ce156

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.elasticsearch.cluster.action.index.NodeMappingCreatedAction;
2727
import org.elasticsearch.cluster.routing.IndexRoutingTable;
2828
import org.elasticsearch.common.collect.Lists;
29+
import org.elasticsearch.common.collect.Maps;
30+
import org.elasticsearch.common.collect.Sets;
2931
import org.elasticsearch.common.component.AbstractComponent;
3032
import org.elasticsearch.common.compress.CompressedString;
3133
import org.elasticsearch.common.inject.Inject;
@@ -44,6 +46,7 @@
4446
import java.util.Arrays;
4547
import java.util.List;
4648
import java.util.Map;
49+
import java.util.Set;
4750
import java.util.concurrent.atomic.AtomicBoolean;
4851
import java.util.concurrent.atomic.AtomicInteger;
4952

@@ -64,6 +67,8 @@ public class MetaDataMappingService extends AbstractComponent {
6467

6568
private final NodeMappingCreatedAction mappingCreatedAction;
6669

70+
private final Map<String, Set<String>> indicesAndTypesToRefresh = Maps.newHashMap();
71+
6772
@Inject public MetaDataMappingService(Settings settings, ClusterService clusterService, IndicesService indicesService, NodeMappingCreatedAction mappingCreatedAction) {
6873
super(settings);
6974
this.clusterService = clusterService;
@@ -75,10 +80,27 @@ public class MetaDataMappingService extends AbstractComponent {
7580
* Refreshes mappings if they are not the same between original and parsed version
7681
*/
7782
public void refreshMapping(final String index, final String... types) {
83+
synchronized (indicesAndTypesToRefresh) {
84+
Set<String> sTypes = indicesAndTypesToRefresh.get(index);
85+
if (sTypes == null) {
86+
sTypes = Sets.newHashSet();
87+
indicesAndTypesToRefresh.put(index, sTypes);
88+
}
89+
sTypes.addAll(Arrays.asList(types));
90+
}
7891
clusterService.submitStateUpdateTask("refresh-mapping [" + index + "][" + Arrays.toString(types) + "]", new ClusterStateUpdateTask() {
7992
@Override public ClusterState execute(ClusterState currentState) {
8093
boolean createdIndex = false;
8194
try {
95+
Set<String> sTypes;
96+
synchronized (indicesAndTypesToRefresh) {
97+
sTypes = indicesAndTypesToRefresh.remove(index);
98+
}
99+
// we already processed those types...
100+
if (sTypes == null || sTypes.isEmpty()) {
101+
return currentState;
102+
}
103+
82104
// first, check if it really needs to be updated
83105
final IndexMetaData indexMetaData = currentState.metaData().index(index);
84106
if (indexMetaData == null) {
@@ -91,7 +113,7 @@ public void refreshMapping(final String index, final String... types) {
91113
// we need to create the index here, and add the current mapping to it, so we can merge
92114
indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
93115
createdIndex = true;
94-
for (String type : types) {
116+
for (String type : sTypes) {
95117
// only add the current relevant mapping (if exists)
96118
if (indexMetaData.mappings().containsKey(type)) {
97119
indexService.mapperService().add(type, indexMetaData.mappings().get(type).source().string());
@@ -100,7 +122,7 @@ public void refreshMapping(final String index, final String... types) {
100122
}
101123
IndexMetaData.Builder indexMetaDataBuilder = newIndexMetaDataBuilder(indexMetaData);
102124
List<String> updatedTypes = Lists.newArrayList();
103-
for (String type : types) {
125+
for (String type : sTypes) {
104126
DocumentMapper mapper = indexService.mapperService().documentMapper(type);
105127
if (!mapper.mappingSource().equals(indexMetaData.mappings().get(type).source())) {
106128
updatedTypes.add(type);

0 commit comments

Comments
 (0)