use of org.apache.tephra.DefaultTransactionExecutor in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testCompositeDataset.
private void testCompositeDataset(DatasetFramework framework) throws Exception {
// Doing some admin and data ops
DatasetAdmin admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final KeyValueTable table = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put("key1", "value1");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", table.get("key1"));
}
});
admin.truncate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(null, table.get("key1"));
}
});
}
use of org.apache.tephra.DefaultTransactionExecutor in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testSimpleDataset.
@Test
public void testSimpleDataset() throws Exception {
// Configuring Dataset types
DatasetFramework framework = getFramework();
// system namespace has a module orderedTable-inMemory
Assert.assertTrue(framework.hasSystemType("table"));
// myspace namespace has no modules
Assert.assertFalse(framework.hasType(IN_MEMORY_TYPE));
Assert.assertFalse(framework.hasType(SIMPLE_KV_TYPE));
// add module to namespace 'myspace'
framework.addModule(KEY_VALUE, new SingleTypeModule(SimpleKVTable.class));
// make sure it got added to 'myspace'
Assert.assertTrue(framework.hasType(SIMPLE_KV_TYPE));
// but not to 'system'
Assert.assertFalse(framework.hasSystemType(SimpleKVTable.class.getName()));
Assert.assertFalse(framework.hasInstance(MY_TABLE));
// Creating instance using a type from own namespace
framework.addInstance(SimpleKVTable.class.getName(), MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
// and not to the system namespace
Assert.assertFalse(framework.hasInstance(NamespaceId.SYSTEM.dataset("my_table")));
// Doing some admin and data ops
DatasetAdmin admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final SimpleKVTable kvTable = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(kvTable);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) kvTable);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
kvTable.put("key1", "value1");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", kvTable.get("key1"));
}
});
admin.truncate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(kvTable.get("key1") == null);
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
framework.deleteModule(KEY_VALUE);
// recreate instance without adding a module in 'myspace'. This should use types from default namespace
framework.addInstance("table", MY_TABLE, DatasetProperties.EMPTY);
// verify it got added to the right namespace
Assert.assertTrue(framework.hasInstance(MY_TABLE));
admin = framework.getAdmin(MY_TABLE, null);
Assert.assertNotNull(admin);
final Table table = framework.getDataset(MY_TABLE, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(Bytes.toBytes("key1"), Bytes.toBytes("column1"), Bytes.toBytes("value1"));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("value1", Bytes.toString(table.get(Bytes.toBytes("key1"), Bytes.toBytes("column1"))));
}
});
// cleanup
framework.deleteInstance(MY_TABLE);
}
use of org.apache.tephra.DefaultTransactionExecutor in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testNamespaceInstanceIsolation.
@Test
@SuppressWarnings("ConstantConditions")
public void testNamespaceInstanceIsolation() throws Exception {
DatasetFramework framework = getFramework();
// create 2 namespaces
NamespaceId namespace1 = new NamespaceId("ns1");
NamespaceId namespace2 = new NamespaceId("ns2");
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace1).build());
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace2).build());
namespacedLocationFactory.get(namespace1).mkdirs();
namespacedLocationFactory.get(namespace2).mkdirs();
// create 2 tables, one in each namespace. both tables have the same name.
DatasetId table1ID = namespace1.dataset("table");
DatasetId table2ID = namespace2.dataset("table");
// have slightly different properties so that we can distinguish between them
framework.addInstance(Table.class.getName(), table1ID, DatasetProperties.builder().add("tag", "table1").build());
framework.addInstance(Table.class.getName(), table2ID, DatasetProperties.builder().add("tag", "table2").build());
// perform some data operations to make sure they are not the same underlying table
final Table table1 = framework.getDataset(table1ID, Maps.<String, String>newHashMap(), null);
final Table table2 = framework.getDataset(table2ID, Maps.<String, String>newHashMap(), null);
TransactionExecutor txnl = new DefaultTransactionExecutor(new MinimalTxSystemClient(), (TransactionAware) table1, (TransactionAware) table2);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table1.put(Bytes.toBytes("rowkey"), Bytes.toBytes("column"), Bytes.toBytes("val1"));
table2.put(Bytes.toBytes("rowkey"), Bytes.toBytes("column"), Bytes.toBytes("val2"));
}
});
// check data is different, which means they are different underlying tables
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("val1", Bytes.toString(table1.get(Bytes.toBytes("rowkey"), Bytes.toBytes("column"))));
Assert.assertEquals("val2", Bytes.toString(table2.get(Bytes.toBytes("rowkey"), Bytes.toBytes("column"))));
}
});
// check get all in a namespace only includes those in that namespace
Collection<DatasetSpecificationSummary> specs = framework.getInstances(namespace1);
Assert.assertEquals(1, specs.size());
Assert.assertEquals("table1", specs.iterator().next().getProperties().get("tag"));
specs = framework.getInstances(namespace2);
Assert.assertEquals(1, specs.size());
Assert.assertEquals("table2", specs.iterator().next().getProperties().get("tag"));
// delete one instance and make sure the other still exists
framework.deleteInstance(table1ID);
Assert.assertFalse(framework.hasInstance(table1ID));
Assert.assertTrue(framework.hasInstance(table2ID));
// delete all instances in one namespace and make sure the other still exists
framework.addInstance(Table.class.getName(), table1ID, DatasetProperties.EMPTY);
framework.deleteAllInstances(namespace1);
Assert.assertTrue(framework.hasInstance(table2ID));
// delete one namespace and make sure the other still exists
namespacedLocationFactory.get(namespace1).delete(true);
Assert.assertTrue(framework.hasInstance(table2ID));
}
use of org.apache.tephra.DefaultTransactionExecutor 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