content_server 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. data/bin/backup_server +8 -20
  2. data/bin/content_server +8 -20
  3. data/bin/testing_memory +60 -0
  4. data/bin/testing_server +57 -0
  5. data/ext/run_in_background/mkrf_conf.rb +34 -0
  6. data/lib/content_data/content_data.rb +613 -0
  7. data/lib/content_data/version.rb +3 -0
  8. data/lib/content_data.rb +6 -0
  9. data/lib/content_server/backup_server.rb +65 -86
  10. data/lib/content_server/content_server.rb +47 -77
  11. data/lib/content_server/file_streamer.rb +27 -33
  12. data/lib/content_server/queue_copy.rb +154 -49
  13. data/lib/content_server/queue_indexer.rb +19 -11
  14. data/lib/content_server/remote_content.rb +41 -23
  15. data/lib/content_server/server.rb +91 -0
  16. data/lib/content_server/version.rb +1 -1
  17. data/lib/content_server.rb +0 -15
  18. data/lib/email/email.rb +87 -0
  19. data/lib/email/version.rb +3 -0
  20. data/lib/email.rb +4 -0
  21. data/lib/file_copy/copy.rb +68 -0
  22. data/lib/file_copy/version.rb +4 -0
  23. data/lib/file_copy.rb +4 -0
  24. data/lib/file_indexing/index_agent.rb +170 -0
  25. data/lib/file_indexing/indexer_patterns.rb +72 -0
  26. data/lib/file_indexing/version.rb +3 -0
  27. data/lib/file_indexing.rb +9 -0
  28. data/lib/file_monitoring/file_monitoring.rb +105 -0
  29. data/lib/file_monitoring/monitor_path.rb +304 -0
  30. data/lib/file_monitoring/version.rb +3 -0
  31. data/lib/file_monitoring.rb +29 -0
  32. data/lib/file_utils/file_generator/README +97 -0
  33. data/lib/file_utils/file_generator/file_generator.rb +156 -0
  34. data/lib/file_utils/file_utils.rb +260 -0
  35. data/lib/file_utils/version.rb +3 -0
  36. data/lib/file_utils.rb +4 -0
  37. data/lib/log/version.rb +3 -0
  38. data/lib/log.rb +188 -0
  39. data/lib/networking/tcp.rb +213 -0
  40. data/lib/networking/version.rb +3 -0
  41. data/lib/networking.rb +4 -0
  42. data/lib/params/version.rb +3 -0
  43. data/lib/params.rb +419 -0
  44. data/lib/process_monitoring/monitoring.rb +85 -0
  45. data/lib/process_monitoring/monitoring_info.rb +79 -0
  46. data/lib/process_monitoring/send_email.rb +40 -0
  47. data/lib/process_monitoring/thread_safe_hash.rb +77 -0
  48. data/lib/process_monitoring/version.rb +3 -0
  49. data/lib/process_monitoring.rb +6 -0
  50. data/lib/run_in_background/version.rb +3 -0
  51. data/lib/run_in_background.rb +432 -0
  52. data/lib/testing_memory/testing_memory.rb +187 -0
  53. data/lib/testing_server/testing_server.rb +236 -0
  54. data/lib/testing_server/version.rb +3 -0
  55. data/lib/testing_server.rb +12 -0
  56. data/lib/validations/index_validations.rb +106 -0
  57. data/lib/validations/version.rb +3 -0
  58. data/lib/validations.rb +4 -0
  59. data/spec/content_data/validations_spec.rb +113 -0
  60. data/spec/file_copy/copy_spec.rb +54 -0
  61. data/spec/file_indexing/index_agent_spec.rb +53 -0
  62. data/spec/networking/tcp_spec.rb +95 -0
  63. data/spec/validations/index_validations_spec.rb +77 -0
  64. data/test/content_data/content_data_test.rb +290 -0
  65. data/test/file_generator/file_generator_spec.rb +84 -0
  66. data/test/file_indexing/index_agent_test/New.txt +0 -0
  67. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/bin/libexslt.dll +0 -0
  68. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/bin/libxslt.dll +0 -0
  69. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/bin/xsltproc.exe +0 -0
  70. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libexslt/exslt.h +102 -0
  71. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libexslt/exsltconfig.h +73 -0
  72. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libexslt/exsltexports.h +140 -0
  73. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libexslt/libexslt.h +29 -0
  74. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/attributes.h +38 -0
  75. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/documents.h +93 -0
  76. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/extensions.h +262 -0
  77. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/extra.h +80 -0
  78. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/functions.h +78 -0
  79. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/imports.h +75 -0
  80. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/keys.h +53 -0
  81. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/libxslt.h +30 -0
  82. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/namespaces.h +68 -0
  83. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/numbersInternals.h +69 -0
  84. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/pattern.h +81 -0
  85. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/preproc.h +43 -0
  86. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/security.h +104 -0
  87. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/templates.h +77 -0
  88. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/transform.h +207 -0
  89. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/trio.h +216 -0
  90. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/triodef.h +220 -0
  91. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/variables.h +91 -0
  92. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/win32config.h +101 -0
  93. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xslt.h +103 -0
  94. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltInternals.h +1967 -0
  95. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltconfig.h +172 -0
  96. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltexports.h +142 -0
  97. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltlocale.h +57 -0
  98. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltutils.h +309 -0
  99. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/include/libxslt/xsltwin32config.h +105 -0
  100. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/lib/libexslt.lib +0 -0
  101. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/lib/libexslt_a.lib +0 -0
  102. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/lib/libxslt.lib +0 -0
  103. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/lib/libxslt_a.lib +0 -0
  104. data/test/file_indexing/index_agent_test/libxslt-1.1.26.win32/readme.txt +22 -0
  105. data/test/file_indexing/index_agent_test/patterns.input +3 -0
  106. data/test/file_indexing/index_agent_test.rb +51 -0
  107. data/test/file_monitoring/file_monitoring_test/conf.yml +4 -0
  108. data/test/file_monitoring/file_monitoring_test/conf_win32.yml +5 -0
  109. data/test/file_monitoring/file_monitoring_test/log +56 -0
  110. data/test/file_monitoring/file_monitoring_test.rb +0 -0
  111. data/test/file_monitoring/monitor_path_test/dir1000/test_file.1000 +1000 -0
  112. data/test/file_monitoring/monitor_path_test/dir1000/test_file.1000.0 +1000 -0
  113. data/test/file_monitoring/monitor_path_test/dir1000/test_file.1000.1 +1000 -0
  114. data/test/file_monitoring/monitor_path_test/dir1500/test_file.1500 +1500 -0
  115. data/test/file_monitoring/monitor_path_test/dir1500/test_file.1500.0 +1500 -0
  116. data/test/file_monitoring/monitor_path_test/dir1500/test_file.1500.1 +1500 -0
  117. data/test/file_monitoring/monitor_path_test/test_file.500 +500 -0
  118. data/test/file_monitoring/monitor_path_test/test_file.500.0 +500 -0
  119. data/test/file_monitoring/monitor_path_test/test_file.500.1 +500 -0
  120. data/test/file_monitoring/monitor_path_test.rb +153 -0
  121. data/test/file_utils/fileutil_mksymlink_test/dir1000/dir1500/test_file.1500 +1500 -0
  122. data/test/file_utils/fileutil_mksymlink_test/dir1000/dir1500/test_file.1500.0 +1500 -0
  123. data/test/file_utils/fileutil_mksymlink_test/dir1000/dir1500/test_file.1500.1 +1500 -0
  124. data/test/file_utils/fileutil_mksymlink_test/dir1000/test_file.1000 +1000 -0
  125. data/test/file_utils/fileutil_mksymlink_test/dir1000/test_file.1000.0 +1000 -0
  126. data/test/file_utils/fileutil_mksymlink_test/dir1000/test_file.1000.1 +1000 -0
  127. data/test/file_utils/fileutil_mksymlink_test/test_file.500 +500 -0
  128. data/test/file_utils/fileutil_mksymlink_test/test_file.500.0 +500 -0
  129. data/test/file_utils/fileutil_mksymlink_test/test_file.500.1 +500 -0
  130. data/test/file_utils/fileutil_mksymlink_test.rb +125 -0
  131. data/test/file_utils/time_modification_test.rb +132 -0
  132. data/test/params/params_spec.rb +280 -0
  133. data/test/params/params_test.rb +43 -0
  134. data/test/run_in_background/run_in_background_test.rb +122 -0
  135. data/test/run_in_background/test_app +57 -0
  136. metadata +272 -132
  137. data/lib/content_server/globals.rb +0 -10
@@ -7,9 +7,9 @@ require 'content_server/content_receiver'
7
7
  require 'content_server/queue_indexer'
8
8
  require 'content_server/queue_copy'
9
9
  require 'content_server/remote_content'
10
+ require 'content_server/server'
10
11
  require 'file_indexing'
11
12
  require 'file_monitoring'
12
- require 'content_server/globals'
13
13
  require 'log'
14
14
  require 'networking/tcp'
15
15
  require 'params'
@@ -28,14 +28,6 @@ module ContentServer
28
28
  [{'path'=>File.expand_path(''), 'scan_period'=>300, 'stable_state'=>1}],
29
29
  'Backup server destination folder, default is the relative local folder.')
30
30
 
31
- def self.tmp_content_data_file
32
- @@tmp_content_data_file
33
- end
34
-
35
- def self.local_dynamic_content_data
36
- @@local_dynamic_content_data
37
- end
38
-
39
31
  def run_backup_server
40
32
  Log.info('Start backup server')
41
33
  Thread.abort_on_exception = true
@@ -44,11 +36,11 @@ module ContentServer
44
36
  # create general tmp dir
45
37
  FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
46
38
  # init tmp content data file
47
- @@tmp_content_data_file = File.join(Params['tmp_path'], 'backup.data')
39
+ $tmp_content_data_file = File.join(Params['tmp_path'], 'backup.data')
48
40
 
