Class: MassiveRecord::Adapters::Thrift::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/adapters/thrift/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
# File 'lib/massive_record/adapters/thrift/connection.rb', line 10

def initialize(opts = {})
  @timeout = 4000
  @host    = opts[:host]
  @port    = opts[:port] || 9090
  @instrumenter = ActiveSupport::Notifications.instrumenter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Wrapp HBase API to be able to catch errors and try reconnect



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/massive_record/adapters/thrift/connection.rb', line 65

def method_missing(method, *args)
  begin
    open if not @client
    client.send(method, *args) if @client
  rescue ::Thrift::TransportException => error
    @transport = nil
    @client = nil
    open(:reconnecting => true, :reason => error.class)
    client.send(method, *args) if @client
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/massive_record/adapters/thrift/connection.rb', line 8

def host
  @host
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/massive_record/adapters/thrift/connection.rb', line 8

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/massive_record/adapters/thrift/connection.rb', line 8

def timeout
  @timeout
end

Instance Method Details

#clientObject



45
46
47
# File 'lib/massive_record/adapters/thrift/connection.rb', line 45

def client
  @client
end

#closeObject



41
42
43
# File 'lib/massive_record/adapters/thrift/connection.rb', line 41

def close
  @transport.close.nil?
end

#load_table(table_name) ⇒ Object



60
61
62
# File 'lib/massive_record/adapters/thrift/connection.rb', line 60

def load_table(table_name)
  MassiveRecord::Wrapper::Table.new(self, table_name)
end

#open(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/massive_record/adapters/thrift/connection.rb', line 21

def open(options = {})
  options = options.merge({
    :adapter => 'Thrift',
    :host => @host,
    :port => @port
  })

  @instrumenter.instrument "adapter_connecting.massive_record", options do
    protocol = ::Thrift::BinaryProtocol.new(transport)
    @client = Apache::Hadoop::Hbase::Thrift::Hbase::Client.new(protocol)

    begin
      transport.open()
      true
    rescue
      raise MassiveRecord::Wrapper::Errors::ConnectionException.new, "Unable to connect to HBase on #{@host}, port #{@port}"
    end
  end
end

#open?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/massive_record/adapters/thrift/connection.rb', line 49

def open?
  @transport.try("open?")
end

#tablesObject



53
54
55
56
57
58
# File 'lib/massive_record/adapters/thrift/connection.rb', line 53

def tables
  collection = MassiveRecord::Wrapper::TablesCollection.new
  collection.connection = self
  getTableNames().each{|table_name| collection.push(table_name)}
  collection
end

#transportObject



17
18
19
# File 'lib/massive_record/adapters/thrift/connection.rb', line 17

def transport
  @transport ||= ::Thrift::BufferedTransport.new(::Thrift::Socket.new(@host, @port, @timeout))
end