-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-204] initial commit for task to create tenants
We query the metrics_idx table to determine whether or not a metric exists. Metrics are implicitly created when inserting data points because we write to both the data and the metrics_idx tables. We need to implicitly create tenants in order to set up various background jobs like generating rates and deleting data (see HWKMETRICS-191 for details).
- Loading branch information
John Sanda
committed
Aug 13, 2015
1 parent
807e094
commit 1044a12
Showing
8 changed files
with
241 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
core/metrics-core-impl/src/main/java/org/hawkular/metrics/core/impl/CreateTenants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.metrics.core.impl; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import org.hawkular.metrics.core.api.MetricsService; | ||
import org.hawkular.metrics.core.api.Tenant; | ||
import org.hawkular.metrics.tasks.api.Task2; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import rx.Observable; | ||
import rx.functions.Action1; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
public class CreateTenants implements Action1<Task2> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CreateTenants.class); | ||
|
||
private MetricsService metricsService; | ||
|
||
private DataAccess dataAccess; | ||
|
||
public CreateTenants(MetricsService metricsService, DataAccess dataAccess) { | ||
this.metricsService = metricsService; | ||
this.dataAccess = dataAccess; | ||
} | ||
|
||
@Override public void call(Task2 task) { | ||
long bucket = task.getTrigger().getTriggerTime(); | ||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
Observable<Tenant> tenants = dataAccess.findTenantIds(bucket) | ||
.flatMap(Observable::from) | ||
.map(row -> row.getString(0)) | ||
.flatMap(tenantId -> getTenant(tenantId).map(tenant -> tenant == null ? tenantId : tenant.getId())) | ||
.filter(tenantId -> tenantId != null) | ||
.map(Tenant::new); | ||
|
||
metricsService.createTenants(tenants).subscribe( | ||
aVoid -> {}, | ||
t -> { | ||
logger.warn("Tenant creation failed", t); | ||
latch.countDown(); | ||
}, | ||
() -> { | ||
dataAccess.deleteTenantsBucket(bucket).subscribe( | ||
resultSet -> {}, | ||
t -> { | ||
logger.warn("Failed to delete tenants bucket [" + bucket + "]", t); | ||
latch.countDown(); | ||
}, | ||
latch::countDown | ||
); | ||
} | ||
); | ||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
|
||
private Observable<Tenant> getTenant(String tenantId) { | ||
return dataAccess.findTenant(tenantId) | ||
.flatMap(Observable::from) | ||
.map(Functions::getTenant); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
core/metrics-core-impl/src/test/java/org/hawkular/metrics/core/impl/CreateTenantsITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.metrics.core.impl; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static java.util.UUID.randomUUID; | ||
|
||
import static org.joda.time.Duration.standardMinutes; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.util.List; | ||
|
||
import org.hawkular.metrics.core.api.Tenant; | ||
import org.hawkular.metrics.tasks.api.SingleExecutionTrigger; | ||
import org.hawkular.metrics.tasks.api.Trigger; | ||
import org.hawkular.metrics.tasks.impl.Task2Impl; | ||
import org.joda.time.DateTime; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
public class CreateTenantsITest extends MetricsITest { | ||
|
||
private MetricsServiceImpl metricsService; | ||
|
||
private DateTimeService dateTimeService; | ||
|
||
@BeforeClass | ||
public void initClass() { | ||
initSession(); | ||
|
||
dateTimeService = new DateTimeService(); | ||
|
||
metricsService = new MetricsServiceImpl(); | ||
metricsService.setTaskScheduler(new FakeTaskScheduler()); | ||
|
||
metricsService.startUp(session, getKeyspace(), false, new MetricRegistry()); | ||
} | ||
|
||
@BeforeMethod | ||
public void initMethod() { | ||
session.execute("TRUNCATE tenants"); | ||
session.execute("TRUNCATE tenants_by_time"); | ||
} | ||
|
||
@Test | ||
public void executeWhenThereAreNoTenantsForBucket() { | ||
DateTime bucket = dateTimeService.getTimeSlice(DateTime.now(), standardMinutes(1)); | ||
|
||
Tenant t1 = new Tenant("T1"); | ||
doAction(() -> metricsService.createTenant(t1)); | ||
|
||
Trigger trigger = new SingleExecutionTrigger(bucket.getMillis()); | ||
Task2Impl taskDetails = new Task2Impl(randomUUID(), "$system", 0, "create-tenants", emptyMap(), trigger); | ||
|
||
CreateTenants task = new CreateTenants(metricsService, metricsService.getDataAccess()); | ||
task.call(taskDetails); | ||
|
||
List<Tenant> actual = getOnNextEvents(metricsService::getTenants); | ||
List<Tenant> expected = ImmutableList.of(t1); | ||
|
||
assertEquals(actual, expected, "No new tenants should have been created."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters