filecluster 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ require 'optparse'
5
5
  require 'psych'
6
6
  require 'filecluster'
7
7
  require 'utils'
8
- require 'readline'
8
+ require 'rb-readline'
9
9
 
10
10
  descriptions = {
11
11
  :host => {:short => 'h', :full => 'host', :default => 'localhost', :text => 'mysql host name, default "localhost"', :save => true},
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = FC::VERSION
17
17
 
18
- gem.add_runtime_dependency "readline"
18
+ gem.add_runtime_dependency "rb-readline"
19
19
 
20
20
  gem.add_development_dependency "bundler"
21
21
  gem.add_development_dependency "test-unit"
@@ -4,8 +4,10 @@ module FC
4
4
  class Item < DbBase
5
5
  set_table :items, 'name, tag, outer_id, policy_id, dir, size, status, time, copies'
6
6
 
7
- # create item by local path
8
- # TODO проверка curr_host и local_path одному из доступных стораджей -> создание без копирования (для кусочков)
7
+ # Create item by local path.
8
+ # Additional options:
9
+ # :replace=true - replace item if it exists
10
+ # If item_name is part of local_path it prcessed as inplace - local_path is valid path to the item for policy
9
11
  def self.create_from_local(local_path, item_name, policy, options={})
10
12
  raise 'Path not exists' unless File.exists?(local_path)
11
13
  raise 'Policy is not FC::Policy' unless policy.instance_of?(FC::Policy)
@@ -16,9 +18,15 @@ module FC
16
18
  :size => `du -sb #{local_path}`.to_i
17
19
  })
18
20
  item_params.delete(:replace)
21
+ item_params.delete(:inplace)
19
22
  raise 'Name is empty' if item_params[:name].empty?
20
23
  raise 'Zero size path' if item_params[:size] == 0
21
24
 
25
+ if local_path.include?(item_name)
26
+ storage = policy.get_storages.detect{|s| local_path.index(s.path) == 0 && local_path.sub(s.path, '') == item_params[:name]}
27
+ FC::Error.raise "local_path #{local_path} is not valid path for policy ##{policy.id}" unless storage
28
+ end
29
+
22
30
  # new item?
23
31
  item = FC::Item.where('name=? AND policy_id=?', item_params[:name], policy.id).first
24
32
  if item
@@ -35,11 +43,16 @@ module FC
35
43
  end
36
44
  item.save
37
45
 
38
- storage = policy.get_proper_storage(item.size)
39
- FC::Error.raise 'No available storage', :item_id => item.id unless storage
46
+ if storage
47
+ item_storage = item.make_item_storage(storage, 'ready')
48
+ item.reload
49
+ else
50
+ storage = policy.get_proper_storage(item.size)
51
+ FC::Error.raise 'No available storage', :item_id => item.id unless storage
52
+ item_storage = item.make_item_storage(storage)
53
+ item.copy_item_storage(local_path, storage, item_storage)
54
+ end
40
55
 
41
- item_storage = item.make_item_storage(storage)
42
- item.copy_item_storage(local_path, storage, item_storage)
43
56
  return item
44
57
  end
45
58
 
@@ -4,8 +4,15 @@ module FC
4
4
  class Policy < DbBase
5
5
  set_table :policies, 'storages, copies'
6
6
 
7
+ class << self
8
+ attr_accessor :storages_cache_time
9
+ end
10
+ @storages_cache_time = 20 # ttl for storages cache
11
+
7
12
  def get_storages
8
- FC::Storage.where("name IN (#{storages.split(',').map{|s| "'#{s}'"}.join(',')})")
13
+ return @storages_cache if @storages_cache && Time.new.to_i - @get_storages_time.to_i < self.class.storages_cache_time
14
+ @get_storages_time = Time.new.to_i
15
+ @storages_cache = FC::Storage.where("name IN (#{storages.split(',').map{|s| "'#{s}'"}.join(',')})")
9
16
  end
10
17
 
11
18
  # get available storage for object by size
@@ -31,7 +31,7 @@ module FC
31
31
  end
32
32
 
33
33
  def up?
34
- Time.new.to_i - check_time.to_i <= self.class.check_time_limit
34
+ Time.new.to_i - check_time.to_i < self.class.check_time_limit
35
35
  end
36
36
 
37
37
  # copy local_path to storage
@@ -1,3 +1,3 @@
1
1
  module FC
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'readline'
1
+ require 'rb-readline'
2
2
  require "manage/policies"
3
3
  require "manage/storages"
4
4
  require "manage/show"
@@ -94,4 +94,9 @@ class FunctionalTest < Test::Unit::TestCase
94
94
  assert_equal errors_count+1, FC::Error.where.count, "Error not saved after check size"
95
95
  end
96
96
 
97
+ should "item create_from_local inplace" do
98
+ tmp_file_path = "/tmp/host2-sda/inplace_test"
99
+ `cp #{@@test_file_path} #{tmp_file_path}`
100
+ assert_nothing_raised { @item = FC::Item.create_from_local(tmp_file_path, 'inplace_test', @@policies[0]) }
101
+ end
97
102
  end
@@ -18,10 +18,18 @@ class PolicyTest < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  should "get_storages" do
21
+ FC::Policy.storages_cache_time = 10
21
22
  assert_same_elements @@storages.map(&:id), @@policy.get_storages.map(&:id)
23
+ FC::Storage.new(:name => 'rec2-sdc', :host => 'rec2').save
24
+ @@policy.storages = 'rec1-sda,rec2-sda,rec2-sdc'
25
+ @@policy.save
26
+ assert_equal @@storages.size, @@policy.get_storages.size
27
+ FC::Policy.storages_cache_time = 0
28
+ assert_equal @@storages.size+1, @@policy.get_storages.size
22
29
  end
23
30
 
24
31
  should "get_proper_storage" do
32
+ FC::Policy.storages_cache_time = 0
25
33
  assert_nil @@policy.get_proper_storage(1), 'all storages down'
26
34
  @@storages[0].update_check_time
27
35
  assert_equal @@storages[0].id, @@policy.get_proper_storage(1).id, 'first storages up'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filecluster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-12 00:00:00.000000000 Z
12
+ date: 2013-04-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: readline
15
+ name: rb-readline
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -184,12 +184,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
184
184
  - - ! '>='
185
185
  - !ruby/object:Gem::Version
186
186
  version: '0'
187
+ segments:
188
+ - 0
189
+ hash: 2060100301757257231
187
190
  required_rubygems_version: !ruby/object:Gem::Requirement
188
191
  none: false
189
192
  requirements:
190
193
  - - ! '>='
191
194
  - !ruby/object:Gem::Version
192
195
  version: '0'
196
+ segments:
197
+ - 0
198
+ hash: 2060100301757257231
193
199
  requirements: []
194
200
  rubyforge_project:
195
201
  rubygems_version: 1.8.24