Group Commit of Editlog and FileChannel Log

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.