|
| 1 | +/* |
| 2 | + * OnlineIndexingHeartbeatTest.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.provider.foundationdb; |
| 22 | + |
| 23 | +import com.apple.foundationdb.record.IndexBuildProto; |
| 24 | +import com.apple.foundationdb.record.metadata.Index; |
| 25 | +import com.apple.foundationdb.record.metadata.IndexOptions; |
| 26 | +import com.apple.foundationdb.record.metadata.IndexTypes; |
| 27 | +import com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression; |
| 28 | +import org.assertj.core.api.Assertions; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.UUID; |
| 36 | +import java.util.stream.Collectors; |
| 37 | + |
| 38 | +import static com.apple.foundationdb.record.metadata.Key.Expressions.field; |
| 39 | + |
| 40 | +/** |
| 41 | + * Verify indexing heartbeat activity (query & clear). |
| 42 | + */ |
| 43 | +public class OnlineIndexingHeartbeatTest extends OnlineIndexerTest { |
| 44 | + @Test |
| 45 | + void testHeartbeatLowLevel() { |
| 46 | + List<Index> indexes = new ArrayList<>(); |
| 47 | + indexes.add(new Index("indexA", field("num_value_2"), EmptyKeyExpression.EMPTY, IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS)); |
| 48 | + indexes.add(new Index("indexB", field("num_value_3_indexed"), IndexTypes.VALUE)); |
| 49 | + FDBRecordStoreTestBase.RecordMetaDataHook hook = allIndexesHook(indexes); |
| 50 | + |
| 51 | + final int count = 10; |
| 52 | + IndexingHeartbeat[] heartbeats = new IndexingHeartbeat[count]; |
| 53 | + for (int i = 0; i < count; i++) { |
| 54 | + heartbeats[i] = new IndexingHeartbeat(UUID.randomUUID(), IndexBuildProto.IndexBuildIndexingStamp.Method.BY_INDEX, 100 + i); |
| 55 | + } |
| 56 | + |
| 57 | + openSimpleMetaData(hook); |
| 58 | + try (FDBRecordContext context = openContext()) { |
| 59 | + for (var heartbeat : heartbeats) { |
| 60 | + heartbeat.updateHeartbeat(recordStore, indexes.get(0)); |
| 61 | + heartbeat.updateHeartbeat(recordStore, indexes.get(1)); |
| 62 | + } |
| 63 | + context.commit(); |
| 64 | + } |
| 65 | + |
| 66 | + // Verify query/clear operation |
| 67 | + try (OnlineIndexer indexer = newIndexerBuilder(indexes.get(0)).build()) { |
| 68 | + // Query, unlimited |
| 69 | + Map<UUID, IndexBuildProto.IndexBuildHeartbeat> queried = indexer.getIndexingHeartbeats(0); |
| 70 | + Assertions.assertThat(queried).hasSize(count); |
| 71 | + Assertions.assertThat(queried.keySet()) |
| 72 | + .containsExactlyInAnyOrderElementsOf(Arrays.stream(heartbeats).map(ht -> ht.sessionId).collect(Collectors.toList())); |
| 73 | + |
| 74 | + // Query, partial |
| 75 | + queried = indexer.getIndexingHeartbeats(5); |
| 76 | + Assertions.assertThat(queried).hasSize(5); |
| 77 | + |
| 78 | + // clear, partial |
| 79 | + int countDeleted = indexer.clearIndexingHeartbeats(0, 7); |
| 80 | + Assertions.assertThat(countDeleted).isEqualTo(7); |
| 81 | + queried = indexer.getIndexingHeartbeats(5); |
| 82 | + Assertions.assertThat(queried).hasSize(3); |
| 83 | + } |
| 84 | + |
| 85 | + // Verify that the previous clear does not affect other index |
| 86 | + try (OnlineIndexer indexer = newIndexerBuilder(indexes.get(1)).build()) { |
| 87 | + Map<UUID, IndexBuildProto.IndexBuildHeartbeat> queried = indexer.getIndexingHeartbeats(100); |
| 88 | + Assertions.assertThat(queried).hasSize(count); |
| 89 | + Assertions.assertThat(queried.keySet()) |
| 90 | + .containsExactlyInAnyOrderElementsOf(Arrays.stream(heartbeats).map(ht -> ht.sessionId).collect(Collectors.toList())); |
| 91 | + |
| 92 | + // clear all |
| 93 | + int countDeleted = indexer.clearIndexingHeartbeats(0, 0); |
| 94 | + Assertions.assertThat(countDeleted).isEqualTo(count); |
| 95 | + |
| 96 | + // verify empty |
| 97 | + queried = indexer.getIndexingHeartbeats(0); |
| 98 | + Assertions.assertThat(queried).isEmpty(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments