Module: Roma::CommandPlugin::PluginStorage

Includes:
Roma::CommandPlugin
Defined in:
lib/roma/plugin/plugin_storage.rb

Instance Method Summary collapse

Methods included from Roma::CommandPlugin

included, plugins

Instance Method Details

#ev_add(s) ⇒ Object

“add” means that “add a new data to a store” <command name> <key> <flags> <exptime> <bytes> [noreply]rn <data block>rn



234
# File 'lib/roma/plugin/plugin_storage.rb', line 234

def ev_add(s); set(:add,s); end

#ev_append(s) ⇒ Object

“append” means that “append a new data to the previous one” <command name> <key> <flags> <exptime> <bytes> [noreply]rn <data block>rn



246
# File 'lib/roma/plugin/plugin_storage.rb', line 246

def ev_append(s); set(:append,s); end

#ev_cas(s) ⇒ Object

“cas” means that “store this data but only if no one else has updated since I last fetched it.” <command name> <key> <flags> <exptime> <bytes> <cas-id>rn <data block>rn



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/roma/plugin/plugin_storage.rb', line 259

def ev_cas(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  v = read_bytes(s[4].to_i)
  read_bytes(2)
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes[0] != @nid
    @log.warn("forward cas key=#{key} vn=#{vn} to #{nodes[0]}")
    res = send_cmd(nodes[0],"fcas #{key}\e#{hname} #{d} #{s[3]} #{v.length} #{s[5]}\r\n#{v}\r\n")
    if res == nil || res.start_with?("ERROR")
      return send_data("SERVER_ERROR Message forward failed.\r\n")
    end
    return send_data("#{res}\r\n")
  end

  store_cas(hname, vn, key, d, s[5].to_i, s[3].to_i, v, nodes[1..-1])
end

#ev_decr(s) ⇒ Object

decr <key> <value> [noreply]rn



302
# File 'lib/roma/plugin/plugin_storage.rb', line 302

def ev_decr(s); incr_decr(:decr,s); end

#ev_delete(s) ⇒ Object

delete <key> [<time>] [noreply]rn



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/roma/plugin/plugin_storage.rb', line 118

def ev_delete(s)
  if s.length < 2
    @log.error("delete:wrong number of arguments(#{s})")
    return send_data("CLIENT_ERROR Wrong number of arguments.\r\n")
  end

  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes[0] != @nid
    cmd = "fdelete #{key}\e#{hname}"
    s[2..-1].each{|c| cmd << " #{c}"}
    cmd << "\r\n"
    @log.warn("forward delete #{s[1]}")
    res = send_cmd(nodes[0], cmd)
    if res == nil || res.start_with?("ERROR")
      return send_data("SERVER_ERROR Message forward failed.\r\n")
    end
    return send_data("#{res}\r\n")
  end
  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end

  if @stats.wb_command_map.key?(:delete__prev)
    data = @storages[hname].get(vn, key, d)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:delete__prev], key, data) if data
  end
  
  res = @storages[hname].delete(vn, key, d)
  @stats.delete_count += 1

  return send_data("NOT_DELETED\r\n") unless res
  return send_data("NOT_FOUND\r\n") if res == :deletemark

  if @stats.wb_command_map.key?(:delete)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:delete], key, res[4])
  end

  nodes[1..-1].each{ |nid|
    res2 = send_cmd(nid,"rdelete #{key}\e#{hname} #{res[2]}\r\n")
    unless res2
      Roma::AsyncProcess::queue.push(Roma::AsyncMessage.new('rdelete',[nid,hname,s[1],res[2]]))
      @log.warn("rdelete failed:#{s[1]}\e#{hname} #{d} #{res[2]} -> #{nid}")
    end
  }
  return send_data("NOT_FOUND\r\n") unless res[4]
  send_data("DELETED\r\n")
end

#ev_fadd(s) ⇒ Object



235
# File 'lib/roma/plugin/plugin_storage.rb', line 235

def ev_fadd(s); fset(:add,s); end

#ev_fappend(s) ⇒ Object



247
# File 'lib/roma/plugin/plugin_storage.rb', line 247

def ev_fappend(s); fset(:append,s); end

#ev_fcas(s) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/roma/plugin/plugin_storage.rb', line 279

