001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.asyncfs;
019
020import static org.hamcrest.CoreMatchers.instanceOf;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertThat;
023import static org.junit.Assert.fail;
024
025import java.io.FileNotFoundException;
026import java.io.IOException;
027import org.apache.hadoop.fs.FSDataInputStream;
028import org.apache.hadoop.fs.FSDataOutputStream;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.MiscTests;
035import org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException;
036import org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException;
037import org.apache.hadoop.ipc.RemoteException;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.junit.rules.TestName;
045
046/**
047 * Used to confirm that it is OK to overwrite a file which is being written currently.
048 */
049@Category({ MiscTests.class, MediumTests.class })
050public class TestOverwriteFileUnderConstruction {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestOverwriteFileUnderConstruction.class);
055
056  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
057
058  private static FileSystem FS;
059
060  @Rule
061  public final TestName name = new TestName();
062
063  @BeforeClass
064  public static void setUp() throws Exception {
065    UTIL.startMiniDFSCluster(3);
066    FS = UTIL.getDFSCluster().getFileSystem();
067  }
068
069  @AfterClass
070  public static void tearDown() throws Exception {
071    UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testNotOverwrite() throws IOException {
076    Path file = new Path("/" + name.getMethodName());
077    try (FSDataOutputStream out1 = FS.create(file)) {
078      try {
079        FS.create(file, false);
080        fail("Should fail as there is a file with the same name which is being written");
081      } catch (RemoteException e) {
082        // expected
083        assertThat(e.unwrapRemoteException(), instanceOf(AlreadyBeingCreatedException.class));
084      }
085    }
086  }
087
088  @Test
089  public void testOverwrite() throws IOException {
090    Path file = new Path("/" + name.getMethodName());
091    FSDataOutputStream out1 = FS.create(file);
092    FSDataOutputStream out2 = FS.create(file, true);
093    out1.write(2);
094    out2.write(1);
095    try {
096      out1.close();
097      // a successful close is also OK for us so no assertion here, we just need to confirm that the
098      // data in the file are correct.
099    } catch (FileNotFoundException fnfe) {
100      // hadoop3 throws one of these.
101    } catch (RemoteException e) {
102      // expected
103      assertThat(e.unwrapRemoteException(), instanceOf(LeaseExpiredException.class));
104    }
105    out2.close();
106    try (FSDataInputStream in = FS.open(file)) {
107      assertEquals(1, in.read());
108      assertEquals(-1, in.read());
109    }
110  }
111}