/**
  * Verify the content of the WAL file. Verify that sequenceids are ascending and that the file has
  * expected number of edits.
  *
  * @param wal
  * @return Count of edits.
  * @throws IOException
  */
 private long verify(final Path wal, final boolean verbose) throws IOException {
   HLog.Reader reader = HLogFactory.createReader(wal.getFileSystem(getConf()), wal, getConf());
   long previousSeqid = -1;
   long count = 0;
   try {
     while (true) {
       Entry e = reader.next();
       if (e == null) {
         LOG.debug("Read count=" + count + " from " + wal);
         break;
       }
       count++;
       long seqid = e.getKey().getLogSeqNum();
       if (verbose) LOG.info("seqid=" + seqid);
       if (previousSeqid >= seqid) {
         throw new IllegalStateException(
             "wal=" + wal.getName() + ", previousSeqid=" + previousSeqid + ", seqid=" + seqid);
       }
       previousSeqid = seqid;
     }
   } finally {
     reader.close();
   }
   return count;
 }
  /**
   * Inserts three waledits in the wal file, and reads them back. The first edit is of a regular
   * table, second waledit is for the ROOT table (it will be ignored while reading), and last
   * waledit is for the hbase:meta table, which will be linked to the new system:meta table.
   *
   * @throws IOException
   */
  @Test
  public void testReadOldRootAndMetaEdits() throws IOException {
    LOG.debug("testReadOldRootAndMetaEdits");
    Configuration conf = HBaseConfiguration.create();
    conf.setClass(
        "hbase.regionserver.hlog.writer.impl", SequenceFileLogWriter.class, HLog.Writer.class);
    // kv list to be used for all WALEdits.
    byte[] row = Bytes.toBytes("row");
    KeyValue kv = new KeyValue(row, row, row, row);
    List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(kv);

    HLog.Writer writer = null;
    HLog.Reader reader = null;
    // a regular table
    TableName t = TableName.valueOf("t");
    HRegionInfo tRegionInfo = null;
    int logCount = 0;
    long timestamp = System.currentTimeMillis();
    Path path = new Path(dir, "t");
    try {
      tRegionInfo = new HRegionInfo(t, HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
      HLog.Entry tEntry =
          createAEntry(
              new HLogKey(
                  tRegionInfo.getEncodedNameAsBytes(),
                  t,
                  ++logCount,
                  timestamp,
                  HConstants.DEFAULT_CLUSTER_ID),
              kvs);

      // create a old root edit (-ROOT-).
      HLog.Entry rootEntry =
          createAEntry(
              new HLogKey(
                  Bytes.toBytes(TableName.OLD_ROOT_STR),
                  TableName.OLD_ROOT_TABLE_NAME,
                  ++logCount,
                  timestamp,
                  HConstants.DEFAULT_CLUSTER_ID),
              kvs);

      // create a old meta edit (hbase:meta).
      HLog.Entry oldMetaEntry =
          createAEntry(
              new HLogKey(
                  Bytes.toBytes(TableName.OLD_META_STR),
                  TableName.OLD_META_TABLE_NAME,
                  ++logCount,
                  timestamp,
                  HConstants.DEFAULT_CLUSTER_ID),
              kvs);

      // write above entries
      writer = HLogFactory.createWALWriter(fs, path, conf);
      writer.append(tEntry);
      writer.append(rootEntry);
      writer.append(oldMetaEntry);

      // sync/close the writer
      writer.sync();
      writer.close();

      // read the log and see things are okay.
      reader = HLogFactory.createReader(fs, path, conf);
      HLog.Entry entry = reader.next();
      assertNotNull(entry);
      assertTrue(entry.getKey().getTablename().equals(t));
      assertEquals(
          Bytes.toString(entry.getKey().getEncodedRegionName()),
          Bytes.toString(tRegionInfo.getEncodedNameAsBytes()));

      // read the ROOT waledit, but that will be ignored, and hbase:meta waledit will be read
      // instead.
      entry = reader.next();
      assertEquals(entry.getKey().getTablename(), TableName.META_TABLE_NAME);
      // should reach end of log
      assertNull(reader.next());
    } finally {
      if (writer != null) {
        writer.close();
      }
      if (reader != null) {
        reader.close();
      }
    }
  }