-
Notifications
You must be signed in to change notification settings - Fork 17.6k
[AIRFLOW-6833] HA for webhdfs connection #7454
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a2fc038
[AIRFLOW-6833] Add connection valid function
GuzikJakub 0b06f5f
[AIRFLOW-6833] Lost import
GuzikJakub 60d8e12
[AIRFLOW-6833] Improvement after test
GuzikJakub e05b322
[AIRFLOW-6833] Improvement after test
GuzikJakub f0fa995
[AIRFLOW-6833] Improvement after test
GuzikJakub 6650fa5
[AIRFLOW-6833] Improvement after test
GuzikJakub 9385c8a
[AIRFLOW-6833] Improvement after test
GuzikJakub 8bd4ce3
[AIRFLOW-6833] Improvement after test
GuzikJakub f899ca1
[AIRFLOW-6833] Improvement after test
GuzikJakub 6408b5a
[AIRFLOW-6833] Improvement after test
GuzikJakub 9e1d682
[AIRFLOW-6833] Improvement after test
GuzikJakub 91e2acc
[AIRFLOW-6833] Improvement after test
GuzikJakub 3b7d8bb
[AIRFLOW-6833] Fixes for closing the socket
GuzikJakub 2d35f86
[AIRFLOW-6833] Fixes for closing the socket
GuzikJakub 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| # under the License. | ||
| """Hook for Web HDFS""" | ||
| import logging | ||
| import socket | ||
|
|
||
| from hdfs import HdfsError, InsecureClient | ||
|
|
||
|
|
@@ -56,27 +57,35 @@ def __init__(self, webhdfs_conn_id='webhdfs_default', proxy_user=None): | |
| def get_conn(self): | ||
| """ | ||
| Establishes a connection depending on the security mode set via config or environment variable. | ||
|
|
||
| :return: a hdfscli InsecureClient or KerberosClient object. | ||
| :rtype: hdfs.InsecureClient or hdfs.ext.kerberos.KerberosClient | ||
| """ | ||
| connections = self.get_connections(self.webhdfs_conn_id) | ||
| connection = self._find_valid_server() | ||
| if connection is None: | ||
| raise AirflowWebHDFSHookException("Failed to locate the valid server.") | ||
| return connection | ||
|
|
||
| def _find_valid_server(self): | ||
| connections = self.get_connections(self.webhdfs_conn_id) | ||
| for connection in connections: | ||
| host_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| self.log.info("Trying to connect to %s:%s", connection.host, connection.port) | ||
| try: | ||
| self.log.debug('Trying namenode %s', connection.host) | ||
| client = self._get_client(connection) | ||
| client.status('/') | ||
| self.log.debug('Using namenode %s for hook', connection.host) | ||
| return client | ||
| conn_check = host_socket.connect_ex((connection.host, connection.port)) | ||
| if conn_check == 0: | ||
| self.log.info('Trying namenode %s', connection.host) | ||
| client = self._get_client(connection) | ||
| client.status('/') | ||
| self.log.info('Using namenode %s for hook', connection.host) | ||
| host_socket.close() | ||
| return client | ||
| else: | ||
| self.log.info("Could not connect to %s:%s", connection.host, connection.port) | ||
| host_socket.close() | ||
| except HdfsError as hdfs_error: | ||
| self.log.debug('Read operation on namenode %s failed with error: %s', | ||
| connection.host, hdfs_error) | ||
|
|
||
| hosts = [connection.host for connection in connections] | ||
| error_message = 'Read operations failed on the namenodes below:\n{hosts}'.format( | ||
| hosts='\n'.join(hosts)) | ||
| raise AirflowWebHDFSHookException(error_message) | ||
| self.log.info('Read operation on namenode %s failed with error: %s', | ||
| connection.host, hdfs_error) | ||
| return None | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are failing to close the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ashb The change has been made. |
||
|
|
||
| def _get_client(self, connection): | ||
| connection_str = 'http://{host}:{port}'.format(host=connection.host, port=connection.port) | ||
|
|
||
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
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.
Wait, if there is an HDFS error, doesn't the host_socket need to closed? @GuzikJakub
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.
We don't need the host_socket closure. The connection is only open in the try block.
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.
Got it