Will-Lo commented on code in PR #3715: URL: https://github.com/apache/gobblin/pull/3715#discussion_r1267286952
########## gobblin-runtime/src/test/java/org/apache/gobblin/runtime/api/MysqlMultiActiveLeaseArbiterTest.java: ########## @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.gobblin.runtime.api; + +import com.typesafe.config.Config; +import lombok.extern.slf4j.Slf4j; +import org.apache.gobblin.config.ConfigBuilder; +import org.apache.gobblin.configuration.ConfigurationKeys; +import org.apache.gobblin.metastore.testing.ITestMetastoreDatabase; +import org.apache.gobblin.metastore.testing.TestMetastoreDatabaseFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +@Slf4j +public class MysqlMultiActiveLeaseArbiterTest { + private static final int EPSILON = 30000; + private static final int LINGER = 80000; + private static final String USER = "testUser"; + private static final String PASSWORD = "testPassword"; + private static final String TABLE = "mysql_multi_active_lease_arbiter_store"; + private static final String flowGroup = "testFlowGroup"; + private static final String flowName = "testFlowName"; + private static final String flowExecutionId = "12345677"; + private static DagActionStore.DagAction launchDagAction = + new DagActionStore.DagAction(flowGroup, flowName, flowExecutionId, DagActionStore.FlowActionType.LAUNCH); + + private static final long eventTimeMillis = System.currentTimeMillis(); + private MysqlMultiActiveLeaseArbiter mysqlMultiActiveLeaseArbiter; + + // The setup functionality verifies that the initialization of the tables is done correctly and verifies any SQL + // syntax errors. + @BeforeClass + public void setUp() throws Exception { + ITestMetastoreDatabase testDb = TestMetastoreDatabaseFactory.get(); + + Config config = ConfigBuilder.create() + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.SCHEDULER_EVENT_EPSILON_MILLIS_KEY, EPSILON) + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.SCHEDULER_EVENT_LINGER_MILLIS_KEY, LINGER) + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.STATE_STORE_DB_URL_KEY, testDb.getJdbcUrl()) + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.STATE_STORE_DB_USER_KEY, USER) + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.STATE_STORE_DB_PASSWORD_KEY, PASSWORD) + .addPrimitive(ConfigurationKeys.MYSQL_LEASE_ARBITER_PREFIX + "." + ConfigurationKeys.STATE_STORE_DB_TABLE_KEY, TABLE) + .build(); + + this.mysqlMultiActiveLeaseArbiter = new MysqlMultiActiveLeaseArbiter(config); + } + + /* + Tests all cases of trying to acquire a lease (CASES 1-6 detailed below) for a flow action event with one + participant involved. + */ + @Test + public void testAcquireLeaseSingleParticipant() throws Exception { + // Tests CASE 1 of acquire lease for a flow action event not present in DB + MultiActiveLeaseArbiter.LeaseAttemptStatus firstLaunchStatus = + mysqlMultiActiveLeaseArbiter.tryAcquireLease(launchDagAction, eventTimeMillis); + Assert.assertTrue(firstLaunchStatus instanceof MultiActiveLeaseArbiter.LeaseObtainedStatus); + MultiActiveLeaseArbiter.LeaseObtainedStatus firstObtainedStatus = + (MultiActiveLeaseArbiter.LeaseObtainedStatus) firstLaunchStatus; + Assert.assertTrue(firstObtainedStatus.getEventTimestamp() <= + firstObtainedStatus.getLeaseAcquisitionTimestamp()); + Assert.assertTrue(firstObtainedStatus.getFlowAction().equals( + new DagActionStore.DagAction(flowGroup, flowName, flowExecutionId, DagActionStore.FlowActionType.LAUNCH))); + + // Verify that different DagAction types for the same flow can have leases at the same time + DagActionStore.DagAction killDagAction = new + DagActionStore.DagAction(flowGroup, flowName, flowExecutionId, DagActionStore.FlowActionType.KILL); + MultiActiveLeaseArbiter.LeaseAttemptStatus killStatus = + mysqlMultiActiveLeaseArbiter.tryAcquireLease(killDagAction, eventTimeMillis); + Assert.assertTrue(killStatus instanceof MultiActiveLeaseArbiter.LeaseObtainedStatus); + MultiActiveLeaseArbiter.LeaseObtainedStatus killObtainedStatus = + (MultiActiveLeaseArbiter.LeaseObtainedStatus) killStatus; + Assert.assertTrue( + killObtainedStatus.getLeaseAcquisitionTimestamp() >= killObtainedStatus.getEventTimestamp()); + + // Tests CASE 2 of acquire lease for a flow action event that already has a valid lease for the same event in db Review Comment: You can make tests have dependencies on each other so that they run synchronously, but I guess not urgent if you are still working on improving this -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
