diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index cc08f652b7..e81d0fa1d6 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -162,6 +162,7 @@ 3.2.12 1.5.24 + 1.1.8.4 1.68 2.0.1 5.2.0 @@ -735,6 +736,13 @@ test ${fabric8_kubernetes_version} + + + org.xerial.snappy + snappy-java + ${snappy_java_version} + true + diff --git a/dubbo-rpc/dubbo-rpc-triple/pom.xml b/dubbo-rpc/dubbo-rpc-triple/pom.xml index c4ba1dd3b0..c8a221eb25 100644 --- a/dubbo-rpc/dubbo-rpc-triple/pom.xml +++ b/dubbo-rpc/dubbo-rpc-triple/pom.xml @@ -65,6 +65,10 @@ 2.2.21 test + + org.xerial.snappy + snappy-java + diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Snappy.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Snappy.java new file mode 100644 index 0000000000..be5b11fe81 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Snappy.java @@ -0,0 +1,63 @@ +/* + * 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 org.apache.dubbo.rpc.protocol.tri.compressor; + +import org.apache.dubbo.rpc.RpcException; + +import java.io.IOException; + +/** + * snappy compressor, Provide high-speed compression speed and reasonable compression ratio + * + * @link https://github.com/google/snappy + */ +public class Snappy implements Compressor, DeCompressor { + + public static final String SNAPPY = "snappy"; + + @Override + public String getMessageEncoding() { + return SNAPPY; + } + + @Override + public byte[] compress(byte[] payloadByteArr) throws RpcException { + if (null == payloadByteArr || 0 == payloadByteArr.length) { + return new byte[0]; + } + + try { + return org.xerial.snappy.Snappy.compress(payloadByteArr); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + @Override + public byte[] decompress(byte[] payloadByteArr) { + if (null == payloadByteArr || 0 == payloadByteArr.length) { + return new byte[0]; + } + + try { + return org.xerial.snappy.Snappy.uncompress(payloadByteArr); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } +} diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.Compressor b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.Compressor index c21602f47a..654a42e033 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.Compressor +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.Compressor @@ -1 +1,2 @@ gzip=org.apache.dubbo.rpc.protocol.tri.compressor.Gzip +snappy=org.apache.dubbo.rpc.protocol.tri.compressor.Snappy diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.DeCompressor b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.DeCompressor index 1c33d6e430..654a42e033 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.DeCompressor +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.protocol.tri.compressor.DeCompressor @@ -1 +1,2 @@ -gzip=org.apache.dubbo.rpc.protocol.tri.compressor.Gzip \ No newline at end of file +gzip=org.apache.dubbo.rpc.protocol.tri.compressor.Gzip +snappy=org.apache.dubbo.rpc.protocol.tri.compressor.Snappy diff --git a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/compressor/SnappyTest.java b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/compressor/SnappyTest.java new file mode 100644 index 0000000000..5a8b820510 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/compressor/SnappyTest.java @@ -0,0 +1,62 @@ +/* + * 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 org.apache.dubbo.rpc.protocol.tri.compressor; + +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.FrameworkModel; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * test for snappy + */ +public class SnappyTest { + + 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(); + } + + @ValueSource(strings = {"snappy"}) + @ParameterizedTest + void compression(String compressorName) { + Compressor compressor = ApplicationModel.defaultModel().getDefaultModule() + .getExtensionLoader(Compressor.class) + .getExtension(compressorName); + String loadByStatic = Compressor.getCompressor(new FrameworkModel(), compressorName) + .getMessageEncoding(); + Assertions.assertEquals(loadByStatic, compressor.getMessageEncoding()); + + byte[] compressedByteArr = compressor.compress(TEST_STR.getBytes()); + + DeCompressor deCompressor = ApplicationModel.defaultModel().getDefaultModule() + .getExtensionLoader(DeCompressor.class) + .getExtension(compressorName); + + byte[] decompressedByteArr = deCompressor.decompress(compressedByteArr); + Assertions.assertEquals(new String(decompressedByteArr), TEST_STR); + } +}