|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.data.v2.stub.readrows; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.api.gax.retrying.StreamResumptionStrategy; |
| 20 | +import com.google.bigtable.v2.ReadRowsRequest; |
| 21 | +import com.google.bigtable.v2.ReadRowsRequest.Builder; |
| 22 | +import com.google.bigtable.v2.RowRange; |
| 23 | +import com.google.bigtable.v2.RowSet; |
| 24 | +import com.google.cloud.bigtable.data.v2.internal.ByteStringComparator; |
| 25 | +import com.google.cloud.bigtable.data.v2.models.RowAdapter; |
| 26 | +import com.google.common.base.Preconditions; |
| 27 | +import com.google.protobuf.ByteString; |
| 28 | + |
| 29 | +/** |
| 30 | + * An implementation of a {@link StreamResumptionStrategy} for merged rows. This class tracks the |
| 31 | + * last complete row seen and upon retry can build a request to resume the stream from where it left |
| 32 | + * off. |
| 33 | + * |
| 34 | + * <p>This class is considered an internal implementation detail and not meant to be used by |
| 35 | + * applications. |
| 36 | + */ |
| 37 | +@InternalApi |
| 38 | +public class ReadRowsResumptionStrategy<RowT> |
| 39 | + implements StreamResumptionStrategy<ReadRowsRequest, RowT> { |
| 40 | + private final RowAdapter<RowT> rowAdapter; |
| 41 | + private ByteString lastKey = ByteString.EMPTY; |
| 42 | + // Number of rows processed excluding Marker row. |
| 43 | + private long numProcessed; |
| 44 | + |
| 45 | + public ReadRowsResumptionStrategy(RowAdapter<RowT> rowAdapter) { |
| 46 | + this.rowAdapter = rowAdapter; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean canResume() { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public StreamResumptionStrategy<ReadRowsRequest, RowT> createNew() { |
| 56 | + return new ReadRowsResumptionStrategy<>(rowAdapter); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onProgress(RowT response) { |
| 61 | + // Last key can come from both the last processed row key and a synthetic row marker. The |
| 62 | + // synthetic row marker is emitted when the server has read a lot of data that was filtered out. |
| 63 | + // The row marker can be used to trim the start of the scan, but does not contribute to the row |
| 64 | + // limit. |
| 65 | + lastKey = rowAdapter.getKey(response); |
| 66 | + if (!rowAdapter.isScanMarkerRow(response)) { |
| 67 | + // Only real rows count towards the rows limit. |
| 68 | + numProcessed++; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritDoc} |
| 74 | + * |
| 75 | + * <p>Given a request, this implementation will narrow that request to exclude all row keys and |
| 76 | + * ranges that would produce rows that come before {@link #lastKey}. Furthermore this |
| 77 | + * implementation takes care to update the row limit of the request to account for all of the |
| 78 | + * received rows. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public ReadRowsRequest getResumeRequest(ReadRowsRequest request) { |
| 82 | + // An empty lastKey means that we have not successfully read the first row, |
| 83 | + // so resume with the original request object. |
| 84 | + if (lastKey.isEmpty()) { |
| 85 | + return request; |
| 86 | + } |
| 87 | + |
| 88 | + ReadRowsRequest originalRequest = request; |
| 89 | + |
| 90 | + // Special case: empty query implies full table scan, so make this explicit by adding an |
| 91 | + // unbounded range to the request |
| 92 | + if (request.getRows().getRowKeysList().isEmpty() |
| 93 | + && request.getRows().getRowRangesList().isEmpty()) { |
| 94 | + |
| 95 | + originalRequest = |
| 96 | + request |
| 97 | + .toBuilder() |
| 98 | + .setRows(RowSet.newBuilder().addRowRanges(RowRange.getDefaultInstance())) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + // Start building the resume request. The keys & ranges are cleared and will be recomputed. |
| 103 | + Builder builder = originalRequest.toBuilder(); |
| 104 | + builder.clearRows(); |
| 105 | + |
| 106 | + RowSet.Builder rowSetBuilder = RowSet.newBuilder(); |
| 107 | + |
| 108 | + for (ByteString key : originalRequest.getRows().getRowKeysList()) { |
| 109 | + if (ByteStringComparator.INSTANCE.compare(key, lastKey) > 0) { |
| 110 | + rowSetBuilder.addRowKeys(key); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + for (RowRange rowRange : originalRequest.getRows().getRowRangesList()) { |
| 115 | + RowRange.Builder rowRangeBuilder = RowRange.newBuilder(); |
| 116 | + |
| 117 | + switch (rowRange.getEndKeyCase()) { |
| 118 | + case END_KEY_CLOSED: |
| 119 | + if (ByteStringComparator.INSTANCE.compare(rowRange.getEndKeyClosed(), lastKey) > 0) { |
| 120 | + rowRangeBuilder.setEndKeyClosed(rowRange.getEndKeyClosed()); |
| 121 | + } else { |
| 122 | + continue; |
| 123 | + } |
| 124 | + break; |
| 125 | + case END_KEY_OPEN: |
| 126 | + if (ByteStringComparator.INSTANCE.compare(rowRange.getEndKeyOpen(), lastKey) > 0) { |
| 127 | + rowRangeBuilder.setEndKeyOpen(rowRange.getEndKeyOpen()); |
| 128 | + } else { |
| 129 | + continue; |
| 130 | + } |
| 131 | + break; |
| 132 | + case ENDKEY_NOT_SET: |
| 133 | + rowRangeBuilder.clearEndKey(); |
| 134 | + break; |
| 135 | + default: |
| 136 | + throw new IllegalArgumentException("Unknown endKeyCase: " + rowRange.getEndKeyCase()); |
| 137 | + } |
| 138 | + |
| 139 | + switch (rowRange.getStartKeyCase()) { |
| 140 | + case STARTKEY_NOT_SET: |
| 141 | + rowRangeBuilder.setStartKeyOpen(lastKey); |
| 142 | + break; |
| 143 | + case START_KEY_OPEN: |
| 144 | + if (ByteStringComparator.INSTANCE.compare(rowRange.getStartKeyOpen(), lastKey) < 0) { |
| 145 | + rowRangeBuilder.setStartKeyOpen(lastKey); |
| 146 | + } else { |
| 147 | + rowRangeBuilder.setStartKeyOpen(rowRange.getStartKeyOpen()); |
| 148 | + } |
| 149 | + break; |
| 150 | + case START_KEY_CLOSED: |
| 151 | + if (ByteStringComparator.INSTANCE.compare(rowRange.getStartKeyClosed(), lastKey) <= 0) { |
| 152 | + rowRangeBuilder.setStartKeyOpen(lastKey); |
| 153 | + } else { |
| 154 | + rowRangeBuilder.setStartKeyClosed(rowRange.getStartKeyClosed()); |
| 155 | + } |
| 156 | + break; |
| 157 | + default: |
| 158 | + throw new IllegalArgumentException("Unknown startKeyCase: " + rowRange.getStartKeyCase()); |
| 159 | + } |
| 160 | + rowSetBuilder.addRowRanges(rowRangeBuilder.build()); |
| 161 | + } |
| 162 | + |
| 163 | + // Edge case: retrying a fulfilled request. |
| 164 | + // A fulfilled request is one that has had all of its row keys and ranges fulfilled, or if it |
| 165 | + // had a row limit, has seen enough rows. These requests are replaced with a marker request that |
| 166 | + // will be handled by ReadRowsRetryCompletedCallable. See docs in ReadRowsRetryCompletedCallable |
| 167 | + // for more details. |
| 168 | + if ((rowSetBuilder.getRowRangesCount() == 0 && rowSetBuilder.getRowKeysCount() == 0) |
| 169 | + || (originalRequest.getRowsLimit() > 0 && originalRequest.getRowsLimit() == numProcessed)) { |
| 170 | + return ReadRowsRetryCompletedCallable.FULFILLED_REQUEST_MARKER; |
| 171 | + } |
| 172 | + |
| 173 | + if (originalRequest.getRowsLimit() > 0) { |
| 174 | + Preconditions.checkState( |
| 175 | + originalRequest.getRowsLimit() > numProcessed, |
| 176 | + "Detected too many rows for the current row limit during a retry."); |
| 177 | + builder.setRowsLimit(originalRequest.getRowsLimit() - numProcessed); |
| 178 | + } |
| 179 | + |
| 180 | + builder.setRows(rowSetBuilder.build()); |
| 181 | + return builder.build(); |
| 182 | + } |
| 183 | +} |
0 commit comments