Skip to content

Commit e3b9cdc

Browse files
lhotarisrinath-ctds
authored andcommitted
[feat] Add scripts for updating BK RocksDB ini files (apache#23178)
(cherry picked from commit 3ada566) (cherry picked from commit ca4512c)
1 parent 42e9157 commit e3b9cdc

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
import os
22+
import sys
23+
import configparser
24+
import re
25+
26+
def get_first_word(section_name):
27+
# Split the section name by any non-word character and return the first word
28+
return re.split(r'\W+', section_name)[0]
29+
30+
def update_ini_file(ini_file_path, env_prefix):
31+
# Read the existing INI file
32+
config = configparser.ConfigParser()
33+
config.read(ini_file_path)
34+
35+
# Flag to track if any updates were made
36+
updated = False
37+
38+
# Iterate over environment variables
39+
for key, value in os.environ.items():
40+
if env_prefix and not key.startswith(env_prefix):
41+
continue
42+
43+
stripped_key = key[len(env_prefix):] if env_prefix else key
44+
45+
# Iterate through sections
46+
for section in config.sections():
47+
first_word = get_first_word(section)
48+
prefix = first_word + '_'
49+
if stripped_key.startswith(prefix):
50+
config.set(section, stripped_key[len(prefix):], value)
51+
updated = True
52+
break
53+
elif config.has_option(section, stripped_key):
54+
config.set(section, stripped_key, value)
55+
updated = True
56+
break
57+
58+
# Write the updated INI file only if there were updates
59+
if updated:
60+
with open(ini_file_path, 'w') as configfile:
61+
config.write(configfile)
62+
63+
if __name__ == "__main__":
64+
if len(sys.argv) != 3:
65+
print("Usage: python3 update-ini-from-env.py <path_to_ini_file> <env_prefix>")
66+
sys.exit(1)
67+
68+
ini_file_path = sys.argv[1]
69+
env_prefix = sys.argv[2]
70+
update_ini_file(ini_file_path, env_prefix)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)