aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/datastore/compaction_strategy.h
blob: 7ddaed563b1f5c1bfd61f939788e130dc0a757a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstddef>
#include <cstdint>
#include <iosfwd>

namespace vespalib {

class AddressSpace;
class MemoryUsage;

}

namespace vespalib::datastore {

class CompactionSpec;

/*
 * Class describing compaction strategy for a compactable data structure.
 */
class CompactionStrategy
{
public:
    static constexpr size_t DEAD_BYTES_SLACK = 0x10000u;
    static constexpr size_t DEAD_ADDRESS_SPACE_SLACK = 0x10000u;
private:
    float _maxDeadBytesRatio; // Max ratio of dead bytes before compaction
    float _maxDeadAddressSpaceRatio; // Max ratio of dead address space before compaction
    float _active_buffers_ratio; // Ratio of active buffers to compact for each reason (memory usage, address space usage)
    uint32_t _max_buffers; // Max number of buffers to compact for each reason (memory usage, address space usage)
    bool should_compact_memory(size_t used_bytes, size_t dead_bytes) const noexcept {
        return ((dead_bytes >= DEAD_BYTES_SLACK) &&
                (dead_bytes > used_bytes * getMaxDeadBytesRatio()));
    }
    bool should_compact_address_space(size_t used_address_space, size_t dead_address_space) const noexcept {
        return ((dead_address_space >= DEAD_ADDRESS_SPACE_SLACK) &&
                (dead_address_space > used_address_space * getMaxDeadAddressSpaceRatio()));
    }
public:
    CompactionStrategy() noexcept
        : _maxDeadBytesRatio(0.05),
          _maxDeadAddressSpaceRatio(0.2),
          _active_buffers_ratio(0.1),
          _max_buffers(1)
    { }
    CompactionStrategy(float maxDeadBytesRatio, float maxDeadAddressSpaceRatio) noexcept
        : _maxDeadBytesRatio(maxDeadBytesRatio),
          _maxDeadAddressSpaceRatio(maxDeadAddressSpaceRatio),
          _active_buffers_ratio(0.1),
          _max_buffers(1)
    { }
    CompactionStrategy(float maxDeadBytesRatio, float maxDeadAddressSpaceRatio, uint32_t max_buffers, float active_buffers_ratio) noexcept
        : _maxDeadBytesRatio(maxDeadBytesRatio),
          _maxDeadAddressSpaceRatio(maxDeadAddressSpaceRatio),
          _active_buffers_ratio(active_buffers_ratio),
          _max_buffers(max_buffers)
    { }
    double getMaxDeadBytesRatio() const noexcept { return _maxDeadBytesRatio; }
    double getMaxDeadAddressSpaceRatio() const noexcept { return _maxDeadAddressSpaceRatio; }
    uint32_t get_max_buffers() const noexcept { return _max_buffers; }
    double get_active_buffers_ratio() const noexcept { return _active_buffers_ratio; }
    bool operator==(const CompactionStrategy & rhs) const noexcept {
        return (_maxDeadBytesRatio == rhs._maxDeadBytesRatio) &&
            (_maxDeadAddressSpaceRatio == rhs._maxDeadAddressSpaceRatio) &&
            (_max_buffers == rhs._max_buffers) &&
            (_active_buffers_ratio == rhs._active_buffers_ratio);
    }
    bool operator!=(const CompactionStrategy & rhs) const noexcept { return !(operator==(rhs)); }

    bool should_compact_memory(const MemoryUsage& memory_usage) const noexcept;
    bool should_compact_address_space(const AddressSpace& address_space) const noexcept;
    CompactionSpec should_compact(const MemoryUsage& memory_usage, const AddressSpace& address_space) const noexcept;
    static CompactionStrategy make_compact_all_active_buffers_strategy() noexcept;
};

std::ostream& operator<<(std::ostream& os, const CompactionStrategy& compaction_strategy);

}