-
Notifications
You must be signed in to change notification settings - Fork 7
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
Fix issues caused by differences between redis and elasticache (PP-1693) #2045
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
83484e3
Fix issues caused by differences between redis and elasticache
jonathangreen e155c49
Roll back debugging change
jonathangreen 5ce5211
Fix mypy issue
jonathangreen f11d7d0
Better escaping for both
jonathangreen d4096e8
Code review feedback
jonathangreen 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from functools import cached_property | ||
|
||
from palace.manager.core.exceptions import PalaceValueError | ||
|
||
|
||
class JsonPathEscapeMixin: | ||
r""" | ||
Mixin to provide methods for escaping and unescaping JsonPaths for use in Redis / ElastiCache. | ||
|
||
This is necessary because some characters in object keys are not handled well by AWS ElastiCache, | ||
and other characters seem problematic in Redis. | ||
|
||
This mixin provides methods to escape and unescape these characters, so that they can be used in | ||
object keys, and the keys can be queried via JSONPath without issue. | ||
|
||
In ElastiCache when ~ is used in a key, the key is never updated, despite returning a success. And | ||
when a / is used in a key, the key is interpreted as a nested path, nesting a new key for every | ||
slash in the path. This is not the behavior we want, so we need to escape these characters. | ||
|
||
In Redis, the \ character is used as an escape character, and the " character is used to denote | ||
the end of a string for the JSONPath. This means that these characters need to be escaped as well. | ||
|
||
Characters are escaped by prefixing them with a backtick character, followed by a single character | ||
from _MAPPING that represents the escaped character. The backtick character itself is escaped by | ||
prefixing it with another backtick character. | ||
""" | ||
|
||
_ESCAPE_CHAR = "`" | ||
|
||
_MAPPING = { | ||
"/": "s", | ||
"\\": "b", | ||
'"': "'", | ||
"~": "t", | ||
} | ||
|
||
@cached_property | ||
def _FORWARD_MAPPING(self) -> dict[str, str]: | ||
mapping = {k: "".join((self._ESCAPE_CHAR, v)) for k, v in self._MAPPING.items()} | ||
mapping[self._ESCAPE_CHAR] = "".join((self._ESCAPE_CHAR, self._ESCAPE_CHAR)) | ||
return mapping | ||
|
||
@cached_property | ||
def _REVERSE_MAPPING(self) -> dict[str, str]: | ||
mapping = {v: k for k, v in self._MAPPING.items()} | ||
mapping[self._ESCAPE_CHAR] = self._ESCAPE_CHAR | ||
return mapping | ||
|
||
def _escape_path(self, path: str, elasticache: bool = False) -> str: | ||
escaped = "".join([self._FORWARD_MAPPING.get(c, c) for c in path]) | ||
if elasticache: | ||
# As well as the simple escaping we have defined here, for ElastiCache we need to fully | ||
# escape the path as if it were a JSON string. So we call json.dumps to do this. We | ||
# strip the leading and trailing quotes from the result, as we only want the escaped | ||
# string, not the quotes. | ||
escaped = json.dumps(escaped)[1:-1] | ||
return escaped | ||
|
||
def _unescape_path(self, path: str) -> str: | ||
in_escape = False | ||
unescaped = [] | ||
for char in path: | ||
if in_escape: | ||
if char not in self._REVERSE_MAPPING: | ||
raise PalaceValueError( | ||
f"Invalid escape sequence '{self._ESCAPE_CHAR}{char}'" | ||
) | ||
unescaped.append(self._REVERSE_MAPPING[char]) | ||
in_escape = False | ||
elif char == self._ESCAPE_CHAR: | ||
in_escape = True | ||
else: | ||
unescaped.append(char) | ||
|
||
if in_escape: | ||
raise PalaceValueError("Unterminated escape sequence.") | ||
|
||
return "".join(unescaped) |
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
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,3 +1,5 @@ | ||
import string | ||
|
||
import pytest | ||
|
||
from palace.manager.service.redis.models.marc import ( | ||
|
@@ -21,9 +23,10 @@ def __init__(self, redis_fixture: RedisFixture): | |
self._redis_fixture.client, self.mock_collection_id | ||
) | ||
|
||
self.mock_upload_key_1 = "test1" | ||
self.mock_upload_key_2 = "test2" | ||
self.mock_upload_key_3 = "test3" | ||
# Some keys with special characters to make sure they are handled correctly. | ||
self.mock_upload_key_1 = "test/test1/?$xyz.abc" | ||
self.mock_upload_key_2 = "t'est💣/tëst2.\"ext`" | ||
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. Love the 💣 here! 🤣 |
||
self.mock_upload_key_3 = string.printable | ||
|
||
self.mock_unset_upload_key = "test4" | ||
|
||
|
@@ -49,7 +52,7 @@ def load_test_data(self) -> dict[str, int]: | |
|
||
return return_value | ||
|
||
def test_data_records(self, *keys: str): | ||
def test_data_records(self, *keys: str) -> dict[str, MarcFileUpload]: | ||
return {key: MarcFileUpload(buffer=self.test_data[key]) for key in keys} | ||
|
||
|
||
|
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.
The changes in
src/palace/manager/celery/tasks/marc.py
could come in as a separate PR if desired. They fix a concurrency issue I found while doing other testing. Occasionally, since we were re-queuing while holding the lock, another worker would pick up the new task before this one had released the lock. So this updates the order of operations, so we release the lock before callingtask.replace
.