Class: Nanite::State
Instance Method Summary collapse
- #[](nanite) ⇒ Object
- #[]=(nanite, attributes) ⇒ Object
- #all_services ⇒ Object
- #all_tags ⇒ Object
- #clear_state ⇒ Object
- #delete(nanite) ⇒ Object
- #each ⇒ Object
-
#initialize(redis) ⇒ State
constructor
this class encapsulates the state of a nanite system using redis as the data store.
- #list_nanites ⇒ Object
- #log_redis_error(meth, &blk) ⇒ Object
- #nanites_for(service, *tags) ⇒ Object
- #size ⇒ Object
- #update_state(name, status, services, tags) ⇒ Object
- #update_status(name, status) ⇒ Object
Constructor Details
#initialize(redis) ⇒ State
this class encapsulates the state of a nanite system using redis as the data store. here is the schema, for each agent we store a number of items, for a nanite with the identity: nanite-foobar we store the following things:
nanite-foobar: 0.72 # load average or ‘status’ s-nanite-foobar: { /foo/bar, /foo/nik } # a SET of the provided services tg-nanite-foobar: { foo-42, customer-12 } # a SET of the tags for this agent t-nanite-foobar: 123456789 # unix timestamp of the last state update
also we do an inverted index for quick lookup of agents providing a certain service, so for each service the agent provides, we add the nanite to a SET of all the nanites that provide said service:
foo/bar: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET
we do that same thing for tags: some-tag: { nanite-foobar, nanite-nickelbag, nanite-another } # redis SET
This way we can do a lookup of what nanites provide a set of services and tags based on redis SET intersection:
nanites_for(‘/gems/list’, ‘some-tag’)
> returns a nested array of nanites and their state that provide the intersection
of these two service tags
32 33 34 35 36 37 38 |
# File 'lib/nanite/state.rb', line 32 def initialize(redis) Nanite::Log.info("[setup] initializing redis state: #{redis}") host, port = redis.split(':') host ||= '127.0.0.1' port ||= '6379' @redis = Redis.new :host => host, :port => port end |
Instance Method Details
#[](nanite) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nanite/state.rb', line 47 def [](nanite) log_redis_error("[]") do status = @redis[nanite] = @redis["t-#{nanite}"] services = @redis.smembers("s-#{nanite}") = @redis.smembers("tg-#{nanite}") return nil unless status && && services {:services => services, :status => status, :timestamp => .to_i, :tags => } end end |
#[]=(nanite, attributes) ⇒ Object
58 59 60 61 62 |
# File 'lib/nanite/state.rb', line 58 def []=(nanite, attributes) log_redis_error("[]=") do update_state(nanite, attributes[:status], attributes[:services], attributes[:tags]) end end |
#all_services ⇒ Object
87 88 89 90 91 |
# File 'lib/nanite/state.rb', line 87 def all_services log_redis_error("all_services") do @redis.smembers("naniteservices") end end |
#all_tags ⇒ Object
93 94 95 96 97 |
# File 'lib/nanite/state.rb', line 93 def log_redis_error("all_tags") do @redis.smembers("nanitetags") end end |
#clear_state ⇒ Object
145 146 147 148 149 |
# File 'lib/nanite/state.rb', line 145 def clear_state log_redis_error("clear_state") do @redis.keys("*").each {|k| @redis.delete k} end end |
#delete(nanite) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/nanite/state.rb', line 64 def delete(nanite) log_redis_error("delete") do (@redis.smembers("s-#{nanite}")||[]).each do |srv| @redis.srem(srv, nanite) if @redis.scard(srv) == 0 @redis.del(srv) @redis.srem("naniteservices", srv) end end (@redis.smembers("tg-#{nanite}")||[]).each do |tag| @redis.srem(tag, nanite) if @redis.scard(tag) == 0 @redis.del(tag) @redis.sdelete("nanitetags", tag) end end @redis.del nanite @redis.del "s-#{nanite}" @redis.del "t-#{nanite}" @redis.del "tg-#{nanite}" end end |
#each ⇒ Object
151 152 153 154 155 |
# File 'lib/nanite/state.rb', line 151 def each list_nanites.each do |nan| yield nan, self[nan] end end |
#list_nanites ⇒ Object
135 136 137 138 139 |
# File 'lib/nanite/state.rb', line 135 def list_nanites log_redis_error("list_nanites") do @redis.keys("nanite-*") end end |
#log_redis_error(meth, &blk) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/nanite/state.rb', line 40 def log_redis_error(meth,&blk) blk.call rescue Exception => e Nanite::Log.info("redis error in method: #{meth}") raise e end |
#nanites_for(service, *tags) ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/nanite/state.rb', line 157 def nanites_for(service, *) keys = .dup << service log_redis_error("nanites_for") do res = [] (@redis.sinter(keys)||[]).each do |nan| res << [nan, self[nan]] end res end end |
#size ⇒ Object
141 142 143 |
# File 'lib/nanite/state.rb', line 141 def size list_nanites.size end |
#update_state(name, status, services, tags) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/nanite/state.rb', line 99 def update_state(name, status, services, ) old_services = @redis.smembers("s-#{name}") if old_services (old_services - services).each do |s| @redis.srem(s, name) @redis.srem("naniteservices", s) end end = @redis.smembers("tg-#{name}") if ( - ).each do |t| @redis.srem(t, name) @redis.srem("nanitetags", t) end end @redis.del("s-#{name}") services.each do |srv| @redis.sadd(srv, name) @redis.sadd("s-#{name}", srv) @redis.sadd("naniteservices", srv) end @redis.del("tg-#{name}") .each do |tag| next if tag.nil? @redis.sadd(tag, name) @redis.sadd("tg-#{name}", tag) @redis.sadd("nanitetags", tag) end update_status(name, status) end |
#update_status(name, status) ⇒ Object
130 131 132 133 |
# File 'lib/nanite/state.rb', line 130 def update_status(name, status) @redis[name] = status @redis["t-#{name}"] = Time.now.utc.to_i end |