Class: Herdis::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/herdis/client.rb

Defined Under Namespace

Classes: DeadClusterException, ReDistributed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/herdis/client.rb', line 46

def initialize(*args)
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @shepherds = {}
  args.each_with_index do |url, index|
    @shepherds["initial#{index}"] = {"url" => url}
  end
  begin
    refresh_cluster
  rescue DeadClusterException => e
    raise "No such cluster: #{url}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/herdis/client.rb', line 118

def method_missing(meth, *args, &block)
  if @dredis
    begin
      @dredis.send(meth, *args, &block)
    rescue DeadClusterException => e
      refresh_cluster
      retry
    rescue Errno::ECONNREFUSED => e
      refresh_cluster
      retry
    rescue RuntimeError => e
      if e.message == "ERR operation not permitted"
        refresh_cluster
        retry
      else
        raise e
      end
    end
  else
    super.send(meth, *args, &block)
  end
end

Instance Attribute Details

#dredisObject (readonly)

Returns the value of attribute dredis.



44
45
46
# File 'lib/herdis/client.rb', line 44

def dredis
  @dredis
end

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/herdis/client.rb', line 44

def options
  @options
end

#shepherdsObject (readonly)

Returns the value of attribute shepherds.



44
45
46
# File 'lib/herdis/client.rb', line 44

def shepherds
  @shepherds
end

Instance Method Details

#create_urls(cluster) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/herdis/client.rb', line 59

def create_urls(cluster)
  hash = {}
  cluster.each do |shepherd_id, shepherd_status|
    shepherd_url = URI.parse(shepherd_status["url"])
    (shepherd_status["masters"] || []).each do |shard_id|
      hash[shard_id.to_i] = "redis://#{shepherd_url.host}:#{shepherd_status["first_port"].to_i + shard_id.to_i}/"
    end
  end
  urls = hash.keys.sort.collect do |key|
    hash[key]
  end
end

#refresh_clusterObject



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
# File 'lib/herdis/client.rb', line 87

def refresh_cluster
  cluster = nil
  while cluster.nil?
    raise DeadClusterException.new if @shepherds.empty?
    random_shepherd_id = @shepherds.keys[rand(@shepherds.size)]
    cluster_request = 
      EM::HttpRequest.new(@shepherds[random_shepherd_id]["url"]).get(:path => "/cluster",
                                                                     :head => {"Accept" => "application/json"})
    if cluster_request.response_header.status == 0
      @shepherds.delete(random_shepherd_id)
    else
      cluster = Yajl::Parser.parse(cluster_request.response)
      begin
        urls = create_urls(cluster)
        validate(urls)
      rescue Errno::ECONNREFUSED => e
        cluster = nil
      rescue RuntimeError => e
        if e.message == "ERR operation not permitted"
          cluster = nil
        else
          raise e
        end
      end
    end
  end
  @shepherds = cluster
  @dredis = ReDistributed.new(urls,
                              @options)
end

#validate(urls) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/herdis/client.rb', line 72

def validate(urls)
  unless urls.size == Herdis::Common::SHARDS
    raise "Broken cluster, there should be #{Herdis::Common::SHARDS} shards, but are #{urls.size}"
  end
  creators = Set.new
  urls.each_with_index do |url, index|
    parsed = URI.parse(url)
    r = Redis.new(:host => parsed.host, :port => parsed.port)
    claimed_shard = r.get("Herdis::Shepherd::Shard.id").to_i
    creators << r.get("Herdis::Shepherd::Shard.created_by")
    raise "Broken cluster, shard #{index} claims to be shard #{claimed_shard}" unless claimed_shard == index
    raise "Broken cluster, multiple creators: #{creators.inspect}" if creators.size > 1
  end
end