-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(telemetry): support gzip compression on uploading metrics & logs…
… to s3 Signed-off-by: Shichao Nie <[email protected]>
- Loading branch information
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
automq-shell/src/main/java/com/automq/shell/util/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024, AutoMQ HK Limited. | ||
* | ||
* The use of this file is governed by the Business Source License, | ||
* as detailed in the file "/LICENSE.S3Stream" included in this repository. | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
package com.automq.shell.util; | ||
|
||
import com.automq.stream.s3.ByteBufAlloc; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.zip.GZIPInputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
public class Utils { | ||
|
||
public static ByteBuf compress(ByteBuf input) throws IOException { | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); | ||
|
||
byte[] buffer = new byte[input.readableBytes()]; | ||
input.readBytes(buffer); | ||
gzipOutputStream.write(buffer); | ||
gzipOutputStream.close(); | ||
|
||
ByteBuf compressed = ByteBufAlloc.byteBuffer(byteArrayOutputStream.size()); | ||
compressed.writeBytes(byteArrayOutputStream.toByteArray()); | ||
return compressed; | ||
} | ||
|
||
public static ByteBuf decompress(ByteBuf input) throws IOException { | ||
byte[] compressedData = new byte[input.readableBytes()]; | ||
input.readBytes(compressedData); | ||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressedData); | ||
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream); | ||
|
||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int bytesRead; | ||
while ((bytesRead = gzipInputStream.read(buffer)) != -1) { | ||
byteArrayOutputStream.write(buffer, 0, bytesRead); | ||
} | ||
|
||
gzipInputStream.close(); | ||
byteArrayOutputStream.close(); | ||
|
||
byte[] uncompressedData = byteArrayOutputStream.toByteArray(); | ||
ByteBuf output = ByteBufAlloc.byteBuffer(uncompressedData.length); | ||
output.writeBytes(uncompressedData); | ||
return output; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
automq-shell/src/test/java/com/automq/shell/util/UtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024, AutoMQ HK Limited. | ||
* | ||
* The use of this file is governed by the Business Source License, | ||
* as detailed in the file "/LICENSE.S3Stream" included in this repository. | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
|
||
package com.automq.shell.util; | ||
|
||
import com.automq.stream.s3.ByteBufAlloc; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
@Tag("S3Unit") | ||
public class UtilsTest { | ||
|
||
@Test | ||
public void testCompression() { | ||
String testStr = "This is a test string"; | ||
ByteBuf input = ByteBufAlloc.byteBuffer(testStr.length()); | ||
input.writeBytes(testStr.getBytes()); | ||
try { | ||
ByteBuf compressed = Utils.compress(input); | ||
ByteBuf decompressed = Utils.decompress(compressed); | ||
String decompressedStr = decompressed.toString(io.netty.util.CharsetUtil.UTF_8); | ||
System.out.printf("Original: %s, Decompressed: %s\n", testStr, decompressedStr); | ||
Assertions.assertEquals(testStr, decompressedStr); | ||
} catch (Exception e) { | ||
Assertions.fail("Exception occurred during compression/decompression: " + e.getMessage()); | ||
} | ||
} | ||
} |