-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Blob] Support blob-write-null-on-missing-file for HTTP URLs. #8219
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
wwj6591812
wants to merge
2
commits into
apache:master
Choose a base branch
from
wwj6591812:support_blob_write_null_0612
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
235 changes: 235 additions & 0 deletions
235
paimon-api/src/test/java/org/apache/paimon/rest/HttpClientUtilsTest.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,235 @@ | ||
| /* | ||
| * 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.paimon.rest; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpHandler; | ||
| import com.sun.net.httpserver.HttpServer; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.net.InetSocketAddress; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** Tests for {@link HttpClientUtils}. */ | ||
| public class HttpClientUtilsTest { | ||
|
|
||
| private HttpServer server; | ||
| private int port; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| server = HttpServer.create(new InetSocketAddress(0), 0); | ||
| port = server.getAddress().getPort(); | ||
| server.start(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| if (server != null) { | ||
| server.stop(0); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsReturnsTrueForAvailableResource() throws Exception { | ||
| registerHandler( | ||
| "/ok", | ||
| exchange -> { | ||
| respond(exchange, 200, "abc".getBytes()); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/ok"))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsReturnsFalseForMissingResource() throws Exception { | ||
| registerHandler( | ||
| "/missing", | ||
| exchange -> { | ||
| respond(exchange, 404, new byte[0]); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/missing"))).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsFallsBackToRangeGetWhenHeadNotAllowed() throws Exception { | ||
| registerHandler( | ||
| "/no-head", | ||
| exchange -> { | ||
| if ("HEAD".equals(exchange.getRequestMethod())) { | ||
| respond(exchange, 405, new byte[0]); | ||
| return; | ||
| } | ||
| respond(exchange, 200, "abc".getBytes()); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/no-head"))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsFallsBackToRangeGetWhenHeadReturnsNotFound() throws Exception { | ||
| registerHandler( | ||
| "/head-404-get-ok", | ||
| exchange -> { | ||
| if ("HEAD".equals(exchange.getRequestMethod())) { | ||
| respond(exchange, 404, new byte[0]); | ||
| return; | ||
| } | ||
| if ("GET".equals(exchange.getRequestMethod()) | ||
| && exchange.getRequestHeaders().getFirst("Range") != null) { | ||
| respond(exchange, 206, "abc".getBytes()); | ||
| return; | ||
| } | ||
| respond(exchange, 404, new byte[0]); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/head-404-get-ok"))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsFallsBackToRangeGetWhenHeadReturnsForbidden() throws Exception { | ||
| registerHandler( | ||
| "/head-403-get-ok", | ||
| exchange -> { | ||
| if ("HEAD".equals(exchange.getRequestMethod())) { | ||
| respond(exchange, 403, new byte[0]); | ||
| return; | ||
| } | ||
| if ("GET".equals(exchange.getRequestMethod()) | ||
| && exchange.getRequestHeaders().getFirst("Range") != null) { | ||
| respond(exchange, 200, "abc".getBytes()); | ||
| return; | ||
| } | ||
| respond(exchange, 403, new byte[0]); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/head-403-get-ok"))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsTreatsEmptyResourceAsExistingWhenRangeReturns416() throws Exception { | ||
| registerHandler( | ||
| "/empty-no-head", | ||
| exchange -> { | ||
| if ("HEAD".equals(exchange.getRequestMethod())) { | ||
| respond(exchange, 405, new byte[0]); | ||
| return; | ||
| } | ||
| if ("GET".equals(exchange.getRequestMethod()) | ||
| && exchange.getRequestHeaders().getFirst("Range") != null) { | ||
| respond(exchange, 416, new byte[0]); | ||
| return; | ||
| } | ||
| respond(exchange, 200, new byte[0]); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/empty-no-head"))).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExistsReturnsFalseOnlyWhenRangeGetAlsoNotFound() throws Exception { | ||
| registerHandler( | ||
| "/head-404-get-404", | ||
| exchange -> { | ||
| if ("HEAD".equals(exchange.getRequestMethod())) { | ||
| respond(exchange, 404, new byte[0]); | ||
| return; | ||
| } | ||
| respond(exchange, 404, new byte[0]); | ||
| }); | ||
|
|
||
| assertThat(HttpClientUtils.exists(url("/head-404-get-404"))).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetAsInputStreamThrowsForNotFound() { | ||
| registerHandler( | ||
| "/get-missing", | ||
| exchange -> { | ||
| respond(exchange, 404, new byte[0]); | ||
| }); | ||
|
|
||
| assertThatThrownBy(() -> HttpClientUtils.getAsInputStream(url("/get-missing"))) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasMessage("HTTP error code: 404"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetAsInputStreamDoesNotLeakConnectionsOnRepeatedNotFound() throws Exception { | ||
| registerHandler( | ||
| "/missing", | ||
| exchange -> { | ||
| respond(exchange, 404, new byte[0]); | ||
| }); | ||
| registerHandler( | ||
| "/ok", | ||
| exchange -> { | ||
| respond(exchange, 200, "x".getBytes()); | ||
| }); | ||
|
|
||
| for (int i = 0; i < 120; i++) { | ||
| assertThatThrownBy(() -> HttpClientUtils.getAsInputStream(url("/missing"))) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasMessage("HTTP error code: 404"); | ||
| } | ||
|
|
||
| try (InputStream in = HttpClientUtils.getAsInputStream(url("/ok"))) { | ||
| assertThat(in.read()).isEqualTo('x'); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsNotFoundError() { | ||
| RuntimeException exception = | ||
| new RuntimeException("wrapper", new RuntimeException("HTTP error code: 404")); | ||
| assertThat(HttpClientUtils.isNotFoundError(exception)).isTrue(); | ||
| assertThat(HttpClientUtils.isNotFoundError(new RuntimeException("HTTP error code: 500"))) | ||
| .isFalse(); | ||
| } | ||
|
|
||
| private void registerHandler(String path, HttpHandler handler) { | ||
| server.createContext(path, handler); | ||
| } | ||
|
|
||
| private String url(String path) { | ||
| return "http://127.0.0.1:" + port + path; | ||
| } | ||
|
|
||
| private static void respond(HttpExchange exchange, int statusCode, byte[] body) | ||
| throws IOException { | ||
| boolean headRequest = "HEAD".equals(exchange.getRequestMethod()); | ||
| long responseLength = headRequest ? -1 : body.length; | ||
| exchange.sendResponseHeaders(statusCode, responseLength); | ||
| if (!headRequest && body.length > 0) { | ||
| try (OutputStream outputStream = exchange.getResponseBody()) { | ||
| outputStream.write(body); | ||
| } | ||
| } else { | ||
| exchange.close(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
The range fallback still treats a valid empty HTTP resource as an error. If HEAD is rejected or unavailable,
Range: bytes=0-0against a zero-length object commonly returns 416 because the requested range is unsatisfiable, even though a normal GET would succeed with an empty body. In that caseexists()throws here, so Flink writes usingblob-write-null-on-missing-filecan fail for existing empty HTTP blobs. Could we handle 416 as an existing resource for this probe (or fall back to a full GET) and add a test with HEAD disabled plus an empty response?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.
Thanks for catching this, @JingsongLi .
You're right — for a zero-length HTTP resource, Range: bytes=0-0 can legitimately return 416 even though a normal GET would succeed with an empty body, and treating that as an error breaks exists() for existing empty blobs.
I've updated HttpClientUtils.exists() to treat HTTP 416 from the range probe as an existing resource, and added testExistsTreatsEmptyResourceAsExistingWhenRangeReturns416 (HEAD disabled + empty response + range GET returns 416). Please take another look when you have a chance.