-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add a java level freeze/unfreeze API #35353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2c825f5
Add a java level freeze/unfreeze API
s1monw b41dfb7
fix compilation
s1monw 492700c
Merge branch 'master' into issues/34352/integrate
s1monw 6dbc9cd
Add tests that ensures we never access more than one shard concurrently
s1monw 8105e62
split out opening the index from freeze action
s1monw 00839c8
cleanup
s1monw 695dc97
Merge branch 'master' into issues/34352/integrate
s1monw 1768a85
apply feedback
s1monw fbcf410
Merge branch 'master' into issues/34352/integrate
s1monw cf11d24
Merge branch 'master' into issues/34352/integrate
s1monw 6ac35a3
apply feedback
s1monw 1a1ea68
apply feedback
s1monw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
221 changes: 221 additions & 0 deletions
221
...in/core/src/main/java/org/elasticsearch/xpack/core/action/TransportFreezeIndexAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.action; | ||
|
||
import org.elasticsearch.ResourceNotFoundException; | ||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.IndicesRequest; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.DestructiveOperations; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.master.AcknowledgedRequest; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
import org.elasticsearch.cluster.AckedClusterStateUpdateTask; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.block.ClusterBlockException; | ||
import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
import org.elasticsearch.cluster.block.ClusterBlocks; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.metadata.MetaData; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.Priority; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.CollectionUtils; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.IndexSettings; | ||
import org.elasticsearch.index.engine.FrozenEngine; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
|
||
public final class TransportFreezeIndexAction extends | ||
TransportMasterNodeAction<TransportFreezeIndexAction.FreezeRequest, AcknowledgedResponse> { | ||
|
||
private final DestructiveOperations destructiveOperations; | ||
|
||
@Inject | ||
public TransportFreezeIndexAction(TransportService transportService, ClusterService clusterService, | ||
ThreadPool threadPool, ActionFilters actionFilters, | ||
IndexNameExpressionResolver indexNameExpressionResolver, | ||
DestructiveOperations destructiveOperations) { | ||
super(FreezeIndexAction.NAME, transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver, | ||
FreezeRequest::new); | ||
this.destructiveOperations = destructiveOperations; | ||
} | ||
@Override | ||
protected String executor() { | ||
return ThreadPool.Names.SAME; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, FreezeRequest request, ActionListener<AcknowledgedResponse> listener) { | ||
destructiveOperations.failDestructive(request.indices()); | ||
super.doExecute(task, request, listener); | ||
} | ||
|
||
@Override | ||
protected AcknowledgedResponse newResponse() { | ||
return new AcknowledgedResponse(); | ||
} | ||
|
||
@Override | ||
protected void masterOperation(FreezeRequest request, ClusterState state, ActionListener<AcknowledgedResponse> listener) { | ||
final Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request); | ||
if (concreteIndices == null || concreteIndices.length == 0) { | ||
throw new ResourceNotFoundException("index not found"); | ||
} | ||
|
||
clusterService.submitStateUpdateTask("toggle-frozen-settings", | ||
new AckedClusterStateUpdateTask<AcknowledgedResponse>(Priority.URGENT, request, listener) { | ||
@Override | ||
public ClusterState execute(final ClusterState currentState) { | ||
final MetaData.Builder builder = MetaData.builder(currentState.metaData()); | ||
ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks()); | ||
for (Index index : concreteIndices) { | ||
IndexMetaData meta = currentState.metaData().getIndexSafe(index); | ||
if (meta.getState() != IndexMetaData.State.CLOSE) { | ||
throw new IllegalStateException("index [" + index.getName() + "] is not closed"); | ||
} | ||
final IndexMetaData.Builder imdBuilder = IndexMetaData.builder(meta); | ||
final Settings.Builder settingsBuilder = | ||
Settings.builder() | ||
.put(currentState.metaData().index(index).getSettings()) | ||
.put("index.blocks.write", request.freeze()) | ||
.put(FrozenEngine.INDEX_FROZEN.getKey(), request.freeze()) | ||
.put(IndexSettings.INDEX_SEARCH_THROTTLED.getKey(), request.freeze()); | ||
if (request.freeze()) { | ||
blocks.addIndexBlock(index.getName(), IndexMetaData.INDEX_WRITE_BLOCK); | ||
// we never remove this block when unfreeze for now. we don't know if it was read-only | ||
s1monw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
imdBuilder.settings(settingsBuilder); | ||
builder.put(imdBuilder.build(), true); | ||
} | ||
return ClusterState.builder(currentState).blocks(blocks).metaData(builder).build(); | ||
} | ||
|
||
@Override | ||
protected AcknowledgedResponse newResponse(boolean acknowledged) { | ||
return new AcknowledgedResponse(acknowledged); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected ClusterBlockException checkBlock(FreezeRequest request, ClusterState state) { | ||
return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, | ||
indexNameExpressionResolver.concreteIndexNames(state, request)); | ||
} | ||
|
||
public static class FreezeIndexAction extends Action<AcknowledgedResponse> { | ||
|
||
public static final FreezeIndexAction INSTANCE = new FreezeIndexAction(); | ||
public static final String NAME = "indices:admin/freeze"; | ||
|
||
private FreezeIndexAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public AcknowledgedResponse newResponse() { | ||
return new AcknowledgedResponse(); | ||
} | ||
} | ||
|
||
public static class FreezeRequest extends AcknowledgedRequest<FreezeRequest> | ||
implements IndicesRequest.Replaceable { | ||
private String[] indices; | ||
private boolean freeze = true; | ||
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, false, true); | ||
|
||
|
||
s1monw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public FreezeRequest(String... indices) { | ||
this.indices = indices; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException validationException = null; | ||
if (CollectionUtils.isEmpty(indices)) { | ||
validationException = addValidationError("index is missing", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
|
||
s1monw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void setFreeze(boolean freeze) { | ||
this.freeze = freeze; | ||
} | ||
|
||
public boolean freeze() { | ||
return freeze; | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
indicesOptions = IndicesOptions.readIndicesOptions(in); | ||
indices = in.readStringArray(); | ||
freeze = in.readBoolean(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
indicesOptions.writeIndicesOptions(out); | ||
out.writeStringArray(indices); | ||
out.writeBoolean(freeze); | ||
} | ||
|
||
/** | ||
* The indices to be opened | ||
* @return the indices to be opened | ||
s1monw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
@Override | ||
public String[] indices() { | ||
return indices; | ||
} | ||
|
||
/** | ||
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions. | ||
* For example indices that don't exist. | ||
* | ||
* @return the current behaviour when it comes to index names and wildcard indices expressions | ||
*/ | ||
@Override | ||
public IndicesOptions indicesOptions() { | ||
return indicesOptions; | ||
} | ||
|
||
/** | ||
* Specifies what type of requested indices to ignore and how to deal with wildcard expressions. | ||
* For example indices that don't exist. | ||
* | ||
* @param indicesOptions the desired behaviour regarding indices to ignore and wildcard indices expressions | ||
* @return the request itself | ||
*/ | ||
public FreezeRequest indicesOptions(IndicesOptions indicesOptions) { | ||
this.indicesOptions = indicesOptions; | ||
return this; | ||
} | ||
|
||
@Override | ||
public IndicesRequest indices(String... indices) { | ||
this.indices = indices; | ||
return this; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.