Skip to content

Commit

Permalink
[bug 1408981] Update bundle key generation. (#307)
Browse files Browse the repository at this point in the history
* [bug 1408981] Update bundle key generation.

 - Use only startpage_version and locale used in the templates.

 - Include in the key either template for AS or for about:home, so that changes
   in one template does not affect the cached keys of the other.

* Hard-code date in template hash calculation.

Hard-code the date in template hash calculation to prevent getting new bundle
keys between deployments and also share the keys between the different cluster
regions.
  • Loading branch information
glogiotatidis authored Oct 23, 2017
1 parent 00702d5 commit de42b93
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
21 changes: 15 additions & 6 deletions snippets/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
render_to_string(
'base/fetch_snippets.jinja',
{
'date': '',
'snippet_ids': [],
'snippets_json': '',
'locale': 'xx',
Expand All @@ -54,6 +55,7 @@
render_to_string(
'base/fetch_snippets_as.jinja',
{
'date': '',
'snippet_ids': [],
'snippets_json': '',
'locale': 'xx',
Expand Down Expand Up @@ -152,21 +154,28 @@ def __init__(self, client):
@cached_property
def key(self):
"""A unique key for this bundle as a sha1 hexdigest."""
# Key should consist of snippets that are in the bundle plus any
# properties of the client that may change the snippet code
# being sent.
# Key should consist of snippets that are in the bundle. This part
# accounts for all the properties sent by the Client, since the
# self.snippets lists snippets are all filters and CMRs have been
# applied.
key_properties = [
'{id}-{date}-{templatedate}'.format(id=snippet.id,
date=snippet.modified.isoformat(),
templatedate=snippet.template.modified.isoformat())
for snippet in self.snippets]

key_properties.extend(self.client)
# Additional values used to calculate the key are the templates and the
# variables used to render them besides snippets.
key_properties.extend([
SNIPPET_FETCH_TEMPLATE_HASH,
SNIPPET_FETCH_TEMPLATE_AS_HASH,
self.client.startpage_version,
self.client.locale,
util.current_firefox_major_version(),

])
if self.client.startpage_version == '5':
key_properties.append(SNIPPET_FETCH_TEMPLATE_AS_HASH)
else:
key_properties.append(SNIPPET_FETCH_TEMPLATE_HASH)

key_string = u'_'.join(key_properties)
return hashlib.sha1(key_string.encode('utf-8')).hexdigest()
Expand Down
6 changes: 5 additions & 1 deletion snippets/base/templates/base/fetch_snippets.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% include 'base/includes/snippet.css' %}
<script type="text/javascript">
//<![CDATA[
// Generated on {{ utcnow() }}
{#
The date variable gets populated when we calculate the template hash for caching
and gets the default utcnow() time otherwise.
#}
// Generated on {{ date|default(utcnow()) }}
// Included snippets:
{% for id in snippet_ids %}
// - {{ id }}
Expand Down
6 changes: 5 additions & 1 deletion snippets/base/templates/base/fetch_snippets_as.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{% include 'base/includes/snippet_as.css' %}
</style>
<script type="application/javascript">
// Generated on {{ utcnow() }}
{#
The date variable gets populated when we calculate the template hash for caching
and gets the default utcnow() time otherwise.
#}
// Generated on {{ date|default(utcnow()) }}
// Included snippets:
{% for id in snippet_ids %}
// - {{ id }}
Expand Down
12 changes: 0 additions & 12 deletions snippets/base/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,6 @@ def test_key_locale(self):

self.assertNotEqual(bundle1.key, bundle2.key)

def test_key_name(self):
"""
bundle.key must be different between bundles if they have
different names.
"""
client1 = self._client(name='firefox')
client2 = self._client(name='firefox-test')
bundle1 = SnippetBundle(client1)
bundle2 = SnippetBundle(client2)

self.assertNotEqual(bundle1.key, bundle2.key)

def test_key_equal(self):
client1 = self._client(locale='en-US', startpage_version='4')
client2 = self._client(locale='en-US', startpage_version='4')
Expand Down

0 comments on commit de42b93

Please sign in to comment.