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

Added configurable prefixes for Redis Tracker and Lock stores #6500

Merged
merged 36 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5e73237
Added configurable prefixes for Redis Tracker and Lock stores
bjbredis Aug 27, 2020
81ec8ae
Testing fixes
bjbredis Aug 27, 2020
961b449
Docs and test fix
bjbredis Aug 27, 2020
da21e2e
Docs
bjbredis Aug 27, 2020
c532922
Formatting
bjbredis Aug 27, 2020
959edb8
Changelog added.
bjbredis Aug 27, 2020
f0f6970
Cleanup validation logic.
bjbredis Aug 27, 2020
c1c3eb8
Formatting
bjbredis Aug 27, 2020
397dc1c
Post-linting changes.
bjbredis Aug 28, 2020
035c669
Fixed a test.
bjbredis Aug 28, 2020
d311713
Merge branch 'master' into add-prefix-redis-lockstore-trackerstore
bjbredis Aug 28, 2020
a7722c2
Update changelog/6498.improvement.md
bjbredis Sep 8, 2020
570b55a
Update docs/docs/lock-stores.mdx
bjbredis Sep 8, 2020
44dc016
Update data/test_endpoints/example_endpoints.yml
bjbredis Sep 8, 2020
80937c0
Added configurable prefixes for Redis Tracker and Lock stores
bjbredis Aug 27, 2020
0a07e11
Update docs/docs/lock-stores.mdx
bjbredis Oct 7, 2020
36d31f8
Fix wording in improvements
bjbredis Oct 7, 2020
56a3657
.
bjbredis Oct 7, 2020
c76ee9f
fixing docs
bjbredis Oct 7, 2020
83ed792
Update docs/docs/tracker-stores.mdx
bjbredis Oct 7, 2020
9fd4dad
Update docs/docs/tracker-stores.mdx
bjbredis Oct 7, 2020
322af40
Update docs/docs/tracker-stores.mdx
bjbredis Oct 7, 2020
dd63ec5
update docs/docs/tracker-stores.mdx
bjbredis Oct 7, 2020
703cfc4
Update rasa/core/tracker_store.py
bjbredis Oct 7, 2020
b4f60f3
Update rasa/core/tracker_store.py
bjbredis Oct 7, 2020
efac04b
Update rasa/core/lock_store.py
bjbredis Oct 7, 2020
93c3fdc
Update rasa/core/lock_store.py
bjbredis Oct 7, 2020
f7942d7
fixed tests
bjbredis Oct 29, 2020
19645bc
merge fixes
bjbredis Nov 2, 2020
0329fb8
more merge fixes
bjbredis Nov 3, 2020
4ff7345
Merge branch 'master' into add-prefix-redis-lockstore-trackerstore
bjbredis Nov 3, 2020
b37f130
lint fixes
bjbredis Nov 3, 2020
579e662
Merge branch 'master' into add-prefix-redis-lockstore-trackerstore
bjbredis Nov 3, 2020
98da0cf
flake8 linting
bjbredis Nov 3, 2020
1ee88d2
Merge branch 'add-prefix-redis-lockstore-trackerstore' of https://git…
bjbredis Nov 3, 2020
7377dc6
formatting changes from Black
bjbredis Nov 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/6498.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Adding configurable prefixes to Redis Tracker and Lock stores so that a single Redis instance (and logical DB) can support multiple conversation trackers and lock.
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
All conversations will be prefixed with `tracker:...` and all locks prefixed with `lock:...`. Additionally, you can add an alphanumeric-only `prefix: value` in `endpoints.yml` such that keys in redis will take the form `value:tracker:...` and `value:lock:...` respectively.
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions data/test_endpoints/example_endpoints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tracker_store:
port: 6379
db: 0
password: password
prefix: xyz
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
record_exp: 30000
# example of mongoDB external tracker store config
#tracker_store:
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/lock-stores.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ address the same node when sending messages for a given conversation ID.
port: <port of your redis instance, usually 6379>
password: <password used for authentication>
db: <number of your database within redis, e.g. 0>
prefix: <alphanumeric value to prepend to lock_store keys>
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
```

3. To start the Rasa Core server using your Redis backend, add the `--endpoints`
Expand All @@ -76,6 +77,9 @@ address the same node when sending messages for a given conversation ID.

* `db` (default: `1`): The number of your redis database

* `prefix` (default: `None`): The prefix to prepend to lock_store keys. Must
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
be alphanumeric

* `password` (default: `None`): Password used for authentication
(`None` equals no authentication)

Expand Down
31 changes: 18 additions & 13 deletions docs/docs/tracker-stores.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ USER 1001`}</code></pre>
2. Add required configuration to your `endpoints.yml`:

```yaml-rasa
tracker_store:
type: redis
url: <url of the redis instance, e.g. localhost>
port: <port of your redis instance, usually 6379>
db: <number of your database within redis, e.g. 0>
password: <password used for authentication>
use_ssl: <whether or not the communication is encrypted, default `false`>
tracker_store:
type: redis
url: <url of the redis instance, e.g. localhost>
port: <port of your redis instance, usually 6379>
db: <number of your database within redis, e.g. 0>
prefix: <alphanumeric value to prepend to tracker_store keys>
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
password: <password used for authentication>
use_ssl: <whether or not the communication is encrypted, default `false`>
```

3. To start the Rasa server using your configured Redis instance,
Expand All @@ -219,12 +220,13 @@ USER 1001`}</code></pre>

```yaml-rasa {3}
tracker_store:
type: redis
url: <url of the redis instance, e.g. localhost>
port: <port of your redis instance, usually 6379>
db: <number of your database within redis, e.g. 0>
password: <password used for authentication>
use_ssl: <whether or not the communication is encrypted, default `false`>
type: redis
url: <url of the redis instance, e.g. localhost>
port: <port of your redis instance, usually 6379>
db: <number of your database within redis, e.g. 0>
prefix: <alphanumeric value to prepend to tracker_store keys>
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
password: <password used for authentication>
use_ssl: <whether or not the communication is encrypted, default `false`>
```


Expand All @@ -237,6 +239,9 @@ USER 1001`}</code></pre>

* `db` (default: `0`): The number of your redis database

* `prefix` (default: `None`): The prefix to prepend to lock_store keys. Must
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
be alphanumeric

* `password` (default: `None`): Password used for authentication
(`None` equals no authentication)

Expand Down
22 changes: 19 additions & 3 deletions rasa/core/lock_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def __init__(
db: int = 1,
password: Optional[Text] = None,
use_ssl: bool = False,
prefix: Optional[Text] = None,
socket_timeout: float = DEFAULT_SOCKET_TIMEOUT_IN_SECONDS,
) -> None:
"""Create a lock store which uses Redis for persistence.
Expand All @@ -210,6 +211,8 @@ def __init__(
password: The password which should be used for authentication with the
Redis database.
use_ssl: `True` if SSL should be used for the connection to Redis.
prefix: prefix to prepend to all keys used by the lockstore. Must be
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
alphanumeric.
socket_timeout: Timeout in seconds after which an exception will be raised
in case Redis doesn't respond within `socket_timeout` seconds.
"""
Expand All @@ -223,19 +226,32 @@ def __init__(
ssl=use_ssl,
socket_timeout=socket_timeout,
)

if (prefix is None) or (prefix == ""):
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
self.prefix = "lock:"
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
elif (prefix is not None) and (
(isinstance(prefix, str)) and (prefix.isalnum())
):
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
self.prefix = prefix + ":lock:"
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
else:
self.prefix = "lock:"
logger.warning(
f"Omitting provided non-alphanumeric key prefix: '{prefix}'."
)

super().__init__()

def get_lock(self, conversation_id: Text) -> Optional[TicketLock]:
serialised_lock = self.red.get(conversation_id)
serialised_lock = self.red.get(self.prefix + conversation_id)
if serialised_lock:
return TicketLock.from_dict(json.loads(serialised_lock))

def delete_lock(self, conversation_id: Text) -> None:
deletion_successful = self.red.delete(conversation_id)
deletion_successful = self.red.delete(self.prefix + conversation_id)
self._log_deletion(conversation_id, deletion_successful)

def save_lock(self, lock: TicketLock) -> None:
self.red.set(lock.conversation_id, lock.dumps())
self.red.set(self.prefix + lock.conversation_id, lock.dumps())


class InMemoryLockStore(LockStore):
Expand Down
20 changes: 17 additions & 3 deletions rasa/core/tracker_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def __init__(
password: Optional[Text] = None,
event_broker: Optional[EventBroker] = None,
record_exp: Optional[float] = None,
prefix: Optional[Text] = None,
use_ssl: bool = False,
):
import redis
Expand All @@ -271,6 +272,19 @@ def __init__(
host=host, port=port, db=db, password=password, ssl=use_ssl
)
self.record_exp = record_exp

if (prefix is None) or (prefix == ""):
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
self.prefix = "tracker:"
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
elif (prefix is not None) and (
(isinstance(prefix, str)) and (prefix.isalnum())
):
bjbredis marked this conversation as resolved.
Show resolved Hide resolved
self.prefix = prefix + ":tracker:"
else:
self.prefix = "tracker:"
logger.warning(
f"Omitting provided non-alphanumeric key prefix: '{prefix}'."
)

super().__init__(domain, event_broker)

def save(self, tracker, timeout=None):
Expand All @@ -282,7 +296,7 @@ def save(self, tracker, timeout=None):
timeout = self.record_exp

serialised_tracker = self.serialise_tracker(tracker)
self.red.set(tracker.sender_id, serialised_tracker, ex=timeout)
self.red.set(self.prefix + tracker.sender_id, serialised_tracker, ex=timeout)

def retrieve(self, sender_id):
"""
Expand All @@ -292,15 +306,15 @@ def retrieve(self, sender_id):
Returns:
DialogueStateTracker
"""
stored = self.red.get(sender_id)
stored = self.red.get(self.prefix + sender_id)
if stored is not None:
return self.deserialise_tracker(sender_id, stored)
else:
return None

def keys(self) -> Iterable[Text]:
"""Returns keys of the Redis Tracker Store"""
return self.red.keys()
return self.red.keys(self.prefix + "*")


class DynamoTrackerStore(TrackerStore):
Expand Down
3 changes: 3 additions & 0 deletions tests/core/test_lock_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __init__(self):
# added in redis==3.3.0, but not yet in fakeredis
self.red.connection_pool.connection_class.health_check_interval = 0

# an optional configuration defined in endpoints.yml
self.prefix = "lock:"

super(RedisLockStore, self).__init__()


Expand Down
3 changes: 3 additions & 0 deletions tests/core/test_trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def __init__(self, _domain: Domain) -> None:
# added in redis==3.3.0, but not yet in fakeredis
self.red.connection_pool.connection_class.health_check_interval = 0

# Defined in RedisTrackerStore but needs to be added for the MockRedisTrackerStore
self.prefix = "tracker:"

TrackerStore.__init__(self, _domain)


Expand Down