49
41
  if Params['enable_monitoring']
50
42
  Log.info("Initializing monitoring of process params on port:#{Params['process_monitoring_web_port']}")
51
- ::ContentServer::Globals.process_vars.set('server_name', 'backup_server')
43
+ $process_vars.set('server_name', 'backup_server')
52
44
  end
53
45
 
54
46
  # # # # # # # # # # # #
@@ -62,16 +54,27 @@ module ContentServer
62
54
  Log.info(" Path:'#{path['path']}'")
63
55
  }
64
56
 
57
+ # initial global local content data object
58
+ $local_content_data_lock = Mutex.new
59
+ $local_content_data = ContentData::ContentData.new
60
+
65
61
  # Read here for initial content data that exist from previous system run
66
- initial_content_data = ContentData::ContentData.new
67
62
  content_data_path = Params['local_content_data_path']
68
- initial_content_data.from_file(content_data_path) if File.exists?(content_data_path)
69
- # Update local dynamic content with existing content
70
- @@local_dynamic_content_data = ContentData::DynamicContentData.new
71
- @@local_dynamic_content_data.update(initial_content_data)
63
+ if File.exists?(content_data_path) and !File.directory?(content_data_path)
64
+ Log.info("reading initial content data that exist from previous system run from file:#{content_data_path}")
65
+ $local_content_data.from_file(content_data_path)
66
+ else
67
+ if File.directory?(content_data_path)
68
+ raise("Param:'local_content_data_path':'#{Params['local_content_data_path']}'cannot be a directory name")
69
+ end
70
+ # create directory if needed
71
+ dir = File.dirname(Params['local_content_data_path'])
72
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
73
+ end
72
74
 
75
+ Log.info("Init monitoring")
73
76
  monitoring_events = Queue.new
74
- fm = FileMonitoring::FileMonitoring.new(@@local_dynamic_content_data)
77
+ fm = FileMonitoring::FileMonitoring.new()
75
78
  fm.set_event_queue(monitoring_events)
76
79
  # Start monitoring and writing changes to queue
77
80
  all_threads << Thread.new do
@@ -81,36 +84,46 @@ module ContentServer
81
84
  # # # # # # # # # # # # # #
82
85
  # Initialize/Start local indexer
83
86
  Log.debug1('Start indexer')
84
- queue_indexer = QueueIndexer.new(monitoring_events, @@local_dynamic_content_data)
87
+ queue_indexer = QueueIndexer.new(monitoring_events)
85
88
  # Start indexing on demand and write changes to queue
86
89
  all_threads << queue_indexer.run
87
90
 
88
91
  # # # # # # # # # # # # # # # # # # # # # # # #
89
- # Start dump local content data to file thread
90
- Log.debug1('Start dump local content data to file thread')
92
+ # thread: Start dump local content data to file
93
+ Log.debug1('Init thread: flush local content data to file')
91
94
  all_threads << Thread.new do
