Skip to content

Commit

Permalink
[HWKMETRICS-190] Fetch tenantIds from the metrics_idx table also
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Burman committed Aug 25, 2015
1 parent 780823c commit 6e82839
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class DataAccessImpl implements DataAccess {
private PreparedStatement insertTenantId;

private PreparedStatement findAllTenantIds;
private PreparedStatement findAllTenantIdsFromMetricsIdx;

private PreparedStatement insertTenantIdIntoBucket;

Expand Down Expand Up @@ -161,6 +162,8 @@ protected void initPreparedStatements() {

findAllTenantIds = session.prepare("SELECT DISTINCT id FROM tenants");

findAllTenantIdsFromMetricsIdx = session.prepare("SELECT DISTINCT tenant_id, type FROM metrics_idx");

findTenantIdsByTime = session.prepare("SELECT tenant FROM tenants_by_time WHERE bucket = ?");

insertTenantIdIntoBucket = session.prepare("INSERT into tenants_by_time (bucket, tenant) VALUES (?, ?)");
Expand Down Expand Up @@ -358,7 +361,8 @@ public Observable<ResultSet> insertTenant(Tenant tenant) {

@Override
public Observable<ResultSet> findAllTenantIds() {
return rxSession.execute(findAllTenantIds.bind());
return rxSession.execute(findAllTenantIds.bind())
.concatWith(rxSession.execute(findAllTenantIdsFromMetricsIdx.bind()));
}

@Override public Observable<ResultSet> findTenantIds(long time) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,25 +400,30 @@ public Observable<Tenant> getTenants() {
return dataAccess.findAllTenantIds()
.flatMap(Observable::from)
.map(row -> row.getString(0))
.flatMap(dataAccess::findTenant)
.flatMap(Observable::from)
.map(Functions::getTenant);
.distinct()
.flatMap(id ->
dataAccess.findTenant(id)
.flatMap(Observable::from)
.map(Functions::getTenant)
.switchIfEmpty(Observable.just(new Tenant(id)))
);
}

private List<String> loadTenantIds() {
Iterable<String> tenantIds = dataAccess.findAllTenantIds()
.flatMap(Observable::from)
.map(row -> row.getString(0))
.distinct()
.toBlocking()
.toIterable();
return ImmutableList.copyOf(tenantIds);
}

@Override
public Observable<Void> createMetric(Metric<?> metric) {
if (metric.getType() == COUNTER_RATE) {
throw new IllegalArgumentException(metric + " cannot be created. " + COUNTER_RATE + " metrics are " +
"internally generated metrics and cannot be created by clients.");
if(!MetricType.userTypes().contains(metric.getId().getType())) {
throw new IllegalArgumentException(metric + " cannot be created. " + metric.getId().getType() +
" metrics are internally generated metrics and cannot be created by clients.");
}

ResultSetFuture future = dataAccess.insertMetricInMetricsIndex(metric);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ public void createTenants() throws Exception {
new MetricId(t1.getId(), AVAILABILITY, makeSafe(AVAILABILITY.getText())), 1)));
}

@Test
public void createMetricsIdxTenants() throws Exception {
Metric<Double> em1 = new Metric<>(new MetricId("t123", GAUGE, "em1"));
metricsService.createMetric(em1).toBlocking().lastOrDefault(null);

Metric actual = metricsService.findMetric(em1.getId())
.toBlocking()
.lastOrDefault(null);
assertNotNull(actual);
assertEquals(actual, em1, "The metric does not match the expected value");

Set<Tenant> actualTenants = ImmutableSet.copyOf(metricsService.getTenants().toBlocking().toIterable());
assertEquals(actualTenants.size(), 1);

Tenant t1 = new Tenant("t123", ImmutableMap.of(GAUGE, 1, AVAILABILITY, 1));
metricsService.createTenant(t1).toBlocking().lastOrDefault(null);

actualTenants = ImmutableSet.copyOf(metricsService.getTenants().toBlocking().toIterable());
assertEquals(actualTenants.size(), 1);
}

@Test
public void createAndFindMetrics() throws Exception {
Metric<Double> em1 = new Metric<>(new MetricId("t1", GAUGE, "em1"));
Expand Down

0 comments on commit 6e82839

Please sign in to comment.