Skip to content

Commit

Permalink
Handle missing units values in mongodb data.
Browse files Browse the repository at this point in the history
Provide a units value if it's missing in the existing mongodb data. This
is for the backward compatibility issue when the data had been already
stored before the counter_unit field was added in our meter definition.

Fixed bug #1098603.

Change-Id: I1ff3e325dc9b226e4441de6f5f9118fced67cf33
  • Loading branch information
lianhao committed Mar 8, 2013
1 parent b345cef commit 43a9933
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ceilometer/storage/impl_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ def get_meters(self, user=None, project=None, resource=None, source=None,
m = {}
m['name'] = r_meter['counter_name']
m['type'] = r_meter['counter_type']
m['unit'] = r_meter['counter_unit']
# Return empty string if 'counter_unit' is not valid for
# backward compaitiblity.
m['unit'] = r_meter.get('counter_unit', '')
m['resource_id'] = r['_id']
m['project_id'] = r['project_id']
m['user_id'] = r['user_id']
Expand Down
76 changes: 76 additions & 0 deletions tests/storage/test_impl_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@
"""

import copy
import datetime
import mox

from tests.storage import base

from ceilometer.collector import meter
from ceilometer import counter
from ceilometer.storage.impl_test import TestConnection
from ceilometer.tests.db import require_map_reduce

Expand Down Expand Up @@ -150,3 +155,74 @@ class StatisticsTest(base.StatisticsTest, MongoDBEngineTestBase):
def setUp(self):
super(StatisticsTest, self).setUp()
require_map_reduce(self.conn)


class CompatibilityTest(MongoDBEngineTestBase):

def prepare_data(self):
def old_record_metering_data(self, data):
self.db.user.update(
{'_id': data['user_id']},
{'$addToSet': {'source': data['source'],
},
},
upsert=True,
)
self.db.project.update(
{'_id': data['project_id']},
{'$addToSet': {'source': data['source'],
},
},
upsert=True,
)
received_timestamp = datetime.datetime.utcnow()
self.db.resource.update(
{'_id': data['resource_id']},
{'$set': {'project_id': data['project_id'],
'user_id': data['user_id'],
# Current metadata being used and when it was
# last updated.
'timestamp': data['timestamp'],
'received_timestamp': received_timestamp,
'metadata': data['resource_metadata'],
'source': data['source'],
},
'$addToSet': {'meter': {'counter_name': data['counter_name'],
'counter_type': data['counter_type'],
},
},
},
upsert=True,
)

record = copy.copy(data)
self.db.meter.insert(record)
return

# Stubout with the old version DB schema, the one w/o 'counter_unit'
self.stubs.Set(self.conn,
'record_metering_data',
old_record_metering_data)
self.counters = []
c = counter.Counter(
'volume.size',
'gauge',
'GiB',
5,
'user-id',
'project1',
'resource-id',
timestamp=datetime.datetime(2012, 9, 25, 10, 30),
resource_metadata={'display_name': 'test-volume',
'tag': 'self.counter',
}
)
self.counters.append(c)
msg = meter.meter_message_from_counter(c,
secret='not-so-secret',
source='test')
self.conn.record_metering_data(self.conn, msg)

def test_counter_unit(self):
meters = list(self.conn.get_meters())
self.assertEqual(len(meters), 1)

0 comments on commit 43a9933

Please sign in to comment.