From 09b6f2315c909b19d5922a43680f1e8fbc7728a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=88=E9=93=AD?= Date: Wed, 28 Feb 2024 14:23:25 +0800 Subject: [PATCH 1/6] support for https://github.com/sofastack/sofa-rpc/issues/1398 --- bom/pom.xml | 8 ++ codec/codec-api/pom.xml | 5 ++ .../rpc/codec/biz2/Bzip2RpcCompressor.java | 76 +++++++++++++++++++ .../rpc/codec/gzip/GzipRpcCompressor.java | 70 +++++++++++++++++ .../com.alipay.sofa.rpc.codec.Compressor | 4 +- .../codec/biz2/Bzip2RpcCompressorTest.java | 51 +++++++++++++ .../rpc/codec/gzip/GzipRpcCompressorTest.java | 52 +++++++++++++ 7 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java create mode 100644 codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java create mode 100644 codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java create mode 100644 codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java diff --git a/bom/pom.xml b/bom/pom.xml index a97d8cc8f..895ebbfb5 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -60,6 +60,8 @@ true true + + 1.25.0 @@ -598,6 +600,12 @@ 0.16.0 test + + + org.apache.commons + commons-compress + ${commons_compress_version} + diff --git a/codec/codec-api/pom.xml b/codec/codec-api/pom.xml index 47b42b4fe..921fa0e31 100644 --- a/codec/codec-api/pom.xml +++ b/codec/codec-api/pom.xml @@ -38,6 +38,11 @@ junit test + + + org.apache.commons + commons-compress + diff --git a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java new file mode 100644 index 000000000..6199e3b95 --- /dev/null +++ b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.codec.biz2; + +import com.alipay.sofa.rpc.codec.Compressor; +import com.alipay.sofa.rpc.ext.Extension; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +/** + * bzip2 compressor, faster compression efficiency + * + * @author chengming + * @version Bzip2RpcCompressor.java, v 0.1 2024年02月28日 10:45 AM chengming + * @link https://commons.apache.org/proper/commons-compress/ + */ +@Extension(value = "bzip2", code = 3) +public class Bzip2RpcCompressor implements Compressor { + + @Override + public byte[] compress(byte[] src) { + if (null == src || 0 == src.length) { + return new byte[0]; + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + BZip2CompressorOutputStream cos; + try { + cos = new BZip2CompressorOutputStream(out); + cos.write(src); + cos.close(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + return out.toByteArray(); + } + + @Override + public byte[] deCompress(byte[] src) { + if (null == src || 0 == src.length) { + return new byte[0]; + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(src); + try { + BZip2CompressorInputStream unZip = new BZip2CompressorInputStream(in); + byte[] buffer = new byte[2048]; + int n; + while ((n = unZip.read(buffer)) >= 0) { + out.write(buffer, 0, n); + } + } catch (Exception e) { + throw new IllegalStateException(e); + } + return out.toByteArray(); + } +} \ No newline at end of file diff --git a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java new file mode 100644 index 000000000..fb30fbcd3 --- /dev/null +++ b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.codec.gzip; + +import com.alipay.sofa.rpc.codec.Compressor; +import com.alipay.sofa.rpc.ext.Extension; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +/** + * @author chengming + * @version GzipRpcCompressor.java, v 0.1 2024年02月28日 11:25 AM chengming + */ +@Extension(value = "bzip2", code = 4) +public class GzipRpcCompressor implements Compressor { + + @Override + public byte[] compress(byte[] src) { + if (null == src || 0 == src.length) { + return new byte[0]; + } + + ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOutStream)) { + gzipOutputStream.write(src); + } catch (Exception exception) { + throw new IllegalStateException(exception); + } + + return byteOutStream.toByteArray(); + } + + @Override + public byte[] deCompress(byte[] src) { + if (null == src || 0 == src.length) { + return new byte[0]; + } + + ByteArrayInputStream byteInStream = new ByteArrayInputStream(src); + ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); + try (GZIPInputStream gzipInputStream = new GZIPInputStream(byteInStream)) { + int readByteNum; + byte[] bufferArr = new byte[256]; + while ((readByteNum = gzipInputStream.read(bufferArr)) >= 0) { + byteOutStream.write(bufferArr, 0, readByteNum); + } + } catch (Exception exception) { + throw new IllegalStateException(exception); + } + + return byteOutStream.toByteArray(); + } +} \ No newline at end of file diff --git a/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor b/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor index a0d34faa1..6e5c5a33f 100644 --- a/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor +++ b/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor @@ -1 +1,3 @@ -snappy=com.alipay.sofa.rpc.codec.snappy.SnappyRpcCompressor \ No newline at end of file +snappy=com.alipay.sofa.rpc.codec.snappy.SnappyRpcCompressor +bzip2=com.alipay.sofa.rpc.codec.biz2.Bzip2RpcCompressor +gzip=com.alipay.sofa.rpc.codec.gzip.GzipRpcCompressor \ No newline at end of file diff --git a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java new file mode 100644 index 000000000..3e9112e35 --- /dev/null +++ b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.codec.biz2; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; + +/** + * @author chengming + * @version Bzip2RpcCompressorTest.java, v 0.1 2024年02月28日 2:19 PM chengming + */ +public class Bzip2RpcCompressorTest { + + private static final String TEST_STR; + + static { + StringBuilder builder = new StringBuilder(); + int charNum = 1000000; + for (int i = 0; i < charNum; i++) { + builder.append("a"); + } + + TEST_STR = builder.toString(); + } + + @Test + public void testCompression() throws UnsupportedEncodingException { + Bzip2RpcCompressor compressor = new Bzip2RpcCompressor(); + byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); + Assert.assertNotNull(TEST_STR); + + String s1 = new String(compressor.deCompress(bs), "utf-8"); + Assert.assertEquals(TEST_STR, s1); + } +} \ No newline at end of file diff --git a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java new file mode 100644 index 000000000..440f86fe8 --- /dev/null +++ b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.codec.gzip; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; + +/** + * @author chengming + * @version GzipRpcCompressorTest.java, v 0.1 2024年02月28日 2:09 PM chengming + */ +public class GzipRpcCompressorTest { + + private static final String TEST_STR; + + static { + StringBuilder builder = new StringBuilder(); + int charNum = 1000000; + for (int i = 0; i < charNum; i++) { + builder.append("a"); + } + + TEST_STR = builder.toString(); + } + + @Test + public void testCompression() throws UnsupportedEncodingException { + GzipRpcCompressor compressor = new GzipRpcCompressor(); + byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); + Assert.assertNotNull(TEST_STR); + + String s1 = new String(compressor.deCompress(bs), "utf-8"); + Assert.assertEquals(TEST_STR, s1); + } + +} From 26518ca3e1cc61e24f560afe7f8425a099b91032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=88=E9=93=AD?= Date: Thu, 28 Mar 2024 13:16:46 +0800 Subject: [PATCH 2/6] support compress --- .../java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java index fb30fbcd3..5f68209db 100644 --- a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java +++ b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressor.java @@ -28,7 +28,7 @@ * @author chengming * @version GzipRpcCompressor.java, v 0.1 2024年02月28日 11:25 AM chengming */ -@Extension(value = "bzip2", code = 4) +@Extension(value = "gzip", code = 4) public class GzipRpcCompressor implements Compressor { @Override From fc1b52d36b197d22eadbe3b0e1a320b34e1fe4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=88=E9=93=AD?= Date: Thu, 28 Mar 2024 13:21:32 +0800 Subject: [PATCH 3/6] support compress --- bom/pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bom/pom.xml b/bom/pom.xml index 845903be8..d34eead8b 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -516,6 +516,18 @@ grpc-stub ${grpc.version} + + + io.fabric8 + kubernetes-client + ${fabric8_kubernetes_version} + + + io.fabric8 + kubernetes-server-mock + test + ${fabric8_kubernetes_version} + From cf0ae0d2dcdbc75e5de05b9bccc80e6605f36b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=88=E9=93=AD?= Date: Thu, 28 Mar 2024 13:24:28 +0800 Subject: [PATCH 4/6] support compress --- bom/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bom/pom.xml b/bom/pom.xml index d34eead8b..db9423d33 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -529,6 +529,8 @@ ${fabric8_kubernetes_version} + + org.apache.curator @@ -613,6 +615,12 @@ 0.16.0 test + + + org.apache.commons + commons-compress + ${commons_compress_version} + From 69782300ede581bda5e53ba3a81a350f0c7abf71 Mon Sep 17 00:00:00 2001 From: evenliu Date: Thu, 9 May 2024 20:24:29 +0800 Subject: [PATCH 5/6] Update codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor b/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor index 6e5c5a33f..84b01a049 100644 --- a/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor +++ b/codec/codec-api/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.codec.Compressor @@ -1,3 +1,3 @@ snappy=com.alipay.sofa.rpc.codec.snappy.SnappyRpcCompressor -bzip2=com.alipay.sofa.rpc.codec.biz2.Bzip2RpcCompressor +bzip2=com.alipay.sofa.rpc.codec.bzip2.Bzip2RpcCompressor gzip=com.alipay.sofa.rpc.codec.gzip.GzipRpcCompressor \ No newline at end of file From 2e89cbc243764ce23b6d057d609e8c664d4606a8 Mon Sep 17 00:00:00 2001 From: evenliu Date: Thu, 9 May 2024 20:24:44 +0800 Subject: [PATCH 6/6] Update codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- all/pom.xml | 6 ++++++ .../rpc/codec/{biz2 => bzip2}/Bzip2RpcCompressor.java | 2 +- .../codec/{biz2 => bzip2}/Bzip2RpcCompressorTest.java | 10 ++++++---- .../sofa/rpc/codec/gzip/GzipRpcCompressorTest.java | 8 +++++--- 4 files changed, 18 insertions(+), 8 deletions(-) rename codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/{biz2 => bzip2}/Bzip2RpcCompressor.java (98%) rename codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/{biz2 => bzip2}/Bzip2RpcCompressorTest.java (83%) diff --git a/all/pom.xml b/all/pom.xml index df0690331..7b191a665 100644 --- a/all/pom.xml +++ b/all/pom.xml @@ -74,6 +74,7 @@ 32.0.0-jre 2.12.1 0.4.1 + 1.25.0 @@ -445,6 +446,11 @@ fury-core ${fury.version} + + org.apache.commons + commons-compress + ${commons_compress_version} + diff --git a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressor.java similarity index 98% rename from codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java rename to codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressor.java index 6199e3b95..86993b037 100644 --- a/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressor.java +++ b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressor.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.biz2; +package com.alipay.sofa.rpc.codec.bzip2; import com.alipay.sofa.rpc.codec.Compressor; import com.alipay.sofa.rpc.ext.Extension; diff --git a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressorTest.java similarity index 83% rename from codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java rename to codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressorTest.java index 3e9112e35..c82ebc2a1 100644 --- a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/biz2/Bzip2RpcCompressorTest.java +++ b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/bzip2/Bzip2RpcCompressorTest.java @@ -14,8 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.biz2; +package com.alipay.sofa.rpc.codec.bzip2; +import com.alipay.sofa.rpc.codec.Compressor; +import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory; import org.junit.Assert; import org.junit.Test; @@ -41,10 +43,10 @@ public class Bzip2RpcCompressorTest { @Test public void testCompression() throws UnsupportedEncodingException { - Bzip2RpcCompressor compressor = new Bzip2RpcCompressor(); - byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); - Assert.assertNotNull(TEST_STR); + Compressor compressor = ExtensionLoaderFactory.getExtensionLoader(Compressor.class).getExtension("bzip2"); + Assert.assertTrue(compressor instanceof Bzip2RpcCompressor); + byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); String s1 = new String(compressor.deCompress(bs), "utf-8"); Assert.assertEquals(TEST_STR, s1); } diff --git a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java index 440f86fe8..694b14e78 100644 --- a/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java +++ b/codec/codec-api/src/test/java/com/alipay/sofa/rpc/codec/gzip/GzipRpcCompressorTest.java @@ -16,6 +16,8 @@ */ package com.alipay.sofa.rpc.codec.gzip; +import com.alipay.sofa.rpc.codec.Compressor; +import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory; import org.junit.Assert; import org.junit.Test; @@ -41,10 +43,10 @@ public class GzipRpcCompressorTest { @Test public void testCompression() throws UnsupportedEncodingException { - GzipRpcCompressor compressor = new GzipRpcCompressor(); - byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); - Assert.assertNotNull(TEST_STR); + Compressor compressor = ExtensionLoaderFactory.getExtensionLoader(Compressor.class).getExtension("gzip"); + Assert.assertTrue(compressor instanceof GzipRpcCompressor); + byte[] bs = compressor.compress(TEST_STR.getBytes("utf-8")); String s1 = new String(compressor.deCompress(bs), "utf-8"); Assert.assertEquals(TEST_STR, s1); }