Class: Purplelight::Snapshot
- Inherits:
-
Object
- Object
- Purplelight::Snapshot
- Defined in:
- lib/purplelight/snapshot.rb
Overview
Snapshot orchestrates partition planning, parallel reads, and writing.
Constant Summary collapse
- DEFAULTS =
{ format: :jsonl, compression: :zstd, batch_size: 2_000, partitions: [Etc.respond_to?(:nprocessors) ? [Etc.nprocessors * 2, 4].max : 4, 32].min, queue_size_bytes: 256 * 1024 * 1024, rotate_bytes: 256 * 1024 * 1024, read_concern: { level: :majority }, read_preference: :primary, no_cursor_timeout: true }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(client:, collection:, output:, format: , compression: , partitions: , batch_size: , queue_size_bytes: , rotate_bytes: , query: {}, projection: nil, hint: nil, mapper: nil, resume: { enabled: true, state_path: nil, overwrite_incompatible: false }, sharding: { mode: :by_size, part_bytes: DEFAULTS[:rotate_bytes], prefix: nil }, logger: nil, on_progress: nil, read_concern: , read_preference: , no_cursor_timeout: , telemetry: nil, compression_level: nil, writer_threads: 1, write_chunk_bytes: nil, parquet_row_group: nil, parquet_max_rows: nil) ⇒ Snapshot
constructor
A new instance of Snapshot.
-
#run ⇒ Object
rubocop:disable Naming/PredicateMethod.
Constructor Details
#initialize(client:, collection:, output:, format: , compression: , partitions: , batch_size: , queue_size_bytes: , rotate_bytes: , query: {}, projection: nil, hint: nil, mapper: nil, resume: { enabled: true, state_path: nil, overwrite_incompatible: false }, sharding: { mode: :by_size, part_bytes: DEFAULTS[:rotate_bytes], prefix: nil }, logger: nil, on_progress: nil, read_concern: , read_preference: , no_cursor_timeout: , telemetry: nil, compression_level: nil, writer_threads: 1, write_chunk_bytes: nil, parquet_row_group: nil, parquet_max_rows: nil) ⇒ Snapshot
Returns a new instance of Snapshot.
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 |
# File 'lib/purplelight/snapshot.rb', line 34 def initialize(client:, collection:, output:, format: DEFAULTS[:format], compression: DEFAULTS[:compression], partitions: DEFAULTS[:partitions], batch_size: DEFAULTS[:batch_size], queue_size_bytes: DEFAULTS[:queue_size_bytes], rotate_bytes: DEFAULTS[:rotate_bytes], query: {}, projection: nil, hint: nil, mapper: nil, resume: { enabled: true, state_path: nil, overwrite_incompatible: false }, sharding: { mode: :by_size, part_bytes: DEFAULTS[:rotate_bytes], prefix: nil }, logger: nil, on_progress: nil, read_concern: DEFAULTS[:read_concern], read_preference: DEFAULTS[:read_preference], no_cursor_timeout: DEFAULTS[:no_cursor_timeout], telemetry: nil, compression_level: nil, writer_threads: 1, write_chunk_bytes: nil, parquet_row_group: nil, parquet_max_rows: nil) @client = client @collection = client[collection] @output = output @format = format.to_sym @compression = compression.to_sym @partitions = partitions @batch_size = batch_size @queue_size_bytes = queue_size_bytes @rotate_bytes = rotate_bytes @query = query || {} @projection = projection @hint = hint @mapper = mapper @resume = resume || { enabled: true } @sharding = sharding || { mode: :by_size } @logger = logger @on_progress = on_progress @read_concern = read_concern @read_preference = read_preference @no_cursor_timeout = no_cursor_timeout @compression_level = compression_level @writer_threads = writer_threads || 1 @write_chunk_bytes = write_chunk_bytes @parquet_row_group = parquet_row_group @parquet_max_rows = parquet_max_rows @running = true @telemetry_enabled = telemetry ? telemetry.enabled? : (ENV['PL_TELEMETRY'] == '1') @telemetry = telemetry || ( @telemetry_enabled ? Telemetry.new(enabled: true) : Telemetry::NULL ) end |
Class Method Details
.snapshot ⇒ Object
30 31 32 |
# File 'lib/purplelight/snapshot.rb', line 30 def self.snapshot(...) new(...).run end |
Instance Method Details
#run ⇒ Object
rubocop:disable Naming/PredicateMethod
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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 196 197 198 199 200 201 |
# File 'lib/purplelight/snapshot.rb', line 78 def run dir, prefix = resolve_output(@output, @format) manifest_path = File.join(dir, "#{prefix}.manifest.json") query_digest = Manifest.query_digest(@query, @projection) manifest = if @resume && @resume[:enabled] && File.exist?(manifest_path) m = Manifest.load(manifest_path) unless m.compatible_with?(collection: @collection.name, format: @format, compression: @compression, query_digest: query_digest) if @resume[:overwrite_incompatible] m = Manifest.new(path: manifest_path) else raise IncompatibleResumeError, 'existing manifest incompatible with request; pass overwrite_incompatible: true to reset' end end m else Manifest.new(path: manifest_path) end manifest.configure!(collection: @collection.name, format: @format, compression: @compression, query_digest: query_digest, options: { partitions: @partitions, batch_size: @batch_size, queue_size_bytes: @queue_size_bytes, rotate_bytes: @rotate_bytes, hint: @hint, read_concern: (@read_concern.is_a?(Hash) ? @read_concern : { level: @read_concern }), no_cursor_timeout: @no_cursor_timeout, writer_threads: @writer_threads, compression_level: @compression_level || (ENV['PL_ZSTD_LEVEL']&.to_i if @compression.to_s == 'zstd') || ENV['PL_ZSTD_LEVEL']&.to_i, write_chunk_bytes: @write_chunk_bytes || ENV['PL_WRITE_CHUNK_BYTES']&.to_i, parquet_row_group: @parquet_row_group || ENV['PL_PARQUET_ROW_GROUP']&.to_i, parquet_max_rows: @parquet_max_rows, sharding: @sharding, resume_overwrite_incompatible: @resume && @resume[:overwrite_incompatible] ? true : false, telemetry: @telemetry_enabled }) manifest.ensure_partitions!(@partitions) # Plan partitions t_plan = @telemetry.start(:partition_plan_time) partition_filters = Partitioner.object_id_partitions(collection: @collection, query: @query, partitions: @partitions, telemetry: @telemetry) @telemetry.finish(:partition_plan_time, t_plan) # Reader queue queue = ByteQueue.new(max_bytes: @queue_size_bytes) # Writer writer = case @format when :jsonl WriterJSONL.new(directory: dir, prefix: prefix, compression: @compression, rotate_bytes: @rotate_bytes, logger: @logger, manifest: manifest) when :csv single_file = @sharding && @sharding[:mode].to_s == 'single_file' WriterCSV.new(directory: dir, prefix: prefix, compression: @compression, rotate_bytes: @rotate_bytes, logger: @logger, manifest: manifest, single_file: single_file) when :parquet single_file = @sharding && @sharding[:mode].to_s == 'single_file' row_group = @parquet_row_group || ENV['PL_PARQUET_ROW_GROUP']&.to_i || WriterParquet::DEFAULT_ROW_GROUP_SIZE WriterParquet.new(directory: dir, prefix: prefix, compression: @compression, logger: @logger, manifest: manifest, single_file: single_file, row_group_size: row_group, rotate_rows: @parquet_max_rows) else raise ArgumentError, "format not implemented: #{@format}" end # Start reader threads readers = partition_filters.each_with_index.map do |pf, idx| Thread.new do local_telemetry = @telemetry_enabled ? Telemetry.new(enabled: true) : Telemetry::NULL read_partition(idx: idx, filter_spec: pf, queue: queue, batch_size: @batch_size, manifest: manifest, telemetry: local_telemetry) # Merge per-thread telemetry @telemetry.merge!(local_telemetry) if @telemetry_enabled end end # Writer loop writer_telemetry = @telemetry_enabled ? Telemetry.new(enabled: true) : Telemetry::NULL writer_thread = Thread.new do Thread.current[:pl_telemetry] = writer_telemetry if @telemetry_enabled loop do batch = queue.pop break if batch.nil? writer.write_many(batch) end ensure writer.close end progress_thread = Thread.new do Time.now loop do sleep 2 break unless @running @on_progress&.call({ queue_bytes: queue.size_bytes }) end end # Join readers readers.each(&:join) queue.close writer_thread.join @telemetry.merge!(writer_telemetry) if @telemetry_enabled @running = false progress_thread.join if @telemetry_enabled total = @telemetry.timers.values.sum breakdown = @telemetry.timers .sort_by { |_k, v| -v } .map { |k, v| [k, v, total.zero? ? 0 : ((v / total) * 100.0)] } if @logger @logger.info('Telemetry (seconds and % of timed work):') breakdown.each { |k, v, pct| @logger.info(" #{k}: #{v.round(3)}s (#{pct.round(1)}%)") } else puts 'Telemetry (seconds and % of timed work):' breakdown.each { |k, v, pct| puts " #{k}: #{v.round(3)}s (#{pct.round(1)}%)" } end end true end |