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

WIP feat(redis): redis client and caching logic #264

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
17 changes: 17 additions & 0 deletions indexd/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import flask
import redis
from urllib.parse import urlparse
from .bulk.blueprint import blueprint as indexd_bulk_blueprint
from .index.blueprint import blueprint as indexd_index_blueprint
from .alias.blueprint import blueprint as indexd_alias_blueprint
Expand Down Expand Up @@ -48,5 +50,20 @@ def get_app(settings=None):
pass

app_init(app, settings)
_setup_redis_client(app)

return app


def _setup_redis_client(app):
"""
Sets up the redis client based on config
"""
redis_url_parts = urlparse(app.config["REDIS_HOST"])
ssl = redis_url_parts.scheme == "https"
app.redis_client = redis.Redis(
host=redis_url_parts.netloc,
port=app.config["REDIS_PORT"],
db=app.config["REDIS_DB"],
ssl=ssl,
)
4 changes: 4 additions & 0 deletions indexd/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@

AUTH = SQLAlchemyAuthDriver("sqlite:///auth.sq3")

CONFIG["REDIS_HOST"] = "http://redis-service"
CONFIG["REDIS_PORT"] = "6379"
CONFIG["REDIS_DB"] = 0

settings = {"config": CONFIG, "auth": AUTH}
48 changes: 46 additions & 2 deletions indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,19 @@ def get_index_record(record):
Returns a record.
"""

ret = blueprint.index_driver.get(record)
# check redis
json_record = flask.current_app.redis_client.get(record)
if json_record:
json_record = json.loads(json_record)
else:
# get from db
Comment on lines +223 to +228
Copy link
Contributor

Choose a reason for hiding this comment

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

in fence i do: check redis; if not in redis: call indexd so i think we could skip checking redis again here. 3 cases:

  • request to indexd from another service: we can assume the other service already checked the cache
  • request from a user: now that the request reached indexd, it's too late to improve performance
    • the record is in redis: getting from redis instead of the DB doesn't improve performance (i think)
    • it's not in redis: performance is decreased since indexd checks both

json_record = blueprint.index_driver.get(record)

return flask.jsonify(ret), 200
# update redis
logger.info(f"setting redis record {record}")
flask.current_app.redis_client.set(record, json.dumps(json_record))

return flask.jsonify(json_record), 200


@blueprint.route("/index/", methods=["POST"])
Expand Down Expand Up @@ -311,6 +321,10 @@ def put_index_blank_record(record):
)
ret = {"did": did, "rev": rev, "baseid": baseid}

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand All @@ -330,6 +344,10 @@ def put_index_record(record):

ret = {"did": did, "baseid": baseid, "rev": rev}

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand All @@ -345,6 +363,10 @@ def delete_index_record(record):
# authorize done in delete
blueprint.index_driver.delete(record, rev)

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(record)

return "", 200


Expand Down Expand Up @@ -388,6 +410,10 @@ def add_index_record_version(record):

ret = {"did": did, "baseid": baseid, "rev": rev}

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(did)

return flask.jsonify(ret), 200


Expand Down Expand Up @@ -425,6 +451,11 @@ def append_aliases(record):

aliases = blueprint.index_driver.get_aliases_for_did(record)
aliases_payload = {"aliases": [{"value": alias} for alias in aliases]}

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(record)

return flask.jsonify(aliases_payload), 200


Expand All @@ -448,6 +479,11 @@ def replace_aliases(record):
blueprint.index_driver.replace_aliases_for_did(aliases, record)

aliases_payload = {"aliases": [{"value": alias} for alias in aliases]}

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(record)

return flask.jsonify(aliases_payload), 200


Expand All @@ -456,6 +492,10 @@ def delete_all_aliases(record):
# authorization and error handling done in driver
blueprint.index_driver.delete_all_aliases_for_did(record)

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(record)

return flask.jsonify("Aliases deleted successfully"), 200


Expand All @@ -464,6 +504,10 @@ def delete_one_alias(record, alias):
# authorization and error handling done in driver
blueprint.index_driver.delete_one_alias_for_did(alias, record)

# invalidate redis for this did
logger.info(f"removing redis record {record}")
flask.current_app.redis_client.delete(record)

return flask.jsonify("Aliases deleted successfully"), 200


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ git+https://github.com/uc-cdis/[email protected]#egg=dosclient
git+https://github.com/uc-cdis/[email protected]#egg=hsclient
authutils==4.0.0
gen3rbac==0.1.2
redis==3.4.1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"hsclient",
"authutils",
"gen3rbac",
"redis~=3.4.1",
],
dependency_links=[
"git+https://github.com/uc-cdis/[email protected]#egg=cdislogging",
Expand Down
4 changes: 4 additions & 0 deletions tests/default_test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

os.environ["PRESIGNED_FENCE_URL"] = "https://fictitious-commons.io/"
os.environ["HOSTNAME"] = "fictitious-commons.io"
CONFIG["REDIS_HOST"] = "http://redis-service"
CONFIG["REDIS_PORT"] = "6379"
CONFIG["REDIS_DB"] = 0

settings = {"config": CONFIG, "auth": AUTH}

settings["config"]["TEST_DB"] = "postgres://postgres@localhost/test_migration_db"