def ev_fcas(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = s[2].to_i
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits if d == 0
  v = read_bytes(s[4].to_i)
  read_bytes(2)
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes.include?(@nid) == false
    @log.error("fcas failed key = #{s[1]} vn = #{vn}")
    return send_data("SERVER_ERROR Routing table is inconsistent.\r\n")
  end

  nodes.delete(@nid)
  store_cas(hname, vn, key, d, s[5].to_i, s[3].to_i, v, nodes)
end

#ev_fdecr(s) ⇒ Object



303
# File 'lib/roma/plugin/plugin_storage.rb', line 303

def ev_fdecr(s); fincr_fdecr(:decr,s); end

#ev_fdelete(s) ⇒ Object

fdelete <key> [<time>] [noreply]rn



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
# File 'lib/roma/plugin/plugin_storage.rb', line 172

def ev_fdelete(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes.include?(@nid) == false
    @log.error("fdelete failed delete key=#{s[1]} vn=#{vn}")
    return send_data("SERVER_ERROR Routing table is inconsistent.\r\n")
  end
  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end

  if @stats.wb_command_map.key?(:delete__prev)
    data = @storages[hname].get(vn, key, d)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:delete__prev], key, data) if data
  end
  
  res = @storages[hname].delete(vn, key, d)
  @stats.delete_count += 1

  return send_data("NOT_DELETED\r\n") unless res
  return send_data("NOT_FOUND\r\n") if res == :deletemark

  if @stats.wb_command_map.key?(:delete)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:delete], key, res[4])
  end

  nodes.delete(@nid)
  nodes.each{ |nid|
    res2 = send_cmd(nid,"rdelete #{key}\e#{hname} #{res[2]}\r\n")
    unless res2
      Roma::AsyncProcess::queue.push(Roma::AsyncMessage.new('rdelete',[nid,hname,s[1],res[2]]))
      @log.warn("rdelete failed:#{s[1]}\e#{hname} #{d} #{res[2]} -> #{nid}")
    end
  }
  return send_data("NOT_FOUND\r\n") unless res[4]
  send_data("DELETED\r\n")
end

#ev_fget(s) ⇒ Object

fget <key>



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roma/plugin/plugin_storage.rb', line 54

def ev_fget(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes(vn)

  unless nodes.include?(@nid)
    @log.error("fget failed key=#{s[1]} vn=#{vn}")
    return send_data("SERVER_ERROR Routing table is inconsistent.\r\n")
  end

  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end
  data = @storages[hname].get(vn, key, 0)
  @stats.read_count += 1
  send_data("VALUE #{s[1]} 0 #{data.length}\r\n#{data}\r\n") if data
  send_data("END\r\n")
end

#ev_fincr(s) ⇒ Object



299
# File 'lib/roma/plugin/plugin_storage.rb', line 299

def ev_fincr(s); fincr_fdecr(:incr,s); end

#ev_fprepend(s) ⇒ Object



253
# File 'lib/roma/plugin/plugin_storage.rb', line 253

def ev_fprepend(s); fset(:prepend,s); end

#ev_freplace(s) ⇒ Object



241
# File 'lib/roma/plugin/plugin_storage.rb', line 241

def ev_freplace(s); fset(:replace,s); end

#ev_fset(s) ⇒ Object



15
# File 'lib/roma/plugin/plugin_storage.rb', line 15

def ev_fset(s); fset(:set,s); end

#ev_fset_expt(s) ⇒ Object

fset_expt <key> <expt>



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
# File 'lib/roma/plugin/plugin_storage.rb', line 349

def ev_fset_expt(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes.include?(@nid) == false
    @log.error("fset_expt failed key = #{s[1]} vn = #{vn}")
    return send_data("SERVER_ERROR Routing table is inconsistent.\r\n")
  end
  
  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end

  if @stats.wb_command_map.key?(:set_expt__prev)
    # [vn, t, clk, expt, val]
    data = @storages[hname].get_raw(vn, key, d)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:set_expt__prev], key, data[3].to_s) if data
  end

  expt = chg_time_expt(s[2].to_i)
  ret = @storages[hname].set_expt(vn, key, d, expt)

  if ret
    if @stats.wb_command_map.key?(:set_expt)
      Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:set_expt], key, expt.to_s)
    end
    redundant(nodes[1..-1], hname, key, d, ret[2], ret[3], ret[4])
    send_data("STORED\r\n")
  else
    return send_data("NOT_STORED\r\n")
  end
end

#ev_get(s) ⇒ Object

get <key>*rn



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/roma/plugin/plugin_storage.rb', line 18

def ev_get(s)
  if s.length < 2
    @log.error("get:wrong number of arguments(#{s})")
    return send_data("CLIENT_ERROR Wrong number of arguments.\r\n")
  end

  return ev_gets(s) if s.length > 2

  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes(vn)

  unless nodes.include?(@nid)
    @log.warn("forward get #{s[1]}")
    res = forward_get(nodes[0], s[1], d)
    if res
      send_data(res)
    else
      send_data("SERVER_ERROR Message forward failed.\r\n")
    end
    return
  end

  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end
  data = @storages[hname].get(vn, key, 0)
  @stats.read_count += 1
  send_data("VALUE #{s[1]} 0 #{data.length}\r\n#{data}\r\n") if data
  send_data("END\r\n")
end

#ev_gets(s) ⇒ Object

gets <key>*rn



77
78
79
80
81
82
83
84
85
86
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
# File 'lib/roma/plugin/plugin_storage.rb', line 77

def ev_gets(s)
  nk = {} # {node-id1=>[key1,key2,..],node-id2=>[key3,key4,..]}
  kvn = {} # {key1=>vn1, key2=>vn2, ... }
  s[1..-1].each{|kh|
    key, = kh.split("\e") # split a hash-name
    d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
    kvn[key] = vn = @rttable.get_vnode_id(d)
    nodes = @rttable.search_nodes(vn)
    unless nodes.empty? # check the node existence
      nk[nodes[0]]=[] unless nk.key?(nodes[0])
      nk[nodes[0]] << kh
    end
  }

  res = {} # result data {key1=>val1,key2=>val2,...}
  if nk.key?(@nid)
    nk[@nid].each{|kh|
      key,hname = kh.split("\e")
      hname ||= @defhash
      if @storages.key?(hname)
        vn, t, clk, expt, val = @storages[hname].get_raw(kvn[key], key, 0)
        @stats.read_count += 1
        res[key] = [clk, val] if val && Time.now.to_i <= expt
      end
    }
    nk.delete(@nid)
  end

  nk.each_pair{|nid,keys|
    res.merge!(forward_gets(nid,keys))
  }

  res.each_pair{|key,cv|
    clk, val = cv
    send_data("VALUE #{key} 0 #{val.length} #{clk}\r\n#{val}\r\n")
  }
  send_data("END\r\n")
end

#ev_incr(s) ⇒ Object

incr <key> <value> [noreply]rn



298
# File 'lib/roma/plugin/plugin_storage.rb', line 298

def ev_incr(s); incr_decr(:incr,s); end

#ev_prepend(s) ⇒ Object

“prepend” means that “prepend a new data to the previous one” <command name> <key> <flags> <exptime> <bytes> [noreply]rn <data block>rn



252
# File 'lib/roma/plugin/plugin_storage.rb', line 252

def ev_prepend(s); set(:prepend,s); end

#ev_rdelete(s) ⇒ Object

rdelete <key> <clock>



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/roma/plugin/plugin_storage.rb', line 215

def ev_rdelete(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end
  if @storages[hname].rdelete(vn, key, d, s[2].to_i)
    send_data("DELETED\r\n")
  else
    send_data("NOT_FOUND\r\n")
  end
end

#ev_replace(s) ⇒ Object

“replace” means that “replace the previous data with a new one” <command name> <key> <flags> <exptime> <bytes> [noreply]rn <data block>rn



240
# File 'lib/roma/plugin/plugin_storage.rb', line 240

def ev_replace(s); set(:replace,s); end

#ev_rset_size_of_zredundant(s) ⇒ Object

rset_size_of_zredundant <n>



397
398
399
400
401
402
403
# File 'lib/roma/plugin/plugin_storage.rb', line 397

def ev_rset_size_of_zredundant(s)
  if s.length != 2 || s[1].to_i == 0
    return send_data("usage:set_set_size_of_zredundant <n>\r\n")
  end
  @stats.size_of_zredundant = s[1].to_i
  send_data("STORED\r\n")
end

#ev_set(s) ⇒ Object

“set” means “store this data”. <command name> <key> <flags> <exptime> <bytes> [noreply]rn <data block>rn



14
# File 'lib/roma/plugin/plugin_storage.rb', line 14

def ev_set(s); set(:set,s); end

#ev_set_expt(s) ⇒ Object

set_expt <key> <expt>



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/roma/plugin/plugin_storage.rb', line 306

def ev_set_expt(s)
  key,hname = s[1].split("\e")
  hname ||= @defhash
  d = Digest::SHA1.hexdigest(key).hex % @rttable.hbits
  vn = @rttable.get_vnode_id(d)
  nodes = @rttable.search_nodes_for_write(vn)
  if nodes[0] != @nid
    @log.warn("forward set_expt key=#{key} vn=#{vn} to #{nodes[0]}")
    res = send_cmd(nodes[0],"fset_expt #{s[1]} #{s[2]}\r\n")
    if res
      return send_data("#{res}\r\n")
    end
    return send_data("SERVER_ERROR Message forward failed.\r\n")
  end
  
  unless @storages.key?(hname)
    send_data("SERVER_ERROR #{hname} does not exists.\r\n")
    return
  end

  if @stats.wb_command_map.key?(:set_expt__prev)
@log.debug(":set_export__prev")
    # [vn, t, clk, expt, val]
    data = @storages[hname].get_raw(vn, key, d)
    Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:set_expt__prev], key, data[3].to_s) if data
  end

  expt = chg_time_expt(s[2].to_i)
  ret = @storages[hname].set_expt(vn, key, d, expt)

  if ret
    if @stats.wb_command_map.key?(:set_expt)
@log.debug(":set_export")
      Roma::WriteBehindProcess::push(hname, @stats.wb_command_map[:set_expt], key, expt.to_s)
    end
    redundant(nodes[1..-1], hname, key, d, ret[2], ret[3], ret[4])
    send_data("STORED\r\n")
  else
    return send_data("NOT_STORED\r\n")
  end
end

#ev_set_size_of_zredundant(s) ⇒ Object

set_size_of_zredundant <n>



386
387
388
389
390
391
392
393
394
# File 'lib/roma/plugin/plugin_storage.rb', line 386

def ev_set_size_of_zredundant(s)
  if s.length != 2 || s[1].to_i == 0
    return send_data("usage:set_set_size_of_zredundant <n>\r\n")
  end
  res = broadcast_cmd("rset_size_of_zredundant #{s[1]}\r\n")
  @stats.size_of_zredundant = s[1].to_i
  res[@stats.ap_str] = "STORED"
  send_data("#{res}\r\n")
end