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.hamcrest.MatcherAssert.assertThat;
022import static org.junit.Assert.assertEquals;
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.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException;
035import org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException;
036import org.apache.hadoop.ipc.RemoteException;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044
045/**
046 * Used to confirm that it is OK to overwrite a file which is being written currently.
047 */
048@Category({ MiscTests.class, MediumTests.class })
049public class TestOverwriteFileUnderConstruction extends AsyncFSTestBase {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestOverwriteFileUnderConstruction.class);
054
055  private static FileSystem FS;
056
057  @Rule
058  public final TestName name = new TestName();
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    startMiniDFSCluster(3);
063    FS = CLUSTER.getFileSystem();
064  }
065
066  @AfterClass
067  public static void tearDown() throws Exception {
068    shutdownMiniDFSCluster();
069  }
070
071  @Test
072  public void testNotOverwrite() throws IOException {
073    Path file = new Path("/" + name.getMethodName());
074    try (FSDataOutputStream out1 = FS.create(file)) {
075      try {
076        FS.create(file, false);
077        fail("Should fail as there is a file with the same name which is being written");
078      } catch (RemoteException e) {
079        // expected
080        assertThat(e.unwrapRemoteException(), instanceOf(AlreadyBeingCreatedException.class));
081      }
082    }
083  }
084
085  @Test
086  public void testOverwrite() throws IOException {
087    Path file = new Path("/" + name.getMethodName());
088    FSDataOutputStream out1 = FS.create(file);
089    FSDataOutputStream out2 = FS.create(file, true);
090    out1.write(2);
091    out2.write(1);
092    try {
093      out1.close();
094      // a successful close is also OK for us so no assertion here, we just need to confirm that the
095      // data in the file are correct.
096    } catch (FileNotFoundException fnfe) {
097      // hadoop3 throws one of these.
098    } catch (RemoteException e) {
099      // expected
100      assertThat(e.unwrapRemoteException(), instanceOf(LeaseExpiredException.class));
101    }
102    out2.close();
103    try (FSDataInputStream in = FS.open(file)) {
104      assertEquals(1, in.read());
105      assertEquals(-1, in.read());
106    }
107  }
108}