92
- last_data_flush_time = nil
93
- while true do
94
- if last_data_flush_time.nil? || last_data_flush_time + Params['data_flush_delay'] < Time.now.to_i
95
- Log.info "Writing local content data to #{Params['local_content_data_path']}."
96
- @@local_dynamic_content_data.last_content_data.to_file(@@tmp_content_data_file)
97
- sleep(0.1) # Added to prevent mv access issue
98
- ::FileUtils.mv(@@tmp_content_data_file, Params['local_content_data_path'])
99
- last_data_flush_time = Time.now.to_i
100
- end
101
- sleep(1)
102
- end
95
+ FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
96
+ last_content_data_id = nil
97
+ loop{
98
+ sleep(Params['data_flush_delay'])
99
+ Log.info('Start flush local content data to file.')
100
+ $testing_memory_log.info('Start flush content data to file') if $testing_memory_log
101
+ written_to_file = false
102
+ $local_content_data_lock.synchronize{
103
+ local_content_data_unique_id = $local_content_data.unique_id
104
+ if (local_content_data_unique_id != last_content_data_id)
105
+ last_content_data_id = local_content_data_unique_id
106
+ $local_content_data.to_file($tmp_content_data_file)
107
+ written_to_file = true
108
+ else
109
+ Log.debug1('no need to flush. content data has not changed')
110
+ end
111
+ }
112
+ File.rename($tmp_content_data_file, Params['local_content_data_path']) if written_to_file
113
+ $testing_memory_log.info("End flush content data to file") if $testing_memory_log
114
+ }
103
115
  end
104
- content_server_dynamic_content_data = ContentData::DynamicContentData.new
105
- remote_content = ContentServer::RemoteContentClient.new(content_server_dynamic_content_data,
106
- Params['content_server_hostname'],
116
+
117
+ #initialize global - remote content data
118
+ $remote_content_data_lock = Mutex.new
119
+ $remote_content_data = ContentData::ContentData.new
120
+ remote_content = ContentServer::RemoteContentClient.new(Params['content_server_hostname'],
107
121
  Params['content_server_data_port'],
108
122
  Params['backup_destination_folder'][0]['path'])
109
123
  all_threads.concat(remote_content.run())
110
124
 
111
125
  file_copy_client = FileCopyClient.new(Params['content_server_hostname'],
112
- Params['content_server_files_port'],
113
- @@local_dynamic_content_data)
126
+ Params['content_server_files_port'])
114
127
  all_threads.concat(file_copy_client.threads)
115
128
 
116
129
  # Each
@@ -118,21 +131,21 @@ module ContentServer
118
131
  all_threads << Thread.new do
119
132
  loop do
120
133
  sleep(Params['backup_check_delay'])
121
- local_cd = @@local_dynamic_content_data.last_content_data()
122
- remote_cd = content_server_dynamic_content_data.last_content_data()
123
- diff = ContentData.remove(local_cd, remote_cd)
124
-
125
- Log.debug2("Backup content:\n#{local_cd}")
126
- Log.debug2("Remote content:\n#{remote_cd}")
127
- Log.debug2("Diff content:\n#{diff}")
128
-
129
- unless diff.nil? || diff.empty?
130
- Log.info('Start sync check. Backup and remote contents need a sync, requesting copy files:')
131
- Log.info("Missing contents:\n#{diff}")
132
- file_copy_client.request_copy(diff)
133
- else
134
- Log.info("Start sync check. Local and remote contents are equal. No sync required.")
135
- end
134
+ $local_content_data_lock.synchronize{
135
+ $remote_content_data_lock.synchronize{
136
+ diff = ContentData.remove($local_content_data, $remote_content_data)
137
+ unless diff.nil? || diff.empty?
138
+ Log.debug2("Backup content:\n#{$local_content_data}")
139
+ Log.debug2("Remote content:\n#{$remote_content_data}")
140
+ Log.debug2("Missing contents:\n#{diff}")
141
+ Log.info('Start sync check. Backup and remote contents need a sync, requesting copy files:')
142
+ file_copy_client.request_copy(diff)
143
+ else
144
+ Log.info("Start sync check. Local and remote contents are equal. No sync required.")
145
+ end
146
+ diff = ContentData::ContentData.new # to clear the memory
147
+ }
148
+ }
136
149
  end
137
150
  end
138
151
 
@@ -141,41 +154,7 @@ module ContentServer
141
154
  if Params['enable_monitoring']
142
155
  monitoring_info = MonitoringInfo::MonitoringInfo.new()
143
156
  all_threads << Thread.new do
144
- mutex = Mutex.new
145
- while true do
146
- sleep(Params['process_vars_delay'])
147
- ::ContentServer::Globals.process_vars.set('time', Time.now)
148
- Log.debug3("process_vars:monitoring queue size:#{monitoring_events.size}")
149
- ::ContentServer::Globals.process_vars.set('monitoring queue', monitoring_events.size)
150
- #enable following line to see full list of object:count
151
- #obj_array = ''
152
- total_obj_count = 0
153
- string_count = 0
154
- file_count = 0
155
- dir_count = 0
156
- content_count = 0
157
- mutex.synchronize do
158
- ObjectSpace.each_object(Class) {|obj|
159
- obj_count_per_class = ObjectSpace.each_object(obj).count
160
- #enable following line to see full list of object:count
161
- #obj_array = "#{obj_array} * #{obj.name}:#{obj_count_per_class}"
162
- total_obj_count = total_obj_count + obj_count_per_class
163
- }
164
- string_count = ObjectSpace.each_object(String).count
165
- file_count = ObjectSpace.each_object(::FileMonitoring::FileStat).count
166
- dir_count = ObjectSpace.each_object(::FileMonitoring::DirStat).count
167
- content_count = ObjectSpace.each_object(::ContentData::ContentData).count
168
- end
169
- #enable following line to see full list of object:count
170
- #::ContentServer::Globals.process_vars.set('Live objs full', obj_array)
171
- Log.debug3("process_vars:Live objs cnt:#{total_obj_count}")
172
- ::ContentServer::Globals.process_vars.set('Live objs cnt', total_obj_count)
173
- Log.debug3("process_vars:Live String obj cnt:#{string_count}")
174
- Log.debug3("process_vars:Live File obj cnt:#{file_count}")
175
- Log.debug3("process_vars:Live Dir obj cnt:#{dir_count}")
176
- Log.debug3("process_vars:Live Content data obj cnt:#{content_count}")
177
- ::ContentServer::Globals.process_vars.set('Live String obj cnt', string_count)
178
- end
157
+ ContentServer.monitor_general_process_vars
179
158
  end
180
159
  end
181
160
 
@@ -9,7 +9,7 @@ require 'content_server/queue_copy'
9
9
  require 'content_server/remote_content'
10
10
  require 'file_indexing'
11
11
  require 'file_monitoring'
12
- require 'content_server/globals'
12
+ require 'content_server/server'
13
13
  require 'log'
14
14
  require 'networking/tcp'
15
15
  require 'params'
@@ -21,16 +21,6 @@ module ContentServer
21
21
  # Content server specific flags.
22
22
  Params.integer('local_files_port', 4444, 'Remote port in backup server to copy files')
23
23
  Params.integer('local_content_data_port', 3333, 'Listen to incoming content data requests')
24
- Params.string('local_server_name', `hostname`.strip, 'local server name')
25
- Params.path('tmp_path', '~/.bbfs/tmp', 'tmp path for temporary files')
26
-
27
- def self.tmp_content_data_file
28
- @@tmp_content_data_file
29
- end
30
-
31
- def self.local_dynamic_content_data
32
- @@local_dynamic_content_data
33
- end
34
24
 
35
25
  def run_content_server
36
26
  Log.info('Content server start')
@@ -39,11 +29,11 @@ module ContentServer
39
29
  # create general tmp dir
40
30
  FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
41
31
  # init tmp content data file
42
- @@tmp_content_data_file = File.join(Params['tmp_path'], 'content.data')
32
+ $tmp_content_data_file = File.join(Params['tmp_path'], 'content.data')
43
33
 
44
34
  if Params['enable_monitoring']
45
35
  Log.info("Initializing monitoring of process params on port:#{Params['process_monitoring_web_port']}")
46
- ::ContentServer::Globals.process_vars.set('server_name', 'content_server')
36
+ $process_vars.set('server_name', 'content_server')
47
37
  end
48
38
 
49
39
  # # # # # # # # # # # #
@@ -54,16 +44,27 @@ module ContentServer
54
44
  }
55
45
  monitoring_events = Queue.new
56
46
 
47
+ # initial global local content data object
48
+ $local_content_data_lock = Mutex.new
49
+ $local_content_data = ContentData::ContentData.new
50
+
57
51
  # Read here for initial content data that exist from previous system run
58
- initial_content_data = ContentData::ContentData.new
59
52
  content_data_path = Params['local_content_data_path']
60
- initial_content_data.from_file(content_data_path) if File.exists?(content_data_path)
61
- # Update local dynamic content with existing content
62
- @@local_dynamic_content_data = ContentData::DynamicContentData.new
63
- @@local_dynamic_content_data.update(initial_content_data)
53
+ if File.exists?(content_data_path) and !File.directory?(content_data_path)
54
+ Log.info("reading initial content data that exist from previous system run from file:#{content_data_path}")
55
+ $local_content_data.from_file(content_data_path)
56
+ else
57
+ if File.directory?(content_data_path)
58
+ raise("Param:'local_content_data_path':'#{Params['local_content_data_path']}'cannot be a directory name")
59
+ end
60
+ # create directory if needed
61
+ dir = File.dirname(Params['local_content_data_path'])
62
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
63
+ end
64
64
 
