16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/daemon/copy_task_thread.rb', line 16
def make_copy(task)
begin
task.reload
if task.status != 'copy'
$log.warn("Task ##{task.id} status #{task.status} != copy, skip")
return
end
rescue
$log.warn("Task ##{task.id} not found in db, skip")
return
end
sleep 0.1 while $copy_count > FC::Var.get('daemon_copy_tasks_per_host_limit', 10).to_i
limit = FC::Var.get_current_speed_limit
speed_limit = nil
if limit
if $copy_count == 0
$copy_speed = 0
speed_limit = limit * 0.75
elsif $copy_count <= $copy_cont_avg && (speed_limit = limit / $copy_cont_avg) < limit * 1.1 - $copy_speed
else
while (speed_limit = (limit - $copy_speed) * 0.75) < limit*0.1 do
sleep 0.1
end
end
end
$copy_count += 1
$copy_speed += speed_limit if speed_limit
calc_avg_count()
storage = $storages.detect{|s| s.name == task.storage_name}
begin
item = FC::Item.find(task.item_id)
rescue Exception => e
if e.message.match('Record not found')
$log.warn("Item ##{task.item_id} not found before copy")
task.delete
return nil
else
raise e
end
end
return nil unless item && item.status == 'ready'
available_src_storages = FC::ItemStorage.where("item_id = ? AND status = 'ready'", item.id)
.map do |item_storage|
$all_storages.detect { |a_storage| a_storage.name == item_storage.storage_name && a_storage.up? }
end.compact
src_storage = available_src_storages.detect { |src| src.host == storage.host }
src_storage = available_src_storages.detect { |src| src.dc == storage.dc } unless src_storage
src_storage = available_src_storages.sample unless src_storage
unless src_storage
$log.warn("Item ##{item.id} #{item.name} has no ready item_storage or storage")
return nil
end
$log.debug("Copy from #{src_storage.name} to #{storage.name} #{storage.path}#{item.name}")
item.copy_item_storage(src_storage, storage, task, false, speed_limit)
rescue Exception => e
error "Copy item_storage error: #{e.message}; #{e.backtrace.join(', ')}", :item_id => task.item_id, :item_storage_id => task.id
$curr_tasks.delete(task)
ensure
$copy_count -= 1 if $copy_count > 0
$copy_speed -= speed_limit if speed_limit
end
|