|
| 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 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.accumulo.server; |
| 20 | + |
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.SortedMap; |
| 29 | +import java.util.TreeMap; |
| 30 | + |
| 31 | +import org.apache.accumulo.core.data.Key; |
| 32 | +import org.apache.accumulo.core.data.Range; |
| 33 | +import org.apache.accumulo.core.data.TableId; |
| 34 | +import org.apache.accumulo.core.data.Value; |
| 35 | +import org.apache.accumulo.core.dataImpl.KeyExtent; |
| 36 | +import org.apache.accumulo.core.iteratorsImpl.system.SortedMapIterator; |
| 37 | +import org.apache.accumulo.core.metadata.ReferencedTabletFile; |
| 38 | +import org.apache.accumulo.core.metadata.StoredTabletFile; |
| 39 | +import org.apache.accumulo.core.metadata.schema.DataFileValue; |
| 40 | +import org.apache.accumulo.core.metadata.schema.MetadataSchema; |
| 41 | +import org.apache.accumulo.core.metadata.schema.TabletMetadata; |
| 42 | +import org.apache.accumulo.server.metadata.iterators.SetEqualityIterator; |
| 43 | +import org.apache.hadoop.fs.Path; |
| 44 | +import org.apache.hadoop.io.Text; |
| 45 | +import org.junit.jupiter.api.BeforeEach; |
| 46 | +import org.junit.jupiter.api.Test; |
| 47 | + |
| 48 | +public class SetEqualityIteratorTest { |
| 49 | + |
| 50 | + private SetEqualityIterator setEqualityIterator; |
| 51 | + private SetEqualityIterator setEqualityIteratorNoFiles; |
| 52 | + private SetEqualityIterator setEqualityIteratorOneFile; |
| 53 | + private SortedMapIterator sortedMapIterator; |
| 54 | + private SortedMapIterator sortedMapIteratorNoFiles; |
| 55 | + private SortedMapIterator sortedMapIteratorOneFile; |
| 56 | + |
| 57 | + private KeyExtent extent = new KeyExtent(TableId.of("5"), new Text("df"), new Text("da")); |
| 58 | + |
| 59 | + private StoredTabletFile file1 = |
| 60 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0001/sf1.rf")).insert(); |
| 61 | + private StoredTabletFile file2 = |
| 62 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0001/sf2.rf")).insert(); |
| 63 | + private StoredTabletFile file3 = |
| 64 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0001/sf3.rf")).insert(); |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + public void setUp() throws IOException { |
| 68 | + |
| 69 | + // Create tablet metadata with no files |
| 70 | + TabletMetadata tmNoFiles = TabletMetadata.builder(extent).putFlushId(7).build(); |
| 71 | + |
| 72 | + // Create tablet metadata with one file |
| 73 | + StoredTabletFile singleFile = |
| 74 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0001/sf1.rf")).insert(); |
| 75 | + TabletMetadata tmOneFile = TabletMetadata.builder(extent) |
| 76 | + .putFile(singleFile, new DataFileValue(100, 50)).putFlushId(8).build(); |
| 77 | + |
| 78 | + // Create tablet metadata with multiple files |
| 79 | + TabletMetadata tmMultipleFiles = TabletMetadata.builder(extent) |
| 80 | + .putFile(file1, new DataFileValue(0, 0)).putFile(file2, new DataFileValue(555, 23)) |
| 81 | + .putFile(file3, new DataFileValue(234, 13)).putFlushId(6).build(); |
| 82 | + |
| 83 | + var extent2 = new KeyExtent(extent.tableId(), null, extent.endRow()); |
| 84 | + // create another tablet metadata using extent2 w/ diff files and add it to sortedMap. This |
| 85 | + // will add another row to the test data which ensures that iterator does not go to another row. |
| 86 | + StoredTabletFile file4 = |
| 87 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0002/sf4.rf")).insert(); |
| 88 | + StoredTabletFile file5 = |
| 89 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0002/sf5.rf")).insert(); |
| 90 | + StoredTabletFile file6 = |
| 91 | + new ReferencedTabletFile(new Path("dfs://nn1/acc/tables/1/t-0002/sf6.rf")).insert(); |
| 92 | + TabletMetadata tmMultipleFiles2 = TabletMetadata.builder(extent2) |
| 93 | + .putFile(file4, new DataFileValue(100, 50)).putFile(file5, new DataFileValue(200, 75)) |
| 94 | + .putFile(file6, new DataFileValue(300, 100)).putFlushId(7).build(); |
| 95 | + |
| 96 | + // Convert TabletMetadata to a SortedMap |
| 97 | + SortedMap<Key,Value> sortedMapNoFiles = new TreeMap<>(tmNoFiles.getKeyValues()); |
| 98 | + SortedMap<Key,Value> sortedMapOneFile = new TreeMap<>(tmOneFile.getKeyValues()); |
| 99 | + SortedMap<Key,Value> sortedMap = new TreeMap<>(tmMultipleFiles.getKeyValues()); |
| 100 | + SortedMap<Key,Value> sortedMap2 = new TreeMap<>(tmMultipleFiles2.getKeyValues()); |
| 101 | + // Add the second tablet metadata to the sortedMap |
| 102 | + sortedMap.putAll(sortedMap2); |
| 103 | + |
| 104 | + // Create a SortedMapIterator using the SortedMap |
| 105 | + sortedMapIterator = new SortedMapIterator(sortedMap); |
| 106 | + sortedMapIteratorNoFiles = new SortedMapIterator(sortedMapNoFiles); |
| 107 | + sortedMapIteratorOneFile = new SortedMapIterator(sortedMapOneFile); |
| 108 | + |
| 109 | + // Set the SortedMapIterator as the source for SetEqualityIterator |
| 110 | + setEqualityIterator = new SetEqualityIterator(); |
| 111 | + setEqualityIterator.init(sortedMapIterator, Collections.emptyMap(), null); |
| 112 | + setEqualityIteratorNoFiles = new SetEqualityIterator(); |
| 113 | + setEqualityIteratorNoFiles.init(sortedMapIteratorNoFiles, Collections.emptyMap(), null); |
| 114 | + setEqualityIteratorOneFile = new SetEqualityIterator(); |
| 115 | + setEqualityIteratorOneFile.init(sortedMapIteratorOneFile, Collections.emptyMap(), null); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testTabletWithNoFiles() throws IOException { |
| 120 | + // Creating a test range |
| 121 | + Text tabletRow = new Text(extent.toMetaRow()); |
| 122 | + Text family = MetadataSchema.TabletsSection.DataFileColumnFamily.NAME; |
| 123 | + |
| 124 | + Range range = Range.exact(tabletRow, family); |
| 125 | + |
| 126 | + // Invoking the seek method |
| 127 | + setEqualityIteratorNoFiles.seek(range, Collections.emptyList(), false); |
| 128 | + |
| 129 | + // Asserting the result |
| 130 | + assertEquals(new Key(tabletRow, family), setEqualityIteratorNoFiles.getTopKey()); |
| 131 | + // The iterator should produce a value that is equal to the expected value on the condition |
| 132 | + var condition = SetEqualityIterator.createCondition(Collections.emptySet(), |
| 133 | + storedTabletFile -> ((StoredTabletFile) storedTabletFile).getMetadata().getBytes(UTF_8), |
| 134 | + family); |
| 135 | + assertArrayEquals(condition.getValue().toArray(), |
| 136 | + setEqualityIteratorNoFiles.getTopValue().get()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testTabletWithOneFile() throws IOException { |
| 141 | + // Creating a test range |
| 142 | + Text tabletRow = new Text(extent.toMetaRow()); |
| 143 | + Text family = MetadataSchema.TabletsSection.DataFileColumnFamily.NAME; |
| 144 | + |
| 145 | + Range range = Range.exact(tabletRow, family); |
| 146 | + |
| 147 | + // Invoking the seek method |
| 148 | + setEqualityIteratorOneFile.seek(range, Collections.emptyList(), false); |
| 149 | + |
| 150 | + // Asserting the result |
| 151 | + assertEquals(new Key(tabletRow, family), setEqualityIteratorOneFile.getTopKey()); |
| 152 | + // The iterator should produce a value that is equal to the expected value on the condition |
| 153 | + var condition = SetEqualityIterator.createCondition(Collections.singleton(file1), |
| 154 | + storedTabletFile -> storedTabletFile.getMetadata().getBytes(UTF_8), family); |
| 155 | + assertArrayEquals(condition.getValue().toArray(), |
| 156 | + setEqualityIteratorOneFile.getTopValue().get()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testTabletWithMultipleFiles() throws IOException { |
| 161 | + // Creating a test range |
| 162 | + Text tabletRow = new Text(extent.toMetaRow()); |
| 163 | + Text family = MetadataSchema.TabletsSection.DataFileColumnFamily.NAME; |
| 164 | + |
| 165 | + Range range = Range.exact(tabletRow, family); |
| 166 | + |
| 167 | + // Invoking the seek method |
| 168 | + setEqualityIterator.seek(range, Collections.emptyList(), false); |
| 169 | + |
| 170 | + // Asserting the result |
| 171 | + assertEquals(new Key(tabletRow, family), setEqualityIterator.getTopKey()); |
| 172 | + // The iterator should produce a value that is equal to the expected value on the condition |
| 173 | + var condition = SetEqualityIterator.createCondition(Set.of(file1, file2, file3), |
| 174 | + storedTabletFile -> storedTabletFile.getMetadata().getBytes(UTF_8), family); |
| 175 | + assertArrayEquals(condition.getValue().toArray(), setEqualityIterator.getTopValue().get()); |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments