-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Limit size of shardDeleteResults #133558
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
base: main
Are you sure you want to change the base?
Limit size of shardDeleteResults #133558
Changes from 20 commits
9870591
97e9969
24b7a62
d888113
ee89eb2
92991b9
3190772
a16856c
381d294
203d513
daf09b6
dc70d5b
0355c2a
f072128
abb2d4c
a0d728f
654ebf2
5ef0111
bd9217b
ba81bcf
acd2182
be05a1f
8d66c1e
ce64bf5
0fa5099
3575240
d55893d
3725a3c
ed00f1a
ce6195d
37404a5
fc41d60
d1e81f7
2f0ea30
6babba9
1ff464d
0d01264
d73ffef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.common.io.stream; | ||
|
||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* Prevents writes when the max size is breached | ||
*/ | ||
public class BoundedOutputStream extends FilterOutputStream { | ||
private final int maxSize; | ||
private int size; | ||
|
||
// As soon as a write request exceeds maxSize, permit no more writes, even if there is capacity for them | ||
private boolean closed = false; | ||
|
||
public BoundedOutputStream(OutputStream out, int maxSize) { | ||
super(out); | ||
this.maxSize = maxSize; | ||
this.size = 0; | ||
} | ||
|
||
private boolean hasCapacity(int bytes) { | ||
return size + bytes <= maxSize; | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
if (closed == false && hasCapacity(1)) { | ||
super.write(b); | ||
|
||
/* | ||
We only need to increment size here as both super.write(byte[] b) and | ||
super.write(byte[] b, int off, int len) write each byte individually via this | ||
method, and we have already checked in each respective method whether we have | ||
sufficient capacity for that entire write | ||
*/ | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
size++; | ||
} else { | ||
closed = true; | ||
throw new BoundedOutputStreamFailedWriteException(); | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) throws IOException { | ||
if (closed == false && hasCapacity(b.length)) { | ||
super.write(b); | ||
} else { | ||
closed = true; | ||
throw new BoundedOutputStreamFailedWriteException(); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
if (closed == false && hasCapacity(len)) { | ||
super.write(b, off, len); | ||
} else { | ||
closed = true; | ||
throw new BoundedOutputStreamFailedWriteException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.common.io.stream; | ||
|
||
import org.elasticsearch.ElasticsearchException; | ||
|
||
/** | ||
* An exception indicating we have tried to write to the BoundedOutputStream and have exceeded capacity | ||
*/ | ||
public class BoundedOutputStreamFailedWriteException extends ElasticsearchException { | ||
public BoundedOutputStreamFailedWriteException() { | ||
super("The write failed because there is no more capacity inside the BoundedOutputStream"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,8 @@ | |
import org.elasticsearch.common.compress.DeflateCompressor; | ||
import org.elasticsearch.common.compress.NotXContentException; | ||
import org.elasticsearch.common.io.Streams; | ||
import org.elasticsearch.common.io.stream.BoundedOutputStream; | ||
import org.elasticsearch.common.io.stream.BoundedOutputStreamFailedWriteException; | ||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
import org.elasticsearch.common.io.stream.InputStreamStreamInput; | ||
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; | ||
|
@@ -1006,7 +1008,8 @@ private void createSnapshotsDeletion( | |
SnapshotsServiceUtils.minCompatibleVersion(minimumNodeVersion, originalRepositoryData, snapshotIds), | ||
originalRootBlobs, | ||
blobStore().blobContainer(indicesPath()).children(OperationPurpose.SNAPSHOT_DATA), | ||
originalRepositoryData | ||
originalRepositoryData, | ||
metadata.settings() | ||
); | ||
})); | ||
} | ||
|
@@ -1075,6 +1078,7 @@ class SnapshotsDeletion { | |
* {@link RepositoryData} blob newer than the one identified by {@link #originalRepositoryDataGeneration}. | ||
*/ | ||
private final RepositoryData originalRepositoryData; | ||
private final Settings settings; | ||
|
||
/** | ||
* Executor to use for all repository interactions. | ||
|
@@ -1096,15 +1100,16 @@ class SnapshotsDeletion { | |
/** | ||
* Tracks the shard-level blobs which can be deleted once all the metadata updates have completed. | ||
*/ | ||
private final ShardBlobsToDelete shardBlobsToDelete = new ShardBlobsToDelete(); | ||
private final ShardBlobsToDelete shardBlobsToDelete; | ||
|
||
SnapshotsDeletion( | ||
Collection<SnapshotId> snapshotIds, | ||
long originalRepositoryDataGeneration, | ||
IndexVersion repositoryFormatIndexVersion, | ||
Map<String, BlobMetadata> originalRootBlobs, | ||
Map<String, BlobContainer> originalIndexContainers, | ||
RepositoryData originalRepositoryData | ||
RepositoryData originalRepositoryData, | ||
Settings settings | ||
) { | ||
this.snapshotIds = snapshotIds; | ||
this.originalRepositoryDataGeneration = originalRepositoryDataGeneration; | ||
|
@@ -1113,6 +1118,9 @@ class SnapshotsDeletion { | |
this.originalRootBlobs = originalRootBlobs; | ||
this.originalIndexContainers = originalIndexContainers; | ||
this.originalRepositoryData = originalRepositoryData; | ||
this.settings = settings; | ||
|
||
shardBlobsToDelete = new ShardBlobsToDelete(this.settings); | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------------------------------------------- | ||
|
@@ -1477,6 +1485,7 @@ private void cleanupUnlinkedShardLevelBlobs(ActionListener<Void> listener) { | |
listener.onResponse(null); | ||
return; | ||
} | ||
|
||
snapshotExecutor.execute(ActionRunnable.wrap(listener, l -> { | ||
try { | ||
deleteFromContainer(OperationPurpose.SNAPSHOT_DATA, blobContainer(), filesToDelete); | ||
|
@@ -1666,6 +1675,7 @@ void writeTo(StreamOutput out) throws IOException { | |
} | ||
} | ||
|
||
private final int shardDeleteResultsMaxSize; | ||
/** | ||
* <p> | ||
* Shard-level results, i.e. a sequence of {@link ShardSnapshotMetaDeleteResult} objects, except serialized, concatenated, and | ||
|
@@ -1678,26 +1688,56 @@ void writeTo(StreamOutput out) throws IOException { | |
* need no further synchronization. | ||
* </p> | ||
*/ | ||
// If the size of this continues to be a problem even after compression, consider either a hard limit on its size (preferring leaked | ||
// blobs over an OOME on the master) or else offloading it to disk or to the repository itself. | ||
private final BytesStreamOutput shardDeleteResults = new ReleasableBytesStreamOutput(bigArrays); | ||
private final BytesStreamOutput shardDeleteResults; | ||
|
||
private int resultCount = 0; | ||
|
||
private final StreamOutput compressed = new OutputStreamStreamOutput( | ||
new BufferedOutputStream( | ||
new DeflaterOutputStream(Streams.flushOnCloseStream(shardDeleteResults)), | ||
DeflateCompressor.BUFFER_SIZE | ||
) | ||
); | ||
private final StreamOutput compressed; | ||
|
||
private final ArrayList<Closeable> resources = new ArrayList<>(); | ||
|
||
private final ShardGenerations.Builder shardGenerationsBuilder = ShardGenerations.builder(); | ||
|
||
ShardBlobsToDelete() { | ||
resources.add(compressed); | ||
resources.add(LeakTracker.wrap((Releasable) shardDeleteResults)); | ||
public final Setting<ByteSizeValue> MAX_SHARD_DELETE_RESULTS_SIZE_SETTING = Setting.memorySizeSetting( | ||
"repositories.blobstore.max_shard_delete_results_size", | ||
|
||
"25%", | ||
Setting.Property.NodeScope | ||
|
||
); | ||
|
||
ShardBlobsToDelete(Settings settings) { | ||
this.shardDeleteResultsMaxSize = calculateMaximumShardDeleteResultsSize(settings); | ||
if (this.shardDeleteResultsMaxSize > 0) { | ||
this.shardDeleteResults = new ReleasableBytesStreamOutput(bigArrays); | ||
this.compressed = new OutputStreamStreamOutput( | ||
new BoundedOutputStream( | ||
new BufferedOutputStream( | ||
new DeflaterOutputStream(Streams.flushOnCloseStream(shardDeleteResults)), | ||
DeflateCompressor.BUFFER_SIZE | ||
), | ||
this.shardDeleteResultsMaxSize | ||
) | ||
); | ||
resources.add(compressed); | ||
resources.add(LeakTracker.wrap((Releasable) shardDeleteResults)); | ||
} else { | ||
this.shardDeleteResults = null; | ||
this.compressed = null; | ||
} | ||
} | ||
|
||
/** | ||
* Calculates the maximum size of the shardDeleteResults BytesStreamOutput. | ||
* The size should at most be 2GB, but no more than 25% of the total remaining heap space. | ||
* A buffer of 1MB is maintained, so that even if the stream is of max size, there is room to flush | ||
* @return The maximum number of bytes the shardDeleteResults BytesStreamOutput can consume in the heap | ||
*/ | ||
int calculateMaximumShardDeleteResultsSize(Settings settings) { | ||
DaveCTurner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
long maxSizeInBytes = MAX_SHARD_DELETE_RESULTS_SIZE_SETTING.get(settings).getBytes(); | ||
int oneMBBuffer = 1024 * 1024; | ||
if (maxSizeInBytes > Integer.MAX_VALUE) { | ||
return Integer.MAX_VALUE - oneMBBuffer; | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
return (int) maxSizeInBytes; | ||
} | ||
|
||
synchronized void addShardDeleteResult( | ||
|
@@ -1706,10 +1746,24 @@ synchronized void addShardDeleteResult( | |
ShardGeneration newGeneration, | ||
Collection<String> blobsToDelete | ||
) { | ||
if (compressed == null) { | ||
// No output stream: skip writing, but still update generations | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
shardGenerationsBuilder.put(indexId, shardId, newGeneration); | ||
return; | ||
} | ||
try { | ||
shardGenerationsBuilder.put(indexId, shardId, newGeneration); | ||
new ShardSnapshotMetaDeleteResult(Objects.requireNonNull(indexId.getId()), shardId, blobsToDelete).writeTo(compressed); | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// The resultCount is only incremented after a successful complete write | ||
resultCount += 1; | ||
} catch (BoundedOutputStreamFailedWriteException ex) { | ||
logger.warn( | ||
"Failure to clean up the following dangling blobs, {}, for index {} and shard {}", | ||
blobsToDelete, | ||
indexId, | ||
shardId | ||
); | ||
} catch (IOException e) { | ||
assert false : e; // no IO actually happens here | ||
throw new UncheckedIOException(e); | ||
|
@@ -1721,6 +1775,10 @@ public ShardGenerations getUpdatedShardGenerations() { | |
} | ||
|
||
public Iterator<String> getBlobPaths() { | ||
if (compressed == null || shardDeleteResults == null) { | ||
// No output stream: nothing to return | ||
|
||
return Collections.emptyIterator(); | ||
} | ||
final StreamInput input; | ||
try { | ||
compressed.close(); | ||
|
@@ -1736,6 +1794,8 @@ public Iterator<String> getBlobPaths() { | |
throw new UncheckedIOException(e); | ||
} | ||
|
||
// Iterates through complete ShardSnapshotMetaDeleteResults written to compressed | ||
// Partially written ShardSnapshotMetaDeleteResults are dropped | ||
return Iterators.flatMap(Iterators.forRange(0, resultCount, i -> { | ||
try { | ||
return new ShardSnapshotMetaDeleteResult(input); | ||
|
@@ -1750,6 +1810,9 @@ public Iterator<String> getBlobPaths() { | |
|
||
@Override | ||
public void close() { | ||
if (resources.isEmpty()) { | ||
return; | ||
} | ||
|
||
try { | ||
IOUtils.close(resources); | ||
} catch (IOException e) { | ||
|
@@ -1760,7 +1823,7 @@ public void close() { | |
|
||
// exposed for tests | ||
int sizeInBytes() { | ||
return shardDeleteResults.size(); | ||
return shardDeleteResults == null ? 0 : shardDeleteResults.size(); | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.