-
Notifications
You must be signed in to change notification settings - Fork 9
[WIP] add connection pool class #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nkeyes
wants to merge
14
commits into
master
Choose a base branch
from
connection-pool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
161979d
add connection pool class
471f7e4
fix connection conditional
6e1e438
add comment from HappyBase about making the first connection
3340a9e
only eagerly load the first connection if opts[:auto_connect] is truthy
c0aec39
rename current thread connection variable name to be more specific
17dcb01
remove unneeded mutex/lock
1237a62
Merge branch 'master' of github.com:okcwest/ok_hbase into connection-…
c38ea52
fix current connection reset
faf9b9a
require ok_hbase/pool
10c9266
add OkHbase::Connection#ping?
cc88378
handle connection failures better
4163799
add Connection#reset
51b42ad
track pool connection ids
475684a
Merge branch 'master' of github.com:okcwest/ok_hbase into connection-…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module OkHbase | ||
| class NoConnectionsAvailable < RuntimeError | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| require 'thread' | ||
| require 'timeout' | ||
|
|
||
| require 'ok_hbase/connection' | ||
| require 'ok_hbase/no_connections_available' | ||
|
|
||
|
|
||
| module OkHbase | ||
| class Pool | ||
| @_lock | ||
| @_connection_queue | ||
| @_connection_ids | ||
|
|
||
| def initialize(size, opts={}) | ||
| raise TypeError.new("'size' must be an integer") unless size.is_a? Integer | ||
| raise ArgumentError.new("'size' must be > 0") unless size > 0 | ||
|
|
||
| OkHbase.logger.debug("Initializing connection pool with #{size} connections.") | ||
|
|
||
| @_lock = Mutex.new | ||
| @_connection_queue = Queue.new | ||
| @_connection_ids = [] | ||
|
|
||
| connection_opts = opts.dup | ||
|
|
||
| connection_opts[:auto_connect] = false | ||
|
|
||
| size.times do | ||
| connection = OkHbase::Connection.new(connection_opts) | ||
| @_connection_queue << connection | ||
| @_connection_ids << connection.object_id | ||
| end | ||
|
|
||
| # The first connection is made immediately so that trivial | ||
| # mistakes like unresolvable host names are raised immediately. | ||
| # Subsequent connections are connected lazily. | ||
| self.with_connection {} if opts[:auto_connect] | ||
| end | ||
|
|
||
| def synchronize(&block) | ||
| @_lock.synchronize(&block) | ||
| end | ||
|
|
||
| def with_connection(timeout = nil) | ||
| connection = Thread.current[:ok_hbase_current_connection] | ||
|
|
||
| return_after_use = false | ||
|
|
||
| begin | ||
| unless connection | ||
| return_after_use = true | ||
| connection = get_connection(timeout) | ||
| Thread.current[:ok_hbase_current_connection] = connection | ||
| end | ||
| yield connection | ||
| rescue Apache::Hadoop::Hbase::Thrift::IOError, Thrift::TransportException, SocketError => e | ||
| raise e | ||
| ensure | ||
| if return_after_use | ||
| Thread.current[:ok_hbase_current_connection] = nil | ||
| return_connection(connection) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def get_connection(timeout = nil) | ||
| begin | ||
| connection = Timeout.timeout(timeout) do | ||
| @_connection_queue.deq | ||
| end | ||
| rescue TimeoutError | ||
| raise OkHbase::NoConnectionsAvailable.new("No connection available from pool within specified timeout: #{timeout}") | ||
| end | ||
| begin | ||
| connection.open() | ||
| connection.reset unless connection.ping? | ||
| rescue Apache::Hadoop::Hbase::Thrift::IOError, Thrift::TransportException, SocketError => e | ||
| connection.reset | ||
| raise e | ||
| end | ||
| connection | ||
| end | ||
|
|
||
| def return_connection(connection) | ||
| synchronize do | ||
| return unless @_connection_ids.include? connection.object_id | ||
| end | ||
|
|
||
| @_connection_queue << connection | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked whether enqueueing an element like this is thread safe? Or might you need to wrap that in a synchronize call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the Queue class in ruby is meant specifically for inter-thread usage: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html