Skip to content

Support Personal access token method for Tableau Connection - #34927

Closed
baruch-shteken wants to merge 4 commits into
apache:mainfrom
baruch-shteken:support_personal_access_token_method_for_tableau_connection
Closed

baruch-shteken wants to merge 4 commits into
apache:mainfrom
baruch-shteken:support_personal_access_token_method_for_tableau_connection

Conversation

@baruch-shteken

Copy link
Copy Markdown

closes: #25904

I read the Tableau Python SDK docs and I didn't see any meaningful feature that can help solving this problem.
I decided that what I can do changing the connection setting in the Tableau hook.
Instead of generating the connection 1 time when the hook is initialized (enter function), I would like the tableau connection to be set before any API request to Tableau. This should solve the problem because the Tableau session_token will be newly generated before every call.


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@boring-cyborg

boring-cyborg Bot commented Oct 13, 2023

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

@baruch-shteken baruch-shteken changed the title Tableau_conn refreshes for every api call to support PAT Support Personal access token method for Tableau Connection Oct 13, 2023
@Michae94

Copy link
Copy Markdown

Hey @baruch-shteken , thanks for posting a PR for this issue.
I was wondering how this new setup will work with multiple concurrent tasks using the same PAT.

From my understanding, if a job try to authenticate using the same PAT as a concurrent job, the former job will fail.

Am I missing something ?

@baruch-shteken

Copy link
Copy Markdown
Author

Thanks @Michae94 for your question.
This is my understanding of how PATs work:
When a PAT is used for an authentication, it creates a new session_token which is then used in subsequent REST API calls. When the same PAT is used to authenticate again, it creates a new session_token and invalidates the previous session_token. (Tableau docs)
This is my understanding of how Airflow works:
For the first call to Tableau in a specific task, we use the PAT to authenticate to tableau. This generates a session_token. We can't reuse the session_token that was already created in a different task. (I also checked the library Airflow uses to send Tableau requests and didn't find anything useful we can use).
Example of the problem:
Let's take an example of a DAG that has 2 parallel tasks that refresh 2 Tableau data sources. One of these tasks will generate and use a session_token to refresh the data source. The other task will do the same and also invalidate the first task session_token. When the first task polls Tableau to check if the refresh finished, it will fail because the session_token changed.

From the above, I drew the conclusion that if we authenticate every-time to Tableau for every call, the process should work. That being said, I didn't test it end2end. Should I test it with my private Tableau data sources? Can I create a test for it here?

@Michae94

Michae94 commented Oct 31, 2023 •

Copy link
Copy Markdown

Hey @baruch-shteken , thanks for the answer.
I agree with everything you mentioned above, except the conclusion.

Let's say we have 2 concurrent tasks, completely isolated (not run on the same machine). Both of them will use the same PAT to authenticate.
Example of the sequence:

  • First job authenticate, and start to access Tableau using the session_token retrieved.
  • While the first job is still running, we have our 2nd job that now starts to authenticate and retrieve a new session_token while first job is not finished
  • From my understanding: the first job will interrupt and fail because the session_token used for the job is not viable anymore.

Maybe there is something I missed 🤔
As you said, I recommend testing your implementation with 1 PAT to see if it's working (pull your version of the apache package instead of master on a testing environment, try to run 2 jobs and see what's happening).

@baruch-shteken

baruch-shteken commented Oct 31, 2023 •

Copy link
Copy Markdown
Author

@Michae94
If the two jobs run in a different machine then I think they should use different PATs. I would say that for each app, there should be a dedicated PAT. So Airflow should have its own dedicated PAT.
I will test my code with some actual Tableau data sources this week.

@Michae94

Copy link
Copy Markdown

Ok now I understand better your proposition.
Potentially, it means that this improvement will not be usable for Airflow kubernetes workers, as each task is performed on a different pod (in this context, ~machine).
Thanks for explaning it 🙏

@baruch-shteken

Copy link
Copy Markdown
Author

I don't know how Airflow kubernetes workers work but if they all use the code I propose here and even with the same PAT, it should still be ok. Because each task will authenticate again before sending a request to Tableau.

@eladkal

eladkal commented Nov 7, 2023 •

Copy link
Copy Markdown
Contributor

Example of the problem:
Let's take an example of a DAG that has 2 parallel tasks that refresh 2 Tableau data sources. One of these tasks will generate and use a session_token to refresh the data source. The other task will do the same and also invalidate the first task session_token. When the first task polls Tableau to check if the refresh finished, it will fail because the session_token changed

If this is true then it will never really work regardless of the underlying archetacture (Celery, Kubernetes, etc..)

The question that needs to be answered is how Tableau recommands to do authentication from two seperated proccesses that tries to refresh two different dashboards at the same time.

@baruch-shteken

baruch-shteken commented Nov 24, 2023 •

Copy link
Copy Markdown
Author

I tested my changes locally (I didn't manage to pull my changes from GitHub. However, we use an Airflow docker so I changed the file airflow/providers/tableau/hooks/tableau.py in the docker to my new code).
Tl;dr the code change did solve some of the problem but not all of it.
Explanation: The setup is 6 data source refresh tasks in parallel. Each task triggers the data source refresh and every 3 minutes sends a request to Tableau to check if the refresh finished. When I triggered the DAG, all tasks except 1 failed. I remembered that there is a problem of concurrency because if all tasks try to get a new session token at the same time, only 1 will succeed. So I implemented a 2 seconds delay between the tasks in the DAG file (1 task starts immediately, 1 task starts after 2 seconds, 1 task starts after 4 seconds etc...). After this change, all the tasks finished successfully.
This means that as long as only 1 task requests a session token at any moment, the process will work.

@Michae94 Is the concurrency problem what you meant before in Kubernetes? Do you have any recommendation for me on how to improve the process? Can I configure somewhere in the Airflow code that only 1 Tableau hook object can be active at any moment?
@eladkal I can ask Tableau their recommendation about authenticating from two separated processes that try to refresh two different dashboards at the same time but they need to use the same PAT.

@eladkal

eladkal commented Nov 25, 2023

Copy link
Copy Markdown
Contributor

@Michae94 Is the concurrency problem what you meant before in Kubernetes? Do you have any recommendation for me on how to improve the process? Can I configure somewhere in the Airflow code that only 1 Tableau hook object can be active at any moment?

You can not. This can only be enforced from users side if they create pool for tableau and don't allow more than 1 tableau task to run at the same time.

@eladkal I can ask Tableau their recommendation about authenticating from two separated processes that try to refresh two different dashboards at the same time but they need to use the same PAT.

Sure.

From Airflow point of view - we only can enable capabilities of Tableau SDK so if the SDK is missing a feature that should be a feature request to Tableau.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Jan 10, 2024
@github-actions github-actions Bot closed this Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:tableau stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Personal access token method for Tableau Connection

3 participants