|
13 | 13 | */ |
14 | 14 | package com.facebook.presto.plugin.clp; |
15 | 15 |
|
| 16 | +import com.facebook.presto.plugin.clp.mockdb.ClpMockMetadataDatabase; |
| 17 | +import com.facebook.presto.plugin.clp.mockdb.table.ArchivesTableRows; |
| 18 | +import com.facebook.presto.plugin.clp.split.ClpMySqlSplitProvider; |
16 | 19 | import com.facebook.presto.plugin.clp.split.ClpSplitProvider; |
17 | 20 | import com.facebook.presto.spi.SchemaTableName; |
18 | 21 | import com.google.common.collect.ImmutableList; |
19 | | -import com.google.common.collect.ImmutableSet; |
| 22 | +import com.google.common.collect.ImmutableMap; |
20 | 23 | import org.testng.annotations.AfterMethod; |
21 | 24 | import org.testng.annotations.BeforeMethod; |
22 | 25 | import org.testng.annotations.Test; |
23 | 26 |
|
24 | | -import java.util.ArrayList; |
25 | | -import java.util.HashMap; |
26 | 27 | import java.util.List; |
27 | 28 | import java.util.Map; |
28 | 29 | import java.util.Optional; |
29 | 30 |
|
30 | 31 | import static com.facebook.presto.plugin.clp.ClpMetadata.DEFAULT_SCHEMA_NAME; |
31 | 32 | import static com.facebook.presto.plugin.clp.ClpMetadataDbSetUp.ARCHIVES_STORAGE_DIRECTORY_BASE; |
32 | | -import static com.facebook.presto.plugin.clp.ClpMetadataDbSetUp.ArchivesTableRow; |
33 | | -import static com.facebook.presto.plugin.clp.ClpMetadataDbSetUp.DbHandle; |
34 | | -import static com.facebook.presto.plugin.clp.ClpMetadataDbSetUp.getDbHandle; |
35 | | -import static com.facebook.presto.plugin.clp.ClpMetadataDbSetUp.setupSplit; |
36 | 33 | import static com.google.common.collect.ImmutableList.toImmutableList; |
37 | | -import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 34 | +import static java.lang.String.format; |
38 | 35 | import static org.testng.Assert.assertEquals; |
39 | 36 |
|
40 | 37 | @Test(singleThreaded = true) |
41 | 38 | public class TestClpSplit |
42 | 39 | { |
43 | | - private DbHandle dbHandle; |
| 40 | + private ClpMockMetadataDatabase mockMetadataDatabase; |
44 | 41 | private ClpSplitProvider clpSplitProvider; |
45 | | - private Map<String, List<ArchivesTableRow>> tableSplits; |
| 42 | + private Map<String, ArchivesTableRows> tableSplits; |
46 | 43 |
|
47 | 44 | @BeforeMethod |
48 | 45 | public void setUp() |
49 | 46 | { |
50 | | - dbHandle = getDbHandle("split_testdb"); |
51 | | - tableSplits = new HashMap<>(); |
52 | | - |
53 | | - int numKeys = 3; |
54 | | - int numValuesPerKey = 10; |
55 | | - |
56 | | - for (int i = 0; i < numKeys; i++) { |
57 | | - String key = "test_" + i; |
58 | | - List<ArchivesTableRow> values = new ArrayList<>(); |
59 | | - |
60 | | - for (int j = 0; j < numValuesPerKey; j++) { |
| 47 | + mockMetadataDatabase = ClpMockMetadataDatabase |
| 48 | + .builder() |
| 49 | + .build(); |
| 50 | + ImmutableList.Builder<String> tableNamesBuilder = ImmutableList.builder(); |
| 51 | + ImmutableMap.Builder<String, ArchivesTableRows> splitsMapBuilder = ImmutableMap.builder(); |
| 52 | + |
| 53 | + int numTables = 3; |
| 54 | + int numSplitsPerTable = 10; |
| 55 | + |
| 56 | + for (int i = 0; i < numTables; i++) { |
| 57 | + String tableName = "test_split_" + i; |
| 58 | + tableNamesBuilder.add(tableName); |
| 59 | + |
| 60 | + ImmutableList.Builder<String> idsBuilder = ImmutableList.builder(); |
| 61 | + ImmutableList.Builder<Long> beginTimestampsBuilder = ImmutableList.builder(); |
| 62 | + ImmutableList.Builder<Long> endTimestampsBuilder = ImmutableList.builder(); |
| 63 | + for (int j = 0; j < numSplitsPerTable; j++) { |
61 | 64 | // We generate synthetic begin_timestamp and end_timestamp values for each split |
62 | 65 | // by offsetting two base timestamps (1700000000000L and 1705000000000L) with a |
63 | 66 | // fixed increment per split (10^10 * j). |
64 | | - values.add(new ArchivesTableRow( |
65 | | - "id_" + j, |
66 | | - 1700000000000L + 10000000000L * j, |
67 | | - 1705000000000L + 10000000000L * j)); |
| 67 | + idsBuilder.add(format("id_%s", j)); |
| 68 | + beginTimestampsBuilder.add(1700000000000L + 10000000000L * j); |
| 69 | + endTimestampsBuilder.add(1705000000000L + 10000000000L * j); |
68 | 70 | } |
69 | | - |
70 | | - tableSplits.put(key, values); |
| 71 | + splitsMapBuilder.put(tableName, new ArchivesTableRows(idsBuilder.build(), beginTimestampsBuilder.build(), endTimestampsBuilder.build())); |
71 | 72 | } |
72 | | - clpSplitProvider = setupSplit(dbHandle, tableSplits); |
| 73 | + |
| 74 | + mockMetadataDatabase.addTableToDatasetsTableIfNotExist(tableNamesBuilder.build()); |
| 75 | + tableSplits = splitsMapBuilder.build(); |
| 76 | + mockMetadataDatabase.addSplits(tableSplits); |
| 77 | + ClpConfig config = new ClpConfig() |
| 78 | + .setPolymorphicTypeEnabled(true) |
| 79 | + .setMetadataDbUrl(mockMetadataDatabase.getUrl()) |
| 80 | + .setMetadataDbUser(mockMetadataDatabase.getUsername()) |
| 81 | + .setMetadataDbPassword(mockMetadataDatabase.getPassword()) |
| 82 | + .setMetadataTablePrefix(mockMetadataDatabase.getTablePrefix()); |
| 83 | + clpSplitProvider = new ClpMySqlSplitProvider(config); |
73 | 84 | } |
74 | 85 |
|
75 | 86 | @AfterMethod |
76 | 87 | public void tearDown() |
77 | 88 | { |
78 | | - ClpMetadataDbSetUp.tearDown(dbHandle); |
| 89 | + if (null != mockMetadataDatabase) { |
| 90 | + mockMetadataDatabase.teardown(); |
| 91 | + } |
79 | 92 | } |
80 | 93 |
|
81 | 94 | @Test |
82 | 95 | public void testListSplits() |
83 | 96 | { |
84 | | - for (Map.Entry<String, List<ArchivesTableRow>> entry : tableSplits.entrySet()) { |
| 97 | + for (Map.Entry<String, ArchivesTableRows> entry : tableSplits.entrySet()) { |
85 | 98 | // Without metadata filters |
86 | 99 | compareListSplitsResult(entry, Optional.empty(), ImmutableList.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); |
87 | 100 |
|
@@ -118,29 +131,25 @@ public void testListSplits() |
118 | 131 | } |
119 | 132 |
|
120 | 133 | private void compareListSplitsResult( |
121 | | - Map.Entry<String, List<ArchivesTableRow>> entry, |
| 134 | + Map.Entry<String, ArchivesTableRows> entry, |
122 | 135 | Optional<String> metadataSql, |
123 | | - List<Integer> expectedSplitIds) |
| 136 | + List<Integer> expectedSplitIndexes) |
124 | 137 | { |
125 | 138 | String tableName = entry.getKey(); |
126 | 139 | String tablePath = ARCHIVES_STORAGE_DIRECTORY_BASE + tableName; |
127 | 140 | ClpTableLayoutHandle layoutHandle = new ClpTableLayoutHandle( |
128 | 141 | new ClpTableHandle(new SchemaTableName(DEFAULT_SCHEMA_NAME, tableName), tablePath), |
129 | 142 | Optional.empty(), |
130 | 143 | metadataSql); |
131 | | - List<ArchivesTableRow> expectedSplits = expectedSplitIds.stream() |
132 | | - .map(expectedSplitId -> entry.getValue().get(expectedSplitId)) |
| 144 | + List<String> expectedSplitPaths = expectedSplitIndexes.stream() |
| 145 | + .map(expectedSplitIndex -> format("%s/%s", tablePath, entry.getValue().getIds().get(expectedSplitIndex))) |
133 | 146 | .collect(toImmutableList()); |
134 | 147 | List<ClpSplit> actualSplits = clpSplitProvider.listSplits(layoutHandle); |
135 | | - assertEquals(actualSplits.size(), expectedSplits.size()); |
| 148 | + assertEquals(actualSplits.size(), expectedSplitPaths.size()); |
136 | 149 |
|
137 | | - ImmutableSet<String> actualSplitPaths = actualSplits.stream() |
| 150 | + ImmutableList<String> actualSplitPaths = actualSplits.stream() |
138 | 151 | .map(ClpSplit::getPath) |
139 | | - .collect(toImmutableSet()); |
140 | | - |
141 | | - ImmutableSet<String> expectedSplitPaths = expectedSplits.stream() |
142 | | - .map(split -> tablePath + "/" + split.getId()) |
143 | | - .collect(toImmutableSet()); |
| 152 | + .collect(toImmutableList()); |
144 | 153 |
|
145 | 154 | assertEquals(actualSplitPaths, expectedSplitPaths); |
146 | 155 | } |
|
0 commit comments