Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: Remove unused secondary global index
Browse files Browse the repository at this point in the history
Issue: 103
  • Loading branch information
jrconlin committed Nov 15, 2018
1 parent 991b5d5 commit 1833f5a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 108 deletions.
99 changes: 1 addition & 98 deletions autopush/db.py
Expand Up @@ -266,36 +266,11 @@ def create_router_table(tablename="router", read_throughput=5,
{
'AttributeName': 'uaid',
'AttributeType': 'S'
},
{
'AttributeName': 'last_connect',
'AttributeType': 'N'
}],
ProvisionedThroughput={
'ReadCapacityUnits': read_throughput,
'WriteCapacityUnits': write_throughput,
},
GlobalSecondaryIndexes=[
{
'IndexName': 'AccessIndex',
'KeySchema': [
{
'AttributeName': "last_connect",
'KeyType': "HASH"
}
],
'Projection': {
'ProjectionType': 'INCLUDE',
'NonKeyAttributes': [
'last_connect'
],
},
'ProvisionedThroughput': {
'ReadCapacityUnits': read_throughput,
'WriteCapacityUnits': write_throughput,
}
}
]
}
)
table.meta.client.get_waiter('table_exists').wait(
TableName=tablename)
Expand Down Expand Up @@ -436,23 +411,6 @@ def generate_last_connect():
return int(val)


def generate_last_connect_values(date):
# type: (datetime.date) -> Iterable[int]
"""Generator of last_connect values for a given date
Creates an iterator that yields all the valid values for ``last_connect``
for a given year/month.
"""
year = str(date.year)
month = str(date.month).zfill(2)
for hour in range(0, 24):
for rand_int in range(0, 11):
val = "".join([year, month, str(hour).zfill(2),
str(rand_int).zfill(4)])
yield int(val)


def table_exists(tablename, boto_resource=None):
# type: (str, DynamoDBResource) -> bool
"""Determine if the specified Table exists"""
Expand Down Expand Up @@ -902,61 +860,6 @@ def drop_user(self, uaid):
Key={'uaid': hasher(uaid)})
return result['ResponseMetadata']['HTTPStatusCode'] == 200

def delete_uaids(self, uaids):
# type: (List[str]) -> None
"""Issue a batch delete call for the given uaids"""
with self.table.batch_writer() as batch:
for uaid in uaids:
batch.delete_item(Key={'uaid': uaid})

def drop_old_users(self, months_ago=2):
# type: (int) -> Iterable[int]
"""Drops user records that have no recent connection
Utilizes the last_connect index to locate users that haven't
connected in the given time-frame.
The caller must iterate through this generator to trigger batch
delete calls. Caller should wait as appropriate to avoid exceeding
table limits.
Each iteration will result in a batch delete for the currently
iterated batch. This implies a set of writes equal in size to the
``25 * record-size`` minimum.
.. warning::
Calling list() on this generator will likely exceed provisioned
write through-put as the batch-delete calls will be made as
quickly as possible.
:param months_ago: how many months ago since the last connect
:returns: Iterable of how many deletes were run
"""
prior_date = get_month(-months_ago)

batched = []
for hash_key in generate_last_connect_values(prior_date):
response = self.table.query(
KeyConditionExpression=Key("last_connect").eq(hash_key),
IndexName="AccessIndex",
)
result_set = response.get('Items', [])
for result in result_set:
batched.append(result["uaid"])

if len(batched) == 25:
self.delete_uaids(batched)
batched = []
yield 25

# Delete any leftovers
if batched:
self.delete_uaids(batched)
yield len(batched)

@track_provisioned
def _update_last_connect(self, uaid, last_connect):
self.table.update_item(
Expand Down
10 changes: 0 additions & 10 deletions autopush/tests/test_db.py
Expand Up @@ -475,16 +475,6 @@ def _create_minimal_record(self):
}
return data

def test_drop_old_users(self):
# First create a bunch of users
# Purge any existing users from previous runs.
self.router.drop_old_users(months_ago=0)
for _ in range(0, 53):
self.router.register_user(self._create_minimal_record())

results = self.router.drop_old_users(months_ago=0)
assert list(results) == [25, 25, 3]

def test_old_mobile_user(self):
# Old mobile users (ones that use a bridge) don't regularly check
# in, or update their expiry record. It's important that we don't
Expand Down

0 comments on commit 1833f5a

Please sign in to comment.