Class: S3FileCache

Inherits:
Object show all
Defined in:
lib/cosmos/utilities/s3_file_cache.rb

Constant Summary collapse

MAX_DISK_USAGE =

20 GB

20_000_000_000
TIMESTAMP_FORMAT =

TODO: get from different class?

"%Y%m%d%H%M%S%N"
@@instance =
nil
@@mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE) ⇒ S3FileCache

Returns a new instance of S3FileCache.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 159

def initialize(name = 'default', max_disk_usage = MAX_DISK_USAGE)
  @max_disk_usage = max_disk_usage

  @rubys3_client = Aws::S3::Client.new
  begin
    @rubys3_client.head_bucket(bucket: 'logs')
  rescue Aws::S3::Errors::NotFound
    @rubys3_client.create_bucket(bucket: 'logs')
  end

  # Create local file cache location
  @cache_dir = Dir.mktmpdir
  FileUtils.mkdir_p(@cache_dir)
  at_exit do
    FileUtils.remove_dir(@cache_dir, true)
  end

  @cached_files = S3FileCollection.new

  @thread = Thread.new do
    while true
      file = @cached_files.get_next_to_retrieve
      # Cosmos::Logger.debug "Next file: #{file}"
      if file and (file.size + @cached_files.current_disk_usage()) <= @max_disk_usage
        begin
          file.retrieve
        rescue
          # Will be automatically retried
        end
      else
        sleep(1)
      end
    end
  rescue => err
    Cosmos::Logger.error "S3FileCache thread unexpectedly died\n#{err.formatted}"
  end
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



146
147
148
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 146

def cache_dir
  @cache_dir
end

Class Method Details

.instanceObject



151
152
153
154
155
156
157
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 151

def self.instance
  return @@instance if @@instance
  @@mutex.synchronize do
    @@instance ||= S3FileCache.new
  end
  @@instance
end

Instance Method Details

#file_in_time_range(s3_path, start_time_nsec, end_time_nsec) ⇒ Object

private



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 263

def file_in_time_range(s3_path, start_time_nsec, end_time_nsec)
  basename = File.basename(s3_path)
  file_start_timestamp, file_end_timestamp, other = basename.split("__")
  file_start_time_nsec = DateTime.strptime(file_start_timestamp, TIMESTAMP_FORMAT).to_f * Time::NSEC_PER_SECOND
  file_end_time_nsec = DateTime.strptime(file_end_timestamp, TIMESTAMP_FORMAT).to_f * Time::NSEC_PER_SECOND
  if (start_time_nsec < file_end_time_nsec) and (end_time_nsec >= file_start_time_nsec)
    return true
  else
    return false
  end
end

#reserve_file(cmd_or_tlm, target_name, packet_name, start_time_nsec, end_time_nsec, type = :DECOM, timeout = 60, scope:) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 197

def reserve_file(cmd_or_tlm, target_name, packet_name, start_time_nsec, end_time_nsec, type = :DECOM, timeout = 60, scope:)
  # Cosmos::Logger.debug "reserve_file #{cmd_or_tlm}:#{target_name}:#{packet_name} start:#{start_time_nsec / 1_000_000_000} end:#{end_time_nsec / 1_000_000_000} type:#{type} timeout:#{timeout}"
  # Get List of Files from S3
  total_resp = []
  token = nil
  dates = []
  cur_date = Time.at(start_time_nsec / Time::NSEC_PER_SECOND).beginning_of_day
  end_date = Time.at(end_time_nsec / Time::NSEC_PER_SECOND).beginning_of_day
  cur_date -= 1.day # start looking in the folder for the day before because log files can span across midnight
  while cur_date <= end_date
    dates << cur_date.strftime("%Y%m%d")
    cur_date += 1.day
  end
  prefixes = []
  dates.each do |date|
    while true
      prefixes << "#{scope}/#{type.to_s.downcase}_logs/#{cmd_or_tlm.to_s.downcase}/#{target_name}/#{packet_name}/#{date}"
      resp = @rubys3_client.list_objects_v2({
        bucket: "logs",
        max_keys: 1000,
        prefix: prefixes[-1],
        continuation_token: token
      })
      total_resp.concat(resp.contents)
      break unless resp.is_truncated
      token = resp.next_continuation_token
    end
  end

  # Add to needed files
  files = []
  total_resp.each_with_index do |item, index|
    s3_path = item.key
    if file_in_time_range(s3_path, start_time_nsec, end_time_nsec)
      file = @cached_files.add(s3_path, item.size, index)
      files << file
    end
  end

  # Wait for first file retrieval
  if files.length > 0
    wait_start = Time.now
    file = files[0]
    file.reserve
    while (Time.now - wait_start) < timeout
      return file.local_path if file.local_path
      sleep(1)
    end
    # Remove reservations if we timeout
    file.unreserve
  else
    Cosmos::Logger.info "No files found for #{prefixes}"
  end

  return nil
end

#unreserve_file(filename) ⇒ Object



254
255
256
257
258
259
# File 'lib/cosmos/utilities/s3_file_cache.rb', line 254

def unreserve_file(filename)
  @@mutex.synchronize do
    file = @cached_files.get(filename)
    file.unreserve if file
  end
end