Example #1
0
  /**
   * 写入事务日志
   *
   * @param tlog
   * @return
   * @throws BKException
   * @throws InterruptedException
   */
  public SyncCounter writeLog(TransactionLogRecord tlog) throws InterruptedException {
    int recordSize = tlog.calculateTotalRecordSize();
    long futureFilePosition = handle.getLength() + recordSize;
    if (futureFilePosition >= conf.getBtmConf().getMaxLogSizeInMb() * 1024 * 1024) {
      if (log.isDebugEnabled())
        log.debug(
            "log file is full (size would be: "
                + futureFilePosition
                + ", max allowed: "
                + conf.getBtmConf().getMaxLogSizeInMb()
                + "Mbs");
      return null;
    }

    ByteBuffer buf = ByteBuffer.allocate(recordSize);
    buf.putInt(tlog.getStatus());
    buf.putInt(tlog.getRecordLength());
    buf.putInt(tlog.getHeaderLength());
    buf.putLong(tlog.getTime());
    buf.putInt(tlog.getSequenceNumber());
    buf.putInt(tlog.getCrc32());
    buf.put((byte) tlog.getGtrid().getArray().length);
    buf.put(tlog.getGtrid().getArray());
    Set<String> uniqueNames = tlog.getUniqueNames();
    buf.putInt(uniqueNames.size());
    for (String uniqueName : uniqueNames) {
      buf.putShort((short) uniqueName.length());
      buf.put(uniqueName.getBytes());
    }
    buf.putInt(tlog.getEndRecord());

    byte[] data = buf.array();
    SyncCounter counter = new SyncCounter();
    counter.inc();
    handle.asyncAddEntry(data, new SyncAddCallback(), counter);
    lastCounter.set(counter);
    return counter;
  }