Class: Aerospike::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/command/command.rb

Overview

:nodoc:

Direct Known Subclasses

BatchCommand, SingleCommand

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Command

Returns a new instance of Command.



78
79
80
81
82
# File 'lib/aerospike/command/command.rb', line 78

def initialize(node)
  @node = node

  self
end

Instance Method Details

#executeObject



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/aerospike/command/command.rb', line 394

def execute
  iterations = 0

  # set timeout outside the loop
  limit = Time.now + @policy.timeout

  # Execute command until successful, timed out or maximum iterations have been reached.
  while true
    # too many retries
    iterations += 1
    break if (@policy.max_retries > 0) && (iterations > @policy.max_retries+1)

    # Sleep before trying again, after the first iteration
    sleep(@policy.sleep_between_retries) if iterations > 1 && @policy.sleep_between_retries > 0

    # check for command timeout
    break if @policy.timeout > 0 && Time.now > limit

    begin
      @conn = @node.get_connection(@policy.timeout)
    rescue => e
      # Socket connection error has occurred. Decrease health and retry.
      @node.decrease_health

      Aerospike.logger.error("Node #{@node.to_s}: #{e}")
      next
    end

    # Draw a buffer from buffer pool, and make sure it will be put back
    begin
      @data_buffer = Buffer.get

      # Set command buffer.
      begin
        write_buffer
      rescue => e
        Aerospike.logger.error(e)

        # All runtime exceptions are considered fatal. Do not retry.
        # Close socket to flush out possible garbage. Do not put back in pool.
        @conn.close if @conn
        raise e
      end

      # Reset timeout in send buffer (destined for server) and socket.
      @data_buffer.write_int32((@policy.timeout * 1000).to_i, 22)

      # Send command.
      begin
        @conn.write(@data_buffer, @data_offset)
      rescue => e
        # IO errors are considered temporary anomalies. Retry.
        # Close socket to flush out possible garbage. Do not put back in pool.
        @conn.close if @conn

        Aerospike.logger.error("Node #{@node.to_s}: #{e}")
        # IO error means connection to server @node is unhealthy.
        # Reflect cmd status.
        @node.decrease_health
        next
      end

      # Parse results.
      begin
        parse_result
      rescue => e
        Aerospike.logger.error(e)
        
        # close the connection
        # cancelling/closing the batch/multi commands will return an error, which will
        # close the connection to throw away its data and signal the server about the
        # situation. We will not put back the connection in the buffer.
        @conn.close if @conn
        raise e
      end

      # Reflect healthy status.
      @node.restore_health

      # Put connection back in pool.
      @node.put_connection(@conn)

      # command has completed successfully.  Exit method.
      return
    ensure
      Buffer.put(@data_buffer)
    end

  end # while

  # execution timeout
  raise Aerospike::Exceptions::Timeout.new(limit, iterations)
end

#set_batch_exists(policy, batch_namespace) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/aerospike/command/command.rb', line 264

def set_batch_exists(policy, batch_namespace)
  # Estimate buffer size
  begin_cmd
  keys = batch_namespace.keys
  byte_size = keys.length * DIGEST_SIZE

  @data_offset += (batch_namespace ? batch_namespace.namespace.bytesize : 0)  +
    FIELD_HEADER_SIZE + byte_size + FIELD_HEADER_SIZE

  size_buffer

  write_header(policy, INFO1_READ|INFO1_NOBINDATA, 0, 2, 0)
  write_field_string(batch_namespace.namespace, Aerospike::FieldType::NAMESPACE)
  write_field_header(byte_size, Aerospike::FieldType::DIGEST_RIPE_ARRAY)

  keys.each do |key|
    @data_buffer.write_binary(key.digest, @data_offset)
    @data_offset += key.digest.bytesize
  end
  end_cmd
end

#set_batch_get(policy, batch_namespace, bin_names, read_attr) ⇒ Object



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
322
323
# File 'lib/aerospike/command/command.rb', line 286

