summary refs log tree commit diff
path: root/synapse/config/state_compressor.py
blob: 92a0b7e533e609c17b7fe68ef028ad6a707a621b (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.config._base import Config, ConfigError
from synapse.config._util import validate_config
from synapse.python_dependencies import DependencyException, check_requirements


class StateCompressorConfig(Config):
    section = "statecompressor"

    def read_config(self, config, **kwargs):
        compressor_config = config.get("state_compressor") or {}
        validate_config(
            _STATE_COMPRESSOR_SCHEMA, compressor_config, ("state_compressor",)
        )
        self.compressor_enabled = compressor_config.get("enabled") or False

        if not self.compressor_enabled:
            return

        try:
            check_requirements("auto_compressor")
        except DependencyException as e:
            raise ConfigError from e

        self.compressor_chunk_size = compressor_config.get("chunk_size") or 500
        self.compressor_number_of_chunks = compressor_config.get("number_of_chunks") or 100
        self.compressor_default_levels = (
            compressor_config.get("default_levels") or "100,50,25"
        )
        self.time_between_compressor_runs = self.parse_duration(
            compressor_config.get("time_between_runs") or "1d"
        )

    def generate_config_section(self, **kwargs):
        return """\
        ## State compressor ##

        # The state compressor is an experimental tool which attempts to
        # reduce the number of rows in the state_groups_state table
        # of postgres databases.
        #
        # For more information please see
        # https://matrix-org.github.io/synapse/latest/state_compressor.html
        #
        state_compressor:
          # Whether the state compressor should run (defaults to false)
          # Uncomment to enable it - Note, this requires the 'auto-compressor'
          # library to be installed
          #
          #enabled: true

          # The (rough) number of state groups to load at one time. Defaults
          # to 500.
          #
          #chunk_size: 1000

          # The number of chunks to compress on each run. Defaults to 100.
          #
          #number_of_chunks: 1

          # The default level sizes for the compressor to use. Defaults to
          # 100,50,25.
          #
          #default_levels: 128,64,32.

          # How frequently to run the state compressor. Defaults to 1d
          #
          #time_between_runs: 1w
        """


_STATE_COMPRESSOR_SCHEMA = {
    "type": "object",
    "properties": {
        "enabled": {"type": "boolean"},
        "chunk_size": {"type": "number"},
        "number_of_chunks": {"type": "number"},
        "default_levels": {"type": "string"},
        "time_between_runs": {"type": "string"},
    },
}