| 251 | } |
| 252 | |
| 253 | void Manager::flush(UCharBuffer* buffer, bool sync, bool prepare) |
| 254 | { |
| 255 | fb_assert(!m_shutdown); |
| 256 | fb_assert(buffer && buffer->hasData()); |
| 257 | |
| 258 | const auto prepareBuffer = prepare ? buffer : nullptr; |
| 259 | |
| 260 | MutexLockGuard guard(m_queueMutex, FB_FUNCTION); |
| 261 | |
| 262 | // Add the current chunk to the queue |
| 263 | m_queue.add(buffer); |
| 264 | m_queueSize += buffer->getCount(); |
| 265 | |
| 266 | // If the background thread is lagging too far behind, |
| 267 | // replicate packets synchronously rather than relying |
| 268 | // on the background thread to catch up any time soon |
| 269 | const bool lagging = (m_queueSize > MAX_BG_WRITER_LAG); |
| 270 | |
| 271 | if (sync || prepare || lagging) |
| 272 | { |
| 273 | const auto tdbb = JRD_get_thread_data(); |
| 274 | const auto dbb = tdbb->getDatabase(); |
| 275 | |
| 276 | for (auto& buffer : m_queue) |
| 277 | { |
| 278 | if (buffer) |
| 279 | { |
| 280 | auto length = (ULONG) buffer->getCount(); |
| 281 | fb_assert(length); |
| 282 | bool hasData = true; |
| 283 | |
| 284 | if (m_changeLog) |
| 285 | { |
| 286 | if (prepareBuffer == buffer) |
| 287 | { |
| 288 | // Remove the opPrepareTransaction command from the journal |
| 289 | fb_assert(buffer->back() == opPrepareTransaction); |
| 290 | |
| 291 | const auto block = (Block*) buffer->begin(); |
| 292 | block->length -= sizeof(UCHAR); |
| 293 | length -= sizeof(UCHAR); |
| 294 | hasData = (block->length != 0); |
| 295 | } |
| 296 | |
| 297 | if (hasData) |
| 298 | { |
| 299 | const auto sequence = m_changeLog->write(length, buffer->begin(), sync); |
| 300 | |
| 301 | if (sequence != m_sequence) |
| 302 | { |
| 303 | dbb->setReplSequence(tdbb, sequence); |
| 304 | m_sequence = sequence; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (prepareBuffer == buffer) |
| 309 | { |
| 310 | const auto block = (Block*) buffer->begin(); |
nothing calls this directly
no test coverage detected