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

[airflow][presto] Gracefully handle 503 errors and avoid eval() #985

Merged
merged 2 commits into from
Feb 16, 2016

Conversation

shahafabileah
Copy link
Contributor

TO: @mistercrunch

What? Why?

I was running an airflow job that executes a number of presto queries. The first bunch succeeded, and then the job suddenly died with this error:

    records = presto.get_records(my_hql)
  File "/usr/local/lib/python2.7/dist-packages/airflow/hooks/presto_hook.py", line 50, in get_records
    obj = eval(str(e))
  File "<string>", line 1
    Unexpected status code 503
                    ^
SyntaxError: invalid syntax

What happened here is that presto was temporarily unavailable (a "hiccup") so it returned a 503 error. Our error-handling code expected the response to be eval()'able, which is true in most cases but not in the 503 case. With 503's, the message is just "Unexpected status code 503". You can't eval() that.

We discussed this offline. We both agree that eval() is just a bad thing to do. You suggested using json.loads instead. I tried that and found that str(e) is not valid json. Instead, it's python-hash formatted. For example, it has u prefixes for all the strings.

So I chose to take a different approach. I simply check for the values I expect to find (a la duck-typing). If they exist, I extract them and use them in the PrestoError. If not, I just str() the whole error.

How was it tested?

I executed an invalid query ("blah blah select...") and verified that we handle that error. Here's what I saw:

PrestoException: SYNTAX_ERROR: line 1:1: no viable alternative at input 'blah'

For the other case, rather than taking down presto, I simulated this by manually setting the error object to the string "Unexpected status code 503". Here's what I saw:

PrestoException: Unexpected status code 503

I'm brand new to airflow development so let me know if there are other/better ways to verify my change.

@shahafabileah
Copy link
Contributor Author

Looks like I need to add a test to raise code coverage. I'll investigate...

@mistercrunch
Copy link
Member

No need to raise coverage, we can't test Presto in travis (yet).

But we like lines under 80char to comply to PEP8.

@shahafabileah
Copy link
Contributor Author

My bad -- I have my editor set to 100 chars. I'll revise...

@shahafabileah
Copy link
Contributor Author

@mistercrunch I revised the code to be under 80 chars per line. PTAL.

@aoen
Copy link
Contributor

aoen commented Feb 16, 2016

LGTM

shahafabileah added a commit that referenced this pull request Feb 16, 2016
[airflow][presto] Gracefully handle 503 errors and avoid eval()
@shahafabileah shahafabileah merged commit 5b7ca35 into master Feb 16, 2016
@shahafabileah shahafabileah deleted the shahaf-presto-error branch February 16, 2016 22:38
mobuchowski pushed a commit to mobuchowski/airflow that referenced this pull request Jan 4, 2022
…ache#985)

Signed-off-by: Renovate Bot <bot@renovateapp.com>

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Willy Lulciuc <willy@datakin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants