HBase 遇到的问题以及处理

背景

目前在hbase集群中发现了一些问题,主要是Region 一致性的问题,和RIT问题,根据目前遇到的问题整理了以下问题fix手册。 如果后面遇到新的问题可以再增加

Hbase hbck 处理Region一致性问题

Issue: Regions have the same start/end keys

Cause

Varies.

Resolution

手动合并重叠的区域。 在 HBase HMaster Web UI 表详情页面,选择有问题的表链接。 可以看到属于该表的每个区域的开始键/结束键。 然后合并重叠的区域。 在 HBase shell 中,执行 merge_region ‘xxxxxxxx’,‘yyyyyyy’, true。 例如:

RegionA, startkey:001, endkey:010,

RegionB, startkey:001, endkey:080,

RegionC, startkey:010, endkey:080.

在这种场景下,需要合并RegionA和RegionC,得到与RegionB相同key范围的RegionD,再合并RegionB和RegionD。 xxxxxxx 和 yyyyyy 是每个区域名称末尾的哈希字符串。 这里要小心不要合并两个不连续的区域。 每次合并后,如合并 A 和 C,HBase 将开始对 RegionD 进行压缩。 等待压缩完成,然后再与 RegionD 进行另一次合并。 您可以在 HBase HMaster UI 的区域服务器页面上找到压缩状态。

Issue: Region overlap

Cause

很有可能是在分裂过程中Region Server 重启,或split不彻底,导致重复上线,就会导致两个region有重叠的部分。

Resolution

这种场景下,和以上的处理方式相同,需要注意的是这Region进行Compact的时候不能合并成功,在hbase shell 进行合并后,可以观察HMaster的日志,如果最终可以合并成功,则不会出现error日志信息,如果遇到始终不能合并的Region,可以先尝试将此Region下线掉(unassign region),再次上线(assign region),再执行merge操作。例如:

merge_region 'regionA','regionB',true

在这个过程中有可能需要检查一些hfile文件其中的数据是否已经表中可以使用


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
import org.apache.hadoop.hbase.io.hfile.CacheConfig;
import org.apache.hadoop.hbase.io.hfile.HFile;
import org.apache.hadoop.hbase.io.hfile.HFileScanner;
import org.apache.hadoop.hbase.io.hfile.HFileWriterImpl;
import org.apache.hadoop.hbase.regionserver.HStoreFile;
import org.apache.hadoop.hbase.regionserver.TimeRangeTracker;
import org.apache.hadoop.hbase.util.BloomFilter;
import org.apache.hadoop.hbase.util.BloomFilterFactory;
import org.apache.hadoop.hbase.util.BloomFilterUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.DataInput;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import java.util.Optional;

/**
 * @author wxl
 */
@Component
public class ReadHFile {
    private static final Logger log = LoggerFactory.getLogger(ReadHFile.class);

    private final PrintStream out = System.out;
    private static final String FOUR_SPACES = "    ";

    @Value("${hbase.hfile.path:''}")
    private String hfile;

    public void run() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf("LG_DEVICE_DATA:ALL_DEVICE_DATA"));
        FileSystem fs = FileSystem.get(conf);

        Path path = new Path(hfile);
        FileStatus[] dirs = fs.listStatus(path);
        for (FileStatus hfile : dirs) {
            boolean hFileFormat = HFile.isHFileFormat(fs, hfile.getPath());
            if (!hFileFormat) {
                continue;
            }

            HFile.Reader reader = HFile.createReader(fs, hfile.getPath(), CacheConfig.DISABLED, true, conf);

            //打印meta 信息
            //Map<byte[], byte[]> fileInfo = reader.loadFileInfo();
            //printMeta(reader, fileInfo);
            Optional<byte[]> firstRowKey = reader.getFirstRowKey();
            Optional<byte[]> lastRowKey = reader.getLastRowKey();
            StringBuilder sb = new StringBuilder();
            if (firstRowKey.isPresent()) {
                isExists(table, firstRowKey.get(), false, hfile.getPath().toString());
            }
            if (lastRowKey.isPresent()) {
                isExists(table, lastRowKey.get(), false, hfile.getPath().toString());
            }
            reader.close();

        }

        fs.close();
        table.close();
        connection.close();
    }

    public void isExists(Table table, byte[] rowKey, boolean p, String path) throws IOException {
        Get get = new Get(rowKey);
        get.setFilter(new KeyOnlyFilter());
        Result result = table.get(new Get(rowKey));
        if (p) {
            log.info("result: [{}]", result);
        }
        if (result == null || result.isEmpty()) {
            log.info("rowKey not exists: [{}] filePath: [{}]", new String(rowKey), path);
        }
//        else {
//            byte[] row = result.getRow();
//            log.info("ok: " + new String(row));
//        }
    }

    private void check(Table table, HFileScanner scanner) throws IOException {
        scanner.seekTo();
        byte[] pRowKey = null;
        int count = 0;
        while (scanner.next()) {
            Cell cell = scanner.getKey();
            byte[] rowKey = CellUtil.cloneRow(cell);
            if (!Bytes.equals(pRowKey, rowKey)) {
                boolean p = false;
                ++count;
                if (count % 1000 == 0) {
                    log.info("current count: [{}]", count);
                    //  p = true;
                }
                isExists(table, rowKey, p, "");
            }
            pRowKey = rowKey;
        }
        log.info("count rowKey: [{}]", count);
    }

    private void printMeta(HFile.Reader reader, Map<byte[], byte[]> fileInfo)
            throws IOException {
        out.println("Block index size as per heapsize: "
                + reader.indexSize());
        out.println(asSeparateLines(reader.toString()));
        out.println("Trailer:\n    "
                + asSeparateLines(reader.getTrailer().toString()));
        out.println("Fileinfo:");
        for (Map.Entry<byte[], byte[]> e : fileInfo.entrySet()) {
            out.print(FOUR_SPACES + Bytes.toString(e.getKey()) + " = ");
            if (Bytes.equals(e.getKey(), HStoreFile.MAX_SEQ_ID_KEY)
                    || Bytes.equals(e.getKey(), HStoreFile.DELETE_FAMILY_COUNT)
                    || Bytes.equals(e.getKey(), HStoreFile.EARLIEST_PUT_TS)
                    || Bytes.equals(e.getKey(), HFileWriterImpl.MAX_MEMSTORE_TS_KEY)
                    || Bytes.equals(e.getKey(), Bytes.toBytes("hfile.CREATE_TIME_TS"))
                    || Bytes.equals(e.getKey(), HStoreFile.BULKLOAD_TIME_KEY)) {
                out.println(Bytes.toLong(e.getValue()));
            } else if (Bytes.equals(e.getKey(), HStoreFile.TIMERANGE_KEY)) {
                TimeRangeTracker timeRangeTracker = TimeRangeTracker.parseFrom(e.getValue());
                out.println(timeRangeTracker.getMin() + "...." + timeRangeTracker.getMax());
            } else if (Bytes.equals(e.getKey(), Bytes.toBytes("hfile.AVG_KEY_LEN"))
                    || Bytes.equals(e.getKey(), Bytes.toBytes("hfile.AVG_VALUE_LEN"))
                    || Bytes.equals(e.getKey(), HFileWriterImpl.KEY_VALUE_VERSION)
                    || Bytes.equals(e.getKey(), HFile.FileInfo.MAX_TAGS_LEN)) {
                out.println(Bytes.toInt(e.getValue()));
            } else if (Bytes.equals(e.getKey(), HStoreFile.MAJOR_COMPACTION_KEY)
                    || Bytes.equals(e.getKey(), Bytes.toBytes("hfile.TAGS_COMPRESSED"))
                    || Bytes.equals(e.getKey(), HStoreFile.EXCLUDE_FROM_MINOR_COMPACTION_KEY)) {
                out.println(Bytes.toBoolean(e.getValue()));
            } else if (Bytes.equals(e.getKey(), Bytes.toBytes("hfile.LASTKEY"))) {
                out.println(new KeyValue.KeyOnlyKeyValue(e.getValue()).toString());
            } else {
                out.println(Bytes.toStringBinary(e.getValue()));
            }
        }

        try {
            out.println("Mid-key: " + reader.midKey().map(CellUtil::getCellKeyAsString));
        } catch (Exception e) {
            out.println("Unable to retrieve the midkey");
        }

        // Printing general bloom information
        DataInput bloomMeta = reader.getGeneralBloomFilterMetadata();
        BloomFilter bloomFilter = null;
        if (bloomMeta != null) {
            bloomFilter = BloomFilterFactory.createFromMeta(bloomMeta, reader);
        }

        out.println("Bloom filter:");
        if (bloomFilter != null) {
            out.println(FOUR_SPACES + bloomFilter.toString().replaceAll(
                    BloomFilterUtil.STATS_RECORD_SEP, "\n" + FOUR_SPACES));
        } else {
            out.println(FOUR_SPACES + "Not present");
        }

        // Printing delete bloom information
        bloomMeta = reader.getDeleteBloomFilterMetadata();
        bloomFilter = null;
        if (bloomMeta != null) {
            bloomFilter = BloomFilterFactory.createFromMeta(bloomMeta, reader);
        }

        out.println("Delete Family Bloom filter:");
        if (bloomFilter != null) {
            out.println(FOUR_SPACES
                    + bloomFilter.toString().replaceAll(BloomFilterUtil.STATS_RECORD_SEP,
                    "\n" + FOUR_SPACES));
        } else {
            out.println(FOUR_SPACES + "Not present");
        }
    }

    /**
     * Format a string of the form "k1=v1, k2=v2, ..." into separate lines
     * with a four-space indentation.
     */
    private static String asSeparateLines(String keyValueStr) {
        return keyValueStr.replaceAll(", ([a-zA-Z]+=)",
                ",\n" + FOUR_SPACES + "$1");
    }
}

Issue: ERROR: Found lingering reference file xxxx

Cause

这很可能是由于 RegionServer 崩溃或 VM 重启时region 没有删除完全导致的。

Resolution

hbase hbck -fixReferenceFiles month_hotstatic

检测hbase 的Region是否健康

使用hbase hbck 命令检测,如果发现>=1个问题检测到,则表示Region出现了问题

0 inconsistencies detected.
Status: OK

特别注意

  • 以上问题处理完后需要重启Phoenix Query Server 因其内部有缓存,可能会导致查询问题
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值