-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add time-to-first-byte from remote metric
- Loading branch information
Showing
9 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...c/main/java/io/aiven/kafka/tieredstorage/metrics/TimeToFirstByteMeasuringInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2023 Aiven Oy | ||
* | ||
* 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 | ||
* | ||
* http://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 io.aiven.kafka.tieredstorage.metrics; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
import org.apache.kafka.common.metrics.Sensor; | ||
import org.apache.kafka.common.utils.Time; | ||
|
||
/** | ||
* The input stream that measures the time to first byte. | ||
* | ||
* <p>The time is measured on {@link #read()}, {@link #read(byte[], int, int)}, and {@link #skip(long)}. | ||
* Otherwise, the class delegates everything to the inner {@link InputStream}. | ||
*/ | ||
public class TimeToFirstByteMeasuringInputStream extends FilterInputStream { | ||
private final Time time; | ||
private final Sensor sensor; | ||
|
||
private boolean firstByteArrived = false; | ||
|
||
public TimeToFirstByteMeasuringInputStream(final InputStream in, | ||
final Time time, | ||
final Sensor sensor) { | ||
super(Objects.requireNonNull(in, "in cannot be null")); | ||
this.time = Objects.requireNonNull(time, "time cannot be null"); | ||
this.sensor = Objects.requireNonNull(sensor, "sensor cannot be null"); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (firstByteArrived) { | ||
return super.read(); | ||
} | ||
|
||
final long startMs = time.milliseconds(); | ||
firstByteArrived = true; | ||
final var r = super.read(); | ||
sensor.record(time.milliseconds() - startMs); | ||
return r; | ||
} | ||
|
||
@Override | ||
public int read(final byte[] b, final int off, final int len) throws IOException { | ||
if (firstByteArrived) { | ||
return super.read(b, off, len); | ||
} | ||
|
||
final long startMs = time.milliseconds(); | ||
firstByteArrived = true; | ||
final var r = super.read(b, off, len); | ||
sensor.record(time.milliseconds() - startMs); | ||
return r; | ||
} | ||
|
||
@Override | ||
public long skip(final long n) throws IOException { | ||
if (firstByteArrived) { | ||
return super.skip(n); | ||
} | ||
|
||
final long startMs = time.milliseconds(); | ||
firstByteArrived = true; | ||
final var r = super.skip(n); | ||
sensor.record(time.milliseconds() - startMs); | ||
return r; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...st/java/io/aiven/kafka/tieredstorage/metrics/TimeToFirstByteMeasuringInputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright 2023 Aiven Oy | ||
* | ||
* 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 | ||
* | ||
* http://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 io.aiven.kafka.tieredstorage.metrics; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
import org.apache.kafka.common.metrics.Sensor; | ||
import org.apache.kafka.common.utils.Time; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class TimeToFirstByteMeasuringInputStreamTest { | ||
@Mock | ||
Time time; | ||
@Mock | ||
Sensor sensor; | ||
TimeToFirstByteMeasuringInputStream testInputStream; | ||
|
||
@BeforeEach | ||
void setup() { | ||
when(time.milliseconds()) | ||
.thenReturn(0L) | ||
.thenReturn(3L); | ||
|
||
testInputStream = new TimeToFirstByteMeasuringInputStream( | ||
new ByteArrayInputStream(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), | ||
time, sensor); | ||
} | ||
|
||
@Test | ||
void testReadFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(0); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(1); | ||
assertThat(testInputStream.readAllBytes()).contains(2, 3, 4, 5, 6, 7, 8, 9); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
|
||
@Test | ||
void testReadArrayFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
final byte[] b = new byte[3]; | ||
assertThat(testInputStream.read(b)).isEqualTo(3); | ||
assertThat(b).contains(0, 1, 2); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(3); | ||
assertThat(testInputStream.readAllBytes()).contains(4, 5, 6, 7, 8, 9); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
|
||
@Test | ||
void testReadArrayWithOffsetAndLengthFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
final byte[] b = new byte[3]; | ||
assertThat(testInputStream.read(b, 0, 3)).isEqualTo(3); | ||
assertThat(b).contains(0, 1, 2); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(3); | ||
assertThat(testInputStream.readAllBytes()).contains(4, 5, 6, 7, 8, 9); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
|
||
@Test | ||
void testReadAllBytesFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
assertThat(testInputStream.readAllBytes()).contains(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
assertThat(testInputStream.readAllBytes()).isEmpty(); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
|
||
@Test | ||
void testReadNBytesFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
assertThat(testInputStream.readNBytes(3)).contains(0, 1, 2); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(3); | ||
assertThat(testInputStream.readAllBytes()).contains(4, 5, 6, 7, 8, 9); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
|
||
@Test | ||
void testReadNBytesWithOffsetAndLengthFirst() throws IOException { | ||
assertThat(testInputStream.available()).isEqualTo(10); | ||
|
||
final byte[] b = new byte[3]; | ||
assertThat(testInputStream.readNBytes(b, 0, 3)).isEqualTo(3); | ||
assertThat(b).contains(0, 1, 2); | ||
verify(sensor).record(3.0); | ||
|
||
assertThat(testInputStream.read()).isEqualTo(3); | ||
assertThat(testInputStream.readAllBytes()).contains(4, 5, 6, 7, 8, 9); | ||
assertThat(testInputStream.available()).isZero(); | ||
assertThat(testInputStream.read()).isEqualTo(-1); | ||
verifyNoMoreInteractions(sensor); | ||
} | ||
} |
Oops, something went wrong.