Avoid redundant polling of DB for metadata

Every time self.metadata is read, it triggers a refresh from the database.
We were accessing it multiple times in a loop, which is not only wasteful
but can get very O(n^2) for a WaitCondition with a high Count.

Partial-Bug: #1306743

Change-Id: I1a82afac6c4ef56dbf87722648034c05abef4010
Merge "Avoid redundant polling of DB for metadata" into stable/icehouse