Skip to content
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

Track Shard-Snapshot Index Generation at Repository Root #46250

Merged
Show file tree
Hide file tree
Changes from 159 commits
Commits
Show all changes
182 commits
Select commit Hold shift + click to select a range
e978473
bck
original-brownbear Sep 2, 2019
7afa152
sorta works
original-brownbear Sep 2, 2019
14ab8ca
better
original-brownbear Sep 2, 2019
1c9fc7f
pass
original-brownbear Sep 3, 2019
32e514f
bck
original-brownbear Sep 3, 2019
21253d8
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 3, 2019
8e896de
moar asserts
original-brownbear Sep 3, 2019
3b9a791
fix CCR repo
original-brownbear Sep 3, 2019
ac223a7
fix tests
original-brownbear Sep 3, 2019
9e87c46
fix some inconsistencies
original-brownbear Sep 3, 2019
b7e9aab
remove risky delete
original-brownbear Sep 3, 2019
9149f74
nicer
original-brownbear Sep 3, 2019
7305618
extract fallback logic
original-brownbear Sep 3, 2019
95f179a
comment
original-brownbear Sep 3, 2019
ac3180f
ensure not leaking shard index-N
original-brownbear Sep 3, 2019
a11ef41
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 3, 2019
deba084
some bwc
original-brownbear Sep 3, 2019
4e8316b
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 3, 2019
cd9fe7a
half fix bwc
original-brownbear Sep 3, 2019
0d86637
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 3, 2019
93e3f62
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 4, 2019
51e5b1a
assertion and nicer order
original-brownbear Sep 4, 2019
addcf45
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 4, 2019
8e110c9
add version param
original-brownbear Sep 4, 2019
eb51be5
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 5, 2019
79dd09a
implement BwC
original-brownbear Sep 5, 2019
3a7508e
bulk delete old index-N
original-brownbear Sep 5, 2019
a2b1a5b
fix update on empty shard list
original-brownbear Sep 5, 2019
41841e7
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 5, 2019
91f0931
bck
original-brownbear Sep 5, 2019
50fd078
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 5, 2019
77bf0a5
horrible but safe
original-brownbear Sep 5, 2019
3fe616c
drier
original-brownbear Sep 6, 2019
46c0b6e
closer
original-brownbear Sep 6, 2019
fe9cfa6
stop redundant reading of metadata
original-brownbear Sep 6, 2019
6ccc080
nicer
original-brownbear Sep 6, 2019
3788ca3
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 6, 2019
2fe0182
even safer ...
original-brownbear Sep 6, 2019
7bcd5da
much faster
original-brownbear Sep 6, 2019
a552661
safer
original-brownbear Sep 6, 2019
8145406
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 6, 2019
7513ce5
some doc improvements
original-brownbear Sep 6, 2019
5517724
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 7, 2019
3ed96ec
start extracting index gen math
original-brownbear Sep 7, 2019
8bfe736
nicer
original-brownbear Sep 8, 2019
b89040c
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 8, 2019
363e6d7
simpler
original-brownbear Sep 8, 2019
74869b6
smarter bwc
original-brownbear Sep 8, 2019
490b1ef
nicer
original-brownbear Sep 8, 2019
f123b9b
more sanity
original-brownbear Sep 8, 2019
87727ea
fix more possible inconsistencies
original-brownbear Sep 9, 2019
af14c18
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 9, 2019
bc10987
fix tests
original-brownbear Sep 9, 2019
c93b654
way moar docs
original-brownbear Sep 9, 2019
ee621f9
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 9, 2019
35ec43a
docs, better equals etc.
original-brownbear Sep 9, 2019
ce5374e
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 9, 2019
3ce0b62
update docs and add test todo
original-brownbear Sep 9, 2019
a60fce7
clarify
original-brownbear Sep 9, 2019
5d82911
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 9, 2019
f94e2ff
drier and nicer
original-brownbear Sep 9, 2019
c0bce85
shorter
original-brownbear Sep 9, 2019
8e8ad71
shorter
original-brownbear Sep 9, 2019
8382340
slightly nicer
original-brownbear Sep 9, 2019
47fab04
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 10, 2019
b6fc8c0
nicer
original-brownbear Sep 10, 2019
3880103
nicer apis more docs
original-brownbear Sep 10, 2019
03b287e
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 10, 2019
c0ebec2
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 10, 2019
d64853b
comment
original-brownbear Sep 10, 2019
c75fe5d
nicer comment
original-brownbear Sep 10, 2019
7271184
nicer
original-brownbear Sep 10, 2019
7e1288d
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 10, 2019
15c7e7a
shorter diff
original-brownbear Sep 10, 2019
2eb29ee
smaller diff
original-brownbear Sep 10, 2019
8d535ee
smaller diff
original-brownbear Sep 10, 2019
e1a7492
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 11, 2019
6252c4e
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 12, 2019
f7c0949
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 14, 2019
b3adb0b
nicer and fix stuff
original-brownbear Sep 14, 2019
1ceca5b
smaller diff
original-brownbear Sep 15, 2019
23444c1
smaller diff
original-brownbear Sep 15, 2019
214f12c
nicer diff
original-brownbear Sep 15, 2019
f831f2a
smaller diff
original-brownbear Sep 15, 2019
b5629b2
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 17, 2019
d8d0538
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 17, 2019
cc4ce1b
yet smaller diff
original-brownbear Sep 17, 2019
2f1c565
save line
original-brownbear Sep 17, 2019
0153cd3
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 18, 2019
88221b1
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 18, 2019
a4a3fd5
smaller diff
original-brownbear Sep 18, 2019
54b4fe5
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 18, 2019
2f18238
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 18, 2019
b3bfb82
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 19, 2019
929bc1a
fix typo
original-brownbear Sep 19, 2019
9b5d902
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 23, 2019
72ed551
fix
original-brownbear Sep 23, 2019
3ffd0d0
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 23, 2019
8fd05c6
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 25, 2019
24fc5ec
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Sep 25, 2019
0d95a83
shorter
original-brownbear Sep 25, 2019
c23e5c8
shorter
original-brownbear Sep 25, 2019
d6fcd19
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 1, 2019
dd81d6c
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 1, 2019
55ba2c7
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 1, 2019
e2f2d5b
align new and old path
original-brownbear Oct 1, 2019
8042c38
drier
original-brownbear Oct 1, 2019
aa85262
shorter
original-brownbear Oct 1, 2019
3a0263e
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 1, 2019
5c226d4
shorter
original-brownbear Oct 1, 2019
0f8323d
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 1, 2019
56505b9
shorter
original-brownbear Oct 1, 2019
76b7218
simpler
original-brownbear Oct 2, 2019
6bcfbbe
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 2, 2019
95d6982
simpller
original-brownbear Oct 2, 2019
2b06826
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 2, 2019
5e62251
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 3, 2019
1ed9f22
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 3, 2019
1ac6807
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 3, 2019
381f429
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 3, 2019
cbca20a
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 4, 2019
27028d5
shorter diff
original-brownbear Oct 4, 2019
c574d86
shorter
original-brownbear Oct 4, 2019
c4599fc
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 5, 2019
77c6918
more solid assertion
original-brownbear Oct 5, 2019
f8cb751
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 7, 2019
855b4fc
much shorter
original-brownbear Oct 7, 2019
18e0611
shorter
original-brownbear Oct 7, 2019
0bcdc95
shorter diff
original-brownbear Oct 7, 2019
c36ffb3
simpler
original-brownbear Oct 7, 2019
a87bc8c
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 8, 2019
8bfd187
nicer
original-brownbear Oct 8, 2019
dff329b
fix formatting
original-brownbear Oct 8, 2019
108304e
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 8, 2019
85cd324
fix BwC
original-brownbear Oct 8, 2019
69a1c92
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 8, 2019
9eb2f92
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 8, 2019
17ad546
CR comments
original-brownbear Oct 8, 2019
d084698
Rename + assert
original-brownbear Oct 8, 2019
0a82497
make it a fragment
original-brownbear Oct 8, 2019
4bed2e5
renamings
original-brownbear Oct 8, 2019
0f4b316
fix and test handling same index name for different indices
original-brownbear Oct 9, 2019
454c7d9
fix logging
original-brownbear Oct 9, 2019
52765d7
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 9, 2019
ac36fbd
flatten delete method into one
original-brownbear Oct 9, 2019
e6e74c7
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 9, 2019
673992c
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 9, 2019
98f0faf
some Cr comments
original-brownbear Oct 9, 2019
7606d02
adjust serialization
original-brownbear Oct 9, 2019
1d52fec
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 10, 2019
92860e8
Some CR comments addressed
original-brownbear Oct 10, 2019
74a2f2b
Some CR comments addressed
original-brownbear Oct 10, 2019
4a869d4
add comment
original-brownbear Oct 10, 2019
90c1df2
refactor for easier readability
original-brownbear Oct 10, 2019
b9dbd7c
Add new shard placeholder
original-brownbear Oct 10, 2019
f32bacd
still need that bwc
original-brownbear Oct 10, 2019
ec5ce04
Merge branch 'master' of https://github.com/elastic/elasticsearch int…
original-brownbear Oct 10, 2019
d827ceb
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 11, 2019
1109c26
revert needless change
original-brownbear Oct 11, 2019
3a66c79
track snapshot format in SNapshotsInProgress.Entry
original-brownbear Oct 11, 2019
b069c06
fix indent
original-brownbear Oct 11, 2019
debfa05
remove dead code
original-brownbear Oct 11, 2019
248c17b
fix indent
original-brownbear Oct 11, 2019
3162c5d
unmodifiable map
original-brownbear Oct 11, 2019
9a15067
nit empty map
original-brownbear Oct 11, 2019
eca8365
be less stupid when deleting
original-brownbear Oct 11, 2019
1dffe60
CR: correctly deal with lowering shard count on constant index name
original-brownbear Oct 11, 2019
08fbba4
address nits
original-brownbear Oct 11, 2019
dedc261
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 11, 2019
f8a6eb6
add generation to logging
original-brownbear Oct 11, 2019
3f79cd2
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 17, 2019
8ca5516
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 17, 2019
9e83cd0
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 18, 2019
eaadf31
add comment
original-brownbear Oct 18, 2019
ac34a1e
flag not version
original-brownbear Oct 18, 2019
77eeb7d
flatter ordered blobstore delete
original-brownbear Oct 18, 2019
5295322
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 21, 2019
1bdbf2c
CR: Yannicks patch
original-brownbear Oct 21, 2019
68e407c
slight change
original-brownbear Oct 21, 2019
76fa53f
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 22, 2019
a3c6f3d
CR: fix listener fallthrough
original-brownbear Oct 22, 2019
222c587
Merge remote-tracking branch 'elastic/master' into smarter-incrementa…
original-brownbear Oct 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
logger.debug("Initialized repository cleanup in cluster state for [{}][{}]", repositoryName, repositoryStateId);
threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(ActionRunnable.wrap(listener,
l -> blobStoreRepository.cleanup(
repositoryStateId, ActionListener.wrap(result -> after(null, result), e -> after(e, null)))));
repositoryStateId, newState.nodes().getMinNodeVersion(),
ActionListener.wrap(result -> after(null, result), e -> after(e, null)))));
}

