use of co.cask.cdap.api.dataset.lib.cube.TimeSeries in project cdap by caskdata.
the class CubeDatasetTest method testTxRetryOnFailure.
@Test
public void testTxRetryOnFailure() throws Exception {
// This test ensures that there's no non-transactional cache used in cube dataset. For that, it
// 1) simulates transaction conflict for the first write to cube
// 2) attempts to write again, writes successfully
// 3) uses second cube instance to read the result
//
// In case there's a non-transactional cache used in cube, it would fill entity mappings in the first tx, and only
// use them to write data. Hence, when reading - there will be no mapping in entity table to decode, as first tx
// that wrote it is not visible (was aborted on conflict).
Aggregation agg1 = new DefaultAggregation(ImmutableList.of("dim1", "dim2", "dim3"));
int resolution = 1;
Cube cube1 = getCubeInternal("concurrCube", new int[] { resolution }, ImmutableMap.of("agg1", agg1));
Cube cube2 = getCubeInternal("concurrCube", new int[] { resolution }, ImmutableMap.of("agg1", agg1));
Configuration txConf = HBaseConfiguration.create();
TransactionManager txManager = new TransactionManager(txConf);
txManager.startAndWait();
try {
TransactionSystemClient txClient = new InMemoryTxSystemClient(txManager);
// 1) write and abort after commit to simlate conflict
Transaction tx = txClient.startShort();
((TransactionAware) cube1).startTx(tx);
writeInc(cube1, "metric1", 1, 1, "1", "1", "1");
((TransactionAware) cube1).commitTx();
txClient.abort(tx);
((TransactionAware) cube1).rollbackTx();
// 2) write successfully
tx = txClient.startShort();
((TransactionAware) cube1).startTx(tx);
writeInc(cube1, "metric1", 1, 1, "1", "1", "1");
// let's pretend we had conflict and rollback it
((TransactionAware) cube1).commitTx();
txClient.commitOrThrow(tx);
((TransactionAware) cube1).postTxCommit();
// 3) read using different cube instance
tx = txClient.startShort();
((TransactionAware) cube2).startTx(tx);
verifyCountQuery(cube2, 0, 2, resolution, "metric1", AggregationFunction.SUM, new HashMap<String, String>(), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 1))));
// let's pretend we had conflict and rollback it
((TransactionAware) cube2).commitTx();
txClient.commitOrThrow(tx);
((TransactionAware) cube2).postTxCommit();
} finally {
txManager.stopAndWait();
}
}
use of co.cask.cdap.api.dataset.lib.cube.TimeSeries in project cdap by caskdata.
the class TestAppWithCube method query.
private Collection<TimeSeries> query(URL serviceUrl, CubeQuery query) throws IOException {
URL url = new URL(serviceUrl, "query");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
HttpResponse response = HttpRequests.execute(request);
Assert.assertEquals(200, response.getResponseCode());
return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<TimeSeries>>() {
}.getType());
}
use of co.cask.cdap.api.dataset.lib.cube.TimeSeries in project cdap by caskdata.
the class TestAppWithCube method testApp.
@Category(SlowTests.class)
@Test
public void testApp() throws Exception {
// Deploy the application
ApplicationManager appManager = deployApplication(AppWithCube.class);
ServiceManager serviceManager = appManager.getServiceManager(AppWithCube.SERVICE_NAME).start();
try {
serviceManager.waitForStatus(true);
URL url = serviceManager.getServiceURL();
long tsInSec = System.currentTimeMillis() / 1000;
// round to a minute for testing minute resolution
tsInSec = (tsInSec / 60) * 60;
// add couple facts
add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
add(url, ImmutableList.of(new CubeFact(tsInSec).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 1).addDimensionValue("user", "alex").addDimensionValue("action", "back").addMeasurement("count", MeasureType.COUNTER, 1), new CubeFact(tsInSec + 2).addDimensionValue("user", "alex").addDimensionValue("action", "click").addMeasurement("count", MeasureType.COUNTER, 1)));
// search for tags
Collection<DimensionValue> tags = searchDimensionValue(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, new ArrayList<DimensionValue>()));
Assert.assertEquals(1, tags.size());
DimensionValue tv = tags.iterator().next();
Assert.assertEquals("user", tv.getName());
Assert.assertEquals("alex", tv.getValue());
tags = searchDimensionValue(url, CubeExploreQuery.builder().from().resolution(1, TimeUnit.SECONDS).where().dimension("user", "alex").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
Assert.assertEquals(2, tags.size());
Iterator<DimensionValue> iterator = tags.iterator();
tv = iterator.next();
Assert.assertEquals("action", tv.getName());
Assert.assertEquals("back", tv.getValue());
tv = iterator.next();
Assert.assertEquals("action", tv.getName());
Assert.assertEquals("click", tv.getValue());
// search for measures
Collection<String> measures = searchMeasure(url, new CubeExploreQuery(tsInSec - 60, tsInSec + 60, 1, 100, ImmutableList.of(new DimensionValue("user", "alex"))));
Assert.assertEquals(1, measures.size());
String measure = measures.iterator().next();
Assert.assertEquals("count", measure);
// query for data
// 1-sec resolution
Collection<TimeSeries> data = query(url, CubeQuery.builder().select().measurement("count", AggregationFunction.SUM).from(null).resolution(1, TimeUnit.SECONDS).where().dimension("action", "click").timeRange(tsInSec - 60, tsInSec + 60).limit(100).build());
Assert.assertEquals(1, data.size());
TimeSeries series = data.iterator().next();
List<TimeValue> timeValues = series.getTimeValues();
Assert.assertEquals(2, timeValues.size());
TimeValue timeValue = timeValues.get(0);
Assert.assertEquals(tsInSec, timeValue.getTimestamp());
Assert.assertEquals(2, timeValue.getValue());
timeValue = timeValues.get(1);
Assert.assertEquals(tsInSec + 2, timeValue.getTimestamp());
Assert.assertEquals(1, timeValue.getValue());
// 60-sec resolution
data = query(url, new CubeQuery(null, tsInSec - 60, tsInSec + 60, 60, 100, ImmutableMap.of("count", AggregationFunction.SUM), ImmutableMap.of("action", "click"), new ArrayList<String>(), null));
Assert.assertEquals(1, data.size());
series = data.iterator().next();
timeValues = series.getTimeValues();
Assert.assertEquals(1, timeValues.size());
timeValue = timeValues.get(0);
Assert.assertEquals(tsInSec, timeValue.getTimestamp());
Assert.assertEquals(3, timeValue.getValue());
} finally {
serviceManager.stop();
serviceManager.waitForStatus(false);
}
}
use of co.cask.cdap.api.dataset.lib.cube.TimeSeries in project cdap by caskdata.
the class AbstractCubeTest method testBasics.
@Test
public void testBasics() throws Exception {
Aggregation agg1 = new DefaultAggregation(ImmutableList.of("dim1", "dim2", "dim3"), ImmutableList.of("dim1", "dim2"));
Aggregation agg2 = new DefaultAggregation(ImmutableList.of("dim1", "dim2"), ImmutableList.of("dim1"));
int resolution = 1;
Cube cube = getCube("myCube", new int[] { resolution }, ImmutableMap.of("agg1", agg1, "agg2", agg2));
// write some data
// NOTE: we mostly use different ts, as we are interested in checking incs not at persist, but rather at query time
writeInc(cube, "metric1", 1, 1, "1", "1", "1");
writeInc(cube, "metric1", 1, 1, "1", "1", "1");
writeInc(cube, "metric1", 2, 2, null, "1", "1");
writeInc(cube, "metric1", 3, 3, "1", "2", "1");
writeInc(cube, "metric1", 3, 5, "1", "2", "3");
writeInc(cube, "metric1", 3, 7, "2", "1", "1");
writeInc(cube, "metric1", 4, 4, "1", null, "2");
writeInc(cube, "metric1", 5, 5, null, null, "1");
writeInc(cube, "metric1", 6, 6, "1", null, null);
writeInc(cube, "metric1", 7, 3, "1", "1", null);
// writing using BatchWritable APIs
writeIncViaBatchWritable(cube, "metric1", 8, 2, null, "1", null);
writeIncViaBatchWritable(cube, "metric1", 9, 1, null, null, null);
// writing in batch
cube.add(ImmutableList.of(getFact("metric1", 10, 2, MeasureType.COUNTER, "1", "1", "1", "1"), getFact("metric1", 11, 3, MeasureType.COUNTER, "1", "1", "1", null), getFact("metric1", 12, 4, MeasureType.COUNTER, "2", "1", "1", "1"), getFact("metric1", 13, 5, MeasureType.COUNTER, null, null, null, "1")));
writeInc(cube, "metric2", 1, 1, "1", "1", "1");
// todo: do some write instead of increments - test those as well
// now let's query!
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1"), ImmutableList.of("dim2"), ImmutableList.of(new TimeSeries("metric1", dimensionValues("dim2", "1"), timeValues(1, 2, 7, 3, 10, 2, 11, 3)), new TimeSeries("metric1", dimensionValues("dim2", "2"), timeValues(3, 8))));
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 10, 2, 11, 3))));
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, new HashMap<String, String>(), ImmutableList.of("dim1"), ImmutableList.of(new TimeSeries("metric1", dimensionValues("dim1", "1"), timeValues(1, 2, 3, 8, 4, 4, 6, 6, 7, 3, 10, 2, 11, 3)), new TimeSeries("metric1", dimensionValues("dim1", "2"), timeValues(3, 7, 12, 4))));
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim3", "3"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(3, 5))));
// test querying specific aggregations
verifyCountQuery(cube, "agg1", 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 3, 8, 7, 3, 10, 2, 11, 3))));
verifyCountQuery(cube, "agg2", 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 3, 8, 4, 4, 6, 6, 7, 3, 10, 2, 11, 3))));
// query with different agg functions
verifyCountQuery(cube, "agg1", 0, 15, resolution, "metric1", AggregationFunction.MAX, ImmutableMap.of("dim1", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 3, 5, 7, 3, 10, 2, 11, 3))));
verifyCountQuery(cube, "agg1", 0, 15, resolution, "metric1", AggregationFunction.MIN, ImmutableMap.of("dim1", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 3, 3, 7, 3, 10, 2, 11, 3))));
verifyCountQuery(cube, "agg1", 0, 15, resolution, "metric1", AggregationFunction.LATEST, ImmutableMap.of("dim1", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 2, 3, 5, 7, 3, 10, 2, 11, 3))));
// delete cube data for "metric1" for dim->1,dim2->1,dim3->1 for timestamp 1 - 8 and
// check data for other timestamp is available
CubeDeleteQuery query = new CubeDeleteQuery(0, 8, resolution, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), "metric1");
cube.delete(query);
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), ImmutableList.<String>of(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(10, 2, 11, 3))));
// delete cube data for "metric1" for dim1->1 and dim2->1 and check by scanning dim1->1 and dim2->1 is empty,
query = new CubeDeleteQuery(0, 15, resolution, ImmutableMap.of("dim1", "1", "dim2", "1"), "metric1");
cube.delete(query);
verifyCountQuery(cube, 0, 15, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1"), ImmutableList.<String>of(), ImmutableList.<TimeSeries>of());
}
use of co.cask.cdap.api.dataset.lib.cube.TimeSeries in project cdap by caskdata.
the class AbstractCubeTest method testInterpolate.
@Test
public void testInterpolate() throws Exception {
Aggregation agg1 = new DefaultAggregation(ImmutableList.of("dim1", "dim2", "dim3"), ImmutableList.of("dim1", "dim2", "dim3"));
int resolution = 1;
Cube cube = getCube("myInterpolatedCube", new int[] { resolution }, ImmutableMap.of("agg1", agg1));
// test step interpolation
long startTs = 1;
long endTs = 10;
writeInc(cube, "metric1", startTs, 5, "1", "1", "1");
writeInc(cube, "metric1", endTs, 3, "1", "1", "1");
List<TimeValue> expectedTimeValues = Lists.newArrayList();
for (long i = startTs; i < endTs; i++) {
expectedTimeValues.add(new TimeValue(i, 5));
}
expectedTimeValues.add(new TimeValue(endTs, 3));
verifyCountQuery(cube, startTs, endTs, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), expectedTimeValues)), new Interpolators.Step());
CubeDeleteQuery query = new CubeDeleteQuery(startTs, endTs, resolution, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), "metric1");
cube.delete(query);
// test small-slope linear interpolation
startTs = 1;
endTs = 5;
writeInc(cube, "metric1", startTs, 5, "1", "1", "1");
writeInc(cube, "metric1", endTs, 3, "1", "1", "1");
verifyCountQuery(cube, startTs, endTs, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 5, 2, 5, 3, 4, 4, 4, 5, 3))), new Interpolators.Linear());
query = new CubeDeleteQuery(startTs, endTs, resolution, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), "metric1");
cube.delete(query);
// test big-slope linear interpolation
writeInc(cube, "metric1", startTs, 100, "1", "1", "1");
writeInc(cube, "metric1", endTs, 500, "1", "1", "1");
verifyCountQuery(cube, startTs, endTs, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), timeValues(1, 100, 2, 200, 3, 300, 4, 400, 5, 500))), new Interpolators.Linear());
cube.delete(query);
// test limit on Interpolate
long limit = 20;
writeInc(cube, "metric1", 0, 10, "1", "1", "1");
writeInc(cube, "metric1", limit + 1, 50, "1", "1", "1");
expectedTimeValues.clear();
expectedTimeValues.add(new TimeValue(0, 10));
for (long i = 1; i <= limit; i++) {
expectedTimeValues.add(new TimeValue(i, 0));
}
expectedTimeValues.add(new TimeValue(limit + 1, 50));
verifyCountQuery(cube, 0, 21, resolution, "metric1", AggregationFunction.SUM, ImmutableMap.of("dim1", "1", "dim2", "1", "dim3", "1"), new ArrayList<String>(), ImmutableList.of(new TimeSeries("metric1", new HashMap<String, String>(), expectedTimeValues)), new Interpolators.Step(limit));
}
Aggregations