base_importer.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # frozen_string_literal: true
  2. class Importer::BaseImporter
  3. # @param [Integer] batch_size
  4. # @param [Concurrent::ThreadPoolExecutor] executor
  5. def initialize(batch_size:, executor:)
  6. @batch_size = batch_size
  7. @executor = executor
  8. @wait_for = Concurrent::Set.new
  9. end
  10. # Callback to run when a concurrent work unit completes
  11. # @param [Proc]
  12. def on_progress(&block)
  13. @on_progress = block
  14. end
  15. # Callback to run when a concurrent work unit fails
  16. # @param [Proc]
  17. def on_failure(&block)
  18. @on_failure = block
  19. end
  20. # Reduce resource usage during and improve speed of indexing
  21. def optimize_for_import!
  22. Chewy.client.indices.put_settings index: index.index_name, body: { index: { refresh_interval: -1 } }
  23. end
  24. # Restore original index settings
  25. def optimize_for_search!
  26. Chewy.client.indices.put_settings index: index.index_name, body: { index: { refresh_interval: index.settings_hash[:settings][:index][:refresh_interval] } }
  27. end
  28. # Estimate the amount of documents that would be indexed. Not exact!
  29. # @returns [Integer]
  30. def estimate!
  31. reltuples = ActiveRecord::Base.connection_pool.with_connection { |connection| connection.select_one("SELECT reltuples FROM pg_class WHERE relname = '#{index.adapter.target.table_name}'")['reltuples'].to_i }
  32. # If the table has never yet been vacuumed or analyzed, reltuples contains -1
  33. [reltuples, 0].max
  34. end
  35. # Import data from the database into the index
  36. def import!
  37. raise NotImplementedError
  38. end
  39. # Remove documents from the index that no longer exist in the database
  40. def clean_up!
  41. index.scroll_batches do |documents|
  42. ids = documents.map { |doc| doc['_id'] }
  43. existence_map = index.adapter.target.where(id: ids).pluck(:id).each_with_object({}) { |id, map| map[id.to_s] = true }
  44. tmp = ids.reject { |id| existence_map[id] }
  45. next if tmp.empty?
  46. in_work_unit(tmp) do |deleted_ids|
  47. bulk = Chewy::Index::Import::BulkBuilder.new(index, delete: deleted_ids).bulk_body
  48. Chewy::Index::Import::BulkRequest.new(index).perform(bulk)
  49. [0, bulk.size]
  50. end
  51. end
  52. wait!
  53. end
  54. protected
  55. def in_work_unit(*args, &block)
  56. work_unit = Concurrent::Promises.future_on(@executor, *args, &block)
  57. work_unit.on_fulfillment!(&@on_progress)
  58. work_unit.on_rejection!(&@on_failure)
  59. work_unit.on_resolution! { @wait_for.delete(work_unit) }
  60. @wait_for << work_unit
  61. rescue Concurrent::RejectedExecutionError
  62. sleep(0.1) && retry # Backpressure
  63. end
  64. def wait!
  65. Concurrent::Promises.zip(*@wait_for).wait
  66. end
  67. def index
  68. raise NotImplementedError
  69. end
  70. end