Skip to content

Commit

Permalink
Change compressor plugin to optional.
Browse files Browse the repository at this point in the history
Signed-off-by: BI SHUN KE
  • Loading branch information
bi-shun committed Dec 25, 2017
1 parent 71988ea commit 7033bfa
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Expand Up @@ -293,6 +293,12 @@ endif(WITH_LEVELDB)

find_package(snappy REQUIRED)

option(WITH_BROTLI "Brotli compression support" OFF)
if(WITH_BROTLI)
set(HAVE_BROTLI TRUE)
endif(WITH_BROTLI)


option(WITH_LZ4 "LZ4 compression support" OFF)
if(WITH_LZ4)
find_package(LZ4 1.7 REQUIRED)
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -719,7 +719,6 @@ add_subdirectory(include)
add_subdirectory(librados)
add_subdirectory(libradosstriper)


if (WITH_MGR)
set(mgr_srcs
ceph_mgr.cc
Expand Down
18 changes: 13 additions & 5 deletions src/compressor/CMakeLists.txt
Expand Up @@ -10,22 +10,28 @@ set(compressor_plugin_dir ${CMAKE_INSTALL_PKGLIBDIR}/compressor)
add_subdirectory(snappy)
add_subdirectory(zlib)
add_subdirectory(zstd)
add_subdirectory(brotli)

if (HAVE_LZ4)
add_subdirectory(lz4)
endif()

if (HAVE_BROTLI)
add_subdirectory(brotli)
endif()

set(ceph_compressor_libs
ceph_snappy
ceph_zlib
ceph_zstd
ceph_brotli)
ceph_zstd)

if (HAVE_LZ4)
list(APPEND ceph_compressor_libs ceph_lz4)
endif()

if (HAVE_BROTLI)
list(APPEND ceph_compressor_libs ceph_brotli)
endif()

add_custom_target(compressor_plugins DEPENDS
${ceph_compressor_libs})

Expand All @@ -37,10 +43,12 @@ if(WITH_EMBEDDED)
cephd_compressor_base
cephd_compressor_snappy
cephd_compressor_zlib
cephd_compressor_zstd
cephd_compressor_brotli)
cephd_compressor_zstd)
if (HAVE_LZ4)
list(APPEND cephd_compressor_libs cephd_compressor_lz4)
endif()
if (HAVE_BROTLI)
list(APPEND cephd_compressor_libs cephd_compressor_brotli)
endif()
merge_static_libraries(cephd_compressor ${cephd_compressor_libs})
endif()
4 changes: 4 additions & 0 deletions src/compressor/Compressor.cc
Expand Up @@ -31,7 +31,9 @@ const char * Compressor::get_comp_alg_name(int a) {
#ifdef HAVE_LZ4
case COMP_ALG_LZ4: return "lz4";
#endif
#ifdef HAVE_BROTLI
case COMP_ALG_BROTLI: return "brotli";
#endif
default: return "???";
}
}
Expand All @@ -47,8 +49,10 @@ boost::optional<Compressor::CompressionAlgorithm> Compressor::get_comp_alg_type(
if (s == "lz4")
return COMP_ALG_LZ4;
#endif
#ifdef HAVE_BROTLI
if (s == "brotli")
return COMP_ALG_BROTLI;
#endif
if (s == "" || s == "none")
return COMP_ALG_NONE;

Expand Down
2 changes: 2 additions & 0 deletions src/compressor/Compressor.h
Expand Up @@ -37,7 +37,9 @@ class Compressor {
#ifdef HAVE_LZ4
COMP_ALG_LZ4 = 4,
#endif
#ifdef HAVE_BROTLI
COMP_ALG_BROTLI = 5,
#endif
COMP_ALG_LAST //the last value for range checks
};
// compression options
Expand Down
44 changes: 19 additions & 25 deletions src/compressor/brotli/BrotliCompressor.cc
@@ -1,28 +1,27 @@
#include "brotli/encode.h"
#include "brotli/decode.h"
#include "BrotliCompressor.h"
#include "include/scope_guard.h"

#define MAX_LEN (CEPH_PAGE_SIZE)
#define MIN(a,b) (((a)<(b))?(a):(b))

int BrotliCompressor::compress(const bufferlist &in, bufferlist &out)
{
unsigned have;
BrotliEncoderState* s = BrotliEncoderCreateInstance(NULL, NULL, NULL);
BrotliEncoderState* s = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
auto sg = make_scope_guard([&s] { BrotliEncoderDestroyInstance(s); });
size_t available_in = 0;
const uint8_t* next_in = NULL;
const uint8_t* next_in = nullptr;
size_t available_out = 0;
uint8_t* next_out = NULL;
uint8_t* next_out = nullptr;
if (!s) {
BrotliEncoderDestroyInstance(s);
return -1;
}
BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)9);
BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, 22);
unsigned char* c_in;
for (std::list<buffer::ptr>::const_iterator i = in.buffers().begin(); i != in.buffers().end();) {
c_in = (unsigned char*) (*i).c_str();
size_t len = (*i).length();
for (auto i = in.buffers().begin(); i != in.buffers().end();) {
c_in = (unsigned char*) i->c_str();
size_t len = i->length();
available_in = len;
next_in = c_in;
++i;
Expand All @@ -32,34 +31,31 @@ int BrotliCompressor::compress(const bufferlist &in, bufferlist &out)
bufferptr ptr = buffer::create_page_aligned(max_comp_size);
next_out = (unsigned char*)ptr.c_str();
available_out = max_comp_size;
if (!BrotliEncoderCompressStream(s,finish,&available_in, &next_in, &available_out, &next_out, NULL)) {
if (!BrotliEncoderCompressStream(s,finish,&available_in, &next_in, &available_out, &next_out, nullptr)) {
BrotliEncoderDestroyInstance(s);
return -1;
}
have = max_comp_size - available_out;
unsigned have = max_comp_size - available_out;
out.append(ptr, 0, have);
} while (available_out == 0);
if (BrotliEncoderIsFinished(s)) {
BrotliEncoderDestroyInstance(s);
return 0;
break;
}
}
BrotliEncoderDestroyInstance(s);
return 0;
}

