|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.repositories.gcs; |
| 20 | + |
| 21 | +import com.google.cloud.ReadChannel; |
| 22 | +import com.google.cloud.storage.BlobId; |
| 23 | +import com.google.cloud.storage.Storage; |
| 24 | +import com.google.cloud.storage.StorageException; |
| 25 | +import org.apache.logging.log4j.LogManager; |
| 26 | +import org.apache.logging.log4j.Logger; |
| 27 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 28 | +import org.elasticsearch.common.SuppressForbidden; |
| 29 | +import org.elasticsearch.core.internal.io.IOUtils; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.nio.ByteBuffer; |
| 34 | +import java.nio.channels.Channels; |
| 35 | +import java.nio.channels.ReadableByteChannel; |
| 36 | +import java.nio.file.NoSuchFileException; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 41 | + |
| 42 | +/** |
| 43 | + * Wrapper around reads from GCS that will retry blob downloads that fail part-way through, resuming from where the failure occurred. |
| 44 | + * This should be handled by the SDK but it isn't today. This should be revisited in the future (e.g. before removing |
| 45 | + * the {@link org.elasticsearch.Version#V_7_0_0} version constant) and removed if the SDK handles retries itself in the future. |
| 46 | + */ |
| 47 | +class GoogleCloudStorageRetryingInputStream extends InputStream { |
| 48 | + |
| 49 | + private static final Logger logger = LogManager.getLogger(GoogleCloudStorageRetryingInputStream.class); |
| 50 | + |
| 51 | + static final int MAX_SUPPRESSED_EXCEPTIONS = 10; |
| 52 | + |
| 53 | + private final Storage client; |
| 54 | + |
| 55 | + private final BlobId blobId; |
| 56 | + |
| 57 | + private final int maxRetries; |
| 58 | + |
| 59 | + private InputStream currentStream; |
| 60 | + private int attempt = 1; |
| 61 | + private List<StorageException> failures = new ArrayList<>(MAX_SUPPRESSED_EXCEPTIONS); |
| 62 | + private long currentOffset; |
| 63 | + private boolean closed; |
| 64 | + |
| 65 | + GoogleCloudStorageRetryingInputStream(Storage client, BlobId blobId) throws IOException { |
| 66 | + this.client = client; |
| 67 | + this.blobId = blobId; |
| 68 | + this.maxRetries = client.getOptions().getRetrySettings().getMaxAttempts() + 1; |
| 69 | + currentStream = openStream(); |
| 70 | + } |
| 71 | + |
| 72 | + private InputStream openStream() throws IOException { |
| 73 | + try { |
| 74 | + final ReadChannel readChannel = SocketAccess.doPrivilegedIOException(() -> client.reader(blobId)); |
| 75 | + if (currentOffset > 0L) { |
| 76 | + readChannel.seek(currentOffset); |
| 77 | + } |
| 78 | + return Channels.newInputStream(new ReadableByteChannel() { |
| 79 | + @SuppressForbidden(reason = "Channel is based of a socket not a file") |
| 80 | + @Override |
| 81 | + public int read(ByteBuffer dst) throws IOException { |
| 82 | + try { |
| 83 | + return SocketAccess.doPrivilegedIOException(() -> readChannel.read(dst)); |
| 84 | + } catch (StorageException e) { |
| 85 | + if (e.getCode() == HTTP_NOT_FOUND) { |
| 86 | + throw new NoSuchFileException("Blob [" + blobId.getName() + "] does not exist"); |
| 87 | + } |
| 88 | + throw e; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean isOpen() { |
| 94 | + return readChannel.isOpen(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void close() throws IOException { |
| 99 | + SocketAccess.doPrivilegedVoidIOException(readChannel::close); |
| 100 | + } |
| 101 | + }); |
| 102 | + } catch (StorageException e) { |
| 103 | + throw addSuppressedExceptions(e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int read() throws IOException { |
| 109 | + ensureOpen(); |
| 110 | + while (true) { |
| 111 | + try { |
| 112 | + final int result = currentStream.read(); |
| 113 | + currentOffset += 1; |
| 114 | + return result; |
| 115 | + } catch (StorageException e) { |
| 116 | + reopenStreamOrFail(e); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int read(byte[] b, int off, int len) throws IOException { |
| 123 | + ensureOpen(); |
| 124 | + while (true) { |
| 125 | + try { |
| 126 | + final int bytesRead = currentStream.read(b, off, len); |
| 127 | + if (bytesRead == -1) { |
| 128 | + return -1; |
| 129 | + } |
| 130 | + currentOffset += bytesRead; |
| 131 | + return bytesRead; |
| 132 | + } catch (StorageException e) { |
| 133 | + reopenStreamOrFail(e); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private void ensureOpen() { |
| 139 | + if (closed) { |
| 140 | + assert false : "using GoogleCloudStorageRetryingInputStream after close"; |
| 141 | + throw new IllegalStateException("using GoogleCloudStorageRetryingInputStream after close"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void reopenStreamOrFail(StorageException e) throws IOException { |
| 146 | + if (attempt >= maxRetries) { |
| 147 | + throw addSuppressedExceptions(e); |
| 148 | + } |
| 149 | + logger.debug(new ParameterizedMessage("failed reading [{}] at offset [{}], attempt [{}] of [{}], retrying", |
| 150 | + blobId, currentOffset, attempt, MAX_SUPPRESSED_EXCEPTIONS), e); |
| 151 | + attempt += 1; |
| 152 | + if (failures.size() < MAX_SUPPRESSED_EXCEPTIONS) { |
| 153 | + failures.add(e); |
| 154 | + } |
| 155 | + IOUtils.closeWhileHandlingException(currentStream); |
| 156 | + currentStream = openStream(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void close() throws IOException { |
| 161 | + currentStream.close(); |
| 162 | + closed = true; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public long skip(long n) { |
| 167 | + throw new UnsupportedOperationException("GoogleCloudStorageRetryingInputStream does not support seeking"); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void reset() { |
| 172 | + throw new UnsupportedOperationException("GoogleCloudStorageRetryingInputStream does not support seeking"); |
| 173 | + } |
| 174 | + |
| 175 | + private <T extends Exception> T addSuppressedExceptions(T e) { |
| 176 | + for (StorageException failure : failures) { |
| 177 | + e.addSuppressed(failure); |
| 178 | + } |
| 179 | + return e; |
| 180 | + } |
| 181 | +} |
0 commit comments