Module: Roma::Command::Definition::ClassMethods

Defined in:
lib/roma/command/command_definition.rb

Defined Under Namespace

Classes: CommandContext, CommandParams, StoredData

Instance Method Summary collapse

Instance Method Details

#def_command_with_key(cmd, forward = :one_line, &block) ⇒ Object

def_write_command_with_key



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
# File 'lib/roma/command/command_definition.rb', line 168

def def_command_with_key(cmd, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt
      ctx = CommandContext.new(s, params, stored)
      instance_exec(ctx, &block)
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block) ⇒ Object

def_read_command_with_key_value



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/roma/command/command_definition.rb', line 323

def def_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      params.value = read_bytes(s[idx_of_val_len].to_i)
      read_bytes(2)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s, params.value)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s, params.value)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt
      ctx = CommandContext.new(s, params, stored)
      instance_exec(ctx, &block)
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_command_with_relay(cmd, &block) ⇒ Object



15
16
17
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
52
# File 'lib/roma/command/command_definition.rb', line 15

def def_command_with_relay(cmd, &block)
  # check a duplicated command definition in a same scope
  if public_method_defined? "ev_#{cmd}".to_sym
    raise "ev_#{cmd} already defined."
  end

  # define a command receiver
  define_method "ev_#{cmd}" do |s|
    begin
      res = {}
      res[@stats.ap_str] = instance_exec(s, &block)
      # command relay
      res.merge! broadcast_cmd("r#{cmd} #{s[1..-1].join(' ')}\r\n")
      send_data("#{res}\r\n")
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end

  # define a relaid command receiver
  define_method "ev_r#{cmd}" do |s|
    begin
      send_data("#{instance_exec(s, &block)}\r\n")
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_read_command_with_key(cmd, forward = :one_line, &block) ⇒ Object



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
91
92
93
94
95
# File 'lib/roma/command/command_definition.rb', line 58

def def_read_command_with_key(cmd, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt
      ctx = CommandContext.new(s, params, stored)
      instance_exec(ctx, &block)
      @stats.read_count += 1
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_read_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block) ⇒ Object

def_write_command_with_key_value



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/roma/command/command_definition.rb', line 281

def def_read_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      params.value = read_bytes(s[idx_of_val_len].to_i)
      read_bytes(2)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s, params.value)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s, params.value)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt
      ctx = CommandContext.new(s, params, stored)

      instance_exec(ctx, &block)
      @stats.read_count += 1
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_write_command_with_key(cmd, forward = :one_line, &block) ⇒ Object

def_read_command_with_key



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
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
# File 'lib/roma/command/command_definition.rb', line 97

def def_write_command_with_key(cmd, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt
      if stored && @stats.wb_command_map.key?("#{cmd}__prev".to_sym)
        Roma::WriteBehindProcess::push(params.hash_name,
                                       @stats.wb_command_map["#{cmd}__prev".to_sym],
                                       params.key, stored.value)
      end
      ctx = CommandContext.new(s, params, stored)

      ret = instance_exec(ctx, &block)
      if ret.instance_of? Array
        flg, expt, value, count, msg = ret
        ret = @storages[ctx.params.hash_name].set(ctx.params.vn, 
                                                  ctx.params.key,
                                                  ctx.params.digest,
                                                  expt,
                                                  value)
        if count == :write
          @stats.write_count += 1
        elsif count == :delete
          @stats.delete_count += 1
        end

        if ret
          if @stats.wb_command_map.key?(cmd.to_sym)
            Roma::WriteBehindProcess::push(ctx.params.hash_name,
                                           @stats.wb_command_map[cmd.to_sym],
                                           ctx.params.key, ret[4])
          end
          redundant(ctx.params.nodes[1..-1], ctx.params.hash_name,
                    ctx.params.key, ctx.params.digest, ret[2],
                    expt, ret[4])
          send_data("#{msg}\r\n")
        else
          send_data("NOT_#{msg}\r\n")
        end
      end
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end

#def_write_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block) ⇒ Object

def_command_with_key



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/roma/command/command_definition.rb', line 206

def def_write_command_with_key_value(cmd, idx_of_val_len, forward = :one_line, &block)
  define_method "ev_#{cmd}" do |s|
    return send_data("CLIENT_ERROR does not find key\r\n") if s.length < 2
    begin
      params = CommandParams.new
      params.key, params.hash_name = s[1].split("\e")
      params.hash_name ||= @defhash
      params.digest = Digest::SHA1.hexdigest(params.key).hex % @rttable.hbits
      params.vn = @rttable.get_vnode_id(params.digest)
      params.nodes = @rttable.search_nodes_for_write(params.vn)
      params.value = read_bytes(s[idx_of_val_len].to_i)
      read_bytes(2)
      if params.nodes[0] != @nid
        if forward == :one_line
          return forward_and_one_line_receive(params.nodes[0], s, params.value)
        elsif  forward == :multi_line
          return forward_and_multi_line_receive(params.nodes[0], s, params.value)
        end
      end
      stored = StoredData.new
      unless @storages[params.hash_name]
        return send_data("SERVER_ERROR #{params.hash_name} does not exists.\r\n")
      end

      stored.vn, stored.last, stored.clk, stored.expt, stored.value =
        @storages[params.hash_name].get_raw(params.vn, params.key, params.digest)
      stored = nil if stored.vn == nil || Time.now.to_i > stored.expt

      if stored && @stats.wb_command_map.key?("#{cmd}__prev".to_sym)
        Roma::WriteBehindProcess::push(params.hash_name,
                                       @stats.wb_command_map["#{cmd}__prev".to_sym],
                                       params.key, stored.value)
      end

      ctx = CommandContext.new(s, params, stored)
      
      ret = instance_exec(ctx, &block)
      if ret.instance_of? Array
        flg, expt, value, count, msg = ret
        ret = @storages[ctx.params.hash_name].set(ctx.params.vn, 
                                                  ctx.params.key,
                                                  ctx.params.digest,
                                                  expt,
                                                  value)
        if count == :write
          @stats.write_count += 1
        elsif count == :delete
          @stats.delete_count += 1
        end

        if ret
          if @stats.wb_command_map.key?(cmd.to_sym)
            Roma::WriteBehindProcess::push(ctx.params.hash_name,
                                           @stats.wb_command_map[cmd.to_sym],
                                           ctx.params.key, ret[4])
          end
          redundant(ctx.params.nodes[1..-1], ctx.params.hash_name, 
                    ctx.params.key, ctx.params.digest, ret[2], 
                    expt, ret[4])
          send_data("#{msg}\r\n")
        else
          send_data("NOT_#{msg}\r\n")
        end
      end
    rescue ClientErrorException => e
      send_data("CLIENT_ERROR #{e.message}\r\n")
    rescue ServerErrorException => e
      send_data("SERVER_ERROR #{e.message}\r\n")
    rescue LocalJumpError => e
      @log.warn("#{e} #{$@}")
      e.exit_value
    end
  end
end