def set_batch_get(policy, batch_namespace, bin_names, read_attr)
  # Estimate buffer size
  begin_cmd
  byte_size = batch_namespace.keys.length * DIGEST_SIZE

  @data_offset += batch_namespace.namespace.bytesize +
    FIELD_HEADER_SIZE + byte_size + FIELD_HEADER_SIZE

  if bin_names
    bin_names.each do |bin_name|
      estimate_operation_size_for_bin_name(bin_name)
    end
  end

  size_buffer

  operation_count = 0
  if bin_names
    operation_count = bin_names.length
  end

  write_header(policy, read_attr, 0, 2, operation_count)
  write_field_string(batch_namespace.namespace, Aerospike::FieldType::NAMESPACE)
  write_field_header(byte_size, Aerospike::FieldType::DIGEST_RIPE_ARRAY)

  batch_namespace.keys.each do |key|
    @data_buffer.write_binary(key.digest, @data_offset)
    @data_offset += key.digest.bytesize
  end

  if bin_names
    bin_names.each do |bin_name|
      write_operation_for_bin_name(bin_name, Aerospike::Operation::READ)
    end
  end

  end_cmd
end

#set_delete(policy, key) ⇒ Object

Writes the command for delete operations



116
117
118
119
120
121
122
123
# File 'lib/aerospike/command/command.rb', line 116

def set_delete(policy, key)
  begin_cmd
  field_count = estimate_key_size(key)
  size_buffer
  write_header_with_policy(policy, 0, INFO2_WRITE|INFO2_DELETE, field_count, 0)
  write_key(key)
  end_cmd
end

#set_exists(policy, key) ⇒ Object

Writes the command for exist operations



138
139
140
141
142
143
144
145
# File 'lib/aerospike/command/command.rb', line 138

def set_exists(policy, key)
  begin_cmd
  field_count = estimate_key_size(key)
  size_buffer
  write_header(policy, INFO1_READ|INFO1_NOBINDATA, 0, field_count, 0)
  write_key(key)
  end_cmd
end

#set_operate(policy, key, operations) ⇒ Object

Implements different command operations



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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/aerospike/command/command.rb', line 200

def set_operate(policy, key, operations)
  begin_cmd
  field_count = estimate_key_size(key)
  read_attr = 0
  write_attr = 0
  read_header = false

  operations.each do |operation|
    case operation.op_type
    when Aerospike::Operation::READ
        read_attr |= INFO1_READ

      # Read all bins if no bin is specified.
      read_attr |= INFO1_GET_ALL unless operation.bin_name

    when Aerospike::Operation::READ_HEADER
        # The server does not currently return record header data with _INFO1_NOBINDATA attribute set.
        # The workaround is to request a non-existent bin.
        # TODO: Fix this on server.
        # read_attr |= _INFO1_READ | _INFO1_NOBINDATA
        read_attr |= INFO1_READ
      read_header = true

    else
      write_attr = INFO2_WRITE
    end

    estimate_operation_size_for_operation(operation)
  end
  size_buffer

  if write_attr != 0
    write_header_with_policy(policy, read_attr, write_attr, field_count, operations.length)
  else
    write_header(policy, read_attr, write_attr, field_count, operations.length)
  end
  write_key(key)

  operations.each do |operation|
    write_operation_for_operation(operation)
  end

  write_operation_for_bin(nil, Aerospike::Operation::READ) if read_header

  end_cmd
end

#set_read(policy, key, bin_names) ⇒ Object

Writes the command for get operations (specified bins)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/aerospike/command/command.rb', line 158

def set_read(policy, key, bin_names)
  if bin_names && bin_names.length > 0
    begin_cmd
    field_count = estimate_key_size(key)

    bin_names.each do |bin_name|
      estimate_operation_size_for_bin_name(bin_name)
    end

    size_buffer
    write_header(policy, INFO1_READ, 0, field_count, bin_names.length)
    write_key(key)

    bin_names.each do |bin_name|
      write_operation_for_bin_name(bin_name, Aerospike::Operation::READ)
    end

    end_cmd
  else
    set_read_for_key_only(policy, key)
  end
end

#set_read_for_key_only(policy, key) ⇒ Object

Writes the command for get operations (all bins)



148
149
150
151
152
153
154
155
# File 'lib/aerospike/command/command.rb', line 148

def set_read_for_key_only(policy, key)
  begin_cmd
  field_count = estimate_key_size(key)
  size_buffer
  write_header(policy, INFO1_READ|INFO1_GET_ALL, 0, field_count, 0)
  write_key(key)
  end_cmd
end

#set_read_header(policy, key) ⇒ Object

Writes the command for getting metadata operations



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/aerospike/command/command.rb', line 182

