-
Notifications
You must be signed in to change notification settings - Fork 20
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
(PXP-10739): Add bucket region to DRS #355
Draft
BinamB
wants to merge
19
commits into
master
Choose a base branch
from
feat/pull_bucket_info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
80ea38d
add cache stuff
BinamB df26c62
fix caching
BinamB 6798e91
add app context
BinamB 1cf555f
add cache to class
BinamB 60430cf
create cache file
BinamB 2fa39ef
remove app context thing
BinamB bbbd046
fix logger
BinamB 76321ec
fix if
BinamB b5ab119
fix update urls_metadata
BinamB 07d1a71
just adding things
BinamB 85f5bf5
Merge remote-tracking branch 'origin/master' into feat/pull_bucket_info
BinamB ef10749
Add unit tests
BinamB 5c52fc3
Merge branch 'master' into feat/pull_bucket_info
BinamB 8ba6b33
Merge branch 'master' into feat/pull_bucket_info
BinamB d1ed383
Merge branch 'master' into feat/pull_bucket_info
BinamB 2de3b87
Testing things out
BinamB fbf25d9
logs
BinamB 1e9dd25
remove caching
BinamB bad76d7
remove session
BinamB 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 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from distutils.command.config import config | ||
from functools import cache | ||
from flask_caching import Cache | ||
|
||
cache = Cache(config={"CACHE_TYPE": "simple"}) |
This file contains 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 |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import os | ||
import time | ||
|
||
import datetime | ||
from urllib import response | ||
import uuid | ||
import json | ||
from contextlib import contextmanager | ||
from cdislogging import get_logger | ||
import requests | ||
from sqlalchemy import ( | ||
BigInteger, | ||
Column, | ||
|
@@ -22,7 +27,9 @@ | |
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import joinedload, relationship, sessionmaker | ||
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound | ||
import urllib.parse | ||
|
||
from indexd.cache import cache | ||
from indexd import auth | ||
from indexd.errors import UserError, AuthError | ||
from indexd.index.driver import IndexDriverABC | ||
|
@@ -108,6 +115,72 @@ class IndexRecord(Base): | |
"IndexRecordAlias", backref="index_record", cascade="all, delete-orphan" | ||
) | ||
|
||
def url_to_bucket_region_mapping(self, url): | ||
""" | ||
Map the url location of a bucket to its region | ||
|
||
Args: | ||
url(str): The url of the object location in the bucket | ||
|
||
Returns: | ||
region(str): The region of the bucket where the object is located | ||
""" | ||
|
||
storage_to_config_map = {"s3": "S3_BUCKETS", "gs": "GS_BUCKETS"} | ||
parsed_url = urllib.parse.urlparse(url) | ||
cloud_storage_service = parsed_url.scheme | ||
bucket_name = parsed_url.netloc | ||
|
||
bucket_region_info = cache.get("bucket_region_info") | ||
|
||
# if cache not found then try to retrieve info from fence and cache it | ||
if bucket_region_info is None: | ||
hostname = os.environ["HOSTNAME"] | ||
fence_url = "http://" + hostname + "/user/bucket_info/region" | ||
retry_count = 0 | ||
while retry_count < 3: | ||
response = requests.get(fence_url) | ||
if response.status_code == 200: | ||
if response.json() != None: | ||
# set cache for an hour | ||
bucket_region_info = response.json() | ||
cache.set("bucket_region_info", response.json(), timeout=3600) | ||
else: | ||
print( | ||
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. Use the logger for this. |
||
"/bucket_info/region from fence returned 200 but no data found" | ||
) | ||
break | ||
else: | ||
print( | ||
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. Use the logger. |
||
"/bucket_info/region from fence returned status {} with {}".format( | ||
response.status_code(), response.json() | ||
) | ||
) | ||
time.sleep(2**3) | ||
retry_count += 1 | ||
return None | ||
|
||
# if bucket_region_info is still empty that means that there's no bucket configured in the fence config | ||
if bucket_region_info is None: | ||
# checks cloud provider -> cloud bucket | ||
if ( | ||
bucket_name | ||
in bucket_region_info[storage_to_config_map[cloud_storage_service]] | ||
): | ||
return bucket_region_info[storage_to_config_map[cloud_storage_service]][ | ||
bucket_name | ||
] | ||
else: | ||
print( | ||
"Bucket not configured in fence config for {}".format( | ||
cloud_storage_service + "://" + bucket_name | ||
) | ||
) | ||
return None | ||
else: | ||
print("No buckets not configured in fence config") | ||
return None | ||
|
||
def to_document_dict(self): | ||
""" | ||
Get the full index document | ||
|
@@ -118,9 +191,14 @@ def to_document_dict(self): | |
hashes = {h.hash_type: h.hash_value for h in self.hashes} | ||
metadata = {m.key: m.value for m in self.index_metadata} | ||
|
||
# Call fence /bucket_info/region endpoint to fill some of the urls metadata | ||
urls_metadata = { | ||
u.url: {m.key: m.value for m in u.url_metadata} for u in self.urls | ||
} | ||
|
||
for u in urls: | ||
urls_metadata[u]["region"] = self.url_to_bucket_region_mapping(u) | ||
|
||
created_date = self.created_date.isoformat() | ||
updated_date = self.updated_date.isoformat() | ||
content_created_date = ( | ||
|
Oops, something went wrong.
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.
Do you still need this config here?