use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.
the class DropTableTask method execute.
@Override
public void execute(final BatchConsumer consumer, Row parameters) {
if (tableInfo.isPartitioned()) {
String templateName = PartitionName.templateName(tableInfo.ident().schema(), tableInfo.ident().name());
deleteTemplateAction.execute(new DeleteIndexTemplateRequest(templateName), new ActionListener<DeleteIndexTemplateResponse>() {
@Override
public void onResponse(DeleteIndexTemplateResponse response) {
if (!response.isAcknowledged()) {
warnNotAcknowledged();
}
if (!tableInfo.partitions().isEmpty()) {
deleteESIndex(tableInfo.ident().indexName(), consumer);
} else {
consumer.accept(RowsBatchIterator.newInstance(ROW_ONE), null);
}
}
@Override
public void onFailure(Throwable e) {
e = ExceptionsHelper.unwrapCause(e);
if (e instanceof IndexTemplateMissingException && !tableInfo.partitions().isEmpty()) {
logger.warn(e.getMessage());
deleteESIndex(tableInfo.ident().indexName(), consumer);
} else {
consumer.accept(null, e);
}
}
});
} else {
deleteESIndex(tableInfo.ident().indexName(), consumer);
}
}
use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.
the class RenameTableClusterStateExecutor method execute.
public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
RelationName source = request.sourceTableIdent();
RelationName target = request.targetTableIdent();
boolean isPartitioned = request.isPartitioned();
Metadata currentMetadata = currentState.getMetadata();
Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
if (isPartitioned) {
IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
if (indexTemplateMetadata == null) {
throw new IndexTemplateMissingException("Template for partitioned table is missing");
}
renameTemplate(newMetadata, indexTemplateMetadata, target);
}
RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
try {
Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
for (Index sourceIndex : sourceIndices) {
IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
String sourceIndexName = sourceIndex.getName();
newMetadata.remove(sourceIndexName);
newRoutingTable.remove(sourceIndexName);
blocksBuilder.removeIndexBlocks(sourceIndexName);
IndexMetadata targetMd;
if (isPartitioned) {
PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
} else {
targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
}
newMetadata.put(targetMd, true);
newRoutingTable.addAsFromCloseToOpen(targetMd);
blocksBuilder.addBlocks(targetMd);
}
} catch (IndexNotFoundException e) {
if (isPartitioned == false) {
throw e;
}
// empty partition case, no indices, just a template exists
}
ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
use of org.elasticsearch.indices.IndexTemplateMissingException in project elasticsearch by elastic.
the class ExceptionSerializationTests method testIndexTemplateMissingException.
public void testIndexTemplateMissingException() throws IOException {
IndexTemplateMissingException ex = serialize(new IndexTemplateMissingException("name"));
assertEquals("index_template [name] missing", ex.getMessage());
assertEquals("name", ex.name());
ex = serialize(new IndexTemplateMissingException((String) null));
assertEquals("index_template [null] missing", ex.getMessage());
assertNull(ex.name());
}
use of org.elasticsearch.indices.IndexTemplateMissingException in project crate by crate.
the class TemplateUpgradeService method upgradeTemplates.
void upgradeTemplates(Map<String, BytesReference> changes, Set<String> deletions) {
final AtomicBoolean anyUpgradeFailed = new AtomicBoolean(false);
for (Map.Entry<String, BytesReference> change : changes.entrySet()) {
PutIndexTemplateRequest request = new PutIndexTemplateRequest(change.getKey()).source(change.getValue(), XContentType.JSON);
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
client.admin().indices().putTemplate(request, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
if (response.isAcknowledged() == false) {
anyUpgradeFailed.set(true);
LOGGER.warn("Error updating template [{}], request was not acknowledged", change.getKey());
}
tryFinishUpgrade(anyUpgradeFailed);
}
@Override
public void onFailure(Exception e) {
anyUpgradeFailed.set(true);
LOGGER.warn(new ParameterizedMessage("Error updating template [{}]", change.getKey()), e);
tryFinishUpgrade(anyUpgradeFailed);
}
});
}
for (String template : deletions) {
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest(template);
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
client.admin().indices().deleteTemplate(request, new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
if (response.isAcknowledged() == false) {
anyUpgradeFailed.set(true);
LOGGER.warn("Error deleting template [{}], request was not acknowledged", template);
}
tryFinishUpgrade(anyUpgradeFailed);
}
@Override
public void onFailure(Exception e) {
anyUpgradeFailed.set(true);
if (e instanceof IndexTemplateMissingException == false) {
// we might attempt to delete the same template from different nodes - so that's ok if template doesn't exist
// otherwise we need to warn
LOGGER.warn(new ParameterizedMessage("Error deleting template [{}]", template), e);
}
tryFinishUpgrade(anyUpgradeFailed);
}
});
}
}
Aggregations