def set_read_header(policy, key)
  begin_cmd
  field_count = estimate_key_size(key)
  estimate_operation_size_for_bin_name('')
  size_buffer

  # The server does not currently return record header data with _INFO1_NOBINDATA attribute set.
  # The workaround is to request a non-existent bin.
  # TODO: Fix this on server.
  #command.set_read(INFO1_READ | _INFO1_NOBINDATA);
  write_header(policy, INFO1_READ, 0, field_count, 1)

  write_key(key)
  write_operation_for_bin_name('', Aerospike::Operation::READ)
  end_cmd
end

#set_scan(policy, namespace, set_name, bin_names) ⇒ Object



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
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
391
392
# File 'lib/aerospike/command/command.rb', line 325

def set_scan(policy, namespace, set_name, bin_names)
  # Estimate buffer size
  begin_cmd
  field_count = 0

  if namespace
    @data_offset += namespace.bytesize + FIELD_HEADER_SIZE
    field_count += 1
  end

  if set_name
    @data_offset += set_name.bytesize + FIELD_HEADER_SIZE
    field_count += 1
  end

  # Estimate scan options size.
  @data_offset += 2 + FIELD_HEADER_SIZE
  field_count += 1

  if bin_names
    bin_names.each do |bin_name|
      estimate_operation_size_for_bin_name(bin_name)
    end
  end

  size_buffer
  read_attr = INFO1_READ

  if !policy.include_bin_data
    read_attr |= INFO1_NOBINDATA
  end

  operation_count = 0
  if bin_names
    operation_count = bin_names.length
  end

  write_header(policy, read_attr, 0, field_count, operation_count)

  if namespace
    write_field_string(namespace, Aerospike::FieldType::NAMESPACE)
  end

  if set_name
    write_field_string(set_name, Aerospike::FieldType::TABLE)
  end

  write_field_header(2, Aerospike::FieldType::SCAN_OPTIONS)

  priority = policy.priority & 0xFF
  priority <<= 4
  if policy.fail_on_cluster_change
    priority |= 0x08
  end

  @data_buffer.write_byte(priority, @data_offset)
  @data_offset += 1
  @data_buffer.write_byte(policy.scan_percent.to_i.ord, @data_offset)
  @data_offset += 1

  if bin_names
    bin_names.each do |bin_name|
      write_operation_for_bin_name(bin_name, Aerospike::Operation::READ)
    end
  end

  end_cmd
end

#set_touch(policy, key) ⇒ Object

Writes the command for touch operations



126
127
128
129
130
131
132
133
134
135
# File 'lib/aerospike/command/command.rb', line 126

def set_touch(policy, key)
  begin_cmd
  field_count = estimate_key_size(key)
  estimate_operation_size
  size_buffer
  write_header_with_policy(policy, 0, INFO2_WRITE, field_count, 1)
  write_key(key)
  write_operation_for_operation_type(Aerospike::Operation::TOUCH)
  end_cmd
end

#set_udf(policy, key, package_name, function_name, args) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/aerospike/command/command.rb', line 247

def set_udf(policy, key, package_name, function_name, args)
  begin_cmd
  field_count = estimate_key_size(key)
  arg_bytes = args.to_bytes

  field_count += estimate_udf_size(package_name, function_name, arg_bytes)
  size_buffer

  write_header(policy, 0, INFO2_WRITE, field_count, 0)
  write_key(key)
  write_field_string(package_name, Aerospike::FieldType::UDF_PACKAGE_NAME)
  write_field_string(function_name, Aerospike::FieldType::UDF_FUNCTION)
  write_field_bytes(arg_bytes, Aerospike::FieldType::UDF_ARGLIST)

  end_cmd
end

#set_write(policy, operation, key, bins) ⇒ Object

Writes the command for write operations



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
# File 'lib/aerospike/command/command.rb', line 85

def set_write(policy, operation, key, bins)
  begin_cmd
  field_count = estimate_key_size(key)

  if policy.send_key
    # field header size + key size
    @data_offset += key.user_key_as_value.estimate_size + FIELD_HEADER_SIZE
    field_count += 1
  end

  bins.each do |bin|
    estimate_operation_size_for_bin(bin)
  end

  size_buffer

  write_header_with_policy(policy, 0, INFO2_WRITE, field_count, bins.length)
  write_key(key)

  if policy.send_key
    write_field_value(key.user_key_as_value, Aerospike::FieldType::KEY)
  end

  bins.each do |bin|
    write_operation_for_bin(bin, operation)
  end

  end_cmd
end