Skip to content

Commit

Permalink
Adding HappyBase compat pool module.
Browse files Browse the repository at this point in the history
Only implementing constructor of ConnectionPool for now,
as well as the custom exception that is used by that module.
  • Loading branch information
dhermes committed Sep 4, 2015
1 parent b3d47c9 commit 8232abe
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/happybase-pool.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HappyBase Connection Pool
~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: gcloud_bigtable.happybase.pool
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

happybase-connection
happybase-batch
happybase-pool

Google Cloud Bigtable: Python
=============================
Expand Down
10 changes: 2 additions & 8 deletions gcloud_bigtable/happybase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@
from gcloud_bigtable.happybase.connection import Connection
from gcloud_bigtable.happybase.connection import DEFAULT_HOST
from gcloud_bigtable.happybase.connection import DEFAULT_PORT
from gcloud_bigtable.happybase.pool import ConnectionPool
from gcloud_bigtable.happybase.pool import NoConnectionsAvailable


# Types that have yet to be implemented
class Table(object):
"""Unimplemented Table stub."""


class ConnectionPool(object):
"""Unimplemented ConnectionPool stub."""


class NoConnectionsAvailable(object):
"""Unimplemented NoConnectionsAvailable stub."""


# Values from HappyBase that we don't reproduce / are not relevant.
__version__ = None
71 changes: 71 additions & 0 deletions gcloud_bigtable/happybase/pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Bigtable HappyBase pool module."""


from Queue import LifoQueue
import six
import threading

from gcloud_bigtable.happybase.connection import Connection


class NoConnectionsAvailable(RuntimeError):
"""Exception raised when no connections are available.
This happens if a timeout was specified when obtaining a connection,
and no connection became available within the specified timeout.
"""


class ConnectionPool(object):
"""Thread-safe connection pool.
.. note::
All keyword arguments are passed unmodified to the
:class:`.Connection` constructor **except** for ``autoconnect``.
This is because the ``open`` / ``closed`` status of a connection
is managed by the pool.
:type size: int
:param size: The maximum number of concurrently open connections.
:type kwargs: dict
:param kwargs: Keyword arguments passed to :class:`.Connection`
constructor.
:raises: :class:`TypeError <exceptions.TypeError>` if ``size``
is non an integer.
:class:`ValueError <exceptions.ValueError>` if ``size``
is not positive.
"""
def __init__(self, size, **kwargs):
if not isinstance(size, six.integer_types):
raise TypeError('Pool size arg must be an integer')

if size <= 0:
raise ValueError('Pool size must be positive')

self._lock = threading.Lock()
self._queue = LifoQueue(maxsize=size)
self._thread_connections = threading.local()

connection_kwargs = kwargs
connection_kwargs['autoconnect'] = False

for _ in xrange(size):
connection = Connection(**connection_kwargs)
self._queue.put(connection)
7 changes: 5 additions & 2 deletions gcloud_bigtable/happybase/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ def test_constructor_with_non_default_wal(self):
with self.assertRaises(ValueError):
self._makeOne(table, wal=wal)

def test_constructor_with_negative_batch_size(self):
def test_constructor_with_non_positive_batch_size(self):
table = object()
batch_size = -1
batch_size = -10
with self.assertRaises(ValueError):
self._makeOne(table, batch_size=batch_size)
batch_size = 0
with self.assertRaises(ValueError):
self._makeOne(table, batch_size=batch_size)

Expand Down
100 changes: 100 additions & 0 deletions gcloud_bigtable/happybase/test_pool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest2


class TestConnectionPool(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable.happybase.pool import ConnectionPool
return ConnectionPool

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor_defaults(self):
from Queue import LifoQueue
import threading
from gcloud_bigtable.happybase.connection import Connection

size = 11
pool = self._makeOne(size)

self.assertTrue(isinstance(pool._lock, type(threading.Lock())))
self.assertTrue(isinstance(pool._thread_connections, threading.local))
self.assertEqual(pool._thread_connections.__dict__, {})

queue = pool._queue
self.assertTrue(isinstance(queue, LifoQueue))
self.assertTrue(queue.full())
self.assertEqual(queue.maxsize, size)
for connection in queue.queue:
self.assertTrue(isinstance(connection, Connection))

def test_constructor_passes_kwargs(self):
timeout = 1000
table_prefix = 'foo'
table_prefix_separator = '<>'

size = 1
pool = self._makeOne(size, timeout=timeout, table_prefix=table_prefix,
table_prefix_separator=table_prefix_separator)

for connection in pool._queue.queue:
self.assertEqual(connection.timeout, timeout)
self.assertEqual(connection.table_prefix, table_prefix)
self.assertEqual(connection.table_prefix_separator,
table_prefix_separator)

def test_constructor_ignores_autoconnect(self):
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable.happybase.connection import Connection
from gcloud_bigtable.happybase import pool as MUT

class ConnectionWithOpen(Connection):

_open_called = False

def open(self):
self._open_called = True

# First make sure the custom Connection class does as expected.
connection = ConnectionWithOpen(autoconnect=False)
self.assertFalse(connection._open_called)
connection = ConnectionWithOpen(autoconnect=True)
self.assertTrue(connection._open_called)

# Then make sure autoconnect=True is ignored in a pool.
size = 1
with _Monkey(MUT, Connection=ConnectionWithOpen):
pool = self._makeOne(size, autoconnect=True)

for connection in pool._queue.queue:
self.assertTrue(isinstance(connection, ConnectionWithOpen))
self.assertFalse(connection._open_called)

def test_constructor_non_integer_size(self):
size = None
with self.assertRaises(TypeError):
self._makeOne(size)

def test_constructor_non_positive_size(self):
size = -10
with self.assertRaises(ValueError):
self._makeOne(size)
size = 0
with self.assertRaises(ValueError):
self._makeOne(size)

0 comments on commit 8232abe

Please sign in to comment.