|
| 1 | +package com.snowflake.kafka.connector.internal.streaming.v2; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import com.snowflake.ingest.streaming.ChannelStatus; |
| 10 | +import com.snowflake.ingest.streaming.SnowflakeStreamingIngestChannel; |
| 11 | +import com.snowflake.kafka.connector.internal.SnowflakeKafkaConnectorException; |
| 12 | +import com.snowflake.kafka.connector.internal.streaming.FakeSnowflakeStreamingIngestChannel; |
| 13 | +import com.snowflake.kafka.connector.internal.telemetry.SnowflakeTelemetryService; |
| 14 | +import java.time.Duration; |
| 15 | +import java.time.Instant; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +class SnowpipeStreamingPartitionChannelStatusTest { |
| 20 | + |
| 21 | + private FakeSnowflakeStreamingIngestChannel fakeChannel; |
| 22 | + private SnowflakeTelemetryService telemetryService; |
| 23 | + private TestableSnowpipeStreamingPartitionChannel partitionChannel; |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void setUp() { |
| 27 | + fakeChannel = new FakeSnowflakeStreamingIngestChannel("testPipe", "testChannel"); |
| 28 | + telemetryService = mock(SnowflakeTelemetryService.class); |
| 29 | + partitionChannel = new TestableSnowpipeStreamingPartitionChannel(fakeChannel, telemetryService); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void shouldThrowExceptionWhenErrorCountGreaterThanZeroAndTolerateErrorsFalse() { |
| 34 | + // Given |
| 35 | + ChannelStatus statusWithErrors = createChannelStatusWithErrors(5, "Test error message"); |
| 36 | + fakeChannel.setChannelStatus(statusWithErrors); |
| 37 | + |
| 38 | + // When/Then |
| 39 | + SnowflakeKafkaConnectorException exception = |
| 40 | + assertThrows( |
| 41 | + SnowflakeKafkaConnectorException.class, |
| 42 | + () -> partitionChannel.checkChannelStatusAndLogErrors(false)); |
| 43 | + |
| 44 | + assertTrue(exception.getMessage().contains("5 errors")); |
| 45 | + assertTrue(exception.getMessage().contains("Test error message")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void shouldNotThrowExceptionWhenErrorCountGreaterThanZeroAndTolerateErrorsTrue() { |
| 50 | + // Given |
| 51 | + ChannelStatus statusWithErrors = createChannelStatusWithErrors(5, "Test error message"); |
| 52 | + fakeChannel.setChannelStatus(statusWithErrors); |
| 53 | + |
| 54 | + // When/Then |
| 55 | + assertDoesNotThrow(() -> partitionChannel.checkChannelStatusAndLogErrors(true)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void shouldNotThrowExceptionWhenNoErrors() { |
| 60 | + // Given |
| 61 | + ChannelStatus statusWithoutErrors = createChannelStatusWithErrors(0, null); |
| 62 | + fakeChannel.setChannelStatus(statusWithoutErrors); |
| 63 | + |
| 64 | + // When/Then - should not throw regardless of tolerateErrors setting |
| 65 | + assertDoesNotThrow(() -> partitionChannel.checkChannelStatusAndLogErrors(false)); |
| 66 | + assertDoesNotThrow(() -> partitionChannel.checkChannelStatusAndLogErrors(true)); |
| 67 | + } |
| 68 | + |
| 69 | + private ChannelStatus createChannelStatusWithErrors( |
| 70 | + final long rowsErrorCount, final String lastErrorMessage) { |
| 71 | + ChannelStatus status = mock(ChannelStatus.class); |
| 72 | + when(status.getDatabaseName()).thenReturn("testDatabase"); |
| 73 | + when(status.getSchemaName()).thenReturn("testSchema"); |
| 74 | + when(status.getPipeName()).thenReturn("testPipe"); |
| 75 | + when(status.getChannelName()).thenReturn("testChannel"); |
| 76 | + when(status.getStatusCode()).thenReturn("SUCCESS"); |
| 77 | + when(status.getLatestCommittedOffsetToken()).thenReturn("100"); |
| 78 | + when(status.getCreatedOn()).thenReturn(Instant.now()); |
| 79 | + when(status.getRowsInsertedCount()).thenReturn(1000L); |
| 80 | + when(status.getRowsParsedCount()).thenReturn(1005L); |
| 81 | + when(status.getRowsErrorCount()).thenReturn(rowsErrorCount); |
| 82 | + when(status.getLastErrorOffsetTokenUpperBound()).thenReturn("95"); |
| 83 | + when(status.getLastErrorMessage()).thenReturn(lastErrorMessage); |
| 84 | + when(status.getLastErrorTimestamp()).thenReturn(Instant.now()); |
| 85 | + when(status.getServerAvgProcessingLatency()).thenReturn(Duration.ofMillis(50)); |
| 86 | + when(status.getLastRefreshedOn()).thenReturn(Instant.now()); |
| 87 | + return status; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * A testable version of SnowpipeStreamingPartitionChannel that allows injecting the channel and |
| 92 | + * telemetry service directly. |
| 93 | + */ |
| 94 | + private static class TestableSnowpipeStreamingPartitionChannel { |
| 95 | + private final SnowflakeStreamingIngestChannel channel; |
| 96 | + private final SnowflakeTelemetryService telemetryServiceV2; |
| 97 | + |
| 98 | + TestableSnowpipeStreamingPartitionChannel( |
| 99 | + final SnowflakeStreamingIngestChannel channel, |
| 100 | + final SnowflakeTelemetryService telemetryService) { |
| 101 | + this.channel = channel; |
| 102 | + this.telemetryServiceV2 = telemetryService; |
| 103 | + } |
| 104 | + |
| 105 | + void checkChannelStatusAndLogErrors(final boolean tolerateErrors) { |
| 106 | + final ChannelStatus status = channel.getChannelStatus(); |
| 107 | + logChannelStatus(status); |
| 108 | + handleChannelErrors(status, tolerateErrors); |
| 109 | + } |
| 110 | + |
| 111 | + private void logChannelStatus(final ChannelStatus status) { |
| 112 | + // Just log - actual logging tested via INFO level in real implementation |
| 113 | + System.out.printf( |
| 114 | + "Channel status: databaseName=[%s], schemaName=[%s], pipeName=[%s]," |
| 115 | + + " channelName=[%s], statusCode=[%s], latestCommittedOffsetToken=[%s]," |
| 116 | + + " createdOn=[%s], rowsInsertedCount=[%d], rowsParsedCount=[%d]," |
| 117 | + + " rowsErrorCount=[%d], lastErrorOffsetTokenUpperBound=[%s]," |
| 118 | + + " lastErrorMessage=[%s], lastErrorTimestamp=[%s]," |
| 119 | + + " serverAvgProcessingLatency=[%s], lastRefreshedOn=[%s]%n", |
| 120 | + status.getDatabaseName(), |
| 121 | + status.getSchemaName(), |
| 122 | + status.getPipeName(), |
| 123 | + status.getChannelName(), |
| 124 | + status.getStatusCode(), |
| 125 | + status.getLatestCommittedOffsetToken(), |
| 126 | + status.getCreatedOn(), |
| 127 | + status.getRowsInsertedCount(), |
| 128 | + status.getRowsParsedCount(), |
| 129 | + status.getRowsErrorCount(), |
| 130 | + status.getLastErrorOffsetTokenUpperBound(), |
| 131 | + status.getLastErrorMessage(), |
| 132 | + status.getLastErrorTimestamp(), |
| 133 | + status.getServerAvgProcessingLatency(), |
| 134 | + status.getLastRefreshedOn()); |
| 135 | + } |
| 136 | + |
| 137 | + private void handleChannelErrors(final ChannelStatus status, final boolean tolerateErrors) { |
| 138 | + final long rowsErrorCount = status.getRowsErrorCount(); |
| 139 | + if (rowsErrorCount > 0) { |
| 140 | + final String errorMessage = |
| 141 | + String.format( |
| 142 | + "Channel [%s] has %d errors. Last error message: %s, last error timestamp: %s," |
| 143 | + + " last error offset token upper bound: %s", |
| 144 | + channel.getFullyQualifiedChannelName(), |
| 145 | + rowsErrorCount, |
| 146 | + status.getLastErrorMessage(), |
| 147 | + status.getLastErrorTimestamp(), |
| 148 | + status.getLastErrorOffsetTokenUpperBound()); |
| 149 | + |
| 150 | + if (tolerateErrors) { |
| 151 | + System.out.println("WARN: " + errorMessage); |
| 152 | + } else { |
| 153 | + telemetryServiceV2.reportKafkaConnectFatalError(errorMessage); |
| 154 | + throw new SnowflakeKafkaConnectorException(errorMessage, "5030"); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
0 commit comments