phet commented on code in PR #3890: URL: https://github.com/apache/gobblin/pull/3890#discussion_r1525764729
########## gobblin-service/src/main/java/org/apache/gobblin/service/modules/orchestration/ReminderSettingDagProcLeaseArbiter.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.service.modules.orchestration; + +import java.io.IOException; +import java.util.Date; + +import org.quartz.Job; +import org.quartz.JobBuilder; +import org.quartz.JobDataMap; +import org.quartz.JobDetail; +import org.quartz.JobExecutionContext; +import org.quartz.SchedulerException; +import org.quartz.Trigger; +import org.quartz.TriggerBuilder; + +import com.typesafe.config.Config; + +import javax.inject.Inject; +import lombok.extern.slf4j.Slf4j; + +import org.apache.gobblin.configuration.ConfigurationKeys; +import org.apache.gobblin.runtime.api.DagActionStore; +import org.apache.gobblin.runtime.api.MultiActiveLeaseArbiter; + + +/** + * Decorator used to coordinate multiple hosts with execution components enabled to respond to flow action events with + * added capabilities to properly handle the result of attempted ownership over these flow action events. It uses the + * {@link MultiActiveLeaseArbiter} to determine a single lease owner at a given event time for a flow action event. + * After acquiring the lease, the host can pursue executing the action. Once it has completed this action, it + * marks the lease as completed by calling the + * {@link MultiActiveLeaseArbiter#recordLeaseSuccess(MultiActiveLeaseArbiter.LeaseObtainedStatus)} method. Hosts + * that fail to acquire a lease will use the {@link DagActionReminderScheduler} to set a reminder for the flow action + * event to check back in on the previous lease owner's completion status. + */ +@Slf4j +public class ReminderSettingDagProcLeaseArbiter implements MultiActiveLeaseArbiter { + private final MultiActiveLeaseArbiter decoratedLeaseArbiter; + private final DagActionReminderScheduler _dagActionReminderScheduler; + private final Config config; + + @Inject + public ReminderSettingDagProcLeaseArbiter(Config config, MultiActiveLeaseArbiter leaseArbiter, DagActionReminderScheduler dagActionReminderScheduler) throws IOException { + this.decoratedLeaseArbiter = leaseArbiter; + this._dagActionReminderScheduler = dagActionReminderScheduler; + this.config = config; + } + + /** + * This method is used by the multi-active scheduler and multi-active execution classes (DagTaskStream) to attempt a + * lease for a particular job event and return the status of the attempt. + * @param flowAction + * @param eventTimeMillis + * @param isReminderEvent + * @param skipFlowExecutionIdReplacement + * @return + */ + @Override + public MultiActiveLeaseArbiter.LeaseAttemptStatus tryAcquireLease(DagActionStore.DagAction flowAction, long eventTimeMillis, + boolean isReminderEvent, boolean skipFlowExecutionIdReplacement) { + try { + MultiActiveLeaseArbiter.LeaseAttemptStatus leaseAttemptStatus = this.tryAcquireLease(flowAction, eventTimeMillis, isReminderEvent, skipFlowExecutionIdReplacement); + /* Schedule a reminder for the event unless the lease has been completed to safeguard against case lease owner + fails to complete lease + */ + if (leaseAttemptStatus instanceof MultiActiveLeaseArbiter.LeaseObtainedStatus) { + scheduleReminderForEvent((LeaseObtainedStatus) leaseAttemptStatus); + } else if (leaseAttemptStatus instanceof MultiActiveLeaseArbiter.LeasedToAnotherStatus) { + scheduleReminderForEvent((LeasedToAnotherStatus) leaseAttemptStatus); + } + return leaseAttemptStatus; + } catch (SchedulerException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean recordLeaseSuccess(LeaseObtainedStatus status) + throws IOException { + return this.decoratedLeaseArbiter.recordLeaseSuccess(status); + } + + protected void scheduleReminderForEvent(MultiActiveLeaseArbiter.LeasedToAnotherStatus leaseStatus) + throws SchedulerException { + _dagActionReminderScheduler.scheduleReminder(leaseStatus.getDagAction(), leaseStatus.getMinimumLingerDurationMillis()); + } + + protected void scheduleReminderForEvent(MultiActiveLeaseArbiter.LeaseObtainedStatus leaseStatus) + throws SchedulerException { + _dagActionReminderScheduler.scheduleReminder(leaseStatus.getDagAction(), leaseStatus.getMinimumLingerDurationMillis()); + } Review Comment: I don't like the duplication... and I agree there's no perfect solution here (in the java type system). but casting's imperfect on its own, so I'd probably bite the bullet and add *concrete impls* to the `LeaseAttemptStatus` that both return `null`: ``` public DagAction getDagAction() ... public long getMinimumLingerDurationMillis() ... ``` thereafter the `@Data` or `@Getter`s on these two derived classes will override to a real value for each. thereafter, the test above could be merely: ``` if (!leaseAttemptStatus instanceof MultiActiveLeaseArbiter.NoLongerLeasing) { scheduleReminderForEvent(leaseAttemptStatus); } ... ``` -- 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]