65
- #Start files monitor taking into consideration existing content data
66
- fm = FileMonitoring::FileMonitoring.new(@@local_dynamic_content_data)
65
+ Log.info("Init monitoring")
66
+ monitoring_events = Queue.new
67
+ fm = FileMonitoring::FileMonitoring.new()
67
68
  fm.set_event_queue(monitoring_events)
68
69
  # Start monitoring and writing changes to queue
69
70
  all_threads << Thread.new do
@@ -73,29 +74,38 @@ module ContentServer
73
74
  # # # # # # # # # # # # # #
74
75
  # Initialize/Start local indexer
75
76
  Log.debug1('Start indexer')
76
- queue_indexer = QueueIndexer.new(monitoring_events, @@local_dynamic_content_data)
77
+ queue_indexer = QueueIndexer.new(monitoring_events)
77
78
  # Start indexing on demand and write changes to queue
78
79
  all_threads << queue_indexer.run
79
80
 
80
81
  # # # # # # # # # # # # # # # # # # # # # # # #
81
- # Start dump local content data to file thread
82
- Log.debug1('Start dump local content data to file thread')
82
+ # thread: Start dump local content data to file
83
+ Log.debug1('Init thread: flush local content data to file')
83
84
  all_threads << Thread.new do
84
- last_data_flush_time = nil
85
- while true do
86
- if last_data_flush_time.nil? || last_data_flush_time + Params['data_flush_delay'] < Time.now.to_i
87
- Log.info "Writing local content data to #{Params['local_content_data_path']}."
88
- @@local_dynamic_content_data.last_content_data.to_file(@@tmp_content_data_file)
89
- sleep(0.1) # Added to prevent mv access issue
90
- ::FileUtils.mv(@@tmp_content_data_file, Params['local_content_data_path'])
91
- last_data_flush_time = Time.now.to_i
92
- end
93
- sleep(1)
94
- end
85
+ FileUtils.mkdir_p(Params['tmp_path']) unless File.directory?(Params['tmp_path'])
86
+ last_content_data_id = nil
87
+ loop{
88
+ sleep(Params['data_flush_delay'])
89
+ Log.info('Start flush local content data to file.')
90
+ $testing_memory_log.info('Start flush content data to file') if $testing_memory_log
91
+ written_to_file = false
92
+ $local_content_data_lock.synchronize{
93
+ local_content_data_unique_id = $local_content_data.unique_id
94
+ if (local_content_data_unique_id != last_content_data_id)
95
+ last_content_data_id = local_content_data_unique_id
96
+ $local_content_data.to_file($tmp_content_data_file)
97
+ written_to_file = true
98
+ else
99
+ Log.debug1('no need to flush. content data has not changed')
100
+ end
101
+ }
102
+ File.rename($tmp_content_data_file, Params['local_content_data_path']) if written_to_file
103
+ $testing_memory_log.info("End flush content data to file") if $testing_memory_log
104
+ }
95
105
  end
96
106
 
97
- remote_content_client = RemoteContentServer.new(@@local_dynamic_content_data,
98
- Params['local_content_data_port'])
107
+ # initialize remote connection
108
+ remote_content_client = RemoteContentServer.new(Params['local_content_data_port'])
99
109
  all_threads << remote_content_client.tcp_thread
100
110
 
101
111
  # # # # # # # # # # # # # # # #
@@ -110,47 +120,7 @@ module ContentServer
110
120
  if Params['enable_monitoring']
111
121
  monitoring_info = MonitoringInfo::MonitoringInfo.new()
112
122
  all_threads << Thread.new do
113
- last_data_flush_time = nil
114
- mutex = Mutex.new
115
- while true do
116
- sleep(Params['process_vars_delay'])
117
- ::ContentServer::Globals.process_vars.set('time', Time.now)
118
- Log.debug3("process_vars:monitoring queue size:#{monitoring_events.size}")
119
- ::ContentServer::Globals.process_vars.set('monitoring queue', monitoring_events.size)
120
- Log.debug3("process_vars:copy files events queue size:#{copy_files_events.size}")
121
- ::ContentServer::Globals.process_vars.set('copy files events queue', copy_files_events.size)
122
- #enable following line to see full list of object:count
123
- #obj_array = ''
124
- total_obj_count = 0
125
- string_count = 0
126
- file_count = 0
127
- dir_count = 0
128
- content_count = 0
129
- mutex.synchronize do
130
- ObjectSpace.each_object(Class) {|obj|
131
- obj_count_per_class = ObjectSpace.each_object(obj).count
132
- #enable following line to see full list of object:count
133
- #obj_array = "#{obj_array} * #{obj.name}:#{obj_count_per_class}"
134
- total_obj_count = total_obj_count + obj_count_per_class
135
- }
136
- string_count = ObjectSpace.each_object(String).count
137
- file_count = ObjectSpace.each_object(::FileMonitoring::FileStat).count
138
- dir_count = ObjectSpace.each_object(::FileMonitoring::DirStat).count
139
- content_count = ObjectSpace.each_object(::ContentData::ContentData).count
140
- end
141
- #enable following line to see full list of object:count
142
- #process_vars.set('Live objs full', obj_array)
143
- Log.debug3("process_vars:Live objs cnt:#{total_obj_count}")
144
- Log.debug3("process_vars:Live String obj cnt:#{string_count}")
145
- Log.debug3("process_vars:Live File obj cnt:#{file_count}")
146
- Log.debug3("process_vars:Live Dir obj cnt:#{dir_count}")
147
- Log.debug3("process_vars:Live Content data obj cnt:#{content_count}")
148
- ::ContentServer::Globals.process_vars.set('Live objs cnt', total_obj_count)
149
- ::ContentServer::Globals.process_vars.set('Live String obj cnt', string_count)
150
- ::ContentServer::Globals.process_vars.set('Live File obj cnt', file_count)
151
- ::ContentServer::Globals.process_vars.set('Live Dir obj cnt', dir_count)
152
- ::ContentServer::Globals.process_vars.set('Live Content data obj cnt', content_count)
153
- end
123
+ ContentServer.monitor_general_process_vars
154
124
  end
155
125
  end
156
126
  # Finalize server threads.
@@ -2,7 +2,7 @@ require 'tempfile'
2
2
  require 'thread'
3
3
 
4
4
  require 'file_indexing/index_agent'
5
- require 'content_server/globals'
5
+ require 'content_server/server'
6
6
  require 'log'
7
7
  require 'params'
8
8
 
@@ -32,6 +32,7 @@ module ContentServer
32
32
  " #{e.to_s}")
33
33
  end
34
34
  streams_hash.delete(checksum)
35
+ $process_vars.set('Streams size', streams_hash.size)
35
36
  end
36
37
  end
37
38
 
@@ -57,39 +58,29 @@ module ContentServer
57
58
 
58
59
  def copy_another_chuck(checksum)
59
60
  @stream_queue << [:COPY_CHUNK, checksum]
60
- if Params['enable_monitoring']
61
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
62
- end
61
+ $process_vars.set('File Streamer queue', @stream_queue.size)
63
62
  end
64
63
 
65
64
  def start_streaming(checksum, path)
66
65
  @stream_queue << [:NEW_STREAM, [checksum, path]]
67
- if Params['enable_monitoring']
68
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
69
- end
66
+ $process_vars.set('File Streamer queue', @stream_queue.size)
70
67
  end
71
68
 
72
69
  def abort_streaming(checksum)
73
70
  @stream_queue << [:ABORT_STREAM, checksum]
74
- if Params['enable_monitoring']
75
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
76
- end
71
+ $process_vars.set('File Streamer queue', @stream_queue.size)
77
72
  end
78
73
 
79
74
  def reset_streaming(checksum, new_offset)
80
75
  @stream_queue << [:RESET_STREAM, [checksum, new_offset]]
81
- if Params['enable_monitoring']
82
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
83
- end
76
+ $process_vars.set('File Streamer queue', @stream_queue.size)
84
77
  end
85
78
 
86
79
  def run
87
80
  return Thread.new do
88
81
  loop {
89
82
  stream_pop = @stream_queue.pop
90
- if Params['enable_monitoring']
91
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
92
- end
83
+ $process_vars.set('File Streamer queue', @stream_queue.size)
93
84
  checksum = handle(stream_pop)
94
85
  }
95
86
  end
@@ -101,9 +92,7 @@ module ContentServer
101
92
  checksum, path = content
102
93
  reset_stream(checksum, path, 0)
103
94
  @stream_queue << [:COPY_CHUNK, checksum] if @streams.key?(checksum)
104
- if Params['enable_monitoring']
105
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
106
- end
95
+ $process_vars.set('File Streamer queue', @stream_queue.size)
107
96
  elsif type == :ABORT_STREAM
108
97
  checksum = content
109
98
  Stream.close_delete_stream(checksum, @streams)
@@ -111,9 +100,7 @@ module ContentServer
111
100
  checksum, new_offset = content
112
101
  reset_stream(checksum, nil, new_offset)
113
102
  @stream_queue << [:COPY_CHUNK, checksum] if @streams.key?(checksum)
114
- if Params['enable_monitoring']
115
- ::ContentServer::Globals.process_vars.set('File Streamer queue', @stream_queue.size)
116
- end
103
+ $process_vars.set('File Streamer queue', @stream_queue.size)
117
104
  elsif type == :COPY_CHUNK
118
105
  checksum = content
119
106
  if @streams.key?(checksum)
@@ -143,10 +130,11 @@ module ContentServer
143
130
  file.seek(offset)
144
131
  end
145
132
  Log.debug1("File streamer: #{file.to_s}.")
146
- rescue IOError => e
133
+ @streams[checksum] = Stream.new(checksum, path, file, file.size)
134
+ $process_vars.set('Streams size', @streams.size)
135
+ rescue IOError, Errno::ENOENT => e
147
136
  Log.warning("Could not stream local file #{path}. #{e.to_s}")
148
137
  end
149
- @streams[checksum] = Stream.new(checksum, path, file, file.size)
150
138
  else
151
139
  @streams[checksum].file.seek(offset)
152
140
  end
@@ -197,11 +185,11 @@ module ContentServer
197
185
  end
198
186
  # If last chunk copy.
199
187
  elsif content.nil? && content_checksum.nil?
200
- # Handle the case of backup empty file.
188
+ Log.debug1('Handle the case of backup empty file or last chunk')
189
+ # Handle the case of backup empty file or last chunk.
201
190
  handle_new_stream(file_checksum, 0) if !@streams.key?(file_checksum)
202
191
  # Finalize the file copy.
203
192
  handle_last_chunk(file_checksum)
204
- return false
205
193
  else
206
194
  Log.warning("Unexpected receive chuck message. file_checksum:#{file_checksum}, " \
207
195
  "content.nil?:#{content.nil?}, content_checksum:#{content_checksum}")
@@ -211,7 +199,7 @@ module ContentServer
211
199
 
212
200
  # open new stream
213
201
  def handle_new_stream(file_checksum, file_size)
214
- Log.info("enter handle_new_stream")
202
+ Log.debug1("enter handle_new_stream")
215
203
  # final destination path
216
204
  tmp_path = FileReceiver.destination_filename(
217
205
  File.join(Params['backup_destination_folder'][0]['path'], 'tmp'),
@@ -227,6 +215,7 @@ module ContentServer
227
215
  FileUtils.makedirs(File.dirname(tmp_path)) unless File.directory?(File.dirname(tmp_path))
228
216
  tmp_file = File.new(tmp_path, 'wb')
229
217
  @streams[file_checksum] = Stream.new(file_checksum, tmp_path, tmp_file, file_size)
218
+ $process_vars.set('Streams size', @streams.size)
230
219
  end
231
220
  end
232
221
 
@@ -258,7 +247,6 @@ module ContentServer
258
247
  path = FileReceiver.destination_filename(Params['backup_destination_folder'][0]['path'],
259
248
  file_checksum)
260
249
  Log.debug1("Moving tmp file #{@streams[file_checksum].path} to #{path}")
261
- Log.debug1("Creating directory: #{path}")
262
250
  file_dir = File.dirname(path)
263
251
  FileUtils.makedirs(file_dir) unless File.directory?(file_dir)
264
252
  # Move tmp file to permanent location.
@@ -273,13 +261,19 @@ module ContentServer
273
261
  File.rename(tmp_file_path, path)
274
262
  Log.debug1("End move tmp file to permanent location #{path}.")
275
263
  @file_done_clb.call(local_file_checksum, path) unless @file_done_clb.nil?
276
- rescue IOError => e
277
- Log.warning("Could not move tmp file to permanent file #{path}. #{e.to_s}")
264
+ rescue Exception => e
265
+ Log.warning("Could not move tmp file to permanent path #{path}." +
266
+ " Error msg:#{e.message}\nError type:#{e.type}")
278
267
  end
279
268
  else
280
- Log.error(message)
281
- Log.debug1("Deleting tmp file: #{tmp_file_path}")
282
- File.delete(tmp_file_path)
269
+ begin
270
+ Log.error(message)
271
+ Log.debug1("Deleting tmp file: #{tmp_file_path}")
272
+ File.delete(tmp_file_path)
273
+ rescue Exception => e
274
+ Log.warning("Could not delete tmp file from tmp path #{tmp_file_path}." +
275
+ " Error msg:#{e.message}\nError type:#{e.type}")
276
+ end
283
277
  end
284
278
  else
285
279
  Log.error("Handling last chunk and tmp stream does not exists.")