Skip to content

Commit

Permalink
Added more stuffs
Browse files Browse the repository at this point in the history
- Added yard documentation for the tuning library.
- Modified the `value_with_units` method in tuning helper.
- Added .yardopts to do markdown markup.
- Updated gitignore.
  • Loading branch information
arangamani committed Jan 9, 2014
1 parent b9743de commit 454a196
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ bin/*
VERSION
.kitchen/
.kitchen.local.yml

doc/
.yardoc/
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--markup markdown
94 changes: 58 additions & 36 deletions libraries/tuning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@
# limitations under the License.
#

# The top level module for helpers in this cookbook.
module RsMysql
# The helper for calculating the MySQL tuning attributes.
module Tuning

# The constant to multiple memory (in megabypes) to obtain the gigabytes value.
GB = 1024

# Tunes the MySQL attributes based on the available memory and server usage type.
#
# @param node_tuning [Chef::Node] the chef node containing the MySQL tuning attributes
# @param memory [String, Integer] the total available system memory
# @param server_usage [String, Symbol] the server usage type. should be `'dedicated'` or `'shared'`
#
def self.tune_attributes(node_tuning, memory, server_usage)
factor = server_usage.to_s == 'dedicated' ? 1 : 0.5
memory = memory_in_megabytes(memory)
node_tuning['query_cache_size'] = value_with_units((memory * 0.01).to_i, 'M', factor)
node_tuning['innodb_buffer_pool_size'] = value_with_units((memory * 0.8).to_i, 'M', factor)
node_tuning['query_cache_size'] = value_with_units((memory * factor * 0.01).to_i, 'M')
node_tuning['innodb_buffer_pool_size'] = value_with_units((memory * factor * 0.8).to_i, 'M')

# Fixed parameters, common value for all memory categories.
#
Expand All @@ -36,23 +45,23 @@ def self.tune_attributes(node_tuning, memory, server_usage)
node_tuning['net_read_timeout'] = (30 * factor).to_i
node_tuning['net_write_timeout'] = (30 * factor).to_i
node_tuning['back_log'] = (128 * factor).to_i
node_tuning['max_heap_table_size'] = value_with_units(32, 'M', factor)
node_tuning['read_buffer_size'] = value_with_units(1, 'M', factor)
node_tuning['read_rnd_buffer_size'] = value_with_units(4, 'M', factor)
node_tuning['max_heap_table_size'] = value_with_units((32 * factor).to_i, 'M')
node_tuning['read_buffer_size'] = value_with_units((1 * factor).to_i, 'M')
node_tuning['read_rnd_buffer_size'] = value_with_units((4 * factor).to_i, 'M')
node_tuning['long_query_time'] = 5

# Sets buffer sizes and InnoDB log properties. Overrides buffer sizes for really small servers.
#
if memory < 1 * GB
node_tuning['key_buffer'] = value_with_units(16, 'M', factor)
node_tuning['max_allowed_packet'] = value_with_units(20, 'M', factor)
node_tuning['innodb_log_file_size'] = value_with_units(4, 'M', factor)
node_tuning['innodb_log_buffer_size'] = value_with_units(16, 'M', factor)
node_tuning['key_buffer'] = value_with_units((16 * factor).to_i, 'M')
node_tuning['max_allowed_packet'] = value_with_units((20 * factor).to_i, 'M')
node_tuning['innodb_log_file_size'] = value_with_units((4 * factor).to_i, 'M')
node_tuning['innodb_log_buffer_size'] = value_with_units((16 * factor).to_i, 'M')
else
node_tuning['key_buffer'] = value_with_units(128, 'M', factor)
node_tuning['max_allowed_packet'] = value_with_units(128, 'M', factor)
node_tuning['innodb_log_file_size'] = value_with_units(64, 'M', factor)
node_tuning['innodb_log_buffer_size'] = value_with_units(8, 'M', factor)
node_tuning['key_buffer'] = value_with_units((128 * factor).to_i, 'M')
node_tuning['max_allowed_packet'] = value_with_units((128 * factor).to_i, 'M')
node_tuning['innodb_log_file_size'] = value_with_units((64 * factor).to_i, 'M')
node_tuning['innodb_log_buffer_size'] = value_with_units((8 * factor).to_i, 'M')
end

# Adjusts tunable values based on available system memory range.
Expand All @@ -65,51 +74,64 @@ def self.tune_attributes(node_tuning, memory, server_usage)
#
if memory < 3 * GB
node_tuning['table_open_cache'] = (256 * factor).to_i
node_tuning['sort_buffer_size'] = value_with_units(2, 'M', factor)
node_tuning['innodb_additional_mem_pool_size'] = value_with_units(50, 'M', factor)
node_tuning['myisam_sort_buffer_size'] = value_with_units(64, 'M', factor)
node_tuning['sort_buffer_size'] = value_with_units((2 * factor).to_i, 'M')
node_tuning['innodb_additional_mem_pool_size'] = value_with_units((50 * factor).to_i, 'M')
node_tuning['myisam_sort_buffer_size'] = value_with_units((64 * factor).to_i, 'M')
elsif memory < 10 * GB
node_tuning['table_open_cache'] = (512 * factor).to_i
node_tuning['sort_buffer_size'] = value_with_units(4, 'M', factor)
node_tuning['innodb_additional_mem_pool_size'] = value_with_units(200, 'M', factor)
node_tuning['myisam_sort_buffer_size'] = value_with_units(96, 'M', factor)
node_tuning['sort_buffer_size'] = value_with_units((4 * factor).to_i, 'M')
node_tuning['innodb_additional_mem_pool_size'] = value_with_units((200 * factor).to_i, 'M')
node_tuning['myisam_sort_buffer_size'] = value_with_units((96 * factor).to_i, 'M')
elsif memory < 25 * GB
node_tuning['table_open_cache'] = (1024 * factor).to_i
node_tuning['sort_buffer_size'] = value_with_units(8, 'M', factor)
node_tuning['innodb_additional_mem_pool_size'] = value_with_units(300, 'M', factor)
node_tuning['myisam_sort_buffer_size'] = value_with_units(128, 'M', factor)
node_tuning['sort_buffer_size'] = value_with_units((8 * factor).to_i, 'M')
node_tuning['innodb_additional_mem_pool_size'] = value_with_units((300 * factor).to_i, 'M')
node_tuning['myisam_sort_buffer_size'] = value_with_units((128 * factor).to_i, 'M')
elsif memory < 50 * GB
node_tuning['table_open_cache'] = (2048 * factor).to_i
node_tuning['sort_buffer_size'] = value_with_units(16, 'M', factor)
node_tuning['innodb_additional_mem_pool_size'] = value_with_units(400, 'M', factor)
node_tuning['myisam_sort_buffer_size'] = value_with_units(256, 'M', factor)
node_tuning['sort_buffer_size'] = value_with_units((16 * factor).to_i, 'M')
node_tuning['innodb_additional_mem_pool_size'] = value_with_units((400 * factor).to_i, 'M')
node_tuning['myisam_sort_buffer_size'] = value_with_units((256 * factor).to_i, 'M')
else
node_tuning['table_open_cache'] = (4096 * factor).to_i
node_tuning['sort_buffer_size'] = value_with_units(32, 'M', factor)
node_tuning['innodb_additional_mem_pool_size'] = value_with_units(500, 'M', factor)
node_tuning['myisam_sort_buffer_size'] = value_with_units(512, 'M', factor)
node_tuning['sort_buffer_size'] = value_with_units((32 * factor).to_i, 'M')
node_tuning['innodb_additional_mem_pool_size'] = value_with_units((500 * factor).to_i, 'M')
node_tuning['myisam_sort_buffer_size'] = value_with_units((512 * factor).to_i, 'M')
end
end

private

# Given the memory in either kilobytes as a String or in bytes as an Integer (the formats Ohai returns),
# this method will return the memory in megabytes.
#
# @param memory [String, Integer] memory in kilobytes or bytes
#
# @return [Numeric] memory in megabytes
#
def self.memory_in_megabytes(memory)
# If ohai returns the total memory in String, it will most likely be in kB.
if memory.is_a?(String) && memory =~ /\d+kB/i
memory.to_i / 1024
# If it returns an integer, it will most likely be in bytes.
elsif memory.is_a?(Integer)
memory / (1024 * 1024)
else
nil
memory / (1024 * 1024)
end
end

def self.value_with_units(value, unit, factor)
raise 'Value must convert to an integer.' unless value.to_i
raise 'Units must be k, K, m, M, g, G' unless unit =~ /[KMG]/i
raise "Factor must be between 1.0 and 0.0. You gave: #{factor}" if factor > 1.0 || factor < 0.0
"#{(value * factor).to_i}#{unit}"
# Given the value, unit, this method will return the value with the unit.
#
# @param value [Integer] the value of a tunable attribute
# @param unit [String] the unit. Should be one of `'k'`, `'K'`, `'m'`, `'M'`, `'g'`, `'G'`.
#
# @return [String] the value with the unit.
#
# @example An Example
# >> value_with_units(8, 'M')
# => '8M'
#
def self.value_with_units(value, unit)
"#{value}#{unit}"
end
end
end

0 comments on commit 454a196

Please sign in to comment.