use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class UsageDatasetTest method testAllMappings.
@Test
public void testAllMappings() throws Exception {
final UsageDataset usageDataset = getUsageDataset("testAllMappings");
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) usageDataset);
// Add mappings
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
usageDataset.register(flow11, datasetInstance1);
usageDataset.register(service21, datasetInstance3);
usageDataset.register(flow12, stream1);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// Verify app mappings
Assert.assertEquals(ImmutableSet.of(datasetInstance1), usageDataset.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance3), usageDataset.getDatasets(service21));
Assert.assertEquals(ImmutableSet.of(stream1), usageDataset.getStreams(flow12));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow22));
// Verify dataset/stream mappings
Assert.assertEquals(ImmutableSet.of(flow11), usageDataset.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.of(flow12), usageDataset.getPrograms(stream1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream2));
}
});
// --------- Delete app1 -----------
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
usageDataset.unregister(flow11.getParent());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// Verify app mappings
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance3), usageDataset.getDatasets(service21));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow12));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow22));
// Verify dataset/stream mappings
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream2));
// Verify app mappings
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance3), usageDataset.getDatasets(service21));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow12));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow22));
// Verify dataset/stream mappings
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream2));
// Verify app mappings
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance3), usageDataset.getDatasets(service21));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow12));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow22));
// Verify dataset/stream mappings
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream2));
}
});
// --------- Delete app2 -----------
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
usageDataset.unregister(flow21.getParent());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// Verify app mappings
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(flow12));
Assert.assertEquals(ImmutableSet.<DatasetId>of(), usageDataset.getDatasets(service21));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow12));
Assert.assertEquals(ImmutableSet.<StreamId>of(), usageDataset.getStreams(flow22));
// Verify dataset/stream mappings
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream1));
Assert.assertEquals(ImmutableSet.<ProgramId>of(), usageDataset.getPrograms(stream2));
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class UsageRegistryTest method testUsageRegistry.
@Test
public void testUsageRegistry() {
// instantiate a usage registry
UsageRegistry registry = new DefaultUsageRegistry(new TransactionExecutorFactory() {
@Override
public TransactionExecutor createExecutor(Iterable<TransactionAware> iterable) {
return dsFrameworkUtil.newInMemoryTransactionExecutor(iterable);
}
}, new ForwardingDatasetFramework(dsFrameworkUtil.getFramework()) {
@Nullable
@Override
public <T extends Dataset> T getDataset(DatasetId datasetInstanceId, Map<String, String> arguments, @Nullable ClassLoader classLoader) throws DatasetManagementException, IOException {
T t = super.getDataset(datasetInstanceId, arguments, classLoader);
if (t instanceof UsageDataset) {
@SuppressWarnings("unchecked") T t1 = (T) new WrappedUsageDataset((UsageDataset) t);
return t1;
}
return t;
}
});
// register usage for a stream and a dataset for single and multiple "owners", including a non-program
registry.register(flow11, datasetInstance1);
registry.register(flow12, stream1);
registry.registerAll(ImmutableList.of(flow21, flow22), datasetInstance2);
registry.registerAll(ImmutableList.of(flow21, flow22), stream1);
int count = WrappedUsageDataset.registerCount;
// validate usage
Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
// register datasets again
registry.register(flow11, datasetInstance1);
registry.registerAll(ImmutableList.of(flow21, flow22), datasetInstance2);
// validate that this does re-register previous usages (DefaultUsageRegistry no longer avoids re-registration)
count += 3;
Assert.assertEquals(count, WrappedUsageDataset.registerCount);
// validate usage
Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
// unregister app
registry.unregister(flow11.getParent());
// validate usage for that app is gone
Assert.assertEquals(ImmutableSet.of(), registry.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.of(), registry.getStreams(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
Assert.assertEquals(ImmutableSet.of(), registry.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(stream1));
// register application 1 again
registry.register(flow11, datasetInstance1);
registry.register(flow12, stream1);
// validate it was re-registered
Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
// validate that this actually re-registered previous usages (through code in wrapped usage dataset)
Assert.assertEquals(count + 2, WrappedUsageDataset.registerCount);
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class LineageDatasetTest method testMultipleRelations.
@Test
public void testMultipleRelations() throws Exception {
final LineageDataset lineageDataset = getLineageDataset("testMultipleRelations");
Assert.assertNotNull(lineageDataset);
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) lineageDataset);
final RunId runId1 = RunIds.generate(10000);
final RunId runId2 = RunIds.generate(20000);
final RunId runId3 = RunIds.generate(30000);
final RunId runId4 = RunIds.generate(40000);
final DatasetId datasetInstance1 = NamespaceId.DEFAULT.dataset("dataset1");
final DatasetId datasetInstance2 = NamespaceId.DEFAULT.dataset("dataset2");
final StreamId stream1 = NamespaceId.DEFAULT.stream("stream1");
final StreamId stream2 = NamespaceId.DEFAULT.stream("stream2");
final ProgramId program1 = NamespaceId.DEFAULT.app("app1").flow("flow1");
final FlowletId flowlet1 = program1.flowlet("flowlet1");
final ProgramId program2 = NamespaceId.DEFAULT.app("app2").worker("worker2");
final ProgramId program3 = NamespaceId.DEFAULT.app("app3").service("service3");
final ProgramRunId run11 = program1.run(runId1.getId());
final ProgramRunId run22 = program2.run(runId2.getId());
final ProgramRunId run23 = program2.run(runId3.getId());
final ProgramRunId run34 = program3.run(runId4.getId());
final long now = System.currentTimeMillis();
final long run11Data1AccessTime = now;
final long run22Data2AccessTime = now + 1;
final long run22Stream1AccessTime = now + 2;
final long run23Stream2AccessTime = now + 1;
final long run23Data2AccessTime = now + 3;
//noinspection UnnecessaryLocalVariable
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
lineageDataset.addAccess(run11, datasetInstance1, AccessType.READ, run11Data1AccessTime, flowlet1);
lineageDataset.addAccess(run22, datasetInstance2, AccessType.WRITE, run22Data2AccessTime);
lineageDataset.addAccess(run22, stream1, AccessType.READ, run22Stream1AccessTime);
lineageDataset.addAccess(run23, stream2, AccessType.READ, run23Stream2AccessTime);
lineageDataset.addAccess(run23, datasetInstance2, AccessType.WRITE, run23Data2AccessTime);
lineageDataset.addAccess(run34, datasetInstance2, AccessType.READ_WRITE, System.currentTimeMillis());
lineageDataset.addAccess(run34, stream2, AccessType.UNKNOWN, System.currentTimeMillis());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(ImmutableSet.of(new Relation(datasetInstance1, program1, AccessType.READ, runId1, ImmutableSet.of(flowlet1))), lineageDataset.getRelations(datasetInstance1, 0, 100000, Predicates.<Relation>alwaysTrue()));
Assert.assertEquals(ImmutableSet.of(new Relation(datasetInstance2, program2, AccessType.WRITE, runId2), new Relation(datasetInstance2, program2, AccessType.WRITE, runId3), new Relation(datasetInstance2, program3, AccessType.READ_WRITE, runId4)), lineageDataset.getRelations(datasetInstance2, 0, 100000, Predicates.<Relation>alwaysTrue()));
Assert.assertEquals(ImmutableSet.of(new Relation(stream1, program2, AccessType.READ, runId2)), lineageDataset.getRelations(stream1, 0, 100000, Predicates.<Relation>alwaysTrue()));
Assert.assertEquals(ImmutableSet.of(new Relation(stream2, program2, AccessType.READ, runId3), new Relation(stream2, program3, AccessType.UNKNOWN, runId4)), lineageDataset.getRelations(stream2, 0, 100000, Predicates.<Relation>alwaysTrue()));
Assert.assertEquals(ImmutableSet.of(new Relation(datasetInstance2, program2, AccessType.WRITE, runId2), new Relation(stream1, program2, AccessType.READ, runId2), new Relation(datasetInstance2, program2, AccessType.WRITE, runId3), new Relation(stream2, program2, AccessType.READ, runId3)), lineageDataset.getRelations(program2, 0, 100000, Predicates.<Relation>alwaysTrue()));
// Reduced time range
Assert.assertEquals(ImmutableSet.of(new Relation(datasetInstance2, program2, AccessType.WRITE, runId2), new Relation(datasetInstance2, program2, AccessType.WRITE, runId3)), lineageDataset.getRelations(datasetInstance2, 0, 35000, Predicates.<Relation>alwaysTrue()));
Assert.assertEquals(toSet(program1, datasetInstance1), lineageDataset.getEntitiesForRun(run11));
Assert.assertEquals(ImmutableList.of(run11Data1AccessTime), lineageDataset.getAccessTimesForRun(run11));
Assert.assertEquals(toSet(program2, datasetInstance2, stream1), lineageDataset.getEntitiesForRun(run22));
Assert.assertEquals(ImmutableList.of(run22Data2AccessTime, run22Stream1AccessTime), lineageDataset.getAccessTimesForRun(run22));
Assert.assertEquals(toSet(program2, datasetInstance2, stream2), lineageDataset.getEntitiesForRun(run23));
Assert.assertEquals(ImmutableList.of(run23Data2AccessTime, run23Stream2AccessTime), lineageDataset.getAccessTimesForRun(run23));
Assert.assertEquals(toSet(program3, datasetInstance2, stream2), lineageDataset.getEntitiesForRun(run34));
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class HBaseTableTest method testColumnFamily.
@Test
public void testColumnFamily() throws Exception {
DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
String tableName = "testcf";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
final BufferingTable table = getTable(CONTEXT1, tableName, props);
TransactionSystemClient txClient = new DetachedTxSystemClient();
TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(new Put("row", "column", "testValue"));
}
});
final BufferingTable table2 = getTable(CONTEXT1, tableName, props);
executor = new DefaultTransactionExecutor(txClient, table2);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
}
});
// Verify the column family name
TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
Assert.assertNotNull(hcd);
Assert.assertEquals("t", hcd.getNameAsString());
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class TransactionServiceTest method testHA.
@Test(timeout = 30000)
public void testHA() throws Exception {
// NOTE: we play with blocking/nonblocking a lot below
// as until we integrate with "leader election" stuff, service blocks on start if it is not a leader
// TODO: fix this by integration with generic leader election stuff
CConfiguration cConf = CConfiguration.create();
// tests should use the current user for HDFS
cConf.set(Constants.CFG_HDFS_USER, System.getProperty("user.name"));
cConf.set(Constants.Zookeeper.QUORUM, zkServer.getConnectionStr());
cConf.set(Constants.CFG_LOCAL_DATA_DIR, tmpFolder.newFolder().getAbsolutePath());
Injector injector = Guice.createInjector(new ConfigModule(cConf), new ZKClientModule(), new NonCustomLocationUnitTestModule().getModule(), new DiscoveryRuntimeModule().getDistributedModules(), new TransactionMetricsModule(), new AbstractModule() {
@Override
protected void configure() {
bind(NamespaceQueryAdmin.class).to(SimpleNamespaceQueryAdmin.class);
bind(UGIProvider.class).to(UnsupportedUGIProvider.class);
bind(OwnerAdmin.class).to(DefaultOwnerAdmin.class);
}
}, new DataFabricModules().getDistributedModules(), Modules.override(new DataSetsModules().getDistributedModules()).with(new AbstractModule() {
@Override
protected void configure() {
bind(MetadataStore.class).to(NoOpMetadataStore.class);
}
}), new AuthorizationTestModule(), new AuthorizationEnforcementModule().getInMemoryModules(), new AuthenticationContextModules().getNoOpModule());
ZKClientService zkClient = injector.getInstance(ZKClientService.class);
zkClient.startAndWait();
try {
final Table table = createTable("myTable");
// tx service client
// NOTE: we can init it earlier than we start services, it should pick them up when they are available
TransactionSystemClient txClient = injector.getInstance(TransactionSystemClient.class);
TransactionExecutor txExecutor = new DefaultTransactionExecutor(txClient, ImmutableList.of((TransactionAware) table));
// starting tx service, tx client can pick it up
TransactionService first = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
first.startAndWait();
Assert.assertNotNull(txClient.startShort());
verifyGetAndPut(table, txExecutor, null, "val1");
// starting another tx service should not hurt
TransactionService second = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
// NOTE: we don't have to wait for start as client should pick it up anyways, but we do wait to ensure
// the case with two active is handled well
second.startAndWait();
// wait for affect a bit
TimeUnit.SECONDS.sleep(1);
Assert.assertNotNull(txClient.startShort());
verifyGetAndPut(table, txExecutor, "val1", "val2");
// shutting down the first one is fine: we have another one to pick up the leader role
first.stopAndWait();
Assert.assertNotNull(txClient.startShort());
verifyGetAndPut(table, txExecutor, "val2", "val3");
// doing same trick again to failover to the third one
TransactionService third = createTxService(zkServer.getConnectionStr(), Networks.getRandomPort(), hConf, tmpFolder.newFolder());
// NOTE: we don't have to wait for start as client should pick it up anyways
third.start();
// stopping second one
second.stopAndWait();
Assert.assertNotNull(txClient.startShort());
verifyGetAndPut(table, txExecutor, "val3", "val4");
// releasing resources
third.stop();
} finally {
try {
dropTable("myTable");
} finally {
zkClient.stopAndWait();
}
}
}
Aggregations