v0.52 Improve handling and look at how we can improve ActiveRecord config for PostgreSQL and avoid insufficient pool size

This commit is contained in:
David Sainty 2024-08-19 22:01:47 +10:00
parent af3c46e1cf
commit e312a4ed12

View File

@ -1075,13 +1075,17 @@ class GossamerForumsImporter < ImportScripts::Base
# Get list of TOPICS / OP posts, i.e. post ids that have no parent / root id - SELECT post_id FROM gforum_Post WHERE post_root_id = 0; # Get list of TOPICS / OP posts, i.e. post ids that have no parent / root id - SELECT post_id FROM gforum_Post WHERE post_root_id = 0;
def threaded_topic_import def threaded_topic_import
# Define the custom connection pool settings # Update connection pool settings
custom_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new( ActiveRecord::Base.establish_connection(
ActiveRecord::Base.connection_pool.spec.to_h.merge(pool: 40, timeout: 5000) ActiveRecord::Base.connection_db_config.configuration_hash.merge(pool: 40, timeout: 5000)
) )
# Register the custom connection pool under a unique identifier ### # Define the custom connection pool settings
ActiveRecord::Base.connection_handler.connection_pools['CustomPool'] = custom_pool ### custom_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
### ActiveRecord::Base.connection_pool.spec.to_h.merge(pool: 40, timeout: 5000)
### )
### # Register the custom connection pool under a unique identifier
### ActiveRecord::Base.connection_handler.connection_pools['CustomPool'] = custom_pool
# Use CachedThreadPool for dynamic thread management # Use CachedThreadPool for dynamic thread management
#### pool = Concurrent::CachedThreadPool.new #### pool = Concurrent::CachedThreadPool.new
@ -1181,7 +1185,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Use connection pooling for PostgreSQL and synchronize access to shared resources # Use connection pooling for PostgreSQL and synchronize access to shared resources
# ActiveRecord::Base.connection_pool.with_connection do # ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.connected_to(pool: 'CustomPool') do # ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.connected_to do
post_status = fetch_post_status(post_id) post_status = fetch_post_status(post_id)
if post_status.nil? || post_status == 0 if post_status.nil? || post_status == 0
puts "Starting import for post_id #{post_id}" puts "Starting import for post_id #{post_id}"
@ -1322,14 +1327,11 @@ class GossamerForumsImporter < ImportScripts::Base
unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id']) unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id'])
puts "TIJ EE post_id #{post_id}" puts "TIJ EE post_id #{post_id}"
# ActiveRecord::Base.transaction do # ActiveRecord::Base.transaction do
## ActiveRecord::Base.connected_to(pool: 'CustomPool') do
## ActiveRecord::Base.transaction do
# Create the new topic in Discourse # Create the new topic in Discourse
begin begin
suffix = 1 suffix = 1
topic_created = false topic_created = false
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
while !topic_created while !topic_created
begin begin
@ -1359,7 +1361,6 @@ class GossamerForumsImporter < ImportScripts::Base
# puts e.backtrace.join("\n") # Print the full stack trace # puts e.backtrace.join("\n") # Print the full stack trace
end end
end end
end
end end
# Workaround... take a copy of topic.id # Workaround... take a copy of topic.id
@ -1388,7 +1389,6 @@ class GossamerForumsImporter < ImportScripts::Base
puts "TIJ GG post_id #{post_id}" puts "TIJ GG post_id #{post_id}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# Create the initial post in the new topic # Create the initial post in the new topic
post = Post.create!( post = Post.create!(
@ -1402,7 +1402,6 @@ class GossamerForumsImporter < ImportScripts::Base
) )
post.custom_fields['original_gossamer_id'] = row['post_id'] post.custom_fields['original_gossamer_id'] = row['post_id']
post.save! post.save!
end
end end
sqlite_mutex.synchronize do sqlite_mutex.synchronize do
@ -1413,11 +1412,9 @@ class GossamerForumsImporter < ImportScripts::Base
puts "TIJ HH post_id #{post_id}" puts "TIJ HH post_id #{post_id}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# Handle any attachments associated with the post # Handle any attachments associated with the post
handle_post_attachments(row['post_id'], post, discourse_user_id, mysql_client) handle_post_attachments(row['post_id'], post, discourse_user_id, mysql_client)
end
end end
# Create URL mappings for the new topic # Create URL mappings for the new topic
@ -1467,7 +1464,6 @@ class GossamerForumsImporter < ImportScripts::Base
end end
puts "TIJ JJ post_id #{post_id} reply post_id #{reply_row['post_id']} reply_post_views #{reply_post_views || 0} post_number #{post_number} current_topic_id #{current_topic_id} reply_post_views #{reply_post_views || 0}" puts "TIJ JJ post_id #{post_id} reply post_id #{reply_row['post_id']} reply_post_views #{reply_post_views || 0} post_number #{post_number} current_topic_id #{current_topic_id} reply_post_views #{reply_post_views || 0}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# Create the reply post in the existing topic # Create the reply post in the existing topic
post = Post.create!( post = Post.create!(
@ -1481,7 +1477,6 @@ class GossamerForumsImporter < ImportScripts::Base
) )
post.custom_fields['original_gossamer_id'] = reply_row['post_id'] post.custom_fields['original_gossamer_id'] = reply_row['post_id']
post.save! post.save!
end
end end
puts "TIJ KK post_id #{post_id}" puts "TIJ KK post_id #{post_id}"
@ -1499,12 +1494,10 @@ class GossamerForumsImporter < ImportScripts::Base
end end
end end
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
# Handle any attachments associated with the reply # Handle any attachments associated with the reply
handle_post_attachments(reply_row['post_id'], post, reply_user_id, mysql_client) handle_post_attachments(reply_row['post_id'], post, reply_user_id, mysql_client)
end end
end
# # Update the highest processed post_id in the database (thread-safe) # # Update the highest processed post_id in the database (thread-safe)
# update_highest_processed_post_id_thread_safe(reply_row['post_id']) # update_highest_processed_post_id_thread_safe(reply_row['post_id'])
@ -1523,7 +1516,6 @@ class GossamerForumsImporter < ImportScripts::Base
puts "Error importing topic with post_id #{row['post_id']}: #{e.message}" puts "Error importing topic with post_id #{row['post_id']}: #{e.message}"
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
end end
# end
# end # end
else else
puts "Topic for post_id #{row['post_id']} already exists, skipping creation." puts "Topic for post_id #{row['post_id']} already exists, skipping creation."