|
| 1 | +/* |
| 2 | + * Copyright 2026 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 | + |
| 17 | +package com.google.cloud.bigtable.data.v2.internal.channels; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.bigtable.v2.BigtableGrpc; |
| 22 | +import com.google.bigtable.v2.CheckAndMutateRowRequest; |
| 23 | +import com.google.bigtable.v2.MutateRowRequest; |
| 24 | +import com.google.bigtable.v2.MutateRowsRequest; |
| 25 | +import com.google.bigtable.v2.Mutation; |
| 26 | +import com.google.bigtable.v2.Mutation.DeleteFromRow; |
| 27 | +import com.google.bigtable.v2.Mutation.SetCell; |
| 28 | +import com.google.bigtable.v2.Mutation.TimestampOrigin; |
| 29 | +import com.google.protobuf.ByteString; |
| 30 | +import io.grpc.CallOptions; |
| 31 | +import io.grpc.Channel; |
| 32 | +import io.grpc.ClientCall; |
| 33 | +import io.grpc.MethodDescriptor; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | + |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +public class MillisTimestampInterceptorTest { |
| 41 | + private static final long UNALIGNED_MICROS = 1_234_567_890_123_456L; |
| 42 | + private static final long ALIGNED_MICROS = 1_234_567_890_123_000L; |
| 43 | + |
| 44 | + private CapturingChannel channel; |
| 45 | + private MillisTimestampInterceptor interceptor; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + channel = new CapturingChannel(); |
| 50 | + interceptor = new MillisTimestampInterceptor(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void mutateRowTruncatesAutoGeneratedTimestamps() { |
| 55 | + MutateRowRequest request = |
| 56 | + MutateRowRequest.newBuilder() |
| 57 | + .addMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.CLIENT_AUTO_GENERATED)) |
| 58 | + .build(); |
| 59 | + |
| 60 | + MutateRowRequest sent = send(BigtableGrpc.getMutateRowMethod(), request); |
| 61 | + |
| 62 | + assertThat(sent.getMutations(0).getSetCell().getTimestampMicros()).isEqualTo(ALIGNED_MICROS); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void mutateRowLeavesUserSpecifiedTimestampsAlone() { |
| 67 | + MutateRowRequest request = |
| 68 | + MutateRowRequest.newBuilder() |
| 69 | + .addMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.USER_SPECIFIED)) |
| 70 | + .build(); |
| 71 | + |
| 72 | + assertThat(send(BigtableGrpc.getMutateRowMethod(), request)).isEqualTo(request); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void mutateRowLeavesNonSetCellMutationsAlone() { |
| 77 | + MutateRowRequest request = |
| 78 | + MutateRowRequest.newBuilder() |
| 79 | + .addMutations( |
| 80 | + Mutation.newBuilder() |
| 81 | + .setDeleteFromRow(DeleteFromRow.getDefaultInstance()) |
| 82 | + .setTimestampOrigin(TimestampOrigin.CLIENT_AUTO_GENERATED)) |
| 83 | + .build(); |
| 84 | + |
| 85 | + assertThat(send(BigtableGrpc.getMutateRowMethod(), request)).isEqualTo(request); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void mutateRowsTruncatesEveryEntry() { |
| 90 | + MutateRowsRequest request = |
| 91 | + MutateRowsRequest.newBuilder() |
| 92 | + .addEntries( |
| 93 | + MutateRowsRequest.Entry.newBuilder() |
| 94 | + .setRowKey(ByteString.copyFromUtf8("row1")) |
| 95 | + .addMutations(setCell(ALIGNED_MICROS, TimestampOrigin.CLIENT_AUTO_GENERATED))) |
| 96 | + .addEntries( |
| 97 | + MutateRowsRequest.Entry.newBuilder() |
| 98 | + .setRowKey(ByteString.copyFromUtf8("row2")) |
| 99 | + .addMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.CLIENT_AUTO_GENERATED)) |
| 100 | + .addMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.USER_SPECIFIED))) |
| 101 | + .build(); |
| 102 | + |
| 103 | + MutateRowsRequest sent = send(BigtableGrpc.getMutateRowsMethod(), request); |
| 104 | + |
| 105 | + assertThat(sent.getEntries(0)).isEqualTo(request.getEntries(0)); |
| 106 | + assertThat(sent.getEntries(1).getRowKey()).isEqualTo(ByteString.copyFromUtf8("row2")); |
| 107 | + assertThat(sent.getEntries(1).getMutations(0).getSetCell().getTimestampMicros()) |
| 108 | + .isEqualTo(ALIGNED_MICROS); |
| 109 | + assertThat(sent.getEntries(1).getMutations(1).getSetCell().getTimestampMicros()) |
| 110 | + .isEqualTo(UNALIGNED_MICROS); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void checkAndMutateRowTruncatesBothBranches() { |
| 115 | + CheckAndMutateRowRequest request = |
| 116 | + CheckAndMutateRowRequest.newBuilder() |
| 117 | + .addTrueMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.CLIENT_AUTO_GENERATED)) |
| 118 | + .addFalseMutations(setCell(UNALIGNED_MICROS, TimestampOrigin.CLIENT_AUTO_GENERATED)) |
| 119 | + .build(); |
| 120 | + |
| 121 | + CheckAndMutateRowRequest sent = send(BigtableGrpc.getCheckAndMutateRowMethod(), request); |
| 122 | + |
| 123 | + assertThat(sent.getTrueMutations(0).getSetCell().getTimestampMicros()) |
| 124 | + .isEqualTo(ALIGNED_MICROS); |
| 125 | + assertThat(sent.getFalseMutations(0).getSetCell().getTimestampMicros()) |
| 126 | + .isEqualTo(ALIGNED_MICROS); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void otherRequestsPassThrough() { |
| 131 | + com.google.bigtable.v2.ReadRowsRequest request = |
| 132 | + com.google.bigtable.v2.ReadRowsRequest.newBuilder().setTableName("my-table").build(); |
| 133 | + |
| 134 | + assertThat(send(BigtableGrpc.getReadRowsMethod(), request)).isEqualTo(request); |
| 135 | + } |
| 136 | + |
| 137 | + private static Mutation setCell(long timestampMicros, TimestampOrigin timestampOrigin) { |
| 138 | + return Mutation.newBuilder() |
| 139 | + .setSetCell( |
| 140 | + SetCell.newBuilder() |
| 141 | + .setFamilyName("cf") |
| 142 | + .setColumnQualifier(ByteString.copyFromUtf8("q")) |
| 143 | + .setTimestampMicros(timestampMicros) |
| 144 | + .setValue(ByteString.copyFromUtf8("v"))) |
| 145 | + .setTimestampOrigin(timestampOrigin) |
| 146 | + .build(); |
| 147 | + } |
| 148 | + |
| 149 | + @SuppressWarnings("unchecked") |
| 150 | + private <ReqT, RespT> ReqT send(MethodDescriptor<ReqT, RespT> method, ReqT request) { |
| 151 | + interceptor.interceptCall(method, CallOptions.DEFAULT, channel).sendMessage(request); |
| 152 | + return (ReqT) channel.lastMessage; |
| 153 | + } |
| 154 | + |
| 155 | + /** A channel whose calls do nothing except record the last message that was sent. */ |
| 156 | + private static class CapturingChannel extends Channel { |
| 157 | + private Object lastMessage; |
| 158 | + |
| 159 | + @Override |
| 160 | + public <ReqT, RespT> ClientCall<ReqT, RespT> newCall( |
| 161 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) { |
| 162 | + return new ClientCall<ReqT, RespT>() { |
| 163 | + @Override |
| 164 | + public void start(Listener<RespT> responseListener, io.grpc.Metadata headers) {} |
| 165 | + |
| 166 | + @Override |
| 167 | + public void request(int numMessages) {} |
| 168 | + |
| 169 | + @Override |
| 170 | + public void cancel(String message, Throwable cause) {} |
| 171 | + |
| 172 | + @Override |
| 173 | + public void halfClose() {} |
| 174 | + |
| 175 | + @Override |
| 176 | + public void sendMessage(ReqT message) { |
| 177 | + lastMessage = message; |
| 178 | + } |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public String authority() { |
| 184 | + return "fake-authority"; |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments