HLog的全部实现在包:
org.apache.hadoop.hbase.regionserver.wal 中
相关的配置为
参数名 | 默认值 | 含义 |
hbase.regionserver.hlog.enabled | true | 是否启用WAL |
hbase.regionserver.hlog.writer.impl | SequenceFileLogWriter | HLog.Writer实现类 |
hbase.regionserver.hlog.reader.impl | SequenceFileLogReader | HLog.Reader实现类 |
hbase.regionserver.hlog.keyclass | HLogKey | HLog.Entry的key实现类 |
hbase.regionserver.wal.enablecompression | false | 是否对日志压缩 |
io.file.buffer.size | 4096 | 读取文件的buffer大小 |
hbase.regionserver.hlog.replication | 1 | 复制类型 |
hbase.regionserver.hlog.blocksize | 32M | 文件系统块大小 |
hbase.regionserver.hlog.lowreplication.rolllimit | 5 | 若低于副本数,尝试几次 |
hbase.regionserver.hlog.tolerable.lowreplication | ?? | 未知 |
hbase.regionserver.maxlogs | 32 | 最大日志个数 |
hbase.regionserver.logroll.multiplier | 0.95 | 到HDFS块95%时回滚 |
hbase.regionserver.hlog.lowreplication.rolllimit
其中在Hlog.syncer方法中调用checkLowReplication方法用来判断是否hlog在hdfs上的副本数低于配置项,
若低于则requestLogRoll,最终调用logRollRequested方法,但是调用次数不超过默认5次
核心类HLog,负责创建读,写接口的实现,完成最终的写入数据,读数据等。
HLog.Reader HLog读接口,实现类:SequenceFileLogReader
HLog.Writer HLog写接口,实现类:SequenceFileLogWriter
HLog中包含了若干 HLog.Entry,Entry是一个K/V键值对
键为:HLogKey
值为:WALEdit
工具类:
HLogSplitter(./hbase hlog命令实现类)
Compressor(负责将input内容压缩到output中,或者input内容解压到output中)
WALActionsListener 日志的观察者监听类,当日志发生变化了可以触发观察者,比如replication功能就是实现了这个接口。
HLog是一个二进制格式的文件,文件开头二进制信息:
SEQ0org.apache.hadoop.hbase.regionserver.wal.HLogKey0org.apache.hadoop.hbase.regionserver.wal.WALEdit
一个HLog文件如下:
这里注明了key和value使用的分别是HLogKey和WALEdit两个类
HLog中包含若干Entry,HLog是一个顺序的文件结构,没有索引,所有的Entry都是顺序挨个读取的。
HLog格式如下图:
HLog$Entry包含一个key,value
key: HLogKey
value: WALEdit
Entry二进制内容如下:
1.总数据长度(hlogkey+waledit)
2.key长度
3.数据
HLogKey
1.变长int
2.编码后的region名字(若启用压缩则写入压缩后的内容)
3.表名(若启用压缩则写入压缩后的内容)
4.long长度的序列号
5.long长度的时间戳
6.1字节UUID标识(是否用默认的UUID标识)
7.UUID前8个字节
8.UUID后8个字节
WALEdit
1.int长度版本号
2.int长度KeyValue个数
3.遍历写入KeyValue(若启用压缩则写入压缩后的内容)
3-1.KeyValue长度(int)
3-2.KeyValue的二进制数据(4字节的key长,4字节的value长,2字节的rowkey长,key,1字节的family长,family,
qualify,8字节的timestampe,1字节的keytype,变成long的memstoreMVC
4.socpe个数(若scope为null则写入0)
5.遍历写入每个scope(int长度)
HLog读取,写入的列子
/**
* 读取HLog中的内容
* @throws IOException
*/
public void read()throws IOException {
FileSystem fs = FileSystem.get(cfg);
Path path = new Path("/test/hbase/hlog.1234567890");
HLog.Reader reader = HLog.getReader(fs, path, cfg);
HLog.Entry entry = reader.next();
HLogKey key = entry.getKey();
WALEdit edit = entry.getEdit();
System.out.println( Bytes.toString(key.getEncodedRegionName()) );
System.out.println( Bytes.toString(key.getTablename()) );
System.out.println( key.getLogSeqNum() );
System.out.println( key.getWriteTime() );
System.out.println( key.getClusterId().toString() );
List<KeyValue> list = edit.getKeyValues();
for(KeyValue kv:list) {
System.out.println(kv.toString());
}
while( (entry=reader.next()) !=null ) {
System.out.println(entry);
}
}
/**
* 将数据写入到HLog,用HLog.Writer直接写入数据
* @throws IOException
*/
public void write() throws IOException {
FileSystem fs = FileSystem.get(cfg);
Path path = new Path("/test/hbase/hlog.1234567890");
HLog.Writer writer = HLog.createWriter(fs, path, cfg);
byte[] encodedRegionName = Bytes.toBytes("myregion");
byte[] tablename = Bytes.toBytes("test");
long logSeqNum = 100;
long now = System.currentTimeMillis();
UUID clusterId = UUID.randomUUID();
HLogKey key1 = new HLogKey(encodedRegionName, tablename, logSeqNum, now, clusterId);
WALEdit edit1 = new WALEdit();
edit1.add(generator("key111111111111111111111111", "column", "a",System.currentTimeMillis(), new byte[] { '2' }));
HLog.Entry entry1 = new Entry(key1, edit1);
HLogKey key2 = new HLogKey(encodedRegionName, tablename, 101L, now, UUID.randomUUID());
WALEdit edit2 = new WALEdit();
edit2.add(generator("key333333333333333333333333", "column", "b",System.currentTimeMillis(), VALUES));
HLog.Entry entry2 = new Entry(key2, edit2);
writer.append(entry2);
writer.append(entry1);
writer.sync();
writer.close();
}
/**
* 使用HLog写入数据
* @throws IOException
*/
public void hlogWriter() throws IOException {
FileSystem fs = FileSystem.get(cfg);
String regionDir = "/test/hbase/hlog";
HLog log = new HLog(fs, new Path(regionDir, "logs"), new Path(regionDir, "oldlogs"), cfg);
byte[] tableName = Bytes.toBytes("test");
byte[] startKey = Bytes.toBytes("aaaaa1111111111");
byte[] endKey = Bytes.toBytes("zzzzz9999999999");
HRegionInfo info = new HRegionInfo(tableName, startKey, endKey);
WALEdit edit = new WALEdit();
edit.add(generator("key333333333333333333333333", "column", "a",System.currentTimeMillis(), new byte[] { '2' }));
edit.add(generator("key333333333333333333333333", "column", "b",System.currentTimeMillis(), VALUES));
UUID uuid = UUID.randomUUID();
long now = System.currentTimeMillis();
HColumnDescriptor family = new HColumnDescriptor("column");
HTableDescriptor descriptor = new HTableDescriptor();
descriptor.addFamily(family);
log.append(info, tableName, edit, uuid, now, descriptor);
log.sync();
log.hflush();
log.hsync();
log.close();
}
/**
* 生成KeyValue
* @param key
* @param column
* @param qualifier
* @param timestamp
* @param value
* @return
*/
public KeyValue generator(String key, String column, String qualifier,
long timestamp, byte[] value) {
byte[] keyBytes = Bytes.toBytes(key);
byte[] familyBytes = Bytes.toBytes(column);
byte[] qualifierBytes = Bytes.toBytes(qualifier);
Type type = Type.Put;
byte[] valueBytes = value;
KeyValue kv = new KeyValue(keyBytes, 0, keyBytes.length, familyBytes,
0, familyBytes.length, qualifierBytes, 0,
qualifierBytes.length, timestamp, type, valueBytes, 0,
valueBytes.length);
return kv;
}