use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class TimeseriesTableTest method testDataSet.
@Test
public void testDataSet() throws Exception {
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(table);
// this test runs all operations synchronously
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
byte[] metric1 = Bytes.toBytes("metric1");
byte[] metric2 = Bytes.toBytes("metric2");
byte[] tag1 = Bytes.toBytes("111");
byte[] tag2 = Bytes.toBytes("22");
byte[] tag3 = Bytes.toBytes("3");
byte[] tag4 = Bytes.toBytes("123");
long hour = TimeUnit.HOURS.toMillis(1);
long second = TimeUnit.SECONDS.toMillis(1);
long ts = System.currentTimeMillis();
// m1e1 = metric: 1, entity: 1
TimeseriesTable.Entry m1e1 = new TimeseriesTable.Entry(metric1, Bytes.toBytes(3L), ts, tag3, tag2, tag1);
table.write(m1e1);
TimeseriesTable.Entry m1e2 = new TimeseriesTable.Entry(metric1, Bytes.toBytes(10L), ts + 2 * second, tag3);
table.write(m1e2);
TimeseriesTable.Entry m1e3 = new TimeseriesTable.Entry(metric1, Bytes.toBytes(15L), ts + 2 * hour, tag1);
table.write(m1e3);
TimeseriesTable.Entry m1e4 = new TimeseriesTable.Entry(metric1, Bytes.toBytes(23L), ts + 3 * hour, tag2, tag3);
table.write(m1e4);
TimeseriesTable.Entry m1e5 = new TimeseriesTable.Entry(metric1, Bytes.toBytes(55L), ts + 3 * hour + 2 * second);
table.write(m1e5);
TimeseriesTable.Entry m2e1 = new TimeseriesTable.Entry(metric2, Bytes.toBytes(4L), ts);
table.write(m2e1);
TimeseriesTable.Entry m2e2 = new TimeseriesTable.Entry(metric2, Bytes.toBytes(11L), ts + 2 * second, tag2);
table.write(m2e2);
TimeseriesTable.Entry m2e3 = new TimeseriesTable.Entry(metric2, Bytes.toBytes(16L), ts + 2 * hour, tag2);
table.write(m2e3);
TimeseriesTable.Entry m2e4 = new TimeseriesTable.Entry(metric2, Bytes.toBytes(24L), ts + 3 * hour, tag1, tag3);
table.write(m2e4);
TimeseriesTable.Entry m2e5 = new TimeseriesTable.Entry(metric2, Bytes.toBytes(56L), ts + 3 * hour + 2 * second, tag3, tag1);
table.write(m2e5);
// whole interval is searched
assertReadResult(table.read(metric1, ts, ts + 5 * hour), m1e1, m1e2, m1e3, m1e4, m1e5);
assertReadResult(table.read(metric1, ts, ts + 5 * hour, tag2), m1e1, m1e4);
assertReadResult(table.read(metric1, ts, ts + 5 * hour, tag4));
assertReadResult(table.read(metric1, ts, ts + 5 * hour, tag2, tag4));
// This is extreme case, should not be really used by anyone. Still we want to test that it won't fail.
// It returns nothing because there's hard limit on the number of rows traversed during the read.
assertReadResult(table.read(metric1, 0, Long.MAX_VALUE));
// test pagination read
assertReadResult(table.read(metric1, ts, ts + 5 * hour, 1, 2), m1e2, m1e3);
// part of the interval
assertReadResult(table.read(metric1, ts + second, ts + 2 * second), m1e2);
assertReadResult(table.read(metric1, ts + hour, ts + 3 * hour), m1e3, m1e4);
assertReadResult(table.read(metric1, ts + second, ts + 3 * hour), m1e2, m1e3, m1e4);
assertReadResult(table.read(metric1, ts + second, ts + 3 * hour, tag3), m1e2, m1e4);
assertReadResult(table.read(metric1, ts + second, ts + 3 * hour, tag3, tag2), m1e4);
// different metric
assertReadResult(table.read(metric2, ts + hour, ts + 3 * hour, tag2), m2e3);
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class TimeseriesTableTest method testInvalidTimeRangeCondition.
@Test(expected = TransactionFailureException.class)
public void testInvalidTimeRangeCondition() throws Exception {
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
long ts = System.currentTimeMillis();
table.read(Bytes.toBytes("any"), ts, ts - 100);
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class AppMetadataStoreTest method testOldRunRecordFormat.
@Test
public void testOldRunRecordFormat() throws Exception {
DatasetId storeTable = NamespaceId.DEFAULT.dataset("testOldRunRecordFormat");
datasetFramework.addInstance(Table.class.getName(), storeTable, DatasetProperties.EMPTY);
Table table = datasetFramework.getDataset(storeTable, ImmutableMap.<String, String>of(), null);
Assert.assertNotNull(table);
final AppMetadataStore metadataStoreDataset = new AppMetadataStore(table, cConf, new AtomicBoolean(false));
TransactionExecutor txnl = txExecutorFactory.createExecutor(Collections.singleton((TransactionAware) metadataStoreDataset));
ApplicationId application = NamespaceId.DEFAULT.app("app");
final ProgramId program = application.program(ProgramType.values()[ProgramType.values().length - 1], "program");
final RunId runId = RunIds.generate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
metadataStoreDataset.recordProgramStartOldFormat(program, runId.getId(), RunIds.getTime(runId, TimeUnit.SECONDS), null, null, null);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Set<RunId> runIds = metadataStoreDataset.getRunningInRange(0, Long.MAX_VALUE);
Assert.assertEquals(1, runIds.size());
RunRecordMeta meta = metadataStoreDataset.getRun(program, runIds.iterator().next().getId());
Assert.assertNotNull(meta);
Assert.assertEquals(runId.getId(), meta.getPid());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
metadataStoreDataset.recordProgramStopOldFormat(program, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramRunStatus.COMPLETED, null);
Map<ProgramRunId, RunRecordMeta> runRecordMap = metadataStoreDataset.getRuns(program, ProgramRunStatus.COMPLETED, 0, Long.MAX_VALUE, Integer.MAX_VALUE, null);
Assert.assertEquals(1, runRecordMap.size());
ProgramRunId programRunId = runRecordMap.keySet().iterator().next();
Assert.assertEquals(program, programRunId.getParent());
Assert.assertEquals(runId.getId(), programRunId.getRun());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class ObjectMappedTableDatasetTest method testScan.
@Test
public void testScan() throws Exception {
dsFrameworkUtil.createInstance(ObjectMappedTable.class.getName(), RECORDS_ID, ObjectMappedTableProperties.builder().setType(Record.class).build());
try {
final ObjectMappedTableDataset<Record> records = dsFrameworkUtil.getInstance(RECORDS_ID);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) records);
Record record1 = new Record(Integer.MAX_VALUE, Long.MAX_VALUE, Float.MAX_VALUE, Double.MAX_VALUE, "foobar", Bytes.toBytes("foobar"), ByteBuffer.wrap(Bytes.toBytes("foobar")), UUID.randomUUID());
Record record2 = new Record(Integer.MIN_VALUE, Long.MIN_VALUE, Float.MIN_VALUE, Double.MIN_VALUE, "baz", Bytes.toBytes("baz"), ByteBuffer.wrap(Bytes.toBytes("baz")), UUID.randomUUID());
Record record3 = new Record(1, 0L, 3.14f, 3.14159265358979323846, "hello", Bytes.toBytes("world"), ByteBuffer.wrap(Bytes.toBytes("yo")), UUID.randomUUID());
final List<KeyValue<byte[], Record>> recordList = Lists.newArrayList();
recordList.add(new KeyValue<>(Bytes.toBytes("123"), record1));
recordList.add(new KeyValue<>(Bytes.toBytes("456"), record2));
recordList.add(new KeyValue<>(Bytes.toBytes("789"), record3));
for (final KeyValue<byte[], Record> record : recordList) {
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
records.write(record.getKey(), record.getValue());
}
});
}
final List<KeyValue<byte[], Record>> actualList = Lists.newArrayList();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
CloseableIterator<KeyValue<byte[], Record>> results = records.scan((String) null, null);
while (results.hasNext()) {
actualList.add(results.next());
}
results.close();
}
});
Assert.assertEquals(recordList.size(), actualList.size());
for (int i = 0; i < actualList.size(); i++) {
KeyValue<byte[], Record> expected = recordList.get(i);
KeyValue<byte[], Record> actual = actualList.get(i);
Assert.assertArrayEquals(expected.getKey(), actual.getKey());
Assert.assertEquals(expected.getValue(), actual.getValue());
}
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
CloseableIterator<KeyValue<byte[], Record>> results = records.scan("789", null);
KeyValue<byte[], Record> actualRecord = results.next();
Assert.assertFalse(results.hasNext());
Assert.assertArrayEquals(actualRecord.getKey(), recordList.get(2).getKey());
Assert.assertEquals(actualRecord.getValue(), recordList.get(2).getValue());
results.close();
results = records.scan(null, "124");
actualRecord = results.next();
Assert.assertFalse(results.hasNext());
Assert.assertArrayEquals(actualRecord.getKey(), recordList.get(0).getKey());
Assert.assertEquals(actualRecord.getValue(), recordList.get(0).getValue());
results.close();
results = records.scan(null, "123");
Assert.assertFalse(results.hasNext());
results.close();
}
});
actualList.clear();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Scan scan = new Scan(null, null);
CloseableIterator<KeyValue<byte[], Record>> results = records.scan(scan);
while (results.hasNext()) {
actualList.add(results.next());
}
}
});
Assert.assertEquals(recordList.size(), actualList.size());
} finally {
dsFrameworkUtil.deleteInstance(RECORDS_ID);
}
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class ObjectStoreDatasetTest method testWithCustomClassLoader.
@Test
public void testWithCustomClassLoader() throws Exception {
DatasetId kv = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("kv");
// create a dummy class loader that records the name of the class it loaded
final AtomicReference<String> lastClassLoaded = new AtomicReference<>(null);
ClassLoader loader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
lastClassLoaded.set(name);
return super.loadClass(name);
}
};
dsFrameworkUtil.createInstance("keyValueTable", kv, DatasetProperties.EMPTY);
KeyValueTable kvTable = dsFrameworkUtil.getInstance(kv);
Type type = Custom.class;
TypeRepresentation typeRep = new TypeRepresentation(type);
Schema schema = new ReflectionSchemaGenerator().generate(type);
final ObjectStoreDataset<Custom> objectStore = new ObjectStoreDataset<>("kv", kvTable, typeRep, schema, loader);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(objectStore);
// need to call this to actually load the Custom class, because the Custom class is no longer used in the
// ObjectStoreDataset's constructor, but rather lazily when its actually needed.
objectStore.getRecordType();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
objectStore.write("dummy", new Custom(382, Lists.newArrayList("blah")));
}
});
// verify the class name was recorded (the dummy class loader was used).
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(Custom.class.getName(), lastClassLoaded.get());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
deleteAndVerify(objectStore, Bytes.toBytes("dummy"));
}
});
dsFrameworkUtil.deleteInstance(kv);
}
Aggregations