-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29876: Close response streams in YarnQueueHelper #6772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xza-m
wants to merge
1
commit into
apache:master
Choose a base branch
from
xza-m:HIVE-29876-close-response-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
ql/src/test/org/apache/hadoop/hive/ql/exec/tez/TestYarnQueueHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * 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.exec.tez; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.HttpURLConnection; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import org.apache.hadoop.hive.conf.HiveConf; | ||
| import org.apache.hadoop.yarn.conf.YarnConfiguration; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestYarnQueueHelper { | ||
| private YarnQueueHelper helper; | ||
| private HttpURLConnection connection; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| HiveConf conf = new HiveConf(); | ||
| conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, false); | ||
| conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY, "HTTP_ONLY"); | ||
| conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS, "localhost:8088"); | ||
| helper = new YarnQueueHelper(conf); | ||
| connection = mock(HttpURLConnection.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosesErrorStream() throws IOException { | ||
| InputStream stream = responseStream("Access denied"); | ||
| when(connection.getErrorStream()).thenReturn(stream); | ||
|
|
||
| assertEquals("Received 403 (check queue administrators): Access denied", | ||
| helper.handleUnexpectedStatusCode(connection, 403, "check queue administrators")); | ||
|
|
||
| verify(stream).close(); | ||
| verify(connection, never()).getInputStream(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosesFallbackInputStream() throws IOException { | ||
| InputStream stream = responseStream("Unavailable"); | ||
| when(connection.getInputStream()).thenReturn(stream); | ||
|
|
||
| assertEquals("Received 503: Unavailable", helper.handleUnexpectedStatusCode(connection, 503, null)); | ||
|
|
||
| verify(stream).close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosesEmptyErrorStreamWithoutFallingBack() throws IOException { | ||
| InputStream stream = responseStream(""); | ||
| when(connection.getErrorStream()).thenReturn(stream); | ||
|
|
||
| assertEquals("Received 500: ", helper.handleUnexpectedStatusCode(connection, 500, null)); | ||
|
|
||
| verify(stream).close(); | ||
| verify(connection, never()).getInputStream(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMissingResponseStreams() throws IOException { | ||
| assertEquals("Received 200 (No input on successful API call)", | ||
| helper.handleUnexpectedStatusCode(connection, 200, "No input on successful API call")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosesErrorStreamOnReadFailure() throws IOException { | ||
| assertClosesStreamOnReadFailure(true); | ||
| verify(connection, never()).getInputStream(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testClosesFallbackInputStreamOnReadFailure() throws IOException { | ||
| assertClosesStreamOnReadFailure(false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPropagatesFallbackInputStreamFailure() throws IOException { | ||
| IOException failure = new IOException("Cannot get input stream"); | ||
| when(connection.getInputStream()).thenThrow(failure); | ||
|
|
||
| assertSame(failure, assertThrows(IOException.class, | ||
| () -> helper.handleUnexpectedStatusCode(connection, 500, null))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPropagatesCloseFailure() throws IOException { | ||
| InputStream stream = responseStream("Unavailable"); | ||
| IOException failure = new IOException("Cannot close response"); | ||
| doThrow(failure).when(stream).close(); | ||
| when(connection.getErrorStream()).thenReturn(stream); | ||
|
|
||
| assertSame(failure, assertThrows(IOException.class, | ||
| () -> helper.handleUnexpectedStatusCode(connection, 503, null))); | ||
| verify(stream).close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPreservesReadFailureWhenCloseAlsoFails() throws IOException { | ||
| InputStream stream = mock(InputStream.class); | ||
| IOException readFailure = new IOException("Cannot read response"); | ||
| IOException closeFailure = new IOException("Cannot close response"); | ||
| when(stream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(readFailure); | ||
| doThrow(closeFailure).when(stream).close(); | ||
| when(connection.getErrorStream()).thenReturn(stream); | ||
|
|
||
| IOException actual = assertThrows(IOException.class, | ||
| () -> helper.handleUnexpectedStatusCode(connection, 500, null)); | ||
|
|
||
| assertSame(readFailure, actual); | ||
| assertEquals(1, actual.getSuppressed().length); | ||
| assertSame(closeFailure, actual.getSuppressed()[0]); | ||
| verify(stream).close(); | ||
| } | ||
|
|
||
| private void assertClosesStreamOnReadFailure(boolean useErrorStream) throws IOException { | ||
| InputStream stream = mock(InputStream.class); | ||
| IOException failure = new IOException("Cannot read response"); | ||
| when(stream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(failure); | ||
| if (useErrorStream) { | ||
| when(connection.getErrorStream()).thenReturn(stream); | ||
| } else { | ||
| when(connection.getInputStream()).thenReturn(stream); | ||
| } | ||
|
|
||
| assertSame(failure, assertThrows(IOException.class, | ||
| () -> helper.handleUnexpectedStatusCode(connection, 500, null))); | ||
| verify(stream).close(); | ||
| } | ||
|
|
||
| private InputStream responseStream(String body) { | ||
| return spy(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8))); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IOUtils.toString(InputStream input)has been deprecated in the newer versions of commons-io including v2.16.1 being used in hive, since we are already updating this, can we rather useIOUtils.toString(InputStream input, Charset charset)implementation?