Beispiel #1
0
  @Override
  protected void configure() throws JournalException {
    writeLock = LockManager.lockExclusive(getLocation());
    if (writeLock == null || !writeLock.isValid()) {
      close();
      throw new JournalException("Journal is already open for APPEND at %s", getLocation());
    }
    if (txLog.isEmpty()) {
      commit(Tx.TX_NORMAL, 0L, 0L);
    }
    txLog.head(tx);

    File meta = new File(getLocation(), JournalConfiguration.FILE_NAME);
    if (!meta.exists()) {
      try (UnstructuredFile hb = new UnstructuredFile(meta, 12, JournalMode.APPEND)) {
        getMetadata().write(hb);
      }
    }

    super.configure();

    beginTx();
    rollback();
    rollbackPartitionDirs();

    if (tx.journalMaxRowID > 0
        && getPartitionCount() <= Rows.toPartitionIndex(tx.journalMaxRowID)) {
      beginTx();
      commit();
    }
    if (getMetadata().getLag() != -1) {
      this.partitionCleaner = new PartitionCleaner(this, getLocation().getName());
      this.partitionCleaner.start();
    }
  }
Beispiel #2
0
  @Override
  public final void close() {
    if (open) {
      if (partitionCleaner != null) {
        partitionCleaner.halt();
        partitionCleaner = null;
      }
      try {
        if (isCommitOnClose()) {
          commit();
          purgeUnusedTempPartitions(txLog);
        }
        super.close();
        if (writeLock != null) {
          LockManager.release(writeLock);
          writeLock = null;
        }

        if (discardTxtRaf != null) {
          try {
            discardSink.close();
            discardTxtRaf.close();
          } catch (IOException e) {
            LOGGER.warn("Failed to close discard file");
          }
        }
      } catch (JournalException e) {
        throw new JournalRuntimeException(e);
      }
    }
  }
Beispiel #3
0
  public void purgeUnusedTempPartitions(TxLog txLog) throws JournalException {
    final Tx tx = new Tx();
    final String lagPartitionName =
        hasIrregularPartition() ? getIrregularPartition().getName() : null;

    File[] files =
        getLocation()
            .listFiles(
                new FileFilter() {
                  public boolean accept(File f) {
                    return f.isDirectory()
                        && f.getName().startsWith(Constants.TEMP_DIRECTORY_PREFIX)
                        && (lagPartitionName == null || !lagPartitionName.equals(f.getName()));
                  }
                });

    if (files != null) {

      Arrays.sort(files);

      for (int i = 0; i < files.length; i++) {

        if (!txLog.isEmpty()) {
          txLog.head(tx);
          if (files[i].getName().equals(tx.lagName)) {
            continue;
          }
        }

        // get exclusive lock
        Lock lock = LockManager.lockExclusive(files[i]);
        try {
          if (lock != null && lock.isValid()) {
            LOGGER.trace("Purging : %s", files[i]);
            if (!Files.delete(files[i])) {
              LOGGER.info("Could not purge: %s", files[i]);
            }
          } else {
            LOGGER.trace("Partition in use: %s", files[i]);
          }
        } finally {
          LockManager.release(lock);
        }
      }
    }
  }