|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "active_support/core_ext/module/delegation" |
| 4 | + |
| 5 | +module StorageTables |
| 6 | + class Service |
| 7 | + # = Storage Tables Mirror \Service |
| 8 | + # |
| 9 | + # Wraps a set of mirror services and provides a single StorageTables::Service object that will all |
| 10 | + # have the files uploaded to them. A +primary+ service is designated to answer calls to: |
| 11 | + # * +download+ |
| 12 | + # * +exists?+ |
| 13 | + # * +url+ |
| 14 | + # * +url_for_direct_upload+ |
| 15 | + # * +headers_for_direct_upload+ |
| 16 | + class MirrorService < Service |
| 17 | + attr_reader :primary, :mirrors |
| 18 | + |
| 19 | + delegate :download, :download_chunk, :exist?, :url, |
| 20 | + :url_for_direct_upload, :headers_for_direct_upload, :path_for, :compose, to: :primary |
| 21 | + |
| 22 | + # Stitch together from named services. |
| 23 | + def self.build(primary:, mirrors:, name:, configurator:, **) # :nodoc: |
| 24 | + new( |
| 25 | + primary: configurator.build(primary), |
| 26 | + mirrors: mirrors.collect { |mirror_name| configurator.build mirror_name } |
| 27 | + ).tap do |service_instance| |
| 28 | + service_instance.name = name |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def initialize(primary:, mirrors:) # rubocop:disable Lint/MissingSuper |
| 33 | + @primary = primary |
| 34 | + @mirrors = mirrors |
| 35 | + @executor = Concurrent::ThreadPoolExecutor.new( |
| 36 | + min_threads: 1, |
| 37 | + max_threads: mirrors.size, |
| 38 | + max_queue: 0, |
| 39 | + fallback_policy: :caller_runs, |
| 40 | + idle_time: 60 |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + # Upload the +io+ to the +checksum+ specified to all services. The upload to the primary service is done |
| 45 | + # synchronously whereas the upload to the mirrors is done asynchronously. If a +checksum+ is provided, all |
| 46 | + # services will ensure a match when the upload has completed or raise an StorageTables::IntegrityError. |
| 47 | + def upload(checksum, io, **) |
| 48 | + io.rewind |
| 49 | + primary.upload(checksum, io, **) |
| 50 | + mirror_later checksum |
| 51 | + end |
| 52 | + |
| 53 | + # Delete the file at the +checksum+ on all services. |
| 54 | + def delete(checksum) |
| 55 | + perform_across_services :delete, checksum |
| 56 | + end |
| 57 | + |
| 58 | + def mirror_later(checksum) |
| 59 | + StorageTables::MirrorJob.perform_later checksum |
| 60 | + end |
| 61 | + |
| 62 | + # Copy the file at the +checksum+ from the primary service to each of the mirrors where it doesn't already exist. |
| 63 | + def mirror(checksum) |
| 64 | + instrument(:mirror, checksum) do |
| 65 | + if (mirrors_in_need_of_mirroring = mirrors.reject { |service| service.exist?(checksum) }).any? |
| 66 | + primary.open(checksum) do |io| |
| 67 | + mirrors_in_need_of_mirroring.each do |service| |
| 68 | + io.rewind |
| 69 | + service.upload checksum, io |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + private |
| 77 | + |
| 78 | + def each_service(&) |
| 79 | + [primary, *mirrors].each(&) |
| 80 | + end |
| 81 | + |
| 82 | + def perform_across_services(method, *args) |
| 83 | + tasks = each_service.collect do |service| |
| 84 | + Concurrent::Promise.execute(executor: @executor) do |
| 85 | + service.public_send method, *args |
| 86 | + end |
| 87 | + end |
| 88 | + tasks.each(&:value!) |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments