@@ -23,4 +23,56 @@ FlushDecision RawStripeSizeFlushPolicy::shouldFlush(
2323 : FlushDecision::None;
2424}
2525
26+ FlushDecision ChunkFlushPolicy::shouldFlush (
27+ const StripeProgress& stripeProgress) {
28+ const auto relieveMemoryPressure = [&]() {
29+ const uint64_t inMemoryByteSize =
30+ stripeProgress.stripeRawSize + stripeProgress.stripeEncodedSize ;
31+ // Determine when we need to start relieving memory pressure with chunking
32+ if (lastFlushDecision_ == FlushDecision::None &&
33+ inMemoryByteSize > config_.writerMaxMemoryBytes ) {
34+ return FlushDecision::Chunk;
35+ }
36+
37+ // Try chunking when possible to relieve memory pressure
38+ else if (
39+ lastFlushDecision_ == FlushDecision::Chunk &&
40+ stripeProgress.successfullyChunked &&
41+ inMemoryByteSize > config_.writerMinMemoryBytes ) {
42+ return FlushDecision::Chunk;
43+ }
44+
45+ // When chunking is unable to relieve memory pressure, we flush
46+ else if (
47+ lastFlushDecision_ == FlushDecision::Chunk &&
48+ !stripeProgress.successfullyChunked &&
49+ inMemoryByteSize > config_.writerMinMemoryBytes ) {
50+ return FlushDecision::Stripe;
51+ }
52+
53+ return FlushDecision::None;
54+ };
55+
56+ lastFlushDecision_ = relieveMemoryPressure ();
57+ if (lastFlushDecision_ != FlushDecision::None ||
58+ stripeProgress.stripeEncodedSize == 0 ) {
59+ return lastFlushDecision_;
60+ }
61+
62+ // When no writer memory pressure, optimize for storage stripe size
63+ const auto optimizeStorageSize = [&]() {
64+ double compressionRatio = config_.compressionRatioFactor *
65+ (stripeProgress.stripeEncodedRawSize /
66+ stripeProgress.stripeEncodedSize );
67+ double expectedEncodedStripeSize = stripeProgress.stripeEncodedSize +
68+ compressionRatio * stripeProgress.stripeRawSize ;
69+ return expectedEncodedStripeSize >= config_.targetStripeSizeBytes
70+ ? FlushDecision::Stripe
71+ : FlushDecision::None;
72+ };
73+ lastFlushDecision_ = optimizeStorageSize ();
74+
75+ return lastFlushDecision_;
76+ }
77+
2678} // namespace facebook::nimble
0 commit comments