int BrotliCompressor::decompress(bufferlist::iterator &p, size_t compressed_size, bufferlist &out)
{
unsigned have;
const char* c_in;
size_t available_in = 0;
const uint8_t* next_in = NULL;
const uint8_t* next_in = nullptr;
size_t available_out = 0;
uint8_t* next_out = NULL;
BrotliDecoderState* s = BrotliDecoderCreateInstance(NULL, NULL, NULL);
size_t remaining = MIN(p.get_remaining(), compressed_size);
uint8_t* next_out = nullptr;
BrotliDecoderState* s = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
auto sg = make_scope_guard([&s] { BrotliDecoderDestroyInstance(s); });
size_t remaining = std::min<size_t>(p.get_remaining(), compressed_size);
if (!s) {
BrotliDecoderDestroyInstance(s);
return -1;
}
while(remaining) {
Expand All @@ -75,20 +71,18 @@ int BrotliCompressor::decompress(bufferlist::iterator &p, size_t compressed_size
BrotliDecoderDestroyInstance(s);
return -1;
}
have = MAX_LEN - available_out;
unsigned have = MAX_LEN - available_out;
out.append(ptr, 0, have);
} while (available_out == 0);
if (BrotliDecoderIsFinished(s)) {
BrotliDecoderDestroyInstance(s);
return 0;
break;
}
}
BrotliDecoderDestroyInstance(s);
return 0;
}

int BrotliCompressor::decompress(const bufferlist &in, bufferlist &out)
{
bufferlist::iterator i = const_cast<bufferlist&>(in).begin();
return decompress(i, in.length(), out);
}
}
19 changes: 8 additions & 11 deletions src/compressor/brotli/CMakeLists.txt
Expand Up @@ -22,21 +22,18 @@ ExternalProject_Add_Step(brotli_ext forcebuild
COMMAND "true"
ALWAYS 1)

add_library(brotlicommon STATIC IMPORTED)
add_library(brotlidec STATIC IMPORTED)
add_library(brotlienc STATIC IMPORTED)
set(bortli_libs brotlicommon brotlidec brotlienc)
foreach(lib ${bortli_libs})
add_library(${lib} STATIC IMPORTED)
add_dependencies(${lib} brotli_ext)
set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/src/brotli/lib${lib}-static.a")
endforeach()

add_dependencies(brotlicommon brotli_ext)
add_dependencies(brotlidec brotli_ext)
add_dependencies(brotlienc brotli_ext)

set_property(TARGET brotlicommon PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/src/brotli/libbrotlicommon-static.a")
set_property(TARGET brotlidec PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/src/brotli/libbrotlidec-static.a")
set_property(TARGET brotlienc PROPERTY IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/src/brotli/libbrotlienc-static.a")
add_library(ceph_brotli SHARED ${brotli_sources})
add_dependencies(ceph_brotli ${CMAKE_SOURCE_DIR}/src/ceph_ver.h)
target_include_directories(ceph_brotli PRIVATE "${CMAKE_BINARY_DIR}/src/brotli/c/include")
target_link_libraries(ceph_brotli brotlienc brotlidec brotlicommon)
List(REVERSE bortli_libs)
target_link_libraries(ceph_brotli ${bortli_libs})
set_target_properties(ceph_brotli PROPERTIES VERSION 2.0.0 SOVERSION 2)
install(TARGETS ceph_brotli DESTINATION ${compressor_plugin_dir})

Expand Down
3 changes: 3 additions & 0 deletions src/include/config-h.in.cmake
Expand Up @@ -87,6 +87,9 @@
/* Defined if you have LZ4 */
#cmakedefine HAVE_LZ4

/* Defined if you have BROTLI */
#cmakedefine HAVE_BROTLI

/* Defined if you have libaio */
#cmakedefine HAVE_LIBAIO

Expand Down
7 changes: 5 additions & 2 deletions src/test/compressor/test_compression.cc
Expand Up @@ -330,8 +330,11 @@ INSTANTIATE_TEST_CASE_P(
#endif
"zlib/noisal",
"snappy",
"zstd",
"brotli"));
"zstd"
#ifdef HAVE_BROTLI
,"brotli"
#endif
));

#ifdef __x86_64__

Expand Down

0 comments on commit 7033bfa

Please sign in to comment.