Class: RedisRing::ShardConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_ring/shard_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shard_number, configuration) ⇒ ShardConfig

Returns a new instance of ShardConfig.



7
8
9
10
11
12
13
14
# File 'lib/redis_ring/shard_config.rb', line 7

def initialize(shard_number, configuration)
  unless shard_number >= 0 && shard_number < configuration.ring_size
    raise ArgumentError.new("shard number #{shard_number} must be between 0 and #{configuration.ring_size - 1}")
  end

  @shard_number = shard_number
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/redis_ring/shard_config.rb', line 5

def configuration
  @configuration
end

#shard_numberObject (readonly)

Returns the value of attribute shard_number.



5
6
7
# File 'lib/redis_ring/shard_config.rb', line 5

def shard_number
  @shard_number
end

Instance Method Details

#aof_file_nameObject



77
78
79
# File 'lib/redis_ring/shard_config.rb', line 77

def aof_file_name
  file('db_files', "shard-#{shard_number}.aof")
end

#aof_mtimeObject



85
86
87
# File 'lib/redis_ring/shard_config.rb', line 85

def aof_mtime
  mtime(aof_file_name)
end

#common_config_pathObject



93
94
95
# File 'lib/redis_ring/shard_config.rb', line 93

def common_config_path
  File.join(configuration.base_directory, "shared_config.conf")
end

#config_file_nameObject



33
34
35
# File 'lib/redis_ring/shard_config.rb', line 33

def config_file_name
  File.join(configuration.base_directory, 'configs', "shard-#{shard_number}.conf")
end

#db_file_nameObject



73
74
75
# File 'lib/redis_ring/shard_config.rb', line 73

def db_file_name
  file('db_files', "shard-#{shard_number}.rdb")
end

#db_mtimeObject



81
82
83
# File 'lib/redis_ring/shard_config.rb', line 81

def db_mtime
  mtime(db_file_name)
end

#hostObject



37
38
39
# File 'lib/redis_ring/shard_config.rb', line 37

def host
  configuration.host_name
end

#log_fileObject



49
50
51
# File 'lib/redis_ring/shard_config.rb', line 49

def log_file
  File.expand_path(file('logs', "shard-#{shard_number}.log"), working_directory)
end

#passwordObject



89
90
91
# File 'lib/redis_ring/shard_config.rb', line 89

def password
  configuration.password
end

#portObject



41
42
43
# File 'lib/redis_ring/shard_config.rb', line 41

def port
  configuration.base_port + shard_number + 1
end

#redis_pathObject



45
46
47
# File 'lib/redis_ring/shard_config.rb', line 45

def redis_path
  configuration.redis_path
end

#renderObject



16
17
18
19
# File 'lib/redis_ring/shard_config.rb', line 16

def render
  template = ERB.new(File.read(configuration.redis_config_template_path))
  return template.result(binding)
end

#saveObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redis_ring/shard_config.rb', line 21

def save
  FileUtils.mkdir_p(working_directory)

  ['configs', 'logs', 'vm_files', 'db_files'].each do |dir_name|
    FileUtils.mkdir_p(File.join(configuration.base_directory, dir_name))
  end

  FileUtils.touch(common_config_path) unless File.exist?(common_config_path)

  File.open(config_file_name, 'w') { |f| f.write(render) }
end

#vm_max_memoryObject



61
62
63
# File 'lib/redis_ring/shard_config.rb', line 61

def vm_max_memory
  configuration.total_max_memory / configuration.ring_size
end

#vm_page_sizeObject



69
70
71
# File 'lib/redis_ring/shard_config.rb', line 69

def vm_page_size
  configuration.vm_page_size
end

#vm_pagesObject



65
66
67
# File 'lib/redis_ring/shard_config.rb', line 65

def vm_pages
  configuration.total_vm_size / configuration.vm_page_size / configuration.ring_size
end

#vm_swap_fileObject



57
58
59
# File 'lib/redis_ring/shard_config.rb', line 57

def vm_swap_file
  file('vm_files', "shard-#{shard_number}.swap")
end

#working_directoryObject



53
54
55
# File 'lib/redis_ring/shard_config.rb', line 53

def working_directory
  "#{configuration.base_directory}/work/shard-#{shard_number}"
end