In the previous phase, you can see the editlog section in Hadoop source code. This is the same as the logfile section in flume file-channel: Groupcommit. in fact, the earliest contact with this concept is to write redo log in MySQL Note: binlog group commit is supported in mariadb/procona, or mysqlsync_binlog = 0; detailed steps: http://kristiannielsen.livejournal.com/12254.html );
Let's take a look at how Hadoop is handled:
Editlog can be written concurrently by multiple threads. Each thread maintains its latest transaction ID:
PrivateStaticFinalThreadLocal <TransactionId>MyTransactionId=NewThreadLocal <TransactionId> (){
ProtectedSynchronizedTransactionId initialValue (){
ReturnNewTransactionId (Long.MAX_VALUE);
}
};
At the time of submission, obtain the latest transaction ID at the time of submission:
Synchronized(This){
TransactionId id =MyTransactionId. Get ();
Id. txid = txid;
}
Then start synchronization (code deleted ):
// Get your transaction ID
LongMytxid =MyTransactionId. Get (). txid;
BooleanSync =False;
Try{
EditLogOutputStream logStream =Null;
Synchronized(This){
Try{
// If your transaction is not synchronized, but the synchronization is being processed by other threads, it will be blocked.
While(Mytxid> synctxid & isSyncRunning ){
Try{
Wait (1000 );
}Catch(InterruptedException ie ){}
}
// When the wake-up or timeout finds that the transaction has been committed by the group, the system returns
If(Mytxid <= synctxid ){
Return;
}
// Otherwise, start sync.
IsSyncRunning =True;
Sync =True;
// Hadoop's editlog uses double buffer to achieve refresh and write without blocking. Here, the buffer is replaced.
EditLogStream. setReadyToFlush ();
LogStream = editLogStream;
Try{
If(LogStream! =Null){
LogStream. flush ();
}
}Catch(IOException ex ){}
}Finally{
Synchronized(This){
If(Sync ){
IsSyncRunning =False;
}
// Refresh complete, wake up the blocked thread
This. Policyall ();
}
}
The group commit method in the Flume File-channel is similar, but it is more concise:
There are two phases in the same way. Each phase is a synchronization method, and the transactionid and position of Flume are separated. You only need to synchronize the end position of the file each time:
Commit ();
Sync ();
// Update the last submission position when submitting
SynchronizedVoidCommit (ByteBuffer buffer)ThrowsIOException {
Write (buffer );
LastCommitPosition = position ();
}
// If it has already been synchronized, nothing will be done.
SynchronizedVoidSync ()ThrowsIOException {
If(LastSyncPosition <lastCommitPosition ){
GetFileChannel (). force (False);
LastSyncPosition = position ();
SyncCount ++;
}
}
This article is from "MIKE's old blog" blog, please be sure to keep this source http://boylook.blog.51cto.com/7934327/1300543