private void after(@Nullable Exception failure, @Nullable RepositoryCleanupResult result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ public RepositoryData getRepositoryData() {
}

@Override
public void finalizeSnapshot(SnapshotId snapshotId, List<IndexId> indices, long startTime, String failure, int totalShards,
List<SnapshotShardFailure> shardFailures, long repositoryStateId, boolean includeGlobalState,
MetaData metaData, Map<String, Object> userMetadata, ActionListener<SnapshotInfo> listener) {
in.finalizeSnapshot(snapshotId, indices, startTime, failure, totalShards, shardFailures, repositoryStateId,
includeGlobalState, metaData, userMetadata, listener);
public void finalizeSnapshot(SnapshotId snapshotId, ShardGenerations shardGenerations, long startTime, String failure,
int totalShards, List<SnapshotShardFailure> shardFailures, long repositoryStateId,
original-brownbear marked this conversation as resolved.
Show resolved Hide resolved
boolean includeGlobalState, MetaData metaData, Map<String, Object> userMetadata,
Version version, ActionListener<SnapshotInfo> listener) {
in.finalizeSnapshot(snapshotId, shardGenerations, startTime, failure, totalShards, shardFailures, repositoryStateId,
includeGlobalState, metaData, userMetadata, version, listener);
}

@Override
public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, ActionListener<Void> listener) {
in.deleteSnapshot(snapshotId, repositoryStateId, listener);
public void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, Version version, ActionListener<Void> listener) {
in.deleteSnapshot(snapshotId, repositoryStateId, version, listener);
}

@Override
Expand Down Expand Up @@ -118,8 +119,9 @@ public boolean isReadOnly() {

@Override
public void snapshotShard(Store store, MapperService mapperService, SnapshotId snapshotId, IndexId indexId,
IndexCommit snapshotIndexCommit, IndexShardSnapshotStatus snapshotStatus, ActionListener<String> listener) {
in.snapshotShard(store, mapperService, snapshotId, indexId, snapshotIndexCommit, snapshotStatus, listener);
IndexCommit snapshotIndexCommit, IndexShardSnapshotStatus snapshotStatus, Version version,
ActionListener<String> listener) {
in.snapshotShard(store, mapperService, snapshotId, indexId, snapshotIndexCommit, snapshotStatus, version, listener);
}
@Override
public void restoreShard(Store store, SnapshotId snapshotId,
Expand Down
33 changes: 19 additions & 14 deletions server/src/main/java/org/elasticsearch/repositories/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,33 @@ default Repository create(RepositoryMetaData metaData, Function<String, Reposito
* <p>
* This method is called on master after all shards are snapshotted.
*
* @param snapshotId snapshot id
* @param indices list of indices in the snapshot
* @param startTime start time of the snapshot
* @param failure global failure reason or null
* @param totalShards total number of shards
* @param shardFailures list of shard failures
* @param repositoryStateId the unique id identifying the state of the repository when the snapshot began
* @param snapshotId snapshot id
* @param shardGenerations updated shard generations
* @param startTime start time of the snapshot
* @param failure global failure reason or null
* @param totalShards total number of shards
* @param shardFailures list of shard failures
* @param repositoryStateId the unique id identifying the state of the repository when the snapshot began
* @param includeGlobalState include cluster global state
* @param clusterMetaData cluster metadata
* @param userMetadata user metadata
* @param version minimum ES version the repository should be readable by
* @param listener listener to be called on completion of the snapshot
*/
void finalizeSnapshot(SnapshotId snapshotId, List<IndexId> indices, long startTime, String failure, int totalShards,
List<SnapshotShardFailure> shardFailures, long repositoryStateId, boolean includeGlobalState,
MetaData clusterMetaData, Map<String, Object> userMetadata, ActionListener<SnapshotInfo> listener);
void finalizeSnapshot(SnapshotId snapshotId, ShardGenerations shardGenerations, long startTime, String failure,
int totalShards, List<SnapshotShardFailure> shardFailures, long repositoryStateId,
boolean includeGlobalState, MetaData clusterMetaData, Map<String, Object> userMetadata,
Version version, ActionListener<SnapshotInfo> listener);

/**
* Deletes snapshot
*
* @param snapshotId snapshot id
* @param snapshotId snapshot id
* @param repositoryStateId the unique id identifying the state of the repository when the snapshot deletion began
* @param listener completion listener
* @param version minimum ES version the repository should be readable by
* @param listener completion listener
*/
void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, ActionListener<Void> listener);
void deleteSnapshot(SnapshotId snapshotId, long repositoryStateId, Version version, ActionListener<Void> listener);

/**
* Returns snapshot throttle time in nanoseconds
Expand Down Expand Up @@ -196,7 +201,7 @@ void finalizeSnapshot(SnapshotId snapshotId, List<IndexId> indices, long startTi
* @param listener listener invoked on completion
*/
void snapshotShard(Store store, MapperService mapperService, SnapshotId snapshotId, IndexId indexId, IndexCommit snapshotIndexCommit,
IndexShardSnapshotStatus snapshotStatus, ActionListener<String> listener);
IndexShardSnapshotStatus snapshotStatus, Version version, ActionListener<String> listener);

/**
* Restores snapshot of the shard.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.snapshots.SnapshotId;
import org.elasticsearch.snapshots.SnapshotState;
import org.elasticsearch.snapshots.SnapshotsService;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -55,7 +58,7 @@ public final class RepositoryData {
* An instance initialized for an empty repository.
*/
public static final RepositoryData EMPTY = new RepositoryData(EMPTY_REPO_GEN,
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), ShardGenerations.EMPTY);

/**
* The generational id of the index file from which the repository data was read.
Expand All @@ -78,19 +81,30 @@ public final class RepositoryData {
*/
private final Map<IndexId, Set<SnapshotId>> indexSnapshots;

/**
* Shard generations.
*/
private final ShardGenerations shardGenerations;

public RepositoryData(long genId, Map<String, SnapshotId> snapshotIds, Map<String, SnapshotState> snapshotStates,
Map<IndexId, Set<SnapshotId>> indexSnapshots) {
Map<IndexId, Set<SnapshotId>> indexSnapshots, ShardGenerations shardGenerations) {
this.genId = genId;
this.snapshotIds = Collections.unmodifiableMap(snapshotIds);
this.snapshotStates = Collections.unmodifiableMap(snapshotStates);
this.indices = Collections.unmodifiableMap(indexSnapshots.keySet().stream()
.collect(Collectors.toMap(IndexId::getName, Function.identity())));
this.indexSnapshots = Collections.unmodifiableMap(indexSnapshots);
this.shardGenerations = Objects.requireNonNull(shardGenerations);
assert indices.values().containsAll(shardGenerations.indices()) : "ShardGenerations contained indices "
+ shardGenerations.indices() + " but snapshots only reference indices " + indices.values();
}

protected RepositoryData copy() {
return new RepositoryData(genId, snapshotIds, snapshotStates, indexSnapshots);
return new RepositoryData(genId, snapshotIds, snapshotStates, indexSnapshots, shardGenerations);
}

public ShardGenerations shardGenerations() {
return shardGenerations;
}

/**
Expand Down Expand Up @@ -140,10 +154,15 @@ public List<IndexId> indicesToUpdateAfterRemovingSnapshot(SnapshotId snapshotId)
/**
* Add a snapshot and its indices to the repository; returns a new instance. If the snapshot
* already exists in the repository data, this method throws an IllegalArgumentException.
*
* @param snapshotId Id of the new snapshot
* @param snapshotState State of the new snapshot
* @param shardGenerations Updated shard generations in the new snapshot. For each index contained in the snapshot an array of new
* generations indexed by the shard id they correspond to must be supplied.
*/
public RepositoryData addSnapshot(final SnapshotId snapshotId,
final SnapshotState snapshotState,
final List<IndexId> snapshottedIndices) {
final ShardGenerations shardGenerations) {
if (snapshotIds.containsKey(snapshotId.getUUID())) {
// if the snapshot id already exists in the repository data, it means an old master
// that is blocked from the cluster is trying to finalize a snapshot concurrently with
Expand All @@ -155,10 +174,11 @@ public RepositoryData addSnapshot(final SnapshotId snapshotId,
Map<String, SnapshotState> newSnapshotStates = new HashMap<>(snapshotStates);
newSnapshotStates.put(snapshotId.getUUID(), snapshotState);
Map<IndexId, Set<SnapshotId>> allIndexSnapshots = new HashMap<>(indexSnapshots);
for (final IndexId indexId : snapshottedIndices) {
for (final IndexId indexId : shardGenerations.indices()) {
allIndexSnapshots.computeIfAbsent(indexId, k -> new LinkedHashSet<>()).add(snapshotId);
}
return new RepositoryData(genId, snapshots, newSnapshotStates, allIndexSnapshots);
return new RepositoryData(genId, snapshots, newSnapshotStates, allIndexSnapshots,
ShardGenerations.builder().putAll(this.shardGenerations).putAll(shardGenerations).build());
}

/**
Expand All @@ -171,13 +191,18 @@ public RepositoryData withGenId(long newGeneration) {
if (newGeneration == genId) {
return this;
}
return new RepositoryData(newGeneration, this.snapshotIds, this.snapshotStates, this.indexSnapshots);
return new RepositoryData(newGeneration, this.snapshotIds, this.snapshotStates, this.indexSnapshots, this.shardGenerations);
}

/**
* Remove a snapshot and remove any indices that no longer exist in the repository due to the deletion of the snapshot.
*
* @param snapshotId Snapshot Id
* @param updatedShardGenerations Shard generations that changed as a result of removing the snapshot.
* The {@code String[]} passed for each {@link IndexId} contains the new shard generation id for each
* changed shard indexed by its shardId
*/
public RepositoryData removeSnapshot(final SnapshotId snapshotId) {
public RepositoryData removeSnapshot(final SnapshotId snapshotId, final ShardGenerations updatedShardGenerations) {
Map<String, SnapshotId> newSnapshotIds = snapshotIds.values().stream()
.filter(id -> !snapshotId.equals(id))
.collect(Collectors.toMap(SnapshotId::getUUID, Function.identity()));
Expand Down Expand Up @@ -205,7 +230,10 @@ public RepositoryData removeSnapshot(final SnapshotId snapshotId) {
indexSnapshots.put(indexId, set);
}

return new RepositoryData(genId, newSnapshotIds, newSnapshotStates, indexSnapshots);
return new RepositoryData(genId, newSnapshotIds, newSnapshotStates, indexSnapshots,
ShardGenerations.builder().putAll(shardGenerations).putAll(updatedShardGenerations)
.retainIndices(indexSnapshots.keySet()).build()
);
}

/**
Expand All @@ -231,12 +259,13 @@ public boolean equals(Object obj) {
return snapshotIds.equals(that.snapshotIds)
&& snapshotStates.equals(that.snapshotStates)
&& indices.equals(that.indices)
&& indexSnapshots.equals(that.indexSnapshots);
&& indexSnapshots.equals(that.indexSnapshots)
&& shardGenerations.equals(that.shardGenerations);
}

@Override
public int hashCode() {
return Objects.hash(snapshotIds, snapshotStates, indices, indexSnapshots);
return Objects.hash(snapshotIds, snapshotStates, indices, indexSnapshots, shardGenerations);
}

/**
Expand Down Expand Up @@ -284,6 +313,7 @@ public List<IndexId> resolveNewIndices(final List<String> indicesToResolve) {
return snapshotIndices;
}

private static final String SHARD_GENERATIONS = "shard_generations";
private static final String SNAPSHOTS = "snapshots";
private static final String INDICES = "indices";
private static final String INDEX_ID = "id";
Expand All @@ -294,7 +324,11 @@ public List<IndexId> resolveNewIndices(final List<String> indicesToResolve) {
/**
* Writes the snapshots metadata and the related indices metadata to x-content.
*/
public XContentBuilder snapshotsToXContent(final XContentBuilder builder) throws IOException {
public XContentBuilder snapshotsToXContent(final XContentBuilder builder, final Version version) throws IOException {
original-brownbear marked this conversation as resolved.
Show resolved Hide resolved
final boolean shouldWriteShardGens = version.onOrAfter(SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION);
assert shouldWriteShardGens || shardGenerations.indices().isEmpty() :
"Should not build shard generations in BwC mode but saw generations [" + shardGenerations + "]";

builder.startObject();
// write the snapshots list
builder.startArray(SNAPSHOTS);
Expand All @@ -320,6 +354,13 @@ public XContentBuilder snapshotsToXContent(final XContentBuilder builder) throws
builder.value(snapshotId.getUUID());
}
builder.endArray();
if (shouldWriteShardGens) {
builder.startArray(SHARD_GENERATIONS);
for (String gen : shardGenerations.getGens(indexId)) {
builder.value(gen);
}
builder.endArray();
}
builder.endObject();
}
builder.endObject();
Expand All @@ -334,6 +375,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
final Map<String, SnapshotId> snapshots = new HashMap<>();
final Map<String, SnapshotState> snapshotStates = new HashMap<>();
final Map<IndexId, Set<SnapshotId>> indexSnapshots = new HashMap<>();
final ShardGenerations.Builder shardGenerations = ShardGenerations.builder();

if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
while (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
Expand Down Expand Up @@ -379,6 +421,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
final String indexName = parser.currentName();
final Set<SnapshotId> snapshotIds = new LinkedHashSet<>();
final List<String> gens = new ArrayList<>();

IndexId indexId = null;
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
Expand Down Expand Up @@ -421,10 +464,20 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
+ " references an unknown snapshot uuid [" + uuid + "]");
}
}
} else if (SHARD_GENERATIONS.equals(indexMetaFieldName)) {
XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_ARRAY, parser.currentToken(), parser::getTokenLocation);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
gens.add(parser.textOrNull());
}

}
}
assert indexId != null;
indexSnapshots.put(indexId, snapshotIds);
for (int i = 0; i < gens.size(); i++) {
shardGenerations.put(indexId, i, gens.get(i));
}
}
} else {
throw new ElasticsearchParseException("unknown field name [" + field + "]");
Expand All @@ -433,7 +486,7 @@ public static RepositoryData snapshotsFromXContent(final XContentParser parser,
} else {
throw new ElasticsearchParseException("start object expected");
}
return new RepositoryData(genId, snapshots, snapshotStates, indexSnapshots);
return new RepositoryData(genId, snapshots, snapshotStates, indexSnapshots, shardGenerations.build());
}

}
Loading