Skip to content
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

feat: add more context to row merging errors #281

Merged
merged 3 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -77,6 +77,13 @@ final class StateMachine<RowT> {
private State currentState;
private ByteString lastCompleteRowKey;

// debug stats
private int numScannedNotifications = 0;
private int numRowsCommitted = 0;
private int numChunksProcessed = 0;
private int numCellsInRow = 0;
private int numCellsInLastRow = 0;

// Track current cell attributes: protocol omits them when they are repeated
private ByteString rowKey;
private String familyName;
Expand Down Expand Up @@ -120,6 +127,7 @@ final class StateMachine<RowT> {
*/
void handleLastScannedRow(ByteString key) {
try {
numScannedNotifications++;
currentState = currentState.handleLastScannedRow(key);
} catch (RuntimeException e) {
currentState = null;
Expand Down Expand Up @@ -148,6 +156,7 @@ void handleLastScannedRow(ByteString key) {
*/
void handleChunk(CellChunk chunk) {
try {
numChunksProcessed++;
currentState = currentState.handleChunk(chunk);
} catch (RuntimeException e) {
currentState = null;
Expand Down Expand Up @@ -191,6 +200,7 @@ private void reset() {
expectedCellSize = 0;
remainingCellBytes = 0;
completeRow = null;
numCellsInRow = 0;

adapter.reset();
}
Expand Down Expand Up @@ -326,6 +336,7 @@ State handleChunk(CellChunk chunk) {
return AWAITING_CELL_VALUE;
}
adapter.finishCell();
numCellsInRow++;

if (!chunk.getCommitRow()) {
return AWAITING_NEW_CELL;
Expand Down Expand Up @@ -374,6 +385,7 @@ State handleChunk(CellChunk chunk) {
return AWAITING_CELL_VALUE;
}
adapter.finishCell();
numCellsInRow++;

if (!chunk.getCommitRow()) {
return AWAITING_NEW_CELL;
Expand Down Expand Up @@ -416,12 +428,29 @@ private State handleCommit() {
validate(remainingCellBytes == 0, "Can't commit with remaining bytes");
completeRow = adapter.finishRow();
lastCompleteRowKey = rowKey;
numRowsCommitted++;
numCellsInLastRow = numCellsInRow;
return AWAITING_ROW_CONSUME;
}

private static void validate(boolean condition, String message) {
private void validate(boolean condition, String message) {
if (!condition) {
throw new InvalidInputException(message);
throw new InvalidInputException(
message
+ ". numScannedNotifications: "
+ numScannedNotifications
+ ", numRowsCommitted: "
+ numRowsCommitted
+ ", numChunksProcessed: "
+ numChunksProcessed
+ ", numCellsInRow: "
+ numCellsInRow
+ ", numCellsInLastRow: "
+ numCellsInLastRow
+ ", rowKey: "
+ rowKey
+ ", lastCompleteRowKey: "
+ lastCompleteRowKey);
}
}

Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright 2020 Google LLC
*
* Licensed 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
*
* https://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 com.google.cloud.bigtable.data.v2.stub.readrows;

import static com.google.common.truth.Truth.assertThat;

import com.google.bigtable.v2.ReadRowsResponse;
import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.protobuf.ByteString;
import com.google.protobuf.BytesValue;
import com.google.protobuf.StringValue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class StateMachineTest {
StateMachine<Row> stateMachine;

@Before
public void setUp() throws Exception {
stateMachine = new StateMachine<>(new DefaultRowAdapter().createRowBuilder());
}

@Test
public void testErrorHandlingStats() {
StateMachine.InvalidInputException actualError = null;

ReadRowsResponse.CellChunk chunk =
ReadRowsResponse.CellChunk.newBuilder()
.setRowKey(ByteString.copyFromUtf8("my-key1"))
.setFamilyName(StringValue.newBuilder().setValue("my-family"))
.setQualifier(BytesValue.newBuilder().setValue(ByteString.copyFromUtf8("q")))
.setTimestampMicros(1_000)
.setValue(ByteString.copyFromUtf8("my-value"))
.setCommitRow(true)
.build();
try {
stateMachine.handleChunk(chunk);
stateMachine.consumeRow();
stateMachine.handleChunk(
chunk
.toBuilder()
.setRowKey(ByteString.copyFromUtf8("my-key2"))
.setValueSize(123) // invalid value size
.build());
} catch (StateMachine.InvalidInputException e) {
actualError = e;
}

assertThat(actualError).hasMessageThat().contains("my-key1");
assertThat(actualError).hasMessageThat().contains("my-key2");
assertThat(actualError).hasMessageThat().contains("numScannedNotifications: 0");
assertThat(actualError).hasMessageThat().contains("numChunksProcessed: 2");
assertThat(actualError).hasMessageThat().contains("numCellsInRow: 0");
assertThat(actualError).hasMessageThat().contains("numCellsInLastRow: 1");
}
}