|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.runners.kafka.streams.translation; |
| 19 | + |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.junit.Assert.assertThrows; |
| 23 | + |
| 24 | +import org.apache.beam.sdk.transforms.windowing.BoundedWindow; |
| 25 | +import org.joda.time.Instant; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +/** Tests for {@link WatermarkManager}. */ |
| 29 | +public class WatermarkManagerTest { |
| 30 | + |
| 31 | + private static Instant ts(long millis) { |
| 32 | + return new Instant(millis); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void holdsBeforeAnyReport() { |
| 37 | + WatermarkManager manager = new WatermarkManager(); |
| 38 | + assertThat(manager.isReady(), is(false)); |
| 39 | + assertThat(manager.advance(), is(BoundedWindow.TIMESTAMP_MIN_VALUE)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void holdsUntilEverySourcePartitionReports() { |
| 44 | + WatermarkManager manager = new WatermarkManager(); |
| 45 | + manager.observe(0, ts(100L), 4); |
| 46 | + manager.observe(1, ts(100L), 4); |
| 47 | + manager.observe(2, ts(100L), 4); |
| 48 | + // Three of four partitions reported — still holding. |
| 49 | + assertThat(manager.isReady(), is(false)); |
| 50 | + assertThat(manager.advance(), is(BoundedWindow.TIMESTAMP_MIN_VALUE)); |
| 51 | + |
| 52 | + manager.observe(3, ts(100L), 4); |
| 53 | + assertThat(manager.isReady(), is(true)); |
| 54 | + assertThat(manager.advance(), is(ts(100L))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void emitsMinAcrossPartitionsOnceReady() { |
| 59 | + WatermarkManager manager = new WatermarkManager(); |
| 60 | + manager.observe(0, ts(300L), 4); |
| 61 | + manager.observe(1, ts(100L), 4); |
| 62 | + manager.observe(2, ts(500L), 4); |
| 63 | + manager.observe(3, ts(200L), 4); |
| 64 | + // min(300, 100, 500, 200) = 100 |
| 65 | + assertThat(manager.advance(), is(ts(100L))); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void perPartitionWatermarkIsMonotonic() { |
| 70 | + WatermarkManager manager = new WatermarkManager(); |
| 71 | + manager.observe(0, ts(100L), 1); |
| 72 | + assertThat(manager.advance(), is(ts(100L))); |
| 73 | + // A lower out-of-order report for the same partition is ignored. |
| 74 | + manager.observe(0, ts(50L), 1); |
| 75 | + assertThat(manager.advance(), is(ts(100L))); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void partitionCountChangeClearsReportsAndReopensHold() { |
| 80 | + WatermarkManager manager = new WatermarkManager(); |
| 81 | + manager.observe(0, ts(100L), 2); |
| 82 | + manager.observe(1, ts(100L), 2); |
| 83 | + assertThat(manager.advance(), is(ts(100L))); |
| 84 | + |
| 85 | + // Source set grows to 4. The previous reports are dropped, so the stage holds again until all |
| 86 | + // four of the new partition set have reported. |
| 87 | + manager.observe(0, ts(200L), 4); |
| 88 | + assertThat(manager.reportedPartitionCount(), is(1)); |
| 89 | + assertThat(manager.isReady(), is(false)); |
| 90 | + assertThat(manager.advance(), is(BoundedWindow.TIMESTAMP_MIN_VALUE)); |
| 91 | + |
| 92 | + manager.observe(1, ts(200L), 4); |
| 93 | + manager.observe(2, ts(200L), 4); |
| 94 | + manager.observe(3, ts(200L), 4); |
| 95 | + assertThat(manager.isReady(), is(true)); |
| 96 | + assertThat(manager.advance(), is(ts(200L))); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void emittedWatermarkDoesNotRegressAfterRepartition() { |
| 101 | + WatermarkManager manager = new WatermarkManager(); |
| 102 | + manager.observe(0, ts(100L), 2); |
| 103 | + manager.observe(1, ts(100L), 2); |
| 104 | + assertThat(manager.advance(), is(ts(100L))); |
| 105 | + |
| 106 | + // After a repartition to 3, the new partitions report older watermarks. Once ready again the |
| 107 | + // min is 50, but the emitted stage watermark must not go backwards below 100. |
| 108 | + manager.observe(0, ts(50L), 3); |
| 109 | + manager.observe(1, ts(50L), 3); |
| 110 | + manager.observe(2, ts(50L), 3); |
| 111 | + assertThat(manager.isReady(), is(true)); |
| 112 | + assertThat(manager.advance(), is(ts(100L))); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void partitionCountDecreaseClearsAndRecomputes() { |
| 117 | + WatermarkManager manager = new WatermarkManager(); |
| 118 | + manager.observe(0, ts(100L), 4); |
| 119 | + manager.observe(1, ts(100L), 4); |
| 120 | + manager.observe(2, ts(100L), 4); |
| 121 | + manager.observe(3, ts(50L), 4); |
| 122 | + assertThat(manager.advance(), is(ts(50L))); |
| 123 | + |
| 124 | + // Source set shrinks to 2. Reports are cleared; the stage holds until {0, 1} report again. |
| 125 | + manager.observe(0, ts(100L), 2); |
| 126 | + assertThat(manager.expectedSourcePartitionCount(), is(2)); |
| 127 | + assertThat(manager.reportedPartitionCount(), is(1)); |
| 128 | + assertThat(manager.isReady(), is(false)); |
| 129 | + |
| 130 | + manager.observe(1, ts(100L), 2); |
| 131 | + assertThat(manager.isReady(), is(true)); |
| 132 | + // min is 100; the non-regression clamp keeps it >= the previously emitted 50. |
| 133 | + assertThat(manager.advance(), is(ts(100L))); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void rejectsNullWatermark() { |
| 138 | + WatermarkManager manager = new WatermarkManager(); |
| 139 | + assertThrows(IllegalArgumentException.class, () -> manager.observe(0, null, 4)); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void rejectsNonPositiveTotalSourcePartitions() { |
| 144 | + WatermarkManager manager = new WatermarkManager(); |
| 145 | + assertThrows(IllegalArgumentException.class, () -> manager.observe(0, ts(100L), 0)); |
| 146 | + assertThrows(IllegalArgumentException.class, () -> manager.observe(0, ts(100L), -1)); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void rejectsOutOfRangeSourcePartition() { |
| 151 | + WatermarkManager manager = new WatermarkManager(); |
| 152 | + assertThrows(IllegalArgumentException.class, () -> manager.observe(-1, ts(100L), 4)); |
| 153 | + assertThrows(IllegalArgumentException.class, () -> manager.observe(4, ts(100L), 4)); |
| 154 | + } |
| 155 | +} |
0 commit comments