Class: FC::Storage

Inherits:
DbBase show all
Defined in:
lib/fc/storage.rb

Class Attribute Summary collapse

Attributes inherited from DbBase

#database_fields, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DbBase

create_from_fiels, #delete, find, #reload, #save, set_table, where

Constructor Details

#initialize(params = {}) ⇒ Storage

Returns a new instance of Storage.



32
33
34
35
36
37
38
39
40
# File 'lib/fc/storage.rb', line 32

def initialize(params = {})
  path = (params['path'] || params[:path])
  if path && !path.to_s.empty?
    path += '/' unless path[-1] == '/'
    raise "Storage path must be like '/bla/bla../'" unless path.match(/^\/.*\/$/)
    params['path'] = params[:path] = path
  end
  super params
end

Class Attribute Details

.check_time_limitObject

Returns the value of attribute check_time_limit.



9
10
11
# File 'lib/fc/storage.rb', line 9

def check_time_limit
  @check_time_limit
end

.get_copy_storages_mutexObject

Returns the value of attribute get_copy_storages_mutex.



9
10
11
# File 'lib/fc/storage.rb', line 9

def get_copy_storages_mutex
  @get_copy_storages_mutex
end

.storages_cache_timeObject

Returns the value of attribute storages_cache_time.



9
10
11
# File 'lib/fc/storage.rb', line 9

def storages_cache_time
  @storages_cache_time
end

Class Method Details

.curr_hostObject



15
16
17
# File 'lib/fc/storage.rb', line 15

def self.curr_host
  @uname || @uname = `uname -n`.chomp
end

.select_proper_storage_for_create(storages, size, exclude = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fc/storage.rb', line 19

def self.select_proper_storage_for_create(storages, size, exclude = [])
  list = storages.select do |storage|
    !exclude.include?(storage.name) && storage.up? && storage.size + size < storage.size_limit && storage.write_weight.to_i >= 0
  end
  list = yield(list) if block_given?
  # sort by random(free_rate * write_weight)
  list.map{ |storage| 
    [storage, Kernel.rand(storage.free_rate * (storage.write_weight.to_i == 0 ? 0.01 : storage.write_weight.to_i) * 1000000000)] 
  }.sort{ |a, b|
    a[1] <=> b[1]
  }.map{|el| el[0]}.last
end

Instance Method Details

#check_time_delayObject



71
72
73
# File 'lib/fc/storage.rb', line 71

def check_time_delay
  Time.new.to_i - check_time.to_i
end

#copy_path(local_path, file_name, try_move = false, speed_limit = nil) ⇒ Object

copy local_path to storage



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fc/storage.rb', line 80

def copy_path(local_path, file_name, try_move = false, speed_limit = nil)
  dst_path = "#{self.path}#{file_name}"

  cmd = "rm -rf #{dst_path.shellescape}; mkdir -p #{File.dirname(dst_path).shellescape}"
  cmd = self.class.curr_host == host ? cmd : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\""
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  
  op = try_move && self.class.curr_host == host && File.stat(local_path).dev == File.stat(File.dirname(dst_path)).dev ? 'mv' : 'cp -r'
  speed_limit = (speed_limit * 1000).to_i if speed_limit.to_i > 0
  cmd = self.class.curr_host == host ?
    "#{op} #{local_path.shellescape} #{dst_path.shellescape}" :
    "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{speed_limit.to_i > 0 ? '-l '+speed_limit.to_s : ''} #{local_path.shellescape} #{self.host}:\"#{dst_path.shellescape}\""
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
end

#copy_to_local(file_name, local_path, speed_limit = nil) ⇒ Object

copy object to local_path



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fc/storage.rb', line 98

def copy_to_local(file_name, local_path, speed_limit = nil)
  src_path = "#{self.path}#{file_name}"
  
  r = `rm -rf #{local_path.shellescape}; mkdir -p #{File.dirname(local_path).shellescape} 2>&1`
  raise r if $?.exitstatus != 0
  
  speed_limit = (speed_limit * 1000).to_i if speed_limit.to_i > 0
  cmd = self.class.curr_host == host ? 
    "cp -r #{src_path.shellescape} #{local_path.shellescape}" : 
    "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{speed_limit.to_i > 0 ? '-l '+speed_limit.to_s : ''} #{self.host}:\"#{src_path.shellescape}\" #{local_path.shellescape}"
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
end

#delete_file(file_name) ⇒ Object

delete object from storage



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fc/storage.rb', line 113

def delete_file(file_name)
  dst_path = "#{self.path}#{file_name}"
  cmd = self.class.curr_host == host ? 
    "rm -rf #{dst_path.shellescape}" : 
    "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"rm -rf #{dst_path.shellescape}\""
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  
  speed_limit = (speed_limit * 1000).to_i if speed_limit.to_i > 0
  cmd = self.class.curr_host == host ? 
    "ls -la #{dst_path.shellescape}" : 
    "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"ls -la #{dst_path.shellescape}\""
  r = `#{cmd} 2>/dev/null`
  raise "Path #{dst_path} not deleted" unless r.empty?
end

#file_size(file_name, ignore_errors = false) ⇒ Object

return object size on storage



130
131
132
133
134
135
136
137
138
139
# File 'lib/fc/storage.rb', line 130

def file_size(file_name, ignore_errors = false)
  dst_path = "#{self.path}#{file_name}"
  
  cmd = self.class.curr_host == host ? 
    "du -sb #{dst_path.shellescape}" : 
    "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"du -sb #{dst_path.shellescape}\""
  r = ignore_errors ? `#{cmd} 2>/dev/null` : `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  r.to_i
end

#freeObject



42
43
44
# File 'lib/fc/storage.rb', line 42

def free
  size_limit - size
end

#free_rateObject



50
51
52
53
# File 'lib/fc/storage.rb', line 50

def free_rate
  rate = free.to_f / size_limit
  rate < 0 ? 0.0 : rate
end

#get_copy_storagesObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/fc/storage.rb', line 55

def get_copy_storages
  self.class.get_copy_storages_mutex.synchronize do
    unless @copy_storages_cache && Time.new.to_i - @get_copy_storages_time.to_i < self.class.storages_cache_time
      @get_copy_storages_time = Time.new.to_i
      names = copy_storages.to_s.split(',').map{|s| "'#{s}'"}.join(',')
      @copy_storages_cache = names.empty? ? [] : FC::Storage.where("name IN (#{names}) ORDER BY FIELD(name, #{names})")
    end
  end
  @copy_storages_cache
end

#get_proper_storage_for_copy(size, exclude = []) ⇒ Object

get available storage for copy by size



153
154
155
# File 'lib/fc/storage.rb', line 153

def get_proper_storage_for_copy(size, exclude = [])
  FC::Storage.select_proper_storage_for_create(get_copy_storages, size, exclude)
end

#md5_sum(file_name) ⇒ Object

return object md5_sum on storage



142
143
144
145
146
147
148
149
150
# File 'lib/fc/storage.rb', line 142

def md5_sum(file_name)
  dst_path = "#{self.path}#{file_name}"
  cmd = self.class.curr_host == host ?
      "find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum" :
      "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print \\$1}' | sort | md5sum\""
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  r.to_s[0..31]
end

#size_rateObject



46
47
48
# File 'lib/fc/storage.rb', line 46

def size_rate
  size.to_f / size_limit
end

#up?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/fc/storage.rb', line 75

def up?
  check_time_delay < self.class.check_time_limit
end

#update_check_timeObject



66
67
68
69
# File 'lib/fc/storage.rb', line 66

def update_check_time
  self.check_time = Time.new.to_i
  save
end