ramitg254 commented on code in PR #6981: URL: https://github.com/apache/hive/pull/6981#discussion_r4784291439
########## ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHiveCopyFilesConcurrentInsert.java: ########## @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.hadoop.hive.ql.metadata; + +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RawLocalFileSystem; +import org.apache.hadoop.hive.common.FileUtils; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConfForTest; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Two inserts move a staged {@code 000000_0} into the same directory. Both pass the + * destination {@code exists()} check before either renames, which is the HDFS race + * {@link Hive#copyFiles} must recover from by publishing the loser as {@code _copy_N}. + */ +public class TestHiveCopyFilesConcurrentInsert { + + private static final int WRITERS = 2; + private static final String STAGED_FILE_NAME = "000000_0"; + + private static HiveConf hiveConf; + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @BeforeClass + public static void setUpClass() { + hiveConf = new HiveConfForTest(TestHiveCopyFilesConcurrentInsert.class); + SessionState.start(hiveConf); + } + + @Before + public void setUp() { + hiveConf.setIntVar(HiveConf.ConfVars.HIVE_MOVE_FILES_THREAD_COUNT, 0); + } + + @Test + public void testBaseNameToCopy1() throws Exception { + Set<String> names = moveBoth(STAGED_FILE_NAME); + assertEquals(names.toString(), WRITERS, names.size()); + assertTrue(names.contains(STAGED_FILE_NAME)); + assertTrue(names.contains(STAGED_FILE_NAME + "_copy_1")); + } + + /** + * The destination already holds {@code 000000_0} and {@code 000000_0_copy_1}, so both writers + * choose {@code 000000_0_copy_2}. The loser of that rename must be published as + * {@code 000000_0_copy_3}. + */ + @Test + public void testAdvancementToNextCopy() throws Exception { + String copy2 = STAGED_FILE_NAME + "_copy_2"; + Set<String> names = moveBoth(copy2, STAGED_FILE_NAME, STAGED_FILE_NAME + "_copy_1"); + assertEquals(names.toString(), + new HashSet<>(Arrays.asList( + STAGED_FILE_NAME, STAGED_FILE_NAME + "_copy_1", copy2, STAGED_FILE_NAME + "_copy_3")), + names); + } + Review Comment: added coverage for these scenarios for both atomic and non atomic fs ones, please check the commit 06e2ac73260061e7991fad1311cdea899fce96de here for non atomic fs scenario: a `copy then delete` approach is followed to replicate rename operation so that we can have non atomic rename method here this test https://github.com/apache/hive/pull/6981/changes/06e2ac73260061e7991fad1311cdea899fce96de#diff-18bdf25a7616df3bf5dff40ba8988278a72962a158b99aecd9af79953f5dbce8R167 is added to highlight the fact that for such file system in case of concurrent sessions either they can lead to a single file or ran into errors as well since we are keeping it's logic as it is and not covering this for the fix -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
