|
21 | 21 | package com.apple.foundationdb.record.provider.foundationdb;
|
22 | 22 |
|
23 | 23 | import com.apple.foundationdb.record.IndexBuildProto;
|
| 24 | +import com.apple.foundationdb.record.RecordCoreException; |
24 | 25 | import com.apple.foundationdb.record.metadata.Index;
|
25 | 26 | import com.apple.foundationdb.record.metadata.IndexOptions;
|
26 | 27 | import com.apple.foundationdb.record.metadata.IndexTypes;
|
27 | 28 | import com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression;
|
| 29 | +import com.apple.foundationdb.tuple.Tuple; |
28 | 30 | import org.assertj.core.api.Assertions;
|
29 | 31 | import org.junit.jupiter.api.Test;
|
30 | 32 |
|
|
33 | 35 | import java.util.List;
|
34 | 36 | import java.util.Map;
|
35 | 37 | import java.util.UUID;
|
| 38 | +import java.util.concurrent.atomic.AtomicLong; |
36 | 39 | import java.util.stream.Collectors;
|
| 40 | +import java.util.stream.IntStream; |
37 | 41 |
|
38 | 42 | import static com.apple.foundationdb.record.metadata.Key.Expressions.field;
|
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
39 | 45 |
|
40 | 46 | /**
|
41 | 47 | * Verify indexing heartbeat activity (query & clear).
|
42 | 48 | */
|
43 |
| -public class OnlineIndexingHeartbeatTest extends OnlineIndexerTest { |
| 49 | +class OnlineIndexingHeartbeatTest extends OnlineIndexerTest { |
| 50 | + |
| 51 | + |
44 | 52 | @Test
|
45 | 53 | void testHeartbeatLowLevel() {
|
46 | 54 | List<Index> indexes = new ArrayList<>();
|
@@ -98,4 +106,78 @@ void testHeartbeatLowLevel() {
|
98 | 106 | Assertions.assertThat(queried).isEmpty();
|
99 | 107 | }
|
100 | 108 | }
|
| 109 | + |
| 110 | + @Test |
| 111 | + void testMutualIndexersHeartbeatsClearAfterBuild() { |
| 112 | + // Assert that the heartbeats are cleared after building |
| 113 | + List<Index> indexes = new ArrayList<>(); |
| 114 | + indexes.add(new Index("indexA", field("num_value_2"), EmptyKeyExpression.EMPTY, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS)); |
| 115 | + indexes.add(new Index("indexC", field("num_value_unique"), EmptyKeyExpression.EMPTY, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS)); |
| 116 | + int numRecords = 77; |
| 117 | + populateData(numRecords); |
| 118 | + int boundarySize = 23; |
| 119 | + final List<Tuple> boundariesList = getBoundariesList(numRecords, boundarySize); |
| 120 | + FDBRecordStoreTestBase.RecordMetaDataHook hook = allIndexesHook(indexes); |
| 121 | + openSimpleMetaData(hook); |
| 122 | + disableAll(indexes); |
| 123 | + |
| 124 | + IntStream.rangeClosed(1, 5).parallel().forEach(i -> { |
| 125 | + try (OnlineIndexer indexer = newIndexerBuilder(indexes) |
| 126 | + .setIndexingPolicy(OnlineIndexer.IndexingPolicy.newBuilder() |
| 127 | + .setMutualIndexingBoundaries(boundariesList)) |
| 128 | + .build()) { |
| 129 | + indexer.buildIndex(); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + for (Index index : indexes) { |
| 134 | + try (OnlineIndexer indexer = newIndexerBuilder(index).build()) { |
| 135 | + Assertions.assertThat(indexer.getIndexingHeartbeats(0)).isEmpty(); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testMutualIndexersHeartbeatsClearAfterCrash() { |
| 142 | + // Assert that the heartbeats are cleared after crash |
| 143 | + List<Index> indexes = new ArrayList<>(); |
| 144 | + indexes.add(new Index("indexA", field("num_value_2"), EmptyKeyExpression.EMPTY, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS)); |
| 145 | + indexes.add(new Index("indexC", field("num_value_unique"), EmptyKeyExpression.EMPTY, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS)); |
| 146 | + int numRecords = 98; |
| 147 | + populateData(numRecords); |
| 148 | + int boundarySize = 20; |
| 149 | + final List<Tuple> boundariesList = getBoundariesList(numRecords, boundarySize); |
| 150 | + FDBRecordStoreTestBase.RecordMetaDataHook hook = allIndexesHook(indexes); |
| 151 | + openSimpleMetaData(hook); |
| 152 | + disableAll(indexes); |
| 153 | + |
| 154 | + final String testThrowMsg = "Intentionally crash during test"; |
| 155 | + IntStream.rangeClosed(1, 9).parallel().forEach(i -> { |
| 156 | + final AtomicLong counter = new AtomicLong(0); |
| 157 | + try (OnlineIndexer indexer = newIndexerBuilder(indexes) |
| 158 | + .setIndexingPolicy(OnlineIndexer.IndexingPolicy.newBuilder() |
| 159 | + .setMutualIndexingBoundaries(boundariesList) |
| 160 | + .build()) |
| 161 | + .setConfigLoader(old -> { |
| 162 | + // Unfortunately, we cannot verify that at least one heartbeat exists from this |
| 163 | + // block, as it would have been nesting "asyncToSync" functions. But there are other tests |
| 164 | + // that verify the "sync lock" functionality. |
| 165 | + if (counter.incrementAndGet() > 2) { |
| 166 | + throw new RecordCoreException(testThrowMsg); |
| 167 | + } |
| 168 | + return old; |
| 169 | + }) |
| 170 | + .build()) { |
| 171 | + RecordCoreException e = assertThrows(RecordCoreException.class, indexer::buildIndex); |
| 172 | + assertTrue(e.getMessage().contains(testThrowMsg)); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + for (Index index : indexes) { |
| 177 | + try (OnlineIndexer indexer = newIndexerBuilder(index).build()) { |
| 178 | + Assertions.assertThat(indexer.getIndexingHeartbeats(0)).isEmpty(); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
101 | 183 | }
|
0 commit comments