forked from apache/gobblin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GOBBLIN-336] Encapsulate the non-Helix specific
task execution logic Put the logic in its own class. Also changed: * Use a try-with statement to close the global broker. * Fix a Helix warning: ERROR is replaced with FAILED. Testing: The integration test org.apache.gobblin.cluster.ClusterIntegrationTest passed. Also inlined a method. The old code has a bug: the globalBroker variable will stay null. Closes apache#2193 from HappyRay/encapsulate-non-helix- job-launch-logic
- Loading branch information
Showing
2 changed files
with
175 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
gobblin-cluster/src/main/java/org/apache/gobblin/cluster/SingleHelixTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* 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.cluster; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
import org.apache.gobblin.broker.SharedResourcesBrokerFactory; | ||
import org.apache.gobblin.broker.gobblin_scopes.GobblinScopeTypes; | ||
import org.apache.gobblin.broker.gobblin_scopes.JobScopeInstance; | ||
import org.apache.gobblin.broker.iface.SharedResourcesBroker; | ||
import org.apache.gobblin.runtime.AbstractJobLauncher; | ||
import org.apache.gobblin.runtime.GobblinMultiTaskAttempt; | ||
import org.apache.gobblin.runtime.JobState; | ||
import org.apache.gobblin.runtime.util.StateStores; | ||
import org.apache.gobblin.source.workunit.MultiWorkUnit; | ||
import org.apache.gobblin.source.workunit.WorkUnit; | ||
import org.apache.gobblin.util.JobLauncherUtils; | ||
import org.apache.gobblin.util.SerializationUtils; | ||
|
||
|
||
public class SingleHelixTask { | ||
|
||
private static final Logger _logger = LoggerFactory.getLogger(SingleHelixTask.class); | ||
|
||
private GobblinMultiTaskAttempt _taskattempt; | ||
private String _jobId; | ||
private Path _workUnitFilePath; | ||
private Path _jobStateFilePath; | ||
private FileSystem _fs; | ||
private TaskAttemptBuilder _taskAttemptBuilder; | ||
private StateStores _stateStores; | ||
|
||
SingleHelixTask(String jobId, Path workUnitFilePath, Path jobStateFilePath, FileSystem fs, | ||
TaskAttemptBuilder taskAttemptBuilder, StateStores stateStores) { | ||
_jobId = jobId; | ||
_workUnitFilePath = workUnitFilePath; | ||
_jobStateFilePath = jobStateFilePath; | ||
_fs = fs; | ||
_taskAttemptBuilder = taskAttemptBuilder; | ||
_stateStores = stateStores; | ||
} | ||
|
||
/** | ||
* | ||
* @return the number of work-units processed | ||
* @throws IOException | ||
* @throws InterruptedException | ||
*/ | ||
public int run() | ||
throws IOException, InterruptedException { | ||
List<WorkUnit> workUnits = getWorkUnits(); | ||
int workUnitSize = workUnits.size(); | ||
|
||
JobState jobState = getJobState(); | ||
Config jobConfig = getConfigFromJobState(jobState); | ||
|
||
try (SharedResourcesBroker<GobblinScopeTypes> globalBroker = SharedResourcesBrokerFactory | ||
.createDefaultTopLevelBroker(jobConfig, GobblinScopeTypes.GLOBAL.defaultScopeInstance())) { | ||
SharedResourcesBroker<GobblinScopeTypes> jobBroker = getJobBroker(jobState, globalBroker); | ||
|
||
_taskattempt = _taskAttemptBuilder.build(workUnits.iterator(), _jobId, jobState, jobBroker); | ||
_taskattempt.runAndOptionallyCommitTaskAttempt(GobblinMultiTaskAttempt.CommitPolicy.IMMEDIATE); | ||
return workUnitSize; | ||
} | ||
} | ||
|
||
private SharedResourcesBroker<GobblinScopeTypes> getJobBroker(JobState jobState, | ||
SharedResourcesBroker<GobblinScopeTypes> globalBroker) { | ||
return globalBroker.newSubscopedBuilder(new JobScopeInstance(jobState.getJobName(), jobState.getJobId())).build(); | ||
} | ||
|
||
private Config getConfigFromJobState(JobState jobState) { | ||
Properties jobProperties = jobState.getProperties(); | ||
return ConfigFactory.parseProperties(jobProperties); | ||
} | ||
|
||
private JobState getJobState() | ||
throws java.io.IOException { | ||
JobState jobState = new JobState(); | ||
SerializationUtils.deserializeState(_fs, _jobStateFilePath, jobState); | ||
return jobState; | ||
} | ||
|
||
private List<WorkUnit> getWorkUnits() | ||
throws IOException { | ||
String fileName = _workUnitFilePath.getName(); | ||
String storeName = _workUnitFilePath.getParent().getName(); | ||
WorkUnit workUnit; | ||
|
||
if (_workUnitFilePath.getName().endsWith(AbstractJobLauncher.MULTI_WORK_UNIT_FILE_EXTENSION)) { | ||
workUnit = _stateStores.mwuStateStore.getAll(storeName, fileName).get(0); | ||
} else { | ||
workUnit = _stateStores.wuStateStore.getAll(storeName, fileName).get(0); | ||
} | ||
|
||
// The list of individual WorkUnits (flattened) to run | ||
List<WorkUnit> workUnits = Lists.newArrayList(); | ||
|
||
if (workUnit instanceof MultiWorkUnit) { | ||
// Flatten the MultiWorkUnit so the job configuration properties can be added to each individual WorkUnits | ||
List<WorkUnit> flattenedWorkUnits = JobLauncherUtils.flattenWorkUnits(((MultiWorkUnit) workUnit).getWorkUnits()); | ||
workUnits.addAll(flattenedWorkUnits); | ||
} else { | ||
workUnits.add(workUnit); | ||
} | ||
return workUnits; | ||
} | ||
|
||
public void cancel() { | ||
if (_taskattempt != null) { | ||
try { | ||
_logger.info("Task cancelled: Shutdown starting for tasks with jobId: {}", _jobId); | ||
_taskattempt.shutdownTasks(); | ||
_logger.info("Task cancelled: Shutdown complete for tasks with jobId: {}", _jobId); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException("Interrupted while shutting down task with jobId: " + _jobId, e); | ||
} | ||
} else { | ||
_logger.error("Task cancelled but _taskattempt is null, so ignoring."); | ||
} | ||
} | ||
} |