Module: RightSharding::ShardHandler

Defined in:
lib/right_sharding/shard_handler.rb

Defined Under Namespace

Classes: CurrentRequestCache

Constant Summary collapse

SHARDING_CACHE_EXPIRATION_RANGE =
(30..60).to_a

Class Method Summary collapse

Class Method Details

.fetch_instance_api_token_shard_id_and_hostname(token) ⇒ Object



96
97
98
99
# File 'lib/right_sharding/shard_handler.rb', line 96

def self.fetch_instance_api_token_shard_id_and_hostname(token)
  # FIXME: cyclical dependency - InstanceApiToken is defined in right_site
  InstanceApiToken.get_shard_id_and_hostname(token, instance_api_token_shard_cache_key(token), SHARDING_CACHE_EXPIRATION_RANGE.rand.minutes)
end

.fetch_shard(shard_object, shard_id) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/right_sharding/shard_handler.rb', line 43

def self.fetch_shard(shard_object, shard_id)
  # defer the call to the object after we are sure that it is not in cache
  this_shard_proc = Proc.new {
    shard_object.get_shard_object(shard_id, shard_object_cache_key(shard_id), SHARDING_CACHE_EXPIRATION_RANGE.rand.minutes)
  }

  if CurrentRequestCache.is_activated?
    CurrentRequestCache.find_or_save_this_shard(this_shard_proc)
  else
    # In this case, we are in script/console, migration, or daemon etc, we don't want to cache it in Thread.current
    this_shard_proc.call
  end
end

.fetch_shard_hostname(sharding_object, account_id) ⇒ Object



83
84
85
86
# File 'lib/right_sharding/shard_handler.rb', line 83

def self.fetch_shard_hostname(sharding_object, )
  requested_shard = fetch_shard_id_and_hostname(sharding_object, )
  requested_shard[:shard_hostname]
end

.fetch_shard_id_and_hostname(sharding_object, account_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/right_sharding/shard_handler.rb', line 69

def self.fetch_shard_id_and_hostname(sharding_object, )
  # defer the call to the object after we are sure that it is not in cache
  get_shard_id_and_hostname_proc = Proc.new {
    sharding_object.get_shard_id_and_hostname(, shard_cache_key(), SHARDING_CACHE_EXPIRATION_RANGE.rand.minutes)
  }

  if CurrentRequestCache.is_activated?
    CurrentRequestCache.find_or_save_shard_map(, get_shard_id_and_hostname_proc)
  else
    # In this case, we are in script/console, migration, or daemon etc, we don't want to cache it in Thread.current
    get_shard_id_and_hostname_proc.call
  end
end

.fetch_this_shard(config_object, shard_object) ⇒ Object



57
58
59
# File 'lib/right_sharding/shard_handler.rb', line 57

def self.fetch_this_shard(config_object, shard_object)
  fetch_shard(shard_object, fetch_this_shard_id(config_object))
end

.fetch_this_shard_id(config_object) ⇒ Object


THIS_SHARD_ID



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/right_sharding/shard_handler.rb', line 23

def self.fetch_this_shard_id(config_object)
  # defer the call to the object after we are sure that it is not in cache
  this_shard_id_proc = Proc.new { config_object.find_this_shard_id }

  if CurrentRequestCache.is_activated?
    CurrentRequestCache.find_or_save_this_shard_id(this_shard_id_proc)
  else
    # In this case, we are in script/console, migration, or daemon etc, we don't want to cache it in Thread.current
    this_shard_id_proc.call
  end
end

.instance_api_token_shard_cache_key(token) ⇒ Object


SHARD information for each InstanceApiToken



92
93
94
# File 'lib/right_sharding/shard_handler.rb', line 92

def self.instance_api_token_shard_cache_key(token)
  "instance_api_token/#{token}/shard_id_and_hostname"
end

.shard_cache_key(account_id) ⇒ Object


SHARD information for each Account



65
66
67
# File 'lib/right_sharding/shard_handler.rb', line 65

def self.shard_cache_key()
  "account/#{}/shard_id_and_hostname"
end

.shard_object_cache_key(shard_id) ⇒ Object


THIS_SHARD



39
40
41
# File 'lib/right_sharding/shard_handler.rb', line 39

def self.shard_object_cache_key(shard_id)
  "shard/#{shard_id}"
end