OmniSciDB  0fdbebe030
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Compressor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #include "Compressor.h"
26 #include <blosc.h>
27 #include <string>
28 #include <thread>
29 #include "Logger.h"
30 
31 // we only compress data if the payload size is greater than 512 MB
32 size_t g_compression_limit_bytes{512 * 1024 * 1024};
33 
35  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
36  blosc_init();
37  // We use maximum number of threads here since with tests we found that compression
38  // speed gets lear scalling with corresponding to the number of threads being used.
39 
40  blosc_set_nthreads(std::thread::hardware_concurrency());
41 
42  // We chosse faster compressor, accepting slightly lower compression ratio
43  // https://lz4.github.io/lz4/
44 
45  blosc_set_compressor(BLOSC_LZ4HC_COMPNAME);
46 }
47 
49  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
50  blosc_destroy();
51 }
52 
54  const uint8_t* buffer,
55  const size_t buffer_size,
56  uint8_t* compressed_buffer,
57  const size_t compressed_buffer_size,
58  const size_t min_compressor_bytes = g_compression_limit_bytes) {
59  if (compressed_buffer_size < BLOSC_MIN_HEADER_LENGTH) {
60  // Blosc compressor checks this condition during the initialization
61  // and throw "Output buffer size should be larger than 16 bytes" error
62  // if compressed_buffer_size < 16 (BLOSC_MIN_HEADER_LENGTH)
63  // but after sending interrupt signal, blosc compress function hangs until
64  // thrift timed out error and could not check this code.
65  // here, we can early return by explicitly checking this condition
66  // so as to avoid hangs in query runtime
67  return 0;
68  }
69 
70  if (buffer_size < min_compressor_bytes && min_compressor_bytes != 0) {
71  return 0;
72  }
73  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
74  const auto compressed_len = blosc_compress(5,
75  1,
76  sizeof(unsigned char),
77  buffer_size,
78  buffer,
79  &compressed_buffer[0],
80  compressed_buffer_size);
81 
82  if (compressed_len <= 0) {
83  // something went wrong. blosc retrun codes simply don't provide enough information
84  // for us to decide what.
85  throw CompressionFailedError(std::string("failed to compress result set of length ") +
86  std::to_string(buffer_size));
87  }
88  // we need to tell the other endpoint the size of the acctual data so it can
89  // decide whether it should decompress data or not. So we pass the original
90  // data length. and only send the compressed result if the output of the
91  // compressed result is smaller than the original
92  return compressed_len;
93 }
94 
95 std::string BloscCompressor::compress(const std::string& buffer) {
96  const auto buffer_size = buffer.size();
97  std::vector<uint8_t> compressed_buffer(getScratchSpaceSize(buffer_size));
98  try {
99  const size_t compressed_len = compress((uint8_t*)buffer.c_str(),
100  buffer_size,
101  &compressed_buffer[0],
102  getScratchSpaceSize(buffer_size));
103  if (compressed_len > 0 && compressed_len < buffer_size) {
104  // we need to tell the other endpoint the size of the acctual data so it can
105  // decide whether it should decompress data or not. So we pass the original
106  // data length. and only send the compressed result if the output of the
107  // compressed result is smaller than the original
108  compressed_buffer.resize(compressed_len);
109  return {compressed_buffer.begin(), compressed_buffer.end()};
110  }
111  } catch (const CompressionFailedError& e) {
112  }
113  return buffer;
114 }
115 
116 size_t BloscCompressor::decompress(const uint8_t* compressed_buffer,
117  uint8_t* decompressed_buffer,
118  const size_t decompressed_size) {
119  size_t decompressed_buf_len, compressed_buf_len, block_size, decompressed_len = 0;
121  &compressed_buffer[0], &compressed_buf_len, &decompressed_buf_len, &block_size);
122  // check compressed buffer is a blosc compressed buffer.
123  if (compressed_buf_len > 0 && decompressed_size == decompressed_buf_len) {
124  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
125  decompressed_len =
126  blosc_decompress(&compressed_buffer[0], decompressed_buffer, decompressed_size);
127  }
128 
129  if (decompressed_len == 0) {
131  std::string("failed to decompress buffer for compressed size: ") +
132  std::to_string(compressed_buf_len));
133  }
134  if (decompressed_len != decompressed_size) {
136  std::string("decompression buffer size mismatch. Decompressed buffer length: ") +
137  std::to_string(decompressed_len));
138  }
139  return decompressed_len;
140 }
141 
142 std::string BloscCompressor::decompress(const std::string& buffer,
143  const size_t decompressed_size) {
144  std::vector<uint8_t> decompressed_buffer(decompressed_size);
145  if (buffer.size() == decompressed_size) {
146  return buffer;
147  }
148  try {
149  decompress(
150  (uint8_t*)&buffer[0], (uint8_t*)&decompressed_buffer[0], decompressed_size);
151  return {decompressed_buffer.begin(), decompressed_buffer.end()};
152  } catch (const CompressionFailedError& e) {
153  }
154  return buffer;
155 }
156 
157 size_t BloscCompressor::compressOrMemcpy(const uint8_t* input_buffer,
158  uint8_t* output_buffer,
159  size_t uncompressed_size,
160  const size_t min_compressor_bytes) {
161  try {
162  const auto compressed_size = compress(input_buffer,
163  uncompressed_size,
164  output_buffer,
165  uncompressed_size,
166  min_compressor_bytes);
167  if (compressed_size > 0) {
168  return compressed_size;
169  }
170  } catch (const CompressionFailedError& e) {
171  // catch exceptions from blosc
172  // we copy regardless what happens in compressor
173  if (uncompressed_size > min_compressor_bytes) {
174  LOG(WARNING) << "Compressor failed for byte size of " << uncompressed_size;
175  }
176  }
177  memcpy(output_buffer, input_buffer, uncompressed_size);
178  return uncompressed_size;
179 }
180 
181 bool BloscCompressor::decompressOrMemcpy(const uint8_t* compressed_buffer,
182  const size_t compressed_size,
183  uint8_t* decompressed_buffer,
184  const size_t decompressed_size) {
185  try {
186  decompress(compressed_buffer, decompressed_buffer, decompressed_size);
187  return true;
188  } catch (const CompressionFailedError& e) {
189  // we will memcpy if we find that the buffer is not compressed
190 
191  if (compressed_size > decompressed_size) {
192  throw std::runtime_error(
193  "compressed buffer size is greater than decompressed buffer size.");
194  }
195  }
196  memcpy(decompressed_buffer, compressed_buffer, decompressed_size);
197  return false;
198 }
199 
200 void BloscCompressor::getBloscBufferSizes(const uint8_t* data_ptr,
201  size_t* num_bytes_compressed,
202  size_t* num_bytes_uncompressed,
203  size_t* block_size) {
204  blosc_cbuffer_sizes(data_ptr, num_bytes_uncompressed, num_bytes_compressed, block_size);
205 }
206 
208 
210  static std::mutex compressor_singleton_lock;
211  std::lock_guard<std::mutex> singleton_lock(compressor_singleton_lock);
212  if (instance == NULL) {
213  instance = new BloscCompressor();
214  }
215 
216  return instance;
217 }
218 
219 int BloscCompressor::setThreads(size_t num_threads) {
220  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
221  return blosc_set_nthreads(num_threads);
222 }
223 
224 int BloscCompressor::setCompressor(std::string& compressor_name) {
225  std::lock_guard<std::mutex> compressor_lock_(compressor_lock);
226  // Blosc is resilent enough to detect that the comprressor that was provided to it was
227  // supported or not. If the compressor is invalid or not supported it will simply keep
228  // current compressor.
229  return blosc_set_compressor(compressor_name.c_str());
230 }
int setThreads(size_t num_threads)
Definition: Compressor.cpp:219
size_t decompress(const uint8_t *compressed_buffer, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:116
#define LOG(tag)
Definition: Logger.h:188
size_t getScratchSpaceSize(const size_t len) const
Definition: Compressor.h:47
int setCompressor(std::string &compressor)
Definition: Compressor.cpp:224
size_t g_compression_limit_bytes
Definition: Compressor.cpp:32
size_t compressOrMemcpy(const uint8_t *input_buffer, uint8_t *output_buffer, const size_t uncompressed_size, const size_t min_compressor_bytes)
Definition: Compressor.cpp:157
std::string to_string(char const *&&v)
singleton class to handle concurrancy and state for blosc library. A C++ wraper over a pure C library...
std::mutex compressor_lock
Definition: Compressor.h:86
bool decompressOrMemcpy(const uint8_t *compressed_buffer, const size_t compressed_buffer_size, uint8_t *decompressed_buffer, const size_t decompressed_size)
Definition: Compressor.cpp:181
void getBloscBufferSizes(const uint8_t *data_ptr, size_t *num_bytes_compressed, size_t *num_bytes_uncompressed, size_t *block_size)
Definition: Compressor.cpp:200
static BloscCompressor * getCompressor()
Definition: Compressor.cpp:209
int64_t compress(const uint8_t *buffer, const size_t buffer_size, uint8_t *compressed_buffer, const size_t compressed_buffer_size, const size_t min_compressor_bytes)
Definition: Compressor.cpp:53
static BloscCompressor * instance
Definition: Compressor.h:87