Method: LS4::BasicHADB#initialize

Defined in:
lib/ls4/service/mds_ha.rb

#initialize(expr) ⇒ BasicHADB

Returns a new instance of BasicHADB.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ls4/service/mds_ha.rb', line 37

def initialize(expr)
  @dbmap = {}    # {Address => DB}
  @writers = []   # [Address]
  @readers = []   # [Address]
  @readers_rr = 0

  expr.split('--').each {|line|
    nodes, weights = line.strip.split(';',2)

    addrs = nodes.strip.split(',').map {|addr|
      parse_addr(addr)
    }

    weights = (weights||"").strip.split(',').map {|x| x.to_i }

    @writers << addrs.first

    addrs.each_with_index {|addr,i|
      weight = weights[i] ||= DEFAULT_WEIGHT
      weight.times {
        @readers << addr
      }
      @dbmap[addr] = nil
    }

    $log.info "MDS -- #{addrs.join(',')};#{weights.join(',')}"
  }

  if @dbmap.empty?
    raise "empty expression"
  end

  if @dbmap.size == 1
    # single node
    @readers = [@readers[0]]
  else
    @readers = @readers.sort_by {|addr| rand }
  end

  # open remote database
  @dbmap.keys.each {|addr|
    @dbmap[addr] = open_db(addr)
  }

rescue
  @dbmap.each_pair {|addr,db|
    if db
      close_db(db) rescue nil
    end
  }
  $log.error $!
  $log.error_backtrace $!.backtrace
  raise "MDS: invlaid address expression: #{$!}"
end