improve update mapping on master, if we end up with the same mappings as the one the cluster state has, no need for a new cluster state...

This commit is contained in:
kimchy 2010-12-10 00:17:10 +02:00
parent 167d35807c
commit 3f6ed7e1de
2 changed files with 19 additions and 9 deletions

View File

@ -69,7 +69,7 @@ public class MappingUpdatedAction extends TransportMasterNodeOperationAction<Map
@Override protected MappingUpdatedResponse masterOperation(MappingUpdatedRequest request, ClusterState state) throws ElasticSearchException {
try {
metaDataMappingService.updateMapping(request.index(), request.type(), request.mappingSource());
} catch (IOException e) {
} catch (Exception e) {
throw new ElasticSearchParseException("failed to parse mapping form compressed string", e);
}
return new MappingUpdatedResponse();

View File

@ -61,18 +61,23 @@ public class MetaDataMappingService extends AbstractComponent {
this.indicesService = indicesService;
}
public void updateMapping(final String index, final String type, final CompressedString mappingSource) throws IOException {
updateMapping(index, type, mappingSource.string());
}
public void updateMapping(final String index, final String type, final String mappingSource) {
public void updateMapping(final String index, final String type, final CompressedString mappingSource) {
clusterService.submitStateUpdateTask("update-mapping [" + index + "][" + type + "]", new ClusterStateUpdateTask() {
@Override public ClusterState execute(ClusterState currentState) {
try {
// first, check if it really needs to be updated
final IndexMetaData indexMetaData = currentState.metaData().index(index);
if (indexMetaData == null) {
// index got delete on us, ignore...
return currentState;
}
if (indexMetaData.mappings().containsKey(type) && indexMetaData.mapping(type).source().equals(mappingSource)) {
return currentState;
}
IndexService indexService = indicesService.indexService(index);
if (indexService == null) {
// we need to create the index here, and add the current mapping to it, so we can merge
final IndexMetaData indexMetaData = currentState.metaData().index(index);
indexService = indicesService.createIndex(indexMetaData.index(), indexMetaData.settings(), currentState.nodes().localNode().id());
// only add the current relevant mapping (if exists)
if (indexMetaData.mappings().containsKey(type)) {
@ -83,13 +88,19 @@ public class MetaDataMappingService extends AbstractComponent {
DocumentMapper existingMapper = mapperService.documentMapper(type);
// parse the updated one
DocumentMapper updatedMapper = mapperService.parse(type, mappingSource);
DocumentMapper updatedMapper = mapperService.parse(type, mappingSource.string());
if (existingMapper == null) {
existingMapper = updatedMapper;
} else {
// merge from the updated into the existing, ignore conflicts (we know we have them, we just want the new ones)
existingMapper.merge(updatedMapper, mergeFlags().simulate(false));
}
// if we end up with the same mapping as the original once, ignore
if (indexMetaData.mappings().containsKey(type) && indexMetaData.mapping(type).source().equals(existingMapper.mappingSource())) {
return currentState;
}
// build the updated mapping source
if (logger.isDebugEnabled()) {
try {
@ -102,7 +113,6 @@ public class MetaDataMappingService extends AbstractComponent {
}
MetaData.Builder builder = newMetaDataBuilder().metaData(currentState.metaData());
IndexMetaData indexMetaData = currentState.metaData().index(index);
builder.put(newIndexMetaDataBuilder(indexMetaData).putMapping(new MappingMetaData(existingMapper)));
return newClusterStateBuilder().state(currentState).metaData(builder).build();
} catch (Exception e) {