/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.underfs;

import alluxio.AlluxioURI;
import alluxio.exception.ExceptionMessage;
import alluxio.underfs.options.CreateOptions;
import alluxio.util.IdUtils;
import alluxio.util.io.PathUtils;

import java.io.IOException;
import java.io.OutputStream;

/**
 * An {@link UnderFileSystem} which does not have an atomic create operation.
 */
public abstract class NonAtomicCreateUnderFileSystem extends UnderFileSystem {

  private final long mNonce;
  private String mTemporaryPath;
  private String mPermanentPath;

  /**
   * Constructs a new {@link NonAtomicCreateUnderFileSystem}.
   *
   * @param uri the {@link AlluxioURI} for this UFS
   */
  public NonAtomicCreateUnderFileSystem(AlluxioURI uri) {
    super(uri);
    mNonce = IdUtils.getRandomNonNegativeLong();
  }

  @Override
  public OutputStream create(String path, CreateOptions options) throws IOException {
    mPermanentPath = path;
    mTemporaryPath = PathUtils.temporaryFileName(mNonce, path);

    return new AtomicFileOutputStream(createNonAtomic(path, options), this);
  }

  /**
   * Complete create operation by renaming temporary path to permanent.
   *
   * @throws IOException when rename fails
   */
  public void completeCreate() throws IOException {
    if (!rename(mTemporaryPath, mPermanentPath)) {
      delete(mTemporaryPath, false);
      throw new IOException(
          ExceptionMessage.FAILED_UFS_RENAME.getMessage(mTemporaryPath, mPermanentPath));
    }
  }

  /**
   * Create a non atomic output stream.
   *
   * @param path temporary path
   * @param options the options for create
   * @return a non atomic output stream
   * @throws IOException when create fails
   */
  public abstract OutputStream createNonAtomic(String path, CreateOptions options)
      throws IOException;

}

@@ -560,16 +560,42 @@
 xioURI;%0A
+import alluxio.Constants;%0A
 import a
@@ -629,16 +629,16 @@
 essage;%0A
-
 import a
@@ -735,24 +735,82 @@
 PathUtils;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import java.
@@ -1117,16 +1117,101 @@
 tPath;%0A%0A
+  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);%0A%0A
   /**%0A  
@@ -1705,17 +1705,27 @@
 nAtomic(
-p
+mTemporaryP
 ath, opt
@@ -1984,16 +1984,21 @@
 %7B%0A      
+if (!
 delete(m
@@ -2018,17 +2018,100 @@
 , false)
-;
+) %7B%0A        LOG.error(%22Failed to delete temporary file %7B%7D%22, mTemporaryPath);%0A      %7D
 %0A      t

Fix bug