class SqlPartitioner::BasePartitionsManager
Constants
- FUTURE_PARTITION_NAME
- FUTURE_PARTITION_VALUE
Attributes
Public Class Methods
@param [{Symbol=>Object}] options @options options [SqlPartitioner::BaseAdapter] :adapter for DB communication @options options [String] :table_name target table for the partition management operations @options options [Symbol] :time_unit to use for the table’s ‘timestamp` column, defaults to :seconds @options options [Fixnum] :current_time unix epoch in seconds @options options [Logger] :logger @options options [Fixnum] :lock_wait_timeout (in seconds) Each SQL
statement will be executed with `@@local.lock_wait_timeout`
having been temporarily set to this value. Background: Any partitioning statement must acquire a table lock on the partitioned table, and while it is waiting to acquire this lock, any subsequent queries on that table will be blocked and have to wait. It may take a long time to acquire a table lock if there were already long-running queries in progress. Therefore, setting a short timeout (e.g. 1 second) ensures the partitioning statement will timeout quickly, so any other SQL operations on that table will not be delayed. If the partitioning command times-out, it will have to be retried later.
# File lib/sql_partitioner/base_partitions_manager.rb, line 24 def initialize(options = {}) @adapter = options[:adapter] @tuc = TimeUnitConverter.new(options[:time_unit] || :seconds) @current_timestamp = @tuc.from_seconds((options[:current_time] || Time.now).to_i) @table_name = options[:table_name] @logger = options[:logger] @lock_wait_timeout = options[:lock_wait_timeout] end
Public Instance Methods
Drop partitions by name @param [Array<String>] partition_names array of String partition_names @param [Boolean] dry_run Defaults to false. If true, query wont be executed. @return [String] drop sql if dry run is true @raise [ArgumentError] if input is not an Array or if partition name is
not a string
# File lib/sql_partitioner/base_partitions_manager.rb, line 63 def drop_partitions(partition_names, dry_run = false) _validate_drop_partitions_names(partition_names) drop_sql = SqlPartitioner::SQL.drop_partitions(table_name, partition_names) _execute_and_display_partition_info(drop_sql, dry_run) end
initialize partitioning on the given table based on partition_data provided. partition data should be of form
{partition_name1 => partition_timestamp_1 , partition_name2 => partition_timestamp_2...}
For example:
{'until_2014_03_17' => 1395077901193149, 'until_2014_04_01' => 1396373901193398}
@param [Hash<String,Fixnum>] partition_data of form { partition_name1 => timestamp1..} @param [Boolean] dry_run Defaults to false. If true, query wont be executed. @raise [ArgumentError] if partition data is not hash or if one of name id
is not a String or if one of the value is not Integer
# File lib/sql_partitioner/base_partitions_manager.rb, line 48 def initialize_partitioning(partition_data, dry_run = false) partition_data = partition_data.merge(FUTURE_PARTITION_NAME => FUTURE_PARTITION_VALUE) _validate_partition_data(partition_data) init_sql = SqlPartitioner::SQL.initialize_partitioning(table_name, partition_data) _execute_and_display_partition_info(init_sql, dry_run) end
# File lib/sql_partitioner/base_partitions_manager.rb, line 89 def log(message, prefix = true) message = "[#{self.class.name}]#{message}" if prefix @logger.info "#{message}" end
generates name of for “until_yyyy_mm_dd” from the given timestamp. returns future partition name if value is FUTURE_PARTITION_VALUE
@param [Fixnum] timestamp timestamp for which the name has to be
generated.
@return [String] partition_name
# File lib/sql_partitioner/base_partitions_manager.rb, line 101 def name_from_timestamp(timestamp) if timestamp == FUTURE_PARTITION_VALUE FUTURE_PARTITION_NAME else seconds = @tuc.to_seconds(timestamp) "until_#{Time.at(seconds).utc.strftime("%Y_%m_%d")}" end end
Reorgs future partition into partitions provided as input.
@param [Hash<String,Fixnum>] partition_data of form { partition_name1 => timestamp1..} @param [Boolean] dry_run Defaults to false. If true, query wont be executed. @return [Boolean] true if not dry run and query is executed else false @return [String] sql if dry_run is true
# File lib/sql_partitioner/base_partitions_manager.rb, line 76 def reorg_future_partition(partition_data, dry_run = false) partition_data = partition_data.dup if partition_data.any? partition_data[FUTURE_PARTITION_NAME] = FUTURE_PARTITION_VALUE end _validate_partition_data(partition_data) reorg_sql = SqlPartitioner::SQL.reorg_partitions(table_name, partition_data, FUTURE_PARTITION_NAME) _execute_and_display_partition_info(reorg_sql, dry_run) end