|
| 1 | +package com.linkedin.venice.endToEnd; |
| 2 | + |
| 3 | +import static com.linkedin.venice.ConfigKeys.CLIENT_SYSTEM_STORE_REPOSITORY_REFRESH_INTERVAL_SECONDS; |
| 4 | +import static com.linkedin.venice.ConfigKeys.CLIENT_USE_SYSTEM_STORE_REPOSITORY; |
| 5 | +import static com.linkedin.venice.ConfigKeys.DATA_BASE_PATH; |
| 6 | +import static com.linkedin.venice.ConfigKeys.PERSISTENCE_TYPE; |
| 7 | +import static com.linkedin.venice.utils.TestWriteUtils.getTempDataDirectory; |
| 8 | +import static com.linkedin.venice.vpj.VenicePushJobConstants.DEFAULT_VALUE_FIELD_PROP; |
| 9 | +import static org.testng.Assert.assertNotNull; |
| 10 | + |
| 11 | +import com.linkedin.d2.balancer.D2Client; |
| 12 | +import com.linkedin.davinci.client.DaVinciClient; |
| 13 | +import com.linkedin.davinci.client.DaVinciConfig; |
| 14 | +import com.linkedin.davinci.client.factory.CachingDaVinciClientFactory; |
| 15 | +import com.linkedin.venice.D2.D2ClientUtils; |
| 16 | +import com.linkedin.venice.controllerapi.ControllerClient; |
| 17 | +import com.linkedin.venice.controllerapi.SchemaResponse; |
| 18 | +import com.linkedin.venice.integration.utils.D2TestUtils; |
| 19 | +import com.linkedin.venice.integration.utils.ServiceFactory; |
| 20 | +import com.linkedin.venice.integration.utils.VeniceClusterCreateOptions; |
| 21 | +import com.linkedin.venice.integration.utils.VeniceClusterWrapper; |
| 22 | +import com.linkedin.venice.integration.utils.VeniceRouterWrapper; |
| 23 | +import com.linkedin.venice.meta.PersistenceType; |
| 24 | +import com.linkedin.venice.utils.IntegrationTestPushUtils; |
| 25 | +import com.linkedin.venice.utils.PropertyBuilder; |
| 26 | +import com.linkedin.venice.utils.TestUtils; |
| 27 | +import com.linkedin.venice.utils.TestWriteUtils; |
| 28 | +import com.linkedin.venice.utils.Time; |
| 29 | +import com.linkedin.venice.utils.Utils; |
| 30 | +import com.linkedin.venice.utils.VeniceProperties; |
| 31 | +import io.tehuti.Metric; |
| 32 | +import io.tehuti.metrics.MetricsRepository; |
| 33 | +import java.io.File; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Properties; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | +import org.apache.avro.Schema; |
| 41 | +import org.testng.Assert; |
| 42 | +import org.testng.annotations.AfterClass; |
| 43 | +import org.testng.annotations.BeforeClass; |
| 44 | +import org.testng.annotations.Test; |
| 45 | + |
| 46 | + |
| 47 | +public class TestDaVinciRequestBasedMetaRepository { |
| 48 | + private static final int TEST_TIMEOUT = 2 * Time.MS_PER_MINUTE; |
| 49 | + |
| 50 | + private static final String CLUSTER_NAME = "venice-cluster"; |
| 51 | + private VeniceClusterWrapper clusterWrapper; |
| 52 | + |
| 53 | + private static final String storeNameStringToString = "store-name-string-to-string"; |
| 54 | + private static final String storeNameStringToNameRecord = "store-name-string-to-name-record"; |
| 55 | + |
| 56 | + // StoreName -> ControllerClient |
| 57 | + // Using map to check which stores are created |
| 58 | + private final Map<String, ControllerClient> controllerClients = new HashMap<>(); |
| 59 | + // StoreName -> Directory |
| 60 | + private final Map<String, File> pushJobAvroDataDirs = new HashMap<>(); |
| 61 | + |
| 62 | + private DaVinciConfig daVinciConfig; |
| 63 | + private MetricsRepository dvcMetricsRepo; |
| 64 | + private D2Client daVinciD2RemoteFabric; |
| 65 | + private CachingDaVinciClientFactory daVinciClientFactory; |
| 66 | + |
| 67 | + @BeforeClass(alwaysRun = true) |
| 68 | + public void setUp() throws IOException { |
| 69 | + |
| 70 | + VeniceClusterCreateOptions.Builder options = new VeniceClusterCreateOptions.Builder().clusterName(CLUSTER_NAME) |
| 71 | + .numberOfRouters(1) |
| 72 | + .numberOfServers(2) |
| 73 | + .numberOfControllers(2) |
| 74 | + .replicationFactor(2) |
| 75 | + .forkServer(false); |
| 76 | + clusterWrapper = ServiceFactory.getVeniceCluster(options.build()); |
| 77 | + |
| 78 | + // Create stores |
| 79 | + runPushJob( // String to String |
| 80 | + storeNameStringToString, |
| 81 | + TestWriteUtils |
| 82 | + .writeSimpleAvroFileWithStringToStringSchema(getPushJobAvroFileDirectory(storeNameStringToString))); |
| 83 | + runPushJob( // String to Name Record |
| 84 | + storeNameStringToNameRecord, |
| 85 | + TestWriteUtils.writeSimpleAvroFileWithStringToNameRecordV1Schema( |
| 86 | + getPushJobAvroFileDirectory(storeNameStringToNameRecord))); |
| 87 | + |
| 88 | + // Set up DVC Client Factory |
| 89 | + VeniceProperties backendConfig = |
| 90 | + new PropertyBuilder().put(DATA_BASE_PATH, Utils.getTempDataDirectory().getAbsolutePath()) |
| 91 | + .put(PERSISTENCE_TYPE, PersistenceType.ROCKS_DB) |
| 92 | + .put(CLIENT_USE_SYSTEM_STORE_REPOSITORY, true) |
| 93 | + .put(CLIENT_SYSTEM_STORE_REPOSITORY_REFRESH_INTERVAL_SECONDS, 1) |
| 94 | + .build(); |
| 95 | + daVinciConfig = new DaVinciConfig(); |
| 96 | + daVinciConfig.setUseRequestBasedMetaRepository(true); |
| 97 | + daVinciD2RemoteFabric = D2TestUtils.getAndStartD2Client(clusterWrapper.getZk().getAddress()); |
| 98 | + dvcMetricsRepo = new MetricsRepository(); |
| 99 | + daVinciClientFactory = new CachingDaVinciClientFactory( |
| 100 | + daVinciD2RemoteFabric, |
| 101 | + VeniceRouterWrapper.CLUSTER_DISCOVERY_D2_SERVICE_NAME, |
| 102 | + dvcMetricsRepo, |
| 103 | + backendConfig); |
| 104 | + } |
| 105 | + |
| 106 | + @AfterClass(alwaysRun = true) |
| 107 | + public void cleanUp() { |
| 108 | + |
| 109 | + // Shutdown remote fabric |
| 110 | + D2ClientUtils.shutdownClient(daVinciD2RemoteFabric); |
| 111 | + |
| 112 | + // Close client factory |
| 113 | + daVinciClientFactory.close(); |
| 114 | + |
| 115 | + // Close controller clients |
| 116 | + for (Map.Entry<String, ControllerClient> entry: controllerClients.entrySet()) { |
| 117 | + entry.getValue().close(); |
| 118 | + } |
| 119 | + |
| 120 | + // Close cluster wrapper |
| 121 | + clusterWrapper.close(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test(timeOut = TEST_TIMEOUT) |
| 125 | + public void testDVCRequestBasedMetaRepositoryStringToString() |
| 126 | + throws IOException, ExecutionException, InterruptedException { |
| 127 | + |
| 128 | + try (DaVinciClient<String, Object> storeClient = |
| 129 | + daVinciClientFactory.getAndStartGenericAvroClient(storeNameStringToString, daVinciConfig)) { |
| 130 | + storeClient.subscribeAll().get(); |
| 131 | + |
| 132 | + int recordCount = TestWriteUtils.DEFAULT_USER_DATA_RECORD_COUNT; |
| 133 | + for (int i = 1; i <= recordCount; i++) { |
| 134 | + Assert.assertEquals( |
| 135 | + storeClient.get(Integer.toString(i)).get().toString(), |
| 136 | + TestWriteUtils.DEFAULT_USER_DATA_VALUE_PREFIX + i); |
| 137 | + } |
| 138 | + Assert |
| 139 | + .assertEquals(getMetric(dvcMetricsRepo, "current_version_number.Gauge", storeNameStringToString), (double) 1); |
| 140 | + |
| 141 | + // Perform another push with 200 keys to verify future version ingestion |
| 142 | + recordCount = 200; |
| 143 | + runPushJob( |
| 144 | + storeNameStringToString, |
| 145 | + TestWriteUtils.writeSimpleAvroFileWithStringToStringSchema( |
| 146 | + getPushJobAvroFileDirectory(storeNameStringToString), |
| 147 | + recordCount)); |
| 148 | + |
| 149 | + // Perform another push with 200 keys to verify future version ingestion and version swap |
| 150 | + TestUtils.waitForNonDeterministicAssertion(60, TimeUnit.SECONDS, false, () -> { |
| 151 | + Assert.assertEquals( |
| 152 | + getMetric(dvcMetricsRepo, "current_version_number.Gauge", storeNameStringToString), |
| 153 | + (double) 2); |
| 154 | + }); |
| 155 | + |
| 156 | + for (int i = 1; i <= recordCount; i++) { |
| 157 | + Assert.assertEquals( |
| 158 | + storeClient.get(Integer.toString(i)).get().toString(), |
| 159 | + TestWriteUtils.DEFAULT_USER_DATA_VALUE_PREFIX + i); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Test(timeOut = TEST_TIMEOUT) |
| 165 | + public void testDVCRequestBasedMetaRepositoryStringToNameRecord() throws ExecutionException, InterruptedException { |
| 166 | + |
| 167 | + try (DaVinciClient<String, Object> storeClient = |
| 168 | + daVinciClientFactory.getAndStartGenericAvroClient(storeNameStringToNameRecord, daVinciConfig)) { |
| 169 | + storeClient.subscribeAll().get(); |
| 170 | + |
| 171 | + int recordCount = TestWriteUtils.DEFAULT_USER_DATA_RECORD_COUNT; |
| 172 | + for (int i = 1; i <= recordCount; i++) { |
| 173 | + Assert.assertEquals( |
| 174 | + storeClient.get(Integer.toString(i)).get().toString(), |
| 175 | + TestWriteUtils.renderNameRecord(TestWriteUtils.STRING_TO_NAME_RECORD_V1_SCHEMA, i) |
| 176 | + .get(DEFAULT_VALUE_FIELD_PROP) |
| 177 | + .toString()); |
| 178 | + } |
| 179 | + Assert.assertEquals( |
| 180 | + getMetric(dvcMetricsRepo, "current_version_number.Gauge", storeNameStringToNameRecord), |
| 181 | + (double) 1); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test(timeOut = TEST_TIMEOUT) |
| 186 | + public void testDVCRequestBasedMetaRepositoryStringToNameRecordVersions() |
| 187 | + throws IOException, ExecutionException, InterruptedException { |
| 188 | + |
| 189 | + try (DaVinciClient<String, Object> storeClient = |
| 190 | + daVinciClientFactory.getAndStartGenericAvroClient(storeNameStringToNameRecord, daVinciConfig)) { |
| 191 | + storeClient.subscribeAll().get(); |
| 192 | + |
| 193 | + for (int i = 0; i < TestWriteUtils.countStringToNameRecordSchemas(); i++) { |
| 194 | + Schema schema = TestWriteUtils.getStringToNameRecordSchema(i); |
| 195 | + int currentValueVersion = i + 2; |
| 196 | + |
| 197 | + int recordCount = currentValueVersion * TestWriteUtils.DEFAULT_USER_DATA_RECORD_COUNT; |
| 198 | + runPushJob( |
| 199 | + storeNameStringToNameRecord, |
| 200 | + TestWriteUtils.writeSimpleAvroFileWithStringToNameRecordSchema( |
| 201 | + getPushJobAvroFileDirectory(storeNameStringToNameRecord), |
| 202 | + schema, |
| 203 | + recordCount)); |
| 204 | + |
| 205 | + // Perform another push with 200 keys to verify future version ingestion and version swap |
| 206 | + TestUtils.waitForNonDeterministicAssertion(60, TimeUnit.SECONDS, false, () -> { |
| 207 | + Assert.assertEquals( |
| 208 | + getMetric(dvcMetricsRepo, "current_version_number.Gauge", storeNameStringToNameRecord), |
| 209 | + (double) (currentValueVersion)); |
| 210 | + }); |
| 211 | + |
| 212 | + for (int j = 1; j <= recordCount; j++) { |
| 213 | + Assert.assertEquals( |
| 214 | + storeClient.get(Integer.toString(j)).get().toString(), |
| 215 | + TestWriteUtils.renderNameRecord(schema, j).get(DEFAULT_VALUE_FIELD_PROP).toString()); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private double getMetric(MetricsRepository metricsRepository, String metricName, String storeName) { |
| 222 | + Metric metric = metricsRepository.getMetric("." + storeName + "--" + metricName); |
| 223 | + assertNotNull(metric, "Expected metric " + metricName + " not found."); |
| 224 | + return metric.value(); |
| 225 | + } |
| 226 | + |
| 227 | + private File getPushJobAvroFileDirectory(String storeName) { |
| 228 | + if (!pushJobAvroDataDirs.containsKey(storeName)) { |
| 229 | + pushJobAvroDataDirs.put(storeName, getTempDataDirectory()); |
| 230 | + } |
| 231 | + |
| 232 | + return pushJobAvroDataDirs.get(storeName); |
| 233 | + } |
| 234 | + |
| 235 | + private void runPushJob(String storeName, Schema schema) { |
| 236 | + |
| 237 | + ControllerClient controllerClient; |
| 238 | + File dataDir = getPushJobAvroFileDirectory(storeName); |
| 239 | + String dataDirPath = "file:" + dataDir.getAbsolutePath(); |
| 240 | + |
| 241 | + if (!controllerClients.containsKey(storeName)) { |
| 242 | + // Init store |
| 243 | + controllerClient = IntegrationTestPushUtils.createStoreForJob( |
| 244 | + CLUSTER_NAME, |
| 245 | + schema, |
| 246 | + TestWriteUtils.defaultVPJProps( |
| 247 | + clusterWrapper.getVeniceControllers().get(0).getControllerUrl(), |
| 248 | + dataDirPath, |
| 249 | + storeName)); |
| 250 | + controllerClients.put(storeName, controllerClient); |
| 251 | + } else { |
| 252 | + controllerClient = controllerClients.get(storeName); |
| 253 | + |
| 254 | + // Add new schema |
| 255 | + Schema valueSchema = schema.getField(DEFAULT_VALUE_FIELD_PROP).schema(); |
| 256 | + SchemaResponse schemaResponse = controllerClient.addValueSchema(storeName, valueSchema.toString()); |
| 257 | + Assert.assertFalse(schemaResponse.isError(), schemaResponse.getError()); |
| 258 | + } |
| 259 | + |
| 260 | + Properties props = |
| 261 | + TestWriteUtils.defaultVPJProps(controllerClient.getLeaderControllerUrl(), dataDirPath, storeName); |
| 262 | + TestWriteUtils.runPushJob(storeName + "_" + Utils.getUniqueString("push_job"), props); |
| 263 | + } |
| 264 | +} |
0 commit comments