Knowing ROCKETMQ knows that it will save all the messages to the local file. This file is mappedfile, each file corresponds to a mappedfile. The size bit is 1g by default.
The Mapedfilesizecommitlog settings in Messagestoreconfig, of course, are set by configuration files in half of the cases. File path strength is also in this configuration class.
The file name format is a 20-bit number, generated in this class, similar to this (00000001000000000000 00100000000000000000 09000000000020000000):
Mappedfile is managed by Mappedfilequeue, the following is the method used to generate the file:
/*** Gets the last mappedfile, if it does not exist or if the file is full, create it. * @paramStartoffset *@paramNeedcreate *@return */ PublicMapedfile Getlastmapedfile (Final LongStartoffset,Booleanneedcreate) { //This offeset is the global offset of all mappedfile LongCreateoffset =-1;//create file start offset. -1, do not createMapedfile Mapedfilelast =NULL; { This. Readwritelock.readlock (). Lock (); if( This. Mapedfiles.isempty ()) {//A mapping file does not existCreateoffset = startoffset-(startoffset% This. mapedfilesize); } Else { //gets the last list<mapedfile> mapedfiles = new arraylist<mapedfile> () if a Mappedfile object existsMapedfilelast = This. Mapedfiles.get ( This. Mapedfiles.size ()-1); } This. Readwritelock.readlock (). Unlock (); } if(Mapedfilelast! =NULL&& Mapedfilelast.isfull ()) {//The last file is full//through here you can know that this offset is the offse of all files, not a mappedfile offsetCreateoffset = Mapedfilelast.getfilefromoffset () + This. mapedfilesize; } //Create a file if(Createoffset! =-1 &&needcreate) { //calculates the file name. From here we can learn that Mappedfile's file naming rules://00000001000000000000 00100000000000000000 9.,000,000,000,02e,+20 bit//Filename[n] = filename[n-1] + N * mappedfilesize filename[0] = startoffset-(startoffset% this.mappedfilesize)
String Nextfilepath = This. Storepath + File.separator +Utilall.offset2filename (Createoffset); Log.info ("Shang ' s log >>> create Mappedfile nextfilepath:{}", Nextfilepath); String Nextnextfilepath= This. Storepath +File.separator+ utilall.offset2filename (Createoffset + This. mapedfilesize); //the next file address are calculated, also, on the new time can be added directly to a mapedfilesize to calculate, is 1g. 1024*1024*1024Log.info ("Shang ' s log >>> nextnextfilepath:{}", Nextnextfilepath); Mapedfile Mapedfile=NULL; //two ways to create files if( This. allocatemapedfileservice! =NULL) {Mapedfile= This. Allocatemapedfileservice.putrequestandreturnmapedfile (Nextfilepath, Nextnextfilepath, This. mapedfilesize); } Else { Try{ mapedfile = new Mapedfile (Nextfilepath, this . mapedfilesize); } Catch(IOException e) {log.error ("Create Mapedfile Exception", E); } } if(Mapedfile! =NULL) { This. Readwritelock.writelock (). Lock (); if( This. Mapedfiles.isempty ()) {Mapedfile.setfirstcreateinqueue (true); } This. Mapedfiles.add (Mapedfile); This. Readwritelock.writelock (). Unlock (); } returnMapedfile; } returnMapedfilelast; }
There are two ways to generate Mappedfile files, the first of which can refer to this article: http://www.cnblogs.com/guazi/p/6850988.html
Eventually, the files are generated in the same way as the yellow code. Let's take a look at how the Mappedfile is constructed:
//Create a Mappedfile file PublicMapedfile (FinalString FileName,Final intFileSize)throwsIOException { This. FileName =FileName; This. fileSize =fileSize; This. File =NewFile (fileName); This. Filefromoffset = Long.parselong ( This. File.getname ()); BooleanOK =false; Ensuredirok ( This. File.getparent ()); Try { This. FileChannel =NewRandomaccessfile ( This. File, "RW"). Getchannel (); This. Mappedbytebuffer = This. Filechannel.map (mapmode.read_write, 0, fileSize); Totalmapedvitualmemory.addandget (fileSize); Totalmapedfiles.incrementandget (); OK=true; } Catch(FileNotFoundException e) {log.error ("Create file Channel" + This. FileName + "Failed.", E); Throwe; } Catch(IOException e) {log.error ("Map File" + This. FileName + "Failed.", E); Throwe; } finally { if(!ok && This. filechannel! =NULL) { This. Filechannel.close (); } } }
rocketmq-creating Mappedfile Local Files