Skip to content

Commit 367aa48

Browse files
committed
Do not checksum all bytes at once in plugin install (#44649)
Today when checksumming a plugin zip during plugin install, we read all of the bytes of the zip into memory at once. When trying to run the plugin installer on a small heap (say, 64 MiB), this can lead to the plugin installer running out of memory when checksumming large plugins. This commit addresses this by reading the plugin bytes in 8 KiB chunks, thus using a constant amount of memory independent of the size of the plugin.
1 parent c29a31e commit 367aa48

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java

+17-8
Original file line numberDiff line numberDiff line change
@@ -506,17 +506,26 @@ private Path downloadAndValidate(
506506
}
507507
}
508508

509-
try {
510-
final byte[] zipBytes = Files.readAllBytes(zip);
511-
final String actualChecksum = MessageDigests.toHexString(MessageDigest.getInstance(digestAlgo).digest(zipBytes));
512-
if (expectedChecksum.equals(actualChecksum) == false) {
513-
throw new UserException(
509+
// read the bytes of the plugin zip in chunks to avoid out of memory errors
510+
try (InputStream zis = Files.newInputStream(zip)) {
511+
try {
512+
final MessageDigest digest = MessageDigest.getInstance(digestAlgo);
513+
final byte[] bytes = new byte[8192];
514+
int read;
515+
while ((read = zis.read(bytes)) != -1) {
516+
assert read > 0 : read;
517+
digest.update(bytes, 0, read);
518+
}
519+
final String actualChecksum = MessageDigests.toHexString(digest.digest());
520+
if (expectedChecksum.equals(actualChecksum) == false) {
521+
throw new UserException(
514522
ExitCodes.IO_ERROR,
515523
digestAlgo + " mismatch, expected " + expectedChecksum + " but got " + actualChecksum);
524+
}
525+
} catch (final NoSuchAlgorithmException e) {
526+
// this should never happen as we are using SHA-1 and SHA-512 here
527+
throw new AssertionError(e);
516528
}
517-
} catch (final NoSuchAlgorithmException e) {
518-
// this should never happen as we are using SHA-1 and SHA-512 here
519-
throw new AssertionError(e);
520529
}
521530

522531
if (officialPlugin) {

0 commit comments

Comments
 (0)