Skip to content
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

Set TCP keepalive on Redshift (#782) #826

Merged
merged 2 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion dbt/adapters/postgres/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class PostgresAdapter(dbt.adapters.default.DefaultAdapter):

DEFAULT_TCP_KEEPALIVE = 0 # 0 means to use the default value

@classmethod
@contextmanager
def exception_handler(cls, profile, sql, model_name=None,
Expand Down Expand Up @@ -67,6 +69,13 @@ def open_connection(cls, connection):

base_credentials = connection.get('credentials', {})
credentials = cls.get_credentials(base_credentials.copy())
kwargs = {}
keepalives_idle = credentials.get('keepalives_idle',
cls.DEFAULT_TCP_KEEPALIVE)
# we don't want to pass 0 along to connect() as postgres will try to
# call an invalid setsockopt() call (contrary to the docs).
if keepalives_idle:
kwargs['keepalives_idle'] = keepalives_idle

try:
handle = psycopg2.connect(
Expand All @@ -75,7 +84,8 @@ def open_connection(cls, connection):
host=credentials.get('host'),
password=credentials.get('pass'),
port=credentials.get('port'),
connect_timeout=10)
connect_timeout=10,
**kwargs)

result['handle'] = handle
result['state'] = 'open'
Expand Down
3 changes: 3 additions & 0 deletions dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class RedshiftAdapter(PostgresAdapter):

DEFAULT_TCP_KEEPALIVE = 240
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious... where did this number come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drew linked me to this which has a docstring bit that says:

Note: For Redshift, use keepalives_idle in the extra connection parameters and set it to less than 300 seconds.

I wasn't sure how precise the 300 value was or where they sourced it from, so I figured one minute less would give us a bit more wiggle room on slightly flaky connections and stuff. Now that I think about it, they probably did the same thing with whatever their source was and 300 is also conservative, but hey, a few extra keepalives shouldn't hurt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@classmethod
def type(cls):
return 'redshift'
Expand Down
6 changes: 6 additions & 0 deletions dbt/contracts/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
'schema': {
'type': 'string',
},
'keepalives_idle': {
'type': 'integer',
},
},
'required': ['dbname', 'host', 'user', 'pass', 'port', 'schema'],
}
Expand Down Expand Up @@ -87,6 +90,9 @@
'If using IAM auth, the ttl for the temporary credentials'
)
},
'keepalives_idle': {
'type': 'integer',
},
'required': ['dbname', 'host', 'user', 'port', 'schema']
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/unit/test_postgres_adapter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import mock
import unittest

import dbt.flags as flags

import dbt.adapters
from dbt.adapters.postgres import PostgresAdapter
from dbt.exceptions import ValidationException
from dbt.logger import GLOBAL_LOGGER as logger # noqa
Expand Down Expand Up @@ -37,3 +39,43 @@ def test_acquire_connection(self):

self.assertEquals(connection.get('state'), 'open')
self.assertNotEquals(connection.get('handle'), None)

@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_default_keepalive(self, psycopg2):
connection = PostgresAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='database',
password='password',
port=5432,
connect_timeout=10)

@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_changed_keepalive(self, psycopg2):
self.profile['keepalives_idle'] = 256
connection = PostgresAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='database',
password='password',
port=5432,
connect_timeout=10,
keepalives_idle=256)

@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_set_zero_keepalive(self, psycopg2):
self.profile['keepalives_idle'] = 0
connection = PostgresAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='postgres',
user='root',
host='database',
password='password',
port=5432,
connect_timeout=10)

100 changes: 57 additions & 43 deletions test/unit/test_redshift_adapter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import mock

import dbt.adapters
import dbt.flags as flags
import dbt.utils

Expand All @@ -20,8 +21,7 @@ class TestRedshiftAdapter(unittest.TestCase):
def setUp(self):
flags.STRICT_MODE = True

def test_implicit_database_conn(self):
implicit_database_profile = {
self.profile = {
'dbname': 'redshift',
'user': 'root',
'host': 'database',
Expand All @@ -30,69 +30,83 @@ def test_implicit_database_conn(self):
'schema': 'public'
}

creds = RedshiftAdapter.get_credentials(implicit_database_profile)
self.assertEquals(creds, implicit_database_profile)
def test_implicit_database_conn(self):
creds = RedshiftAdapter.get_credentials(self.profile)
self.assertEquals(creds, self.profile)

def test_explicit_database_conn(self):
explicit_database_profile = {
'method': 'database',
'dbname': 'redshift',
'user': 'root',
'host': 'database',
'pass': 'password',
'port': 5439,
'schema': 'public'
}
self.profile['method'] = 'database'

creds = RedshiftAdapter.get_credentials(explicit_database_profile)
self.assertEquals(creds, explicit_database_profile)
creds = RedshiftAdapter.get_credentials(self.profile)
self.assertEquals(creds, self.profile)

def test_explicit_iam_conn(self):
explicit_iam_profile = {
self.profile.update({
'method': 'iam',
'cluster_id': 'my_redshift',
'iam_duration_s': 1200,
'dbname': 'redshift',
'user': 'root',
'host': 'database',
'port': 5439,
'schema': 'public',
}
})

with mock.patch.object(RedshiftAdapter, 'fetch_cluster_credentials', new=fetch_cluster_credentials):
creds = RedshiftAdapter.get_credentials(explicit_iam_profile)
creds = RedshiftAdapter.get_credentials(self.profile)

expected_creds = dbt.utils.merge(explicit_iam_profile, {'pass': 'tmp_password'})
expected_creds = dbt.utils.merge(self.profile, {'pass': 'tmp_password'})
self.assertEquals(creds, expected_creds)

def test_invalid_auth_method(self):
invalid_profile = {
'method': 'badmethod',
'dbname': 'redshift',
'user': 'root',
'host': 'database',
'pass': 'password',
'port': 5439,
'schema': 'public'
}
self.profile['method'] = 'badmethod'

with self.assertRaises(dbt.exceptions.FailedToConnectException) as context:
with mock.patch.object(RedshiftAdapter, 'fetch_cluster_credentials', new=fetch_cluster_credentials):
RedshiftAdapter.get_credentials(invalid_profile)
RedshiftAdapter.get_credentials(self.profile)

self.assertTrue('badmethod' in context.exception.msg)

def test_invalid_iam_no_cluster_id(self):
invalid_profile = {
'method': 'iam',
'dbname': 'redshift',
'user': 'root',
'host': 'database',
'port': 5439,
'schema': 'public'
}
self.profile['method'] = 'iam'
with self.assertRaises(dbt.exceptions.FailedToConnectException) as context:
with mock.patch.object(RedshiftAdapter, 'fetch_cluster_credentials', new=fetch_cluster_credentials):
RedshiftAdapter.get_credentials(invalid_profile)
RedshiftAdapter.get_credentials(self.profile)

self.assertTrue("'cluster_id' must be provided" in context.exception.msg)


@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_default_keepalive(self, psycopg2):
connection = RedshiftAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='redshift',
user='root',
host='database',
password='password',
port=5439,
connect_timeout=10,
keepalives_idle=RedshiftAdapter.DEFAULT_TCP_KEEPALIVE)

@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_changed_keepalive(self, psycopg2):
self.profile['keepalives_idle'] = 256
connection = RedshiftAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='redshift',
user='root',
host='database',
password='password',
port=5439,
connect_timeout=10,
keepalives_idle=256)

@mock.patch('dbt.adapters.postgres.impl.psycopg2')
def test_set_zero_keepalive(self, psycopg2):
self.profile['keepalives_idle'] = 0
connection = RedshiftAdapter.acquire_connection(self.profile, 'dummy')

psycopg2.connect.assert_called_once_with(
dbname='redshift',
user='root',
host='database',
password='password',
port=5439,
connect_timeout=10)