-
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 5 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 |
---|---|---|
|
@@ -151,6 +151,9 @@ | |
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.UncheckedIOException; | ||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.lang.management.MemoryUsage; | ||
import java.nio.file.NoSuchFileException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
@@ -1477,6 +1480,7 @@ private void cleanupUnlinkedShardLevelBlobs(ActionListener<Void> listener) { | |
listener.onResponse(null); | ||
return; | ||
} | ||
|
||
snapshotExecutor.execute(ActionRunnable.wrap(listener, l -> { | ||
try { | ||
deleteFromContainer(OperationPurpose.SNAPSHOT_DATA, blobContainer(), filesToDelete); | ||
|
@@ -1678,26 +1682,66 @@ 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)); | ||
int maxSizeOfShardDeleteResults = calculateMaximumShardDeleteResultsSize(); | ||
|
||
if (maxSizeOfShardDeleteResults > 0) { | ||
this.shardDeleteResults = new ReleasableBytesStreamOutput(bigArrays, maxSizeOfShardDeleteResults); | ||
this.compressed = new OutputStreamStreamOutput( | ||
new BufferedOutputStream( | ||
new DeflaterOutputStream(Streams.flushOnCloseStream(shardDeleteResults)), | ||
DeflateCompressor.BUFFER_SIZE | ||
) | ||
); | ||
resources.add(compressed); | ||
resources.add(LeakTracker.wrap((Releasable) shardDeleteResults)); | ||
} else { | ||
this.shardDeleteResults = null; | ||
this.compressed = null; | ||
} | ||
|
||
} | ||
|
||
MemoryMXBean getMemoryMXBean() { | ||
return ManagementFactory.getMemoryMXBean(); | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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 | ||
* @return The maximum number of bytes the shardDeleteResults BytesStreamOutput can consume in the heap | ||
*/ | ||
int calculateMaximumShardDeleteResultsSize() { | ||
MemoryMXBean memoryBean = getMemoryMXBean(); | ||
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); | ||
|
||
long heapUsageInBytes = heapUsage.getUsed(); | ||
long maxHeapUsageInBytes = heapUsage.getMax(); | ||
|
||
// Undefined heap | ||
if (maxHeapUsageInBytes == -1) { | ||
logger.warn("The heap is undefined when attempting to instantiate shardDeleteResults"); | ||
return 0; | ||
} | ||
|
||
long heapSpaceLeftInBytes = maxHeapUsageInBytes - heapUsageInBytes; | ||
double maxPercentageHeapSpaceAllocation = 0.25; | ||
long heapSpaceLeftToBeUtilisedInBytes = (long) Math.floor(heapSpaceLeftInBytes * maxPercentageHeapSpaceAllocation); | ||
if (heapSpaceLeftToBeUtilisedInBytes > Integer.MAX_VALUE) { | ||
return Integer.MAX_VALUE; | ||
} else { | ||
return (int) heapSpaceLeftInBytes; | ||
} | ||
} | ||
|
||
synchronized void addShardDeleteResult( | ||
|
@@ -1706,6 +1750,11 @@ 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
|
||
|
@@ -1714,13 +1763,18 @@ synchronized void addShardDeleteResult( | |
assert false : e; // no IO actually happens here | ||
throw new UncheckedIOException(e); | ||
} | ||
// TODO - Catch the out of memory error | ||
DaveCTurner marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
public ShardGenerations getUpdatedShardGenerations() { | ||
return shardGenerationsBuilder.build(); | ||
} | ||
|
||
public Iterator<String> getBlobPaths() { | ||
if (compressed == null || shardDeleteResults == null) { | ||
// No output stream: nothing to return | ||
|
||
return Collections.emptyIterator(); | ||
} | ||
final StreamInput input; | ||
try { | ||
compressed.close(); | ||
|
@@ -1750,6 +1804,9 @@ public Iterator<String> getBlobPaths() { | |
|
||
@Override | ||
public void close() { | ||
if (resources.isEmpty()) { | ||
return; | ||
} | ||
|
||
try { | ||
IOUtils.close(resources); | ||
} catch (IOException e) { | ||
|
@@ -1760,7 +1817,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.