Class: Flor::Storage
- Inherits:
-
Object
- Object
- Flor::Storage
- Defined in:
- lib/flor/unit/storage.rb
Defined Under Namespace
Classes: DbLogger
Constant Summary collapse
- MESSAGE_COLUMNS =
[ :domain, :exid, :point, :content, :status, :ctime, :mtime, :cunit, :munit ].freeze
- POINTER_COLUMNS =
[ :domain, :exid, :nid, :type, :name, :value, :ctime, :cunit, :content ].freeze
- CRECON_STATUSES =
%w[ created consumed ].freeze
- RESCON_STATUSES =
%w[ reserved consumed ].freeze
- POINTS_TO_ARCHIVE =
%w[ terminated failed ceased ].freeze
Instance Attribute Summary collapse
-
#archive ⇒ Object
might be useful for some implementations.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #any_message? ⇒ Boolean
- #consume(messages) ⇒ Object
- #db_version(opts = {}) ⇒ Object
-
#delete_tables ⇒ Object
Delete tables in the storage database that begin with “flor_” and have more than 2 columns (the Sequel schema_info table has 1 column as of this writing).
- #fetch_next_time ⇒ Object
- #fetch_traps(exid) ⇒ Object
-
#initialize(unit) ⇒ Storage
constructor
A new instance of Storage.
- #load_execution(exid) ⇒ Object
- #load_messages(exe_count) ⇒ Object
- #migrate(to = nil, from = nil, opts = nil) ⇒ Object
- #migration_version ⇒ Object
- #on(key, actions = [], &block) ⇒ Object
- #put_execution(exe) ⇒ Object
- #put_message(m) ⇒ Object
- #put_messages(ms, syn = true) ⇒ Object
- #put_timer(message) ⇒ Object
- #put_trap(node, tra) ⇒ Object
- #ready? ⇒ Boolean
- #reserve_all_messages(messages) ⇒ Object
- #shutdown ⇒ Object
- #synchronize(on = true, &block) ⇒ Object
- #trace(exid, nid, tracer, text) ⇒ Object
- #transync(on = true, &block) ⇒ Object
- #trigger_timers ⇒ Object
- #unreserve_messages(max_sec) ⇒ Object
Constructor Details
#initialize(unit) ⇒ Storage
Returns a new instance of Storage.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flor/unit/storage.rb', line 26 def initialize(unit) @unit = unit @models = {} @archive = @unit.conf['sto_archive'] @mutex = @unit.conf['sto_sync'] ? Mutex.new : nil @callbacks = {} connect end |
Instance Attribute Details
#archive ⇒ Object
might be useful for some implementations
24 25 26 |
# File 'lib/flor/unit/storage.rb', line 24 def archive @archive end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
19 20 21 |
# File 'lib/flor/unit/storage.rb', line 19 def callbacks @callbacks end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
19 20 21 |
# File 'lib/flor/unit/storage.rb', line 19 def db @db end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
19 20 21 |
# File 'lib/flor/unit/storage.rb', line 19 def models @models end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
21 22 23 |
# File 'lib/flor/unit/storage.rb', line 21 def mutex @mutex end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
19 20 21 |
# File 'lib/flor/unit/storage.rb', line 19 def unit @unit end |
Class Method Details
.from_blob(content) ⇒ Object
949 950 951 952 |
# File 'lib/flor/unit/storage.rb', line 949 def from_blob(content) content ? JSON.parse(Zlib::Inflate.inflate(content)) : nil end |
.to_blob(h) ⇒ Object
943 944 945 946 947 |
# File 'lib/flor/unit/storage.rb', line 943 def to_blob(h) h ? Sequel.blob(Zlib::Deflate.deflate(JSON.dump(h))) : nil #rescue => e; pp h; raise e end |
Instance Method Details
#any_message? ⇒ Boolean
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/flor/unit/storage.rb', line 297 def synchronize do @db[:flor_messages].where(status: 'created').count > 0 end rescue => err @unit.logger.warn( "#{self.class}#any_message?()", err, '(returning false)') false end |
#consume(messages) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/flor/unit/storage.rb', line 332 def consume() if @archive consume_and_archive() else consume_and_discard() end rescue => err Thread.current[:sto_errored_items] = raise err end |
#db_version(opts = {}) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/flor/unit/storage.rb', line 45 def db_version(opts={}) table, column = migration_table_and_column(opts) (@db[table].first rescue {})[column] end |
#delete_tables ⇒ Object
Delete tables in the storage database that begin with “flor_” and have more than 2 columns (the Sequel schema_info table has 1 column as of this writing)
129 130 131 132 133 134 |
# File 'lib/flor/unit/storage.rb', line 129 def delete_tables @db.tables.each { |t| @db[t].delete \ if t.to_s.match(/^flor_/) && @db[t].columns.size > 2 } end |
#fetch_next_time ⇒ Object
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/flor/unit/storage.rb', line 542 def fetch_next_time t = synchronize do @db[:flor_timers] .select(:ntime) .order(:ntime) .first(status: 'active') end t ? t[:ntime].split('.').first : nil rescue => err @unit.logger.warn( "#{self.class}#fetch_next_time()", err, '(returning nil)') nil end |
#fetch_traps(exid) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/flor/unit/storage.rb', line 312 def fetch_traps(exid) synchronize do traps .where(status: 'active') .where(domain: split_domain(exid)) .all end rescue => err @unit.logger.warn( "#{self.class}#fetch_traps()", err, '(returning [])') [] end |
#load_execution(exid) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/flor/unit/storage.rb', line 136 def load_execution(exid) synchronize do e = @db[:flor_executions] .select(:id, :content) .first(exid: exid) # status active or terminated doesn't matter return { 'exid' => exid, 'nodes' => {}, 'counters' => {}, 'start' => Flor.tstamp, 'size' => 0 } unless e ex = from_blob(e[:content]) fail("couldn't parse execution (db id #{e[:id].to_i})") unless ex ex['id'] = e[:id].to_i ex['size'] = e[:content].size ex end end |
#load_messages(exe_count) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/flor/unit/storage.rb', line 232 def (exe_count) exe_count += 2 # load two more, could prove useful if they vanish like "petits pains" synchronize do _exids_being_processed = @db[:flor_messages] .select(:exid) .exclude(status: CRECON_STATUSES) _exids = @db[:flor_messages] .select(:exid) .exclude(exid: _exids_being_processed) .exclude(status: RESCON_STATUSES) .limit(exe_count) @db[:flor_messages] .where(exid: _exids, status: 'created') .inject({}) { |h, m| (h[m[:exid]] ||= []) << m; h } end rescue => err @unit.logger.warn( "#{self.class}#load_messages()", err, '(returning {})') {} end |
#migrate(to = nil, from = nil, opts = nil) ⇒ Object
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 117 118 119 120 121 122 123 |
# File 'lib/flor/unit/storage.rb', line 90 def migrate(to=nil, from=nil, opts=nil) opts = [ to, from, opts ].find { |e| e.is_a?(Hash) } || {} opts[:target] ||= to if to.is_a?(Integer) opts[:current] ||= from if from.is_a?(Integer) opts[:table], opts[:column] = migration_table_and_column(opts) # # defaults for the migration version table: # { table: :schema_info, # column: :version } skip = opts[:sparse_migrations] || @unit.conf['db_sparse_migrations'] || @unit.conf['sto_sparse_migrations'] if skip && ! opts.has_key?(:allow_missing_migration_files) opts[:allow_missing_migration_files] = true end dir = opts[:migrations] || opts[:migration_dir] || @unit.conf['db_migrations'] || @unit.conf['db_migration_dir'] || @unit.conf['sto_migrations'] || @unit.conf['sto_migration_dir'] || Flor.migration_dir synchronize do Sequel::Migrator.run(@db, dir, opts) end end |
#migration_version ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/flor/unit/storage.rb', line 52 def migration_version Dir[File.join(File.dirname(__FILE__), '../migrations/*.rb')] .inject([]) { |a, fn| m = File.basename(fn).match(/^(\d{4})_/) a << m[1].to_i if m a } .max end |
#on(key, actions = [], &block) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/flor/unit/storage.rb', line 562 def on(key, actions=[], &block) as = case actions when :any, 'any' then [] when Array then actions when Symbol then [ actions ] when String then actions.split(/\s*[;,]\s*/) else [] end .collect(&:to_sym) (@callbacks[key] ||= []) << [ as, block ] end |
#put_execution(exe) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/flor/unit/storage.rb', line 160 def put_execution(exe) status = exe['nodes'].find { |_, n| n['status'].last['status'] != 'ended' } ? 'active' : 'terminated' id = exe['id'] if id exe['end'] ||= Flor.tstamp \ if status == 'terminated' exe['duration'] = Time.parse(exe['end']) - Time.parse(exe['start']) \ if exe['end'] end data = to_blob(exe) exe['size'] = data.size u = @unit.identifier transync do now = Flor.tstamp if id @db[:flor_executions] .where(id: id.to_i) .update( content: data, status: status, mtime: now, munit: u) callback(:executions, :update, id) else exe['id'] = @db[:flor_executions] .insert( domain: Flor.domain(exe['exid']), exid: exe['exid'], content: data, status: status, ctime: now, mtime: now, cunit: u, munit: u) .to_i callback(:executions, :insert, exe['id']) end remove_nodes(exe, status, now) update_pointers(exe, status, now) end exe # return the execution hash rescue => err Thread.current[:sto_errored_items] = [ exe ] raise err end |
#put_message(m) ⇒ Object
392 393 394 395 |
# File 'lib/flor/unit/storage.rb', line 392 def (m) ([ m ]) end |
#put_messages(ms, syn = true) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/flor/unit/storage.rb', line 346 def (ms, syn=true) return if ms.empty? n = Flor.tstamp u = @unit.identifier id = synchronize(syn) do stored, unstored = ms.partition { |m| m['mid'] } # # de-reserve any previously stored message, might happen # for "terminated" messages that got queued back to let # other messages get processed @db[:flor_messages] .where(id: stored.collect { |m| m['mid'] }) .update(status: 'created', mtime: n, munit: u) \ if stored.any? # # store new messages @db[:flor_messages] .import( MESSAGE_COLUMNS, unstored.map { |m| [ Flor.domain(m['exid']), m['exid'], m['point'], to_blob(m), 'created', n, n, u, u ] }) \ if unstored.any? @db[:flor_messages].max(:id) end @unit.wake_up id rescue => err Thread.current[:sto_errored_items] = ms raise err end |
#put_timer(message) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/flor/unit/storage.rb', line 418 def put_timer() type, string = determine_type_and_schedule() next_time = compute_next_time(type, string) now = Flor.tstamp u = @unit.identifier id = synchronize do @db[:flor_timers] .insert( domain: Flor.domain(['exid']), exid: ['exid'], nid: ['nid'], onid: ['onid'] || ['nid'], bnid: ['nid'], type: type, schedule: string, ntime: next_time, content: to_blob(), count: 0, status: 'active', ctime: now, mtime: now, cunit: u, munit: u) end callback(:timers, :insert, id) @unit.wake_up rescue => err Thread.current[:sto_errored_items] = [ ] raise err end |
#put_trap(node, tra) ⇒ Object
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/flor/unit/storage.rb', line 475 def put_trap(node, tra) exid = node['exid'] dom = Flor.domain(exid) now = Flor.tstamp u = @unit.identifier id = synchronize do #points = att_a('point', 'points', nil) ### TODO #tags = att_a('tag', 'tags', nil) # #heats = att_a('heat', 'heats', nil) # #heaps = att_a('heap', 'heaps', nil) # #names = att_a('name', 'names', nil) # # #opts[:heap] = theaps.split(',') if theaps #opts[:heat] = theats.split(',') if theats # @db[:flor_traps] .insert( domain: dom, exid: exid, nid: tra['nid'], onid: tra['onid'] || tra['nid'], bnid: tra['bnid'], trange: tra['range'], tpoints: commaify(tra['points']), ttags: commaify(tra['tags']), theats: commaify(tra['heats']), theaps: commaify(tra['heaps']), content: to_blob(tra), status: 'active', ctime: now, mtime: now, cunit: u, munit: u) end callback(:traps, :insert, id) traps[id] rescue => err Thread.current[:sto_errored_items] = [ node, tra ] raise err end |
#ready? ⇒ Boolean
63 64 65 66 |
# File 'lib/flor/unit/storage.rb', line 63 def ready? db_version == migration_version end |
#reserve_all_messages(messages) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/flor/unit/storage.rb', line 262 def () now = Flor.tstamp count = 0 transync do .each do |m| c = @db[:flor_messages] .where( id: m[:id].to_i, status: 'created', mtime: m[:mtime], munit: m[:munit]) .update( status: 'reserved', mtime: now, munit: @unit.identifier) raise Sequel::Rollback if c != 1 count += 1 end end count == .size # true means success: all the messages could be reserved, # executor is clear to work on the execution rescue => err @unit.logger.warn( "#{self.class}#reserve_all_messages()", err, '(returning false)') false # failure end |
#shutdown ⇒ Object
39 40 41 42 43 |
# File 'lib/flor/unit/storage.rb', line 39 def shutdown @db.disconnect #p [ :disconnected, @db.object_id ] end |
#synchronize(on = true, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flor/unit/storage.rb', line 68 def synchronize(on=true, &block) Thread.current[:sto_errored_items] = nil if on if @mutex && on @mutex.synchronize(&block) else block.call end end |
#trace(exid, nid, tracer, text) ⇒ Object
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/flor/unit/storage.rb', line 524 def trace(exid, nid, tracer, text) text = text.is_a?(String) ? text : JSON.dump(text) synchronize do @db[:flor_traces] .insert( domain: Flor.domain(exid), exid: exid, nid: nid, tracer: tracer, text: text, ctime: Flor.tstamp, cunit: @unit.identifier) end end |
#transync(on = true, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/flor/unit/storage.rb', line 79 def transync(on=true, &block) Thread.current[:sto_errored_items] = nil if on if @mutex && on @mutex.synchronize { @db.transaction(&block) } else block.call end end |
#trigger_timers ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/flor/unit/storage.rb', line 459 def trigger_timers synchronize do load_timers.each do |t| @db.transaction do next unless reschedule_timer(t) == 1 trigger_timer(t) end end end end |
#unreserve_messages(max_sec) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/flor/unit/storage.rb', line 397 def (max_sec) tstamp = Flor.tstamp(Time.now - max_sec) tstamp = tstamp[0..tstamp.rindex('.')] synchronize do @db[:flor_messages] .where(status: 'reserved') .where { mtime < tstamp } .update(status: 'created') end rescue => err @unit.logger.warn( "#{self.class}#unreserve_messages(#{max_sec})", err, '(returning nil)') -1 # not zero, to indicate a problem end |