问题
In HBase, For providing single row transaction support it uses Row Locking Concept. Suppose, for example
Put p=new Put("/*Row Key*/");
This statement will lock the row.
so, until we complete the
table.put(p)
the lock won't gets released.
So, in between if i start a new put i.e
Put p1=new Put("/Row Key");
the p1 put should not work since the row has already been locked but in HBase 0.94 when i tried it's working.
Regarding Row Lock Link Where i had seen about Row Lock Is there any thing wrong in my understanding. If not how single row transaction support is working with out row locking.
回答1:
The way HBase works is that locks are held in the regionserver (not in the client) when the Puts are applied to make sure that rows are written in an atomic block but it does not provide snapshot isolation (you need to use something like omid if you want that).
Assuming your two puts are not sent from the same client (in which case they may be part of the same transaction) and unless HBase will crash or a timeout occur (say because of compaction) somewhere between the the puts. You're two puts will just be applied serially in the HBase depending on the order they arrived.
回答2:
I believe that the row is not locked until you issue the table.put(p) command. When you use Put p = new Put("row key"); you are creating an object to hold information about the write, but you are not actually communicating with the database yet. When you are ready to attempt a write to the database, you type table.put(p). If two put() operations happen on the same row at the same time, one of them may fail. But I don't think it is possible to hold a row lock for a very long time, because the locking happens inside HBase and cannot be controlled externally. So, the process looks like this:
- your code sends put to hbase
- hbase internally locks the row for you
- hbase writes the data
- hbase unlocks the row
This means, if two people try to send put()s to the same row, only one will succeed, and HBase will send an error message to the other client.
Suppose two clients try to put() the same row key at the same time.
- client 1 sends put
- client 2 sends put
- hbase locks row for client 1
- hbase sends an error to client 2 because the row is locked
- hbase writes client 1's data
- hbase unlocks the row
Following the ip address and login time example from the previous question, it means that if someone logs in twice at the same time, from different IP addresses, then only one IP will be written to the database, but the timestamp will match the IP written.
I may not understand this fully, but this is my current understanding. I hope it helps.
来源:https://stackoverflow.com/questions/14907762/row-locking-in-hbase-single-row-transaction-support