Skip to content

Commit

Permalink
Use _id for metrics collections.
Browse files Browse the repository at this point in the history
The metrics collections are capped, so an _id is not required. However, starting
in MongoDB 2.0, you can no longer perform a multi-update without an object _id,
which caused metrics invalidation to fail. The fix is relatively simple, since
the metrics values already had an implicit unique id (expression, level, time
and optional group key): make the id explicit.

Note that this change is not backwards-compatible!
  • Loading branch information
mbostock committed Jan 19, 2012
1 parent 3c36453 commit fec3b51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lib/cube/server/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ exports.putter = function(db) {
var floor = tiers[tier].floor;
metrics.update({
i: false,
l: +tier,
t: {
"_id.l": +tier,
"_id.t": {
$gte: floor(times[0]),
$lte: floor(times[1])
}
Expand Down
59 changes: 28 additions & 31 deletions lib/cube/server/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ var util = require("util"),
types = require("./types"),
reduces = require("./reduces");

var metric_fields = {t: 1, g: 1, v: 1},
metric_options = {sort: {t: 1}, batchSize: 1000},
event_options = {sort: {t: 1}, batchSize: 1000},
update_options = {upsert: true};
var metric_fields = {v: 1},
metric_options = {sort: {_id: 1}, batchSize: 1000},
event_options = {sort: {t: 1}, batchSize: 1000};

// Query for metrics.
// TODO use expression ids, and record how often expressions are queried?
Expand Down Expand Up @@ -68,9 +67,9 @@ exports.getter = function(db) {
// Query for the desired metric in the cache.
type.metrics.find({
i: false,
e: expression.source,
l: tier.key,
t: {
"_id.e": expression.source,
"_id.l": tier.key,
"_id.t": {
$gte: start,
$lt: stop
}
Expand All @@ -84,9 +83,9 @@ exports.getter = function(db) {
cursor.each(function(error, row) {
if (error) throw error;
if (row) {
callback(row.t, row.v, row.g);
if (time < row.t) compute(time, row.t);
time = tier.step(row.t);
callback(row._id.t, row.v, row._id.g);
if (time < row._id.t) compute(time, row._id.t);
time = tier.step(row._id.t);
} else {
if (time < stop) compute(time, stop);
}
Expand Down Expand Up @@ -203,31 +202,29 @@ exports.getter = function(db) {

function saveGroup(time, value, group) {
callback(time, value, group);
if (value) type.metrics.update({
e: expression.source,
l: tier.key,
t: time,
g: group
}, {
$set: {
i: false,
v: new Double(value)
}
}, update_options);
if (value) type.metrics.save({
_id: {
e: expression.source,
l: tier.key,
t: time,
g: group
},
i: false,
v: new Double(value)
});
}

function save(time, value) {
callback(time, value);
if (value) type.metrics.update({
e: expression.source,
l: tier.key,
t: time
}, {
$set: {
i: false,
v: new Double(value)
}
}, update_options);
if (value) type.metrics.save({
_id: {
e: expression.source,
l: tier.key,
t: time
},
i: false,
v: new Double(value)
});
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions schema/schema-create.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
db.createCollection("boards");

["random", "stock", "collectd_df", "collectd_load", "collectd_interface", "collectd_memory"].forEach(function(type) {
["random", "collectd_df", "collectd_load", "collectd_interface", "collectd_memory"].forEach(function(type) {
var event = type + "_events", metric = type + "_metrics";
db.createCollection(event);
db[event].ensureIndex({t: 1});
db.createCollection(metric, {capped: true, size: 1e6, autoIndexId: false});
db[metric].ensureIndex({e: 1, l: 1, t: 1, g: 1}, {unique: true});
db[metric].ensureIndex({i: 1, e: 1, l: 1, t: 1});
db[metric].ensureIndex({i: 1, l: 1, t: 1});
db.createCollection(metric, {capped: true, size: 1e7, autoIndexId: true});
db[metric].ensureIndex({"i": 1, "_id.e": 1, "_id.l": 1, "_id.t": 1});
db[metric].ensureIndex({"i": 1, "_id.l": 1, "_id.t": 1});
});

0 comments on commit fec3b51

Please sign in to comment.