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