Skip to content

Make GoogleCloudStorageRetryingInputStream request same generation on resume #127626

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

Merged

Conversation

nicktindall
Copy link
Contributor

@nicktindall nicktindall commented May 2, 2025

When we resume a download due to an IOException, we now specify the generation of the object that we were originally downloading in the request so that we can detect if the object changed.

Some decisions that were made

  • When resuming we send generation parameter rather than ifGenerationMatch. The former requests a specific generation where the latter requests the latest generation and triggers an error if its not the one specified. This distinction is only important if the bucket we're fetching from has object versioning enabled. If the object is overwritten and object versioning is enabled, resume with generation specified will succeed because the prior version will still be available, but resume with ifGenerationMatch specified would fail because the latest version is no longer the one we were downloading.
    • This is similar to the behaviour of the SDK in their own resumable channels
  • We ignore updates to metadata. We specify the generation, which is only bumped when the object contents are updated. If someone updates the metadata that shouldn't break a resume IMO. We could implement this behaviour by using ifMetagenerationMatch or Etags instead, but I don't think we want that.

Closes ES-11432

I marked this PR as "non-issue" because currently we never update objects with different contents. This is just future proofing.

@nicktindall nicktindall added >enhancement :Distributed Coordination/Snapshot/Restore Anything directly related to the `_snapshot/*` APIs labels May 2, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination)

@elasticsearchmachine elasticsearchmachine added Team:Distributed Coordination Meta label for Distributed Coordination team v9.1.0 labels May 2, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @nicktindall, I've created a changelog YAML for you.

Copy link
Contributor

@JeremyDahlgren JeremyDahlgren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I just had the one question about the assertion in parseGenerationHeader(), the rest were a few small nits. Might be good to get a look from other folks with GCS code experience though.

Copy link
Contributor

@JeremyDahlgren JeremyDahlgren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

# Conflicts:
#	modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java
Copy link
Member

@ywangd ywangd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The usages for the different generation parameters seem correct to me. It would be great if we can test the change against the real thing as well. Is it not viable?

public void testContentsChangeWhileStreaming() throws IOException {
GoogleCloudStorageHttpHandler handler = new GoogleCloudStorageHttpHandler("bucket");
httpServer.createContext("/", handler);
final int enoughBytesToTriggerChunkedDownload = Math.toIntExact(ByteSizeValue.ofMb(30).getBytes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a comment to explain briefly how the size of 30mb is decided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is actually incorrect (I don't think the current client does any chunking for downloads, only uploads). We just need to ensure the blob is large enough that it doesn't get entirely buffered when we read the first byte. This is probably a TCP level thing because as far as I can see the google or Java HTTP infrastructure merely relies on the underlying JVM/OS behaviour. I did some experiments locally and 2MB is usually enough to exceed the buffer, so 30M should be fairly conservative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ce03dae

@@ -212,6 +217,11 @@ public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions<?, ?> ser
);
}

@Override
protected void addSuccessfulDownloadHeaders(HttpExchange exchange) {
exchange.getResponseHeaders().add("x-goog-generation", "1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the actual generation number does not matter, can we randomize it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 2d2960c

Comment on lines +626 to +628
InetSocketAddress currentAddress = httpServer.getAddress();
httpServer.stop(0);
httpServer = MockHttpServer.createHttp(currentAddress, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether rebinding to the same address can be flaky sometimes. We can have it as is for now and see how it goes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be OK as long as we have SO_REUSEADDR set? It seemed to work locally so perhaps that's the default?

@nicktindall
Copy link
Contributor Author

It would be great if we can test the change against the real thing as well. Is it not viable?

There are assertions confirming that we do receive the x-goog-generation header and that it's the shape we expect it to be, so it should be covered by the third party tests (they download blobs using the stream).

It would be tricky to write a test where the generation failed against the real store because we'd need to somehow interrupt the stream to trigger a second request back to the server. I think that'd require some proxying or something. I think the assertions confirming the header is present and the tests simulating the resume interaction should be sufficient?

@ywangd
Copy link
Member

ywangd commented May 20, 2025

require some proxying

Yeah that is what I had in mind, e.g. WebProxyServer. The assertion fo x-goog-generation is indeed helpful. It could be useful to test thegeneration parameter's behaviour against the real storage as well. Or maybe it is also tested elsewhere. In any case, I don't think it is necessary for this PR if at all.

For my knowledge, what is the behaviour when there is no connection interuption and the blob gets overwrite midway?

@nicktindall
Copy link
Contributor Author

For my knowledge, what is the behaviour when there is no connection interuption and the blob gets overwrite midway?

That doesn't appear to be specified, I imagine as long as the download is not interrupted it would probably complete, but I haven't been able to test that. I'll see if I can run locally against the real blob store, there might be an easy way to test it.

@nicktindall
Copy link
Contributor Author

nicktindall commented May 21, 2025

Yeah that is what I had in mind, e.g. WebProxyServer.

I added an integration test using the proxy in eebcb0c

I think it's actually important to do because the documents are inconsistent about the x-goog-generation header (although it's used by the client internally), so best to have these in place. (specifically they mention that it's present in the XML API, but there's no mention of it in the JSON API that we use)

Copy link
Member

@ywangd ywangd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks for adding the 3rd party test.

@@ -26,19 +27,16 @@
*/
abstract class MockHttpProxyServer implements Closeable {

private final HttpServer httpServer;
private HttpServer httpServer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this field volatile to avoid have to think about visibility? Technically we restart the server on a generic thread and shutdown at test cleanup time in a different thread.

@nicktindall nicktindall merged commit 268e39b into elastic:main May 22, 2025
19 of 20 checks passed
@nicktindall nicktindall deleted the ES-11432_check_generation_on_resume branch May 22, 2025 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Distributed Coordination/Snapshot/Restore Anything directly related to the `_snapshot/*` APIs >non-issue Team:Distributed Coordination Meta label for Distributed Coordination team v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants