-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Create first backing index when creating data stream #54467
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
danhermann
merged 10 commits into
elastic:master
from
danhermann:create_first_backing_index
Apr 2, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0ae87c5
create first backing index
danhermann 806acc4
update unit test
danhermann 078dae1
temporarily restrict YML test
danhermann 1284b2b
wip on review comments
danhermann df1bf05
finish review comments and tests
danhermann 47e6b88
fix test
danhermann 0bd4bd4
wip on review comments
danhermann 6c1856f
review comments
danhermann fe2849d
Merge branch 'master' into create_first_backing_index
elasticmachine 9c9a42c
fix test
danhermann 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,6 +24,7 @@ | |||||
import org.elasticsearch.action.ActionRequestValidationException; | ||||||
import org.elasticsearch.action.ActionType; | ||||||
import org.elasticsearch.action.ValidateActions; | ||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest; | ||||||
import org.elasticsearch.action.support.ActionFilters; | ||||||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||||||
import org.elasticsearch.action.support.master.MasterNodeRequest; | ||||||
|
@@ -33,6 +34,7 @@ | |||||
import org.elasticsearch.cluster.block.ClusterBlockException; | ||||||
import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||||||
import org.elasticsearch.cluster.metadata.DataStream; | ||||||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||||||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||||||
import org.elasticsearch.cluster.metadata.Metadata; | ||||||
import org.elasticsearch.cluster.metadata.MetadataCreateIndexService; | ||||||
|
@@ -42,13 +44,14 @@ | |||||
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.unit.TimeValue; | ||||||
import org.elasticsearch.tasks.Task; | ||||||
import org.elasticsearch.threadpool.ThreadPool; | ||||||
import org.elasticsearch.transport.TransportService; | ||||||
|
||||||
import java.io.IOException; | ||||||
import java.util.Collections; | ||||||
import java.util.List; | ||||||
import java.util.Objects; | ||||||
|
||||||
public class CreateDataStreamAction extends ActionType<AcknowledgedResponse> { | ||||||
|
@@ -117,10 +120,14 @@ public int hashCode() { | |||||
|
||||||
public static class TransportAction extends TransportMasterNodeAction<Request, AcknowledgedResponse> { | ||||||
|
||||||
private final MetadataCreateIndexService metadataCreateIndexService; | ||||||
|
||||||
@Inject | ||||||
public TransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, | ||||||
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { | ||||||
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, | ||||||
MetadataCreateIndexService metaDataCreateIndexService) { | ||||||
super(NAME, transportService, clusterService, threadPool, actionFilters, Request::new, indexNameExpressionResolver); | ||||||
this.metadataCreateIndexService = metaDataCreateIndexService; | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
@@ -151,7 +158,7 @@ public void onFailure(String source, Exception e) { | |||||
|
||||||
@Override | ||||||
public ClusterState execute(ClusterState currentState) throws Exception { | ||||||
return createDataStream(currentState, request); | ||||||
return createDataStream(metadataCreateIndexService, currentState, request); | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
@@ -161,16 +168,26 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS | |||||
}); | ||||||
} | ||||||
|
||||||
static ClusterState createDataStream(ClusterState currentState, Request request) { | ||||||
static ClusterState createDataStream(MetadataCreateIndexService metadataCreateIndexService, | ||||||
ClusterState currentState, | ||||||
Request request) throws Exception { | ||||||
if (currentState.metadata().dataStreams().containsKey(request.name)) { | ||||||
throw new IllegalArgumentException("data_stream [" + request.name + "] already exists"); | ||||||
} | ||||||
|
||||||
MetadataCreateIndexService.validateIndexOrAliasName(request.name, | ||||||
(s1, s2) -> new IllegalArgumentException("data_stream [" + s1 + "] " + s2)); | ||||||
|
||||||
String firstBackingIndexName = request.name + "-000001"; | ||||||
CreateIndexClusterStateUpdateRequest createIndexRequest = | ||||||
martijnvg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
new CreateIndexClusterStateUpdateRequest("initialize_data_stream", firstBackingIndexName, firstBackingIndexName) | ||||||
.settings(Settings.builder().put("index.hidden", true).build()); | ||||||
currentState = metadataCreateIndexService.applyCreateIndexRequest(currentState, createIndexRequest, false); | ||||||
IndexMetadata firstBackingIndex = currentState.metadata().index(firstBackingIndexName); | ||||||
assert firstBackingIndex != null; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add:
Suggested change
since that is guaranteed to fail tests (the NPE occurring further down could be swallowed). |
||||||
Metadata.Builder builder = Metadata.builder(currentState.metadata()).put( | ||||||
new DataStream(request.name, request.timestampFieldName, Collections.emptyList())); | ||||||
new DataStream(request.name, request.timestampFieldName, List.of(firstBackingIndex.getIndex()))); | ||||||
logger.info("adding data stream [{}]", request.name); | ||||||
return ClusterState.builder(currentState).metadata(builder).build(); | ||||||
} | ||||||
|
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 |
---|---|---|
|
@@ -56,7 +56,7 @@ public interface IndexAbstraction { | |
|
||
/** | ||
* A write index is a dedicated concrete index, that accepts all the new documents that belong to an index abstraction. | ||
* | ||
* <p> | ||
* A write index may also be a regular concrete index of a index abstraction and may therefore also be returned | ||
* by {@link #getIndices()}. An index abstraction may also not have a dedicated write index. | ||
* | ||
|
@@ -87,7 +87,14 @@ enum Type { | |
* An alias typically refers to many concrete indices and | ||
* may have a write index. | ||
*/ | ||
ALIAS("alias"); | ||
ALIAS("alias"), | ||
|
||
/** | ||
* An index abstraction that refers to a data stream. | ||
* A data stream typically has multiple backing indices, the latest of which | ||
* is the target for index requests. | ||
*/ | ||
DATA_STREAM("data_stream"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add some java doc here? |
||
|
||
private final String displayName; | ||
|
||
|
@@ -181,7 +188,7 @@ public boolean isHidden() { | |
|
||
/** | ||
* Returns the unique alias metadata per concrete index. | ||
* | ||
* <p> | ||
* (note that although alias can point to the same concrete indices, each alias reference may have its own routing | ||
* and filters) | ||
*/ | ||
|
@@ -233,7 +240,7 @@ public void computeAndValidateAliasProperties() { | |
|
||
// Validate hidden status | ||
final Map<Boolean, List<IndexMetadata>> groupedByHiddenStatus = referenceIndexMetadatas.stream() | ||
.collect(Collectors.groupingBy(idxMeta -> Boolean.TRUE.equals(idxMeta.getAliases().get(aliasName).isHidden()))); | ||
.collect(Collectors.groupingBy(idxMeta -> Boolean.TRUE.equals(idxMeta.getAliases().get(aliasName).isHidden()))); | ||
if (isNonEmpty(groupedByHiddenStatus.get(true)) && isNonEmpty(groupedByHiddenStatus.get(false))) { | ||
List<String> hiddenOn = groupedByHiddenStatus.get(true).stream() | ||
.map(idx -> idx.getIndex().getName()).collect(Collectors.toList()); | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can also the index name be checked here inside the indices array field?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@martijnvg, it looks like the format of the backing indices response changed when the data stream's
indices
member was changed fromList<String>
toList<Index>
. It now looks like this:Is that ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is intended. We can add this assert:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never mind my comment, you already made this change :)