Пример #1
0
 @Override
 public void append(HLog.Entry entry) throws IOException {
   entry.setCompressionContext(compressionContext);
   try {
     this.writer.append(entry.getKey(), entry.getEdit());
   } catch (NullPointerException npe) {
     // Concurrent close...
     throw new IOException(npe);
   }
 }
  @Override
  public HLog.Entry next(HLog.Entry reuse) throws IOException {
    this.entryStart = this.reader.getPosition();
    boolean b = true;

    if (nextQueue.isEmpty()) { // Read the whole thing at once and fake reading
      while (b == true) {
        HLogKey key = HLog.newKey(conf);
        WALEdit val = new WALEdit();
        HLog.Entry e = new HLog.Entry(key, val);
        codec.setCompression(compressionContext);
        e.getEdit().setCodec(codec);
        if (compressionContext != null) {
          e.getKey().setCompressionContext(compressionContext);
        }
        b = this.reader.next(e.getKey(), e.getEdit());
        nextQueue.offer(e);
        numberOfFileEntries++;
      }
    }

    if (nextQueue.size() == this.numberOfFileEntries && getFailureType() == FailureType.BEGINNING) {
      throw this.addFileInfoToException(new IOException("fake Exception"));
    } else if (nextQueue.size() == this.numberOfFileEntries / 2
        && getFailureType() == FailureType.MIDDLE) {
      throw this.addFileInfoToException(new IOException("fake Exception"));
    } else if (nextQueue.size() == 1 && getFailureType() == FailureType.END) {
      throw this.addFileInfoToException(new IOException("fake Exception"));
    }

    if (nextQueue.peek() != null) {
      edit++;
    }

    Entry e = nextQueue.poll();

    if (e.getEdit().isEmpty()) {
      return null;
    }
    return e;
  }
  /**
   * 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();
      }
    }
  }