1
+ #!/usr/bin/env python3
2
+ #
3
+ # Licensed to the Apache Software Foundation (ASF) under one
4
+ # or more contributor license agreements. See the NOTICE file
5
+ # distributed with this work for additional information
6
+ # regarding copyright ownership. The ASF licenses this file
7
+ # to you under the Apache License, Version 2.0 (the
8
+ # "License"); you may not use this file except in compliance
9
+ # with the License. You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing,
14
+ # software distributed under the License is distributed on an
15
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ # KIND, either express or implied. See the License for the
17
+ # specific language governing permissions and limitations
18
+ # under the License.
19
+ #
20
+
21
+ # allows tuning of RocksDB configuration via environment variables which were effective
22
+ # before Pulsar 2.11 / BookKeeper 4.15 / https://github.com/apache/bookkeeper/pull/3056
23
+ # the script should be applied to the `conf/entry_location_rocksdb.conf` file
24
+
25
+ import os
26
+ import sys
27
+ import configparser
28
+
29
+ # Constants for section keys
30
+ DB_OPTIONS = "DBOptions"
31
+ CF_OPTIONS = "CFOptions \" default\" "
32
+ TABLE_OPTIONS = "TableOptions/BlockBasedTable \" default\" "
33
+
34
+ def update_ini_file (ini_file_path ):
35
+ config = configparser .ConfigParser ()
36
+ config .read (ini_file_path )
37
+ updated = False
38
+
39
+ # Mapping of environment variables to INI sections and keys
40
+ env_to_ini_mapping = {
41
+ "dbStorage_rocksDB_logPath" : (DB_OPTIONS , "log_path" ),
42
+ "dbStorage_rocksDB_logLevel" : (DB_OPTIONS , "info_log_level" ),
43
+ "dbStorage_rocksDB_lz4CompressionEnabled" : (CF_OPTIONS , "compression" ),
44
+ "dbStorage_rocksDB_writeBufferSizeMB" : (CF_OPTIONS , "write_buffer_size" ),
45
+ "dbStorage_rocksDB_sstSizeInMB" : (CF_OPTIONS , "target_file_size_base" ),
46
+ "dbStorage_rocksDB_blockSize" : (TABLE_OPTIONS , "block_size" ),
47
+ "dbStorage_rocksDB_bloomFilterBitsPerKey" : (TABLE_OPTIONS , "filter_policy" ),
48
+ "dbStorage_rocksDB_blockCacheSize" : (TABLE_OPTIONS , "block_cache" ),
49
+ "dbStorage_rocksDB_numLevels" : (CF_OPTIONS , "num_levels" ),
50
+ "dbStorage_rocksDB_numFilesInLevel0" : (CF_OPTIONS , "level0_file_num_compaction_trigger" ),
51
+ "dbStorage_rocksDB_maxSizeInLevel1MB" : (CF_OPTIONS , "max_bytes_for_level_base" ),
52
+ "dbStorage_rocksDB_format_version" : (TABLE_OPTIONS , "format_version" )
53
+ }
54
+
55
+ # Type conversion functions
56
+ def mb_to_bytes (mb ):
57
+ return str (int (mb ) * 1024 * 1024 )
58
+
59
+ def str_to_bool (value ):
60
+ return True if value .lower () in ["true" , "1" , "yes" ] else False
61
+
62
+ # Iterate over environment variables
63
+ for key , value in os .environ .items ():
64
+ if key .startswith ("PULSAR_PREFIX_" ):
65
+ key = key [len ("PULSAR_PREFIX_" ):]
66
+
67
+ if key in env_to_ini_mapping :
68
+ section , option = env_to_ini_mapping [key ]
69
+ if key in ["dbStorage_rocksDB_writeBufferSizeMB" , "dbStorage_rocksDB_sstSizeInMB" , "dbStorage_rocksDB_maxSizeInLevel1MB" ]:
70
+ value = mb_to_bytes (value )
71
+ elif key == "dbStorage_rocksDB_lz4CompressionEnabled" :
72
+ value = "kLZ4Compression" if str_to_bool (value ) else "kNoCompression"
73
+ elif key == "dbStorage_rocksDB_bloomFilterBitsPerKey" :
74
+ value = "rocksdb.BloomFilter:{}:false" .format (value )
75
+ if config .get (section , option , fallback = None ) != value :
76
+ config .set (section , option , value )
77
+ updated = True
78
+
79
+ # Write the updated INI file only if there were updates
80
+ if updated :
81
+ with open (ini_file_path , 'w' ) as configfile :
82
+ config .write (configfile )
83
+
84
+ if __name__ == "__main__" :
85
+ ini_file_path = sys .argv [1 ] if len (sys .argv ) > 1 else "conf/entry_location_rocksdb.conf"
86
+ update_ini_file (ini_file_path )
0 commit comments