db-charmer 1.6.10 → 1.6.11

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,11 @@
1
+ 1.6.11 (2010-04-16):
2
+
3
+ Bugfix: Change the way we allocate sharding blocks in block map sharding method to
4
+ prevent race-conditions from happening on block to shard assignments.
5
+ Breaking change: We require connections to exist by default in all connection factory
6
+ methods. If you need old behavior, pass should_exist=false explicitly.
7
+
8
+ ----------------------------------------------------------------------------------------
1
9
  1.6.10 (2010-04-09):
2
10
 
3
11
  Multi-Db migrations changed. Now it is possible to call ActiveRecord::Migration.db_magic
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.10
1
+ 1.6.11
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{db-charmer}
8
- s.version = "1.6.10"
8
+ s.version = "1.6.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexey Kovyrin"]
12
- s.date = %q{2010-04-09}
12
+ s.date = %q{2010-04-16}
13
13
  s.description = %q{ActiveRecord Connections Magic (slaves, multiple connections, etc)}
14
14
  s.email = %q{alexey@kovyrin.net}
15
15
  s.extra_rdoc_files = [
@@ -13,7 +13,7 @@ module DbCharmer
13
13
  end
14
14
 
15
15
  # Establishes connection or return an existing one from cache
16
- def self.connect(connection_name, should_exist = false)
16
+ def self.connect(connection_name, should_exist = true)
17
17
  connection_name = connection_name.to_s
18
18
  @@connection_classes[connection_name] ||= establish_connection(connection_name, should_exist)
19
19
  end
@@ -25,7 +25,7 @@ module DbCharmer
25
25
  end
26
26
 
27
27
  # Establish connection with a specified name
28
- def self.establish_connection(connection_name, should_exist = false)
28
+ def self.establish_connection(connection_name, should_exist = true)
29
29
  abstract_class = generate_abstract_class(connection_name, should_exist)
30
30
  DbCharmer::ConnectionProxy.new(abstract_class, connection_name)
31
31
  end
@@ -37,7 +37,7 @@ module DbCharmer
37
37
  end
38
38
 
39
39
  # Generate an abstract AR class with specified connection established
40
- def self.generate_abstract_class(connection_name, should_exist = false)
40
+ def self.generate_abstract_class(connection_name, should_exist = true)
41
41
  # Generate class
42
42
  klass = generate_empty_abstract_ar_class(abstract_connection_class_name(connection_name))
43
43
 
@@ -57,12 +57,12 @@ module DbCharmer
57
57
  self.sharded_connection = DbCharmer::Sharding.sharded_connection(name)
58
58
  end
59
59
 
60
- def setup_connection_magic(conn, should_exist = false)
60
+ def setup_connection_magic(conn, should_exist = true)
61
61
  switch_connection_to(conn, should_exist)
62
62
  self.db_charmer_default_connection = conn
63
63
  end
64
64
 
65
- def setup_slaves_magic(slaves, should_exist = false)
65
+ def setup_slaves_magic(slaves, should_exist = true)
66
66
  self.db_charmer_slaves = slaves.collect do |slave|
67
67
  coerce_to_connection_proxy(slave, should_exist)
68
68
  end
@@ -23,7 +23,7 @@ module DbCharmer
23
23
 
24
24
  def initialize(config)
25
25
  @name = config[:name] or raise(ArgumentError, "Missing required :name parameter!")
26
- @connection = DbCharmer::ConnectionFactory.connect(config[:connection])
26
+ @connection = DbCharmer::ConnectionFactory.connect(config[:connection], true)
27
27
  @block_size = (config[:block_size] || 10000).to_i
28
28
 
29
29
  @map_table = config[:map_table] or raise(ArgumentError, "Missing required :map_table parameter!")
@@ -37,8 +37,14 @@ module DbCharmer
37
37
  def shard_for_key(key)
38
38
  block = block_for_key(key)
39
39
 
40
- # Auto-allocate new blocks
41
- block ||= allocate_new_block_for_key(key)
40
+ begin
41
+ # Auto-allocate new blocks
42
+ block ||= allocate_new_block_for_key(key)
43
+ rescue ActiveRecord::StatementInvalid => e
44
+ raise unless e.message.include?('Duplicate entry')
45
+ block = block_for_key(key)
46
+ end
47
+
42
48
  raise ArgumentError, "Invalid key value, no shards found for this key and could not create a new block!" unless block
43
49
 
44
50
  # Bail if no shard found
@@ -94,13 +100,13 @@ module DbCharmer
94
100
 
95
101
  # Try to insert a new mapping (ignore duplicate key errors)
96
102
  sql = <<-SQL
97
- INSERT IGNORE INTO #{map_table}
98
- SET start_id = #{start_id},
99
- end_id = #{end_id},
100
- shard_id = #{shard.id},
101
- block_size = #{block_size},
102
- created_at = NOW(),
103
- updated_at = NOW()
103
+ INSERT INTO #{map_table}
104
+ SET start_id = #{start_id},
105
+ end_id = #{end_id},
106
+ shard_id = #{shard.id},
107
+ block_size = #{block_size},
108
+ created_at = NOW(),
109
+ updated_at = NOW()
104
110
  SQL
105
111
  connection.execute(sql, "Allocate new block")
106
112
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 6
8
- - 10
9
- version: 1.6.10
8
+ - 11
9
+ version: 1.6.11
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexey Kovyrin
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-09 00:00:00 -04:00
17
+ date: 2010-04-16 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency