Commit Graph

7317 Commits

Author SHA1 Message Date
Chris Hill-Scott
d16d06fdef Cache serialised services in Redis
Same as we’re doing for templates.

This means avoiding a database call, even for services that don’t hit
our API so often.

They’ll still need to go to the database for the API keys, because we’re
not comfortable putting the API key secrets in Redis.

But once a service has got its keys from the database we commit the
transaction, so the connection can be freed up until we need it again to
insert the notification.
2020-06-24 08:52:12 +01:00
Chris Hill-Scott
6c0a4abd52 Serialise UUIDs to string
So that we can serialise them to JSON to store in Redis.
2020-06-24 08:51:54 +01:00
Chris Hill-Scott
6a9818b5fd Cache services and API keys in memory
Same as we’ve done for templates.

For high volume services this should mean avoiding calls to external
services, either the database or Redis.

TTL is set to 2 seconds, so that’s the maximum time it will take for
revoking an API key or renaming a service to propagate.

Some of the tests created services with the same service ID. This
caused intermittent failures because the cache relies on unique service
IDs (like we have in the real world) to key itself.
2020-06-24 08:46:13 +01:00
Chris Hill-Scott
46dbcb7e36 Commit transactions as soon no longer needed
We think that holding open database transactions while we go and do
something else is causing us to have poor performance.

Because we’re not serialising everything as soon as we pull it out of
the database we can guarantee that we don’t need to go back to the
database again.

So let’s see if explicitly closing the transaction helps with
performance.
2020-06-23 16:00:41 +01:00
Chris Hill-Scott
320bca70f7 Serialise service, API keys and permissions
By serialising these straight away we can:
- not go back to the database later, potentially closing the connection
  sooner
- potentially cache the serialised data, meaning we don’t touch the
  database at all
2020-06-23 16:00:41 +01:00
Chris Hill-Scott
d7b2cc6403 Merge pull request #2895 from alphagov/template-schema-cache
Ensure templates are cached with correct schema
2020-06-23 15:23:32 +01:00
Chris Hill-Scott
5ae3b0fc64 Ensure templates are cached with correct schema
For some reason our V1 get template response wraps the whole template in
a dictionary with one key, `'data'`:
0d99033889/app/template/rest.py (L166)

That means when the admin app caches the response it also caches it in
this format.

The API needs to do the same, otherwise it will be cacheing data with a
schema that the admin app isn’t expecting, and vice-versa.
2020-06-23 15:09:38 +01:00
Chris Hill-Scott
0d99033889 Merge pull request #2891 from alphagov/cache-serialised-template
Cache serialised template in Redis and in memory
2020-06-23 14:18:01 +01:00
Chris Hill-Scott
af1b021dbe Add test for when template is found in Redis
Ensures that we’re not calling the dao method when it is
2020-06-23 14:01:20 +01:00
Chris Hill-Scott
cb4b809131 Add extra assertion
To be crystal clear 💎
2020-06-23 13:48:55 +01:00
David McDonald
a2753eafa9 Merge pull request #2894 from alphagov/turn-on-stub
Turn on email stub for load testing
2020-06-23 12:17:09 +01:00
Pea M. Tyczynska
75a7e9b7fd Merge pull request #2892 from alphagov/refactor-crown-dependency-check
Refactor crown dependency check
2020-06-23 11:19:17 +01:00
David McDonald
48abc8ffb5 Turn on email stub for load testing 2020-06-23 11:09:24 +01:00
Chris Hill-Scott
996800f90d Cache serialised template in Redis 2020-06-23 10:03:27 +01:00
Chris Hill-Scott
2f4470edca Serialise UUID to string not UUID object
We need to serialise the template to JSON to store it in Redis. Python’s
built in JSON serialiser doesn’t know what to do with a UUID object, so
we need to manually cast it to a string instead.
2020-06-23 10:03:27 +01:00
Chris Hill-Scott
87afc439fe Cache serialised template in memory 2020-06-23 10:03:26 +01:00
Chris Hill-Scott
3e209bcaac Merge pull request #2893 from alphagov/exclude-created-by
Exclude created_by_id from TemplateSchemaNoDetail
2020-06-23 10:02:03 +01:00
Chris Hill-Scott
be86a16733 Exclude created_by_id from TemplateSchemaNoDetail
We were already excluding `created_by`, but in a different branch we
renamed the property on the schema to `created_by_id`:
https://github.com/alphagov/notifications-api/pull/2873/files#diff-691350bfe8029e08252edaad69e871b9R346-R348

We need to rename it in the exclude list to make sure that this
assertion still passes:
```
assert json_response['data'][0].keys() == {
    'folder',
    'id',
    'name',
    'template_type',
}
```
– 14e7b6b87e/tests/app/template/test_rest.py (L555-L560)
2020-06-23 09:39:11 +01:00
Chris Hill-Scott
14e7b6b87e Merge pull request #2880 from alphagov/serialise-less-stuff-templates
Exclude unneeded fields from all templates response
2020-06-23 09:07:30 +01:00
Pea Tyczynska
7a4f5a4bff Refactor crown dependency check 2020-06-22 18:14:22 +01:00
Chris Hill-Scott
14a17d675f Merge pull request #2873 from alphagov/serialise-template-immediately
Serialise template as soon as possible
2020-06-22 14:37:48 +01:00
Chris Hill-Scott
58a9862cd1 Avoid extra query when serialising Template created_by
It’s a UUID column, but by default Marshmallow wants to select the id
from the users table, not from the templates table, because the two
are foreign-keyed.

Adding the property explicity like this forces it to select from the
`created_by_id` column, but still serialises it to the `created_by`
field to avoid any breaking change.
2020-06-22 14:07:43 +01:00
Chris Hill-Scott
718f8ccad0 Remove content and subject from get all templates
Content and subject are user-submitted so are effectively unbounded in
size. And we’re serialising them for every template when sending the
list of templates to the admin app.

For the service with the most templates this results in a 1.3Mb blob of
JSON going over the wire, and being cached in Redis.

And then the admin app completely ignores these fields, because it does
show template content until you’ve clicked into a single template.

This commit adds a new query parameter, `detailed`, that the admin app
can set to `False`. When it does only the fields needed to render the
`/templates` page are returned.

This is done with a new parameter so as not to break the V1 API.
Although I looked in Kibana and it doesn’t seem like anyone external is
using this endpoint we’ve come this far without breaking the API so…
2020-06-22 13:08:41 +01:00
Chris Hill-Scott
8d61c1ef4b Add description of SerialisedModel 2020-06-22 10:20:54 +01:00
Chris Hill-Scott
5a2f2a9ec2 Rename JSONModel to SerialisedModel 2/2
This class doesn’t actually wrap JSON, it wraps serialised data.

So this name feels better.
2020-06-22 10:20:53 +01:00
Chris Hill-Scott
e6b7e0e16c Rename JSONModel to SerialisedModel 1/2
This class doesn’t actually wrap JSON, it wraps serialised data.

So this name feels better.

This commit only renames the file for an easier diff.
2020-06-22 10:20:53 +01:00
Chris Hill-Scott
608812d314 Don’t store the underlying dict
This will give us smaller objects to cache, and forces us to be explicit
about which properties we’re using.
2020-06-22 10:20:52 +01:00
Chris Hill-Scott
ad2328fc05 Serialise template immediately after fetching
This commit changes the code in post notification endpoint to handle a
serialised template (ie a `dict`) rather than a database object.

This is the first step towards being able to cache the template and not
hit the database on every request.

There should be no functional changes here, it’s just refactoring.

There are some changes to the tests where the signature of functions
has changed.

Importing of the template schema has to be done at a function level,
otherwise Marshmallow gets weird.

This commit also copies the `JSONModel` class from the admin app, which
turns serialised data (a dict made from JSON) into an object on which
certain predefined properties are allowed.

This means we can still do the caching of serialised data, without
having to change too much of the code in the app, or make it ugly by
sprinkling dict lookups everywhere.

We’re not copying all of JSONModel from the admin app, just the bits we
need. We don’t need to compare or hash these objects, they’re just used
for lookups. And redefining `__getattribute__` scares Leo.
2020-06-22 10:20:51 +01:00
Pea M. Tyczynska
dcc407efea Merge pull request #2878 from alphagov/uk-prefix
Make sure people without international sms permission can send to crown dependencies
2020-06-19 16:58:48 +01:00
Pea Tyczynska
ef9f3c1e5f Make sure we check if a service can send to number harmoniously
We were checking this separately in two places in the code. Now
we will have this logic in one place, in validators.

Also pull in utils version that recognises crown depenency numbers
as international.
2020-06-19 15:59:15 +01:00
Pea Tyczynska
b0bb0b9780 Make sure people without international sms permission can send to crown dependencies 2020-06-19 15:58:22 +01:00
Katie Smith
9e7b84bda2 Merge pull request #2864 from alphagov/postage-constraints-3
Drop postage constraints
2020-06-19 15:48:38 +01:00
Katie Smith
ab956c9eb3 Update JSON schema postage validation for new values 2020-06-19 15:36:10 +01:00
Katie Smith
6cd914b9d3 Update models to remove postage constraints and add new postage types 2020-06-19 15:36:10 +01:00
Katie Smith
bdde035a0c Drop postage constraints
The constaints on notifications and notification_history have already
been dropped in production, but still exist in staging and in dev
environments.

The constraints on templates and templates_history exist in all
environments.
2020-06-19 15:36:10 +01:00
Katie Smith
4b1705aa14 Merge pull request #2869 from alphagov/update-schemas
Update template schemas to validate postage
2020-06-19 13:15:59 +01:00
Katie Smith
98a69684c5 Update get_template_by_id_response & post_template_preview_response schemas
To check the format of postage. Neither of these two schemas are used
for validating - they seem to be added for reference.
2020-06-19 08:59:27 +01:00
Katie Smith
72be10c681 Add JSON schema for updating template
We did not have a JSON schema for updating a template. Since we will
remove the postage constraint from the templates table, this adds a JSON
schema for updating a template so that we can use it to check that the
postage is one of the allowed values.
2020-06-19 08:59:27 +01:00
Katie Smith
15112b2148 Update post_create_template_schema
Updated the `post_create_template_schema` to check that the postage is
one of our allowed values.
2020-06-19 08:59:27 +01:00
Leo Hemsted
69043d70ec Merge pull request #2886 from alphagov/statsd-dns-cache
re-enable statsd (and cache statsd DNS lookup)
2020-06-18 11:30:23 +01:00
Leo Hemsted
728e5eee24 re-enable statsd (and cache statsd DNS lookup)
see https://github.com/alphagov/notifications-utils/pull/752 for
implementation details. Cache DNS for 15 seconds. Note: This cache is
per eventlet, so concurrent requests will handle their requests
separately, but the cache does persist between sequential requests.

re-enable statsd for all environments.
2020-06-18 11:20:00 +01:00
Rebecca Law
982af99793 Merge pull request #2885 from alphagov/fix-bug
Fix error for get_notification_by_id when the updated_at is None for a delivered test message
2020-06-18 08:49:24 +01:00
Rebecca Law
be7afdd12b In the effort to reduce the number of database connections I introduced a small bug. This only affected the test templated letter flow, a None type error would happen when trying to creathe completed_at timestamp for a delivered message.
In the previous PR I removed the `update_notification` method to reduce the need for another update query. However, that meant the notification was marked as delivered without an updated_at timestamp.

It is weird to set the updated_at when we create the notification. So is this a better fix? Or do I put the update back now?

I recommend we push this fix now.
2020-06-18 08:30:19 +01:00
David McDonald
3f117282af Merge pull request #2884 from alphagov/statsd-off
Turn off statsd for the API in all environments
2020-06-17 17:40:54 +01:00
David McDonald
92c3170fe3 Turn off statsd for the API in all environments
We have seen bad latency across all apps in the last week. After running
a canary with statsd turned off we have seen these issues dissapear on
that canary instance. We therefore will turn off statsd for all
instances of the API. We will still need to investigate tomorrow what
exactly changed or is causing the issue with statsd as we hadn't made
any changes to it ourself.

Note, this keeps statsd on for all the other apps but this will be a
first step. We will also need to check performance of the other apps
after releasing this.
2020-06-17 17:31:36 +01:00
Rebecca Law
77ce058732 Merge pull request #2882 from alphagov/reduce-db-connection
Reduce the use of db connections
2020-06-17 14:22:58 +01:00
Rebecca Law
a8661fbd4b Add None test back 2020-06-17 13:48:48 +01:00
Rebecca Law
e22f61977f Remove print stmts 2020-06-17 12:13:49 +01:00
Rebecca Law
a5ed8f2079 Update the post letter flow - not able to get reduce the dB transactions used in the letter flow. Prioritising the reduction for the SMS/Email flow.
Only update the daily limit cache if the service is in trial mode.
2020-06-17 12:11:28 +01:00
Chris Hill-Scott
ca5cfe8d01 Merge pull request #2879 from alphagov/serialise-less-stuff
Serialise less stuff from the service model
2020-06-17 08:18:28 +01:00