Class: MysqlCacheManager::CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_cache_manager/cache_manager.rb

Constant Summary collapse

USEFUL_METADATA =
[
  "buffer_pool_pages_total",
  "buffer_pool_pages_data",
  "buffer_pool_pages_misc",
  "buffer_pool_pages_free"
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_class, host, user, password) ⇒ CacheManager

Returns a new instance of CacheManager.



14
15
16
17
18
19
20
21
22
23
# File 'lib/mysql_cache_manager/cache_manager.rb', line 14

def initialize(image_class, host, user, password)
  @cache_image_class = image_class
  @mysql_host = host
  @mysql_user = user
  @mysql_password = password
  @image = nil
  @timing = Hash.new(0.0)

  connect
end

Instance Attribute Details

#imageObject

Returns the value of attribute image.



12
13
14
# File 'lib/mysql_cache_manager/cache_manager.rb', line 12

def image
  @image
end

#innodb_buffer_poolObject

Returns the value of attribute innodb_buffer_pool.



12
13
14
# File 'lib/mysql_cache_manager/cache_manager.rb', line 12

def innodb_buffer_pool
  @innodb_buffer_pool
end

#mysqlObject

Returns the value of attribute mysql.



12
13
14
# File 'lib/mysql_cache_manager/cache_manager.rb', line 12

def mysql
  @mysql
end

#timingObject

Returns the value of attribute timing.



12
13
14
# File 'lib/mysql_cache_manager/cache_manager.rb', line 12

def timing
  @timing
end

Instance Method Details

#connectObject



34
35
36
37
# File 'lib/mysql_cache_manager/cache_manager.rb', line 34

def connect
  @mysql = Mysql.new(@mysql_host, @mysql_user, @mysql_password)
  @innodb_buffer_pool = InnodbBufferPool.new(@mysql)
end

#restore_cache(filename, batch_size = 100) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mysql_cache_manager/cache_manager.rb', line 58

def restore_cache(filename, batch_size=100)
  track_timing("open") do
    @image = @cache_image_class.new(filename, false)
  end

  pages_attempted = 0
  pages_fetched = 0
  @image.each_space do |space|
    @image.each_page_batch(space, batch_size) do |page_batch|
      track_timing("fetch") do
        pages_attempted += page_batch.size
        pages_fetched += @innodb_buffer_pool.fetch_page(space, page_batch)
      end

      if block_given?
        track_timing("yield") do
          yield @mysql, pages_fetched, pages_attempted
        end
      end
    end
  end
  pages_fetched
end

#save_cache(filename) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mysql_cache_manager/cache_manager.rb', line 39

def save_cache(filename)
  track_timing("open") do
    @image = @cache_image_class.new(filename, true)
    @image.empty!
  end

  track_timing("stats") do
    @innodb_buffer_pool.status.each do |k, v|
      if USEFUL_METADATA.include? k
        @image.(k, v)
      end
    end
  end

  track_timing("save") do
    @image.save_pages(@innodb_buffer_pool.each_page)
  end
end

#track_timing(name) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/mysql_cache_manager/cache_manager.rb', line 25

def track_timing(name)
  raise "No block given" unless block_given?
  start_time = Time.now.to_f
  yield
  end_time = Time.now.to_f

  @timing[name] += (end_time - start_time)
end