pgtk 0.21.3 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96d8784e334eb788333eb79d26c99c47f129ea8de8e522732fa37fa333b99a35
4
- data.tar.gz: ec5caad7117ec1f22784c718316b2897effb907f03cd1043e7305a057d9412ee
3
+ metadata.gz: 902941bbf98d37acfad72c9615282264148e6ad6ce1183f2fcd085d3a0cf288e
4
+ data.tar.gz: 00a5488c8dd99f98df1ebce8cef20207514964a1628df8489bd320d0c199ca77
5
5
  SHA512:
6
- metadata.gz: 2266b9797e6f49cecdd4f5e4b198f4d06d9060544a6e2eb89ca99c8f0a05a93e6674b6237af30569e6d474c08e13b02ac706cf9a96daf07938021cd61d518e51
7
- data.tar.gz: 1b723ca7237b8d1e5381481599fd0850e2685fa411a37551360d78548ca0b29ea38da50b849dbad37c8cac1d65422cdcb488b1edd45d9025ccf67336ea586048
6
+ metadata.gz: 47d6993b709ebce71a8cd1525f94230b8c554a8c2edd871ca67627ce6e6724d46ea3530827fa3cb653ae54b56cf4299cb4291197d3f870e602942ffc0b7b6134
7
+ data.tar.gz: cbe8bf7eebf1c038d71079845355d805bc647be88e62523db89180ce9f39e4299a779f104ee648a1f4399f18f90a0e4f3aab371f277df964dec843299b5b733d
data/Gemfile.lock CHANGED
@@ -32,7 +32,7 @@ GEM
32
32
  logger (1.7.0)
33
33
  loog (0.6.1)
34
34
  logger (~> 1.0)
35
- minitest (5.26.0)
35
+ minitest (5.26.1)
36
36
  minitest-reporters (1.7.1)
37
37
  ansi
38
38
  builder
data/lib/pgtk/stash.rb CHANGED
@@ -38,30 +38,26 @@ class Pgtk::Stash
38
38
  # @param [Hash] stash Optional existing stash to use (default: new empty stash)
39
39
  # @option [Hash] queries Internal cache data (default: {})
40
40
  # @option [Hash] tables Internal cache data (default: {})
41
- # @option [Concurrent::ReentrantReadWriteLock] entrance Lock for write internal state
42
- # @option [Concurrent::AtomicBoolean] launched Latch for start timers once
43
41
  # @param [Integer] refill_interval Interval in seconds for recalculate stale queries
44
- # @param [Integer] top Number of queries to recalculate
45
- # @param [Integer] threads Number of threads in threadpool
42
+ # @param [Integer] max_queue_length Number of refilling tasks in the queue
43
+ # @param [Integer] threads Number of threads in tpool
46
44
  # @param [Loog] loog Logger for debugging (default: null logger)
47
45
  def initialize(
48
46
  pool,
49
- stash = {
50
- queries: {},
51
- tables: {},
52
- entrance: Concurrent::ReentrantReadWriteLock.new,
53
- launched: Concurrent::AtomicBoolean.new(false)
54
- },
55
- refill_interval: 5,
56
- top: 100,
57
- threads: 5,
58
- loog: Loog::NULL
47
+ stash: { queries: {}, tables: {} },
48
+ refill_interval: 16,
49
+ max_queue_length: 128,
50
+ threads: 4,
51
+ loog: Loog::NULL,
52
+ entrance: Concurrent::ReentrantReadWriteLock.new,
53
+ launched: Concurrent::AtomicBoolean.new(false)
59
54
  )
60
55
  @pool = pool
61
56
  @stash = stash
62
- @entrance = stash[:entrance]
57
+ @launched = launched
58
+ @entrance = entrance
63
59
  @refill_interval = refill_interval
64
- @top = top
60
+ @max_queue_length = max_queue_length
65
61
  @threads = threads
66
62
  @loog = loog
67
63
  end
@@ -92,11 +88,11 @@ class Pgtk::Stash
92
88
  [
93
89
  @pool.dump,
94
90
  '',
95
- "Pgtk::Stash (refill_interval=#{@refill_interval}s, top=#{@top}q, threads=#{@threads}t):",
96
- " #{'not ' if @stash[:launched].false?}launched",
91
+ "Pgtk::Stash (refill_interval=#{@refill_interval}s, max_queue_length=#{@max_queue_length}, threads=#{@threads}):",
92
+ " #{'not ' if @launched.false?}launched",
97
93
  " #{@stash[:tables].count} tables in cache",
98
94
  " #{@stash[:queries].count} queries in cache:",
99
- qq.sort_by { -_1[2] }.take(20).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" }
95
+ qq.sort_by { -_1[2] }.take(64).map { |a| " #{a[1]}/#{a[2]}p/#{a[3]}s: #{a[0]}" }
100
96
  ].join("\n")
101
97
  end
102
98
 
@@ -128,9 +124,9 @@ class Pgtk::Stash
128
124
  if ret.nil? || @stash.dig(:queries, pure, key, :stale)
129
125
  ret = @pool.exec(pure, params, result)
130
126
  unless pure.include?(' NOW() ')
127
+ tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|$)/).map(&:first).uniq
128
+ raise "No tables at #{pure.inspect}" if tables.empty?
131
129
  @entrance.with_write_lock do
132
- tables = pure.scan(/(?<=^|\s)(?:FROM|JOIN) ([a-z_]+)(?=\s|$)/).map(&:first).uniq
133
- raise "No tables at #{pure.inspect}" if tables.empty?
134
130
  tables.each do |t|
135
131
  @stash[:tables][t] = [] if @stash[:tables][t].nil?
136
132
  @stash[:tables][t].append(pure).uniq!
@@ -159,11 +155,14 @@ class Pgtk::Stash
159
155
  def transaction
160
156
  @pool.transaction do |t|
161
157
  yield Pgtk::Stash.new(
162
- t, @stash,
158
+ t,
159
+ stash: @stash,
163
160
  refill_interval: @refill_interval,
164
- top: @top,
161
+ max_queue_length: @max_queue_length,
165
162
  threads: @threads,
166
- loog: @loog
163
+ loog: @loog,
164
+ entrance: @entrance,
165
+ launched: @launched
167
166
  )
168
167
  end
169
168
  end
@@ -171,9 +170,9 @@ class Pgtk::Stash
171
170
  private
172
171
 
173
172
  def launch!
174
- raise 'Cannot launch multiple times on same cache data' unless @stash[:launched].make_true
175
- Concurrent::FixedThreadPool.new(@threads).then do |threadpool|
176
- Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor: threadpool) do
173
+ raise 'Cannot launch multiple times on same cache data' unless @launched.make_true
174
+ Concurrent::FixedThreadPool.new(@threads).then do |tpool|
175
+ Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor: tpool) do
177
176
  @entrance.with_write_lock do
178
177
  @stash[:queries].each_key do |q|
179
178
  @stash[:queries][q].each_key do |k|
@@ -182,21 +181,23 @@ class Pgtk::Stash
182
181
  end
183
182
  end
184
183
  end
185
- Concurrent::TimerTask.execute(execution_interval: @refill_interval, executor: threadpool) do
184
+ Concurrent::TimerTask.execute(execution_interval: @refill_interval, executor: tpool) do
186
185
  @stash[:queries]
187
186
  .map { |k, v| [k, v.values.sum { |vv| vv[:popularity] }, v.values.any? { |vv| vv[:stale] }] }
188
187
  .select { _1[2] }
189
188
  .sort_by { -_1[1] }
190
- .first(@top)
191
189
  .each do |a|
192
190
  q = a[0]
193
191
  @stash[:queries][q].each_key do |k|
194
192
  next unless @stash[:queries][q][k][:stale]
195
- threadpool.post do
193
+ next if tpool.queue_length >= @max_queue_length
194
+ tpool.post do
195
+ h = @stash[:queries][q][k]
196
+ ret = @pool.exec(q, h[:params], h[:result])
196
197
  @entrance.with_write_lock do
197
198
  h = @stash[:queries][q][k]
198
199
  h[:stale] = false
199
- h[:ret] = @pool.exec(q, h[:params], h[:result])
200
+ h[:ret] = ret
200
201
  end
201
202
  end
202
203
  end
data/lib/pgtk/version.rb CHANGED
@@ -11,5 +11,5 @@ require_relative '../pgtk'
11
11
  # License:: MIT
12
12
  module Pgtk
13
13
  # Current version of the library.
14
- VERSION = '0.21.3'
14
+ VERSION = '0.22.0'
15
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.3
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko