-
Notifications
You must be signed in to change notification settings - Fork 106
Basic Examples
This section guides the developer of Omid-based transactional applications over the different steps required to execute multi-row transactions on HBase and the different APIs involved in the process.
Click here to go directly to a complete example
In order to use transactions, a client application needs to create an instance of the TransactionManager
interface with is part of the Transactional Client described in the architecture (See section About Omid). The current Omid version offers an implementation of a transaction manager for HBase.
To create a transaction manager just add the following code to your application:
...
Configuration conf = HBaseConfiguration.create();
TransactionManager tm = HBaseTransactionManager.newBuilder()
.withConfiguration(conf)
.build();
...
If nothing is specified in the configuration parameters, the client will try to find a connection to the TSOServer
instance checking two different locations in this order:
- First it will look for a TSOServer instance published through ZooKeeper (ZK). Unless specific configuration is provided, it will look for a ZK instance running in
localhost:2181
and will try to find the host:port of theTSOServer
there. - If no ZK instance is found, then the client will check for the host and port address of the TSOServer instance passed as parameters in the configuration. By default those parameters are set to
localhost:1234
.
To set up specific configuration for locating the TSOServer
, please proceed like this:
...
Configuration conf = HBaseConfiguration.create();
// Configure the ZK cluster location to look for the TSOServer's host:port
conf.set("tso.zkcluster", "my_zk_instance_host:my_zk_instance_port");
// Configure directly the host:port of the TSOServer instance
conf.set("tso.host", "my_tso_host");
conf.setInt("tso.port", <my_tso_port>);
...
Once the TransactionManager
is created, client applications can use its interface to demarcate transactional boundaries.
In order to create a transaction the TransactionManager.begin()
method is used:
...
Transaction tx = tm.begin();
...
The transansaction manager will return an instance of the Transaction
interface representing the recently created transaction. This instance is necessary to instruct the operations on the data source, in which transactional context they should operate (See next section).
In order to perform transaction operations on data, the client application requires to use a wrapper on the HBase's HTableInterface
abstraction. The wrapper is called TTable
and is also part of what is described as Transactional Client in the Omid architecture (See section About Omid). TTable
basically offers the same interface as HTableInterface
enhanced with a parameter representing the transactional context. As was previously described, a Transaction
instance is obtained on return of TransactionManager.begin()
method calls.
...
TTable txTable = new TTable(conf, "EXAMPLE_TABLE");
...
Once the access point to the data has been created, applications can use it to trigger transactional operations:
private final byte[] family = Bytes.toBytes("EXAMPLE_CF");
private final byte[] qualifier = Bytes.toBytes("foo");
...
// Retrieve transactionally a specific cell
Get get = new Get(Bytes.toBytes("EXAMPLE_ROW");
get.add(family, qualifier);
Result txGetResult = txTable.get(tx, get);
...
// Add a new cell value inside a transactional context
Put updatedRow = new Put(Bytes.toBytes("EXAMPLE_ROW");
updatedRow.add(family, qualifier, Bytes.toBytes("Another_value"));
txTable.put(tx, updatedRow);
...
Once the client application has finished reading/writting from/into the datasource, it must decide whether to make the changes visible or to discard them. In order to do this, it must instruct the Omid TransactionManager
either to commit()
or to rollback()
the transactional context where the changes were produced. In case of commit, the TSO server will be notified to perform the SI validation phase. If the validation succeeds the changes will be visible to new transactions started from that point on. Otherwise, it will roll back the changes.
In order to commit the data, the client application should do something like this:
...
try {
...
tm.commit(tx);
} catch (RollbackException e) {
// Here the transaction was rolled-back when
// trying to commit due to conflicts with other
// some other concurrent transaction
//
// The client application should do whatever is
// required according to its business logic
...
}
A transaction can also be specifically aborted, for example if something goes wrong when executing the business logic:
...
try {
...
if( ! some_business_logic_condition ) {
tm.rollback(tx);
throw AnyApplicationException("Changes aborted due to...");
}
tm.commit(tx);
} catch (RollbackException e) {
...
}
This example summarizes the steps described above.
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import com.yahoo.omid.transaction.HBaseTransactionManager;
import com.yahoo.omid.transaction.TTable;
import com.yahoo.omid.transaction.Transaction;
import com.yahoo.omid.transaction.TransactionManager;
public class Example {
public static final byte[] exampleRow = Bytes.toBytes("EXAMPLE_ROW");
public static final byte[] family = Bytes.toBytes("EXAMPLE_CF");
public static final byte[] qualifier = Bytes.toBytes("foo");
public static final byte[] dataValueToAvoid = Bytes.toBytes("valToAvoid");
public static final byte[] dataValue = Bytes.toBytes("val");
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
try (TransactionManager tm = HBaseTransactionManager.newBuilder()
.withConfiguration(conf)
.build(),
TTable txTable = new TTable(conf, "EXAMPLE_TABLE")) {
Transaction tx = tm.begin();
System.out.printl("Transaction started");
// Retrieve data transactionally
Get get = new Get(exampleRow);
get.add(family, qualifier);
Result txGetResult = txTable.get(tx, get);
byte[] value = txGetResult.getValue(family, qualifier);
// Add a condition in the application logic to show explicit transaction
// aborts just for illustrative purposes
if (Bytes.equals(retrievedValue, dataValueToAvoid)) {
tm.rollback(tx);
throw new RuntimeException("Illegal value found in database!");
}
// Otherwise, add a value in other column and try to commit the transaction
try {
Put putOnRow = new Put(exampleRow);
putOnRow.add(family, qualifier, dataValue);
txTable.put(tx, putOnRow);
tm.commit(tx);
System.out.println("Transaction committed. New value written to example row");
} catch(RollbackException e) {
System.out.println("Transaction aborted due to conflicts. Changes to row aborted");
}
}
}
}
Omid
Copyright 2011-2015 Yahoo Inc. Licensed under the Apache License, Version 2.0