Skip to content

Commit

Permalink
fix: GCP Core deployment start to fail after some time #472 (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay authored Sep 6, 2024
1 parent c871022 commit a4aaec0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/main/java/com/epam/aidial/core/util/Compression.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,16 @@ public byte[] compress(String type, byte[] data) {
}

@SneakyThrows
public InputStream decompress(String type, InputStream input) {
public byte[] decompress(String type, byte[] input) {
if (!type.equals("gzip")) {
throw new IllegalArgumentException("Unsupported compression: " + type);
}

try {
return new GZIPInputStream(input);
try (InputStream decompressed = new GZIPInputStream(new ByteArrayInputStream(input))) {
return decompressed.readAllBytes();
} catch (ZipException e) {
// special case for GCP cloud storage, due to jclouds bug https://issues.apache.org/jira/projects/JCLOUDS/issues/JCLOUDS-1633
log.warn("Failed to decompress provided input: {}", e.getMessage());
return input;
}
}

@SneakyThrows
public byte[] decompress(String type, byte[] input) {
try (InputStream decompressed = decompress(type, new ByteArrayInputStream(input))) {
return decompressed.readAllBytes();
}
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/epam/aidial/core/util/CompressionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.epam.aidial.core.util;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CompressionTest {

@Test
public void testNormalFlow() {
byte[] content = "Hello world!".getBytes();

assertThrows(IllegalArgumentException.class, () -> Compression.compress("wrong", content));

byte[] compressed = Compression.compress("gzip", content);

assertNotNull(compressed);
assertTrue(compressed.length > 0);
assertThrows(IllegalArgumentException.class, () -> Compression.decompress("wrong", compressed));

byte[] actual = Compression.decompress("gzip", compressed);

assertArrayEquals(content, actual);
}

@Test
public void testGcpWorkaround() {
byte[] content = "Hello world!".getBytes();

byte[] actual = Compression.decompress("gzip", content);

assertEquals(content, actual);
}
}

0 comments on commit a4aaec0

Please sign in to comment.