Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# 

2# Copyright 2018 Canonical Ltd 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15"""Best-practice performance tuning of bcache devices on SD/NVMe based installations.""" 

16import os 

17import shutil 

18import subprocess 

19 

20from charmhelpers.core.hookenv import ( 

21 config, 

22 log, 

23 status_set, 

24) 

25 

26from charms.reactive import ( 

27 hook, 

28 set_flag, 

29 when, 

30) 

31 

32import yaml 

33 

34TUNING_CONFIG_FILE = "/etc/tune-bcache-config.yaml" 

35TUNING_MAP = { 

36 "cache_set": [ 

37 "congested_read_threshold_us", 

38 "congested_write_threshold_us", 

39 ], 

40 "backing_device": [ 

41 "readahead", 

42 "sequential_cutoff", 

43 "writeback_percent", 

44 "cache_mode", 

45 ], 

46} 

47 

48 

49def write_tuning_config(): 

50 """Persist configuration parameters to configuration file.""" 

51 conf = config() 

52 if conf["cache_mode"] not in [ 

53 "writethrough", 

54 "writeback", 

55 "writearound", 

56 "none", 

57 "unmanaged", 

58 ]: 

59 log( 

60 "illegal value for cache_mode ({})".format(conf["cache_mode"]), 

61 level="ERROR", 

62 ) 

63 conf["cache_mode"] = "unmanaged" 

64 

65 try: 

66 with open(TUNING_CONFIG_FILE, "r") as f: 

67 tunings = yaml.safe_load(f) 

68 except FileNotFoundError: 

69 tunings = {} 

70 

71 for section in TUNING_MAP.keys(): 

72 if section not in tunings: 

73 tunings[section] = {} 

74 for option in TUNING_MAP[section]: 

75 tunings[section][option] = conf[option] 

76 

77 with open(TUNING_CONFIG_FILE, "w") as f: 

78 yaml.safe_dump(tunings, f) 

79 

80 

81@hook("upgrade-charm") 

82def upgrade_bcache_tuning(): 

83 """Configure a systemd service to persist configurations across reboots.""" 

84 shutil.copyfile("files/tune-bcache", "/usr/sbin/tune-bcache") 

85 subprocess.check_call( 

86 [ 

87 "sed", 

88 "-e", 

89 "s:TUNING_CONFIG_FILE:{}:".format(TUNING_CONFIG_FILE), 

90 "-i", 

91 "/usr/sbin/tune-bcache", 

92 ] 

93 ) 

94 os.chmod("/usr/sbin/tune-bcache", 0o755) 

95 shutil.copyfile( 

96 "files/tune-bcache.service", "/lib/systemd/system/tune-bcache.service" 

97 ) 

98 write_tuning_config() 

99 subprocess.check_call(["systemctl", "daemon-reload"]) 

100 subprocess.check_call(["systemctl", "enable", "tune-bcache.service"]) 

101 subprocess.check_call(["systemctl", "start", "tune-bcache.service"]) 

102 

103 

104@hook("install") 

105def install_bcache_tuning(): 

106 """Install charm.""" 

107 upgrade_bcache_tuning() 

108 set_flag("bcache-tuning.installed") 

109 

110 

111@hook("stop") 

112def remove_bcache_tuning(): 

113 """Remove bcahe-tuning from unit.""" 

114 subprocess.check_call(["systemctl", "stop", "tune-bcache.service"]) 

115 subprocess.check_call(["systemctl", "disable", "tune-bcache.service"]) 

116 os.unlink("/lib/systemd/system/tune-bcache.service") 

117 os.unlink("/usr/sbin/tune-bcache") 

118 os.unlink(TUNING_CONFIG_FILE) 

119 subprocess.check_call(["systemctl", "daemon-reload"]) 

120 

121 

122@when("bcache-tuning.installed") 

123def assess_status(): 

124 """Keep Juju description up to date.""" 

125 status_set("active", "bcache devices tuned") 

126 

127 

128@when("config.changed") 

129def update_bcache_tuning(): 

130 """Update configuration parameters and restart systemd service.""" 

131 write_tuning_config() 

132 subprocess.check_call(["systemctl", "restart", "tune-bcache.service"])