Skip to content

Commit

Permalink
[INLONG-9862][Manager] Support submit flink job for offline sync (#9865)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloyszhang committed May 16, 2024
1 parent ff3c351 commit 65d3a92
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public class InlongConstants {
*/
public static final String DATAFLOW = "dataflow";

public static final String REGISTER_SCHEDULE_STATUS = "register.schedule.status";

public static final String REGISTERED = "registered";

public static final String STREAMS = "streams";

public static final String RELATIONS = "relations";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.inlong.manager.plugin.flink.dto.FlinkInfo;
import org.apache.inlong.manager.plugin.flink.enums.Constants;
import org.apache.inlong.manager.pojo.sink.StreamSink;
import org.apache.inlong.manager.pojo.sort.util.StreamParseUtils;
import org.apache.inlong.manager.pojo.stream.InlongStreamExtInfo;
import org.apache.inlong.manager.pojo.stream.InlongStreamInfo;
import org.apache.inlong.manager.pojo.workflow.form.process.GroupResourceProcessForm;
Expand Down Expand Up @@ -98,6 +99,14 @@ public ListenerResult listen(WorkflowContext context) throws Exception {
}

for (InlongStreamInfo streamInfo : streamInfos) {
boolean isOfflineSync = InlongConstants.DATASYNC_OFFLINE_MODE
.equals(groupResourceForm.getGroupInfo().getInlongGroupMode());
// do not submit flink job if the group mode is offline and the stream is not config successfully
if (isOfflineSync && !StreamParseUtils.isRegisterScheduleSuccess(streamInfo)) {
log.info("no need to submit flink job for groupId={} streamId={} as the mode is offline "
+ "and the stream is not config successfully yet", groupId, streamInfo.getInlongStreamId());
continue;
}
List<StreamSink> sinkList = streamInfo.getSinkList();
List<String> sinkTypes = sinkList.stream().map(StreamSink::getSinkType).collect(Collectors.toList());
if (CollectionUtils.isEmpty(sinkList) || !SinkType.containSortFlinkSink(sinkTypes)) {
Expand Down Expand Up @@ -131,30 +140,25 @@ public ListenerResult listen(WorkflowContext context) throws Exception {
return ListenerResult.fail(message);
}

boolean isRealTimeSync = InlongConstants.DATASYNC_REALTIME_MODE
.equals(groupResourceForm.getGroupInfo().getInlongGroupMode());

FlinkInfo flinkInfo = new FlinkInfo();

String jobName = Constants.SORT_JOB_NAME_GENERATOR.apply(processForm) + InlongConstants.HYPHEN
+ streamInfo.getInlongStreamId();
flinkInfo.setJobName(jobName);
flinkInfo.setEndpoint(kvConf.get(InlongConstants.SORT_URL));
flinkInfo.setInlongStreamInfoList(Collections.singletonList(streamInfo));
if (isRealTimeSync) {
flinkInfo.setRuntimeExecutionMode(InlongConstants.RUNTIME_EXECUTION_MODE_STREAMING);
} else {
if (isOfflineSync) {
flinkInfo.setRuntimeExecutionMode(InlongConstants.RUNTIME_EXECUTION_MODE_BATCH);
} else {
flinkInfo.setRuntimeExecutionMode(InlongConstants.RUNTIME_EXECUTION_MODE_STREAMING);
}
FlinkOperation flinkOperation = FlinkOperation.getInstance();
try {
flinkOperation.genPath(flinkInfo, dataflow);
// only start job for real-time mode
if (isRealTimeSync) {
flinkOperation.start(flinkInfo);
log.info("job submit success for groupId = {}, streamId = {}, jobId = {}", groupId,
streamInfo.getInlongStreamId(), flinkInfo.getJobId());
}
flinkOperation.start(flinkInfo);
log.info("job submit success for groupId = {}, mode = {}, streamId = {}, jobId = {}",
groupId, groupResourceForm.getGroupInfo().getInlongGroupMode(),
streamInfo.getInlongStreamId(), flinkInfo.getJobId());
} catch (Exception e) {
flinkInfo.setException(true);
flinkInfo.setExceptionMsg(getExceptionStackMsg(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.inlong.manager.pojo.sort.util;

import org.apache.inlong.manager.common.consts.InlongConstants;
import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
import org.apache.inlong.manager.common.enums.TransformType;
import org.apache.inlong.manager.common.util.Preconditions;
import org.apache.inlong.manager.pojo.sink.StreamSink;
import org.apache.inlong.manager.pojo.source.StreamSource;
import org.apache.inlong.manager.pojo.stream.InlongStreamExtInfo;
import org.apache.inlong.manager.pojo.stream.InlongStreamInfo;
import org.apache.inlong.manager.pojo.stream.StreamNode;
import org.apache.inlong.manager.pojo.stream.StreamPipeline;
import org.apache.inlong.manager.pojo.stream.StreamTransform;
Expand All @@ -38,6 +41,8 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
* Utils of stream parse.
Expand Down Expand Up @@ -154,4 +159,20 @@ public static StreamPipeline parseStreamPipeline(String tempView, String inlongS
return GSON.fromJson(tempView, StreamPipeline.class);
}

public static String getStreamExtProperty(String key, InlongStreamInfo streamInfo) {
if (StringUtils.isNotBlank(key) && streamInfo != null && CollectionUtils.isNotEmpty(streamInfo.getExtList())) {
for (InlongStreamExtInfo ext : streamInfo.getExtList()) {
if (key.equalsIgnoreCase(ext.getKeyName())) {
return ext.getKeyValue();
}
}
}
return null;
}

public static boolean isRegisterScheduleSuccess(InlongStreamInfo streamInfo) {
return InlongConstants.REGISTERED
.equalsIgnoreCase(getStreamExtProperty(InlongConstants.REGISTER_SCHEDULE_STATUS, streamInfo));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.apache.inlong.manager.common.plugin.Plugin;
import org.apache.inlong.manager.common.plugin.PluginBinder;
import org.apache.inlong.manager.service.listener.queue.StreamQueueResourceListener;
import org.apache.inlong.manager.service.listener.schedule.StreamScheduleResourceListener;
import org.apache.inlong.manager.service.listener.sink.StreamSinkResourceListener;
import org.apache.inlong.manager.service.listener.sort.StreamSortConfigListener;
import org.apache.inlong.manager.workflow.WorkflowContext;
import org.apache.inlong.manager.workflow.definition.ServiceTaskType;
import org.apache.inlong.manager.workflow.definition.TaskListenerFactory;
import org.apache.inlong.manager.workflow.event.task.QueueOperateListener;
import org.apache.inlong.manager.workflow.event.task.ScheduleOperateListener;
import org.apache.inlong.manager.workflow.event.task.SinkOperateListener;
import org.apache.inlong.manager.workflow.event.task.SortOperateListener;
import org.apache.inlong.manager.workflow.event.task.SourceOperateListener;
Expand Down Expand Up @@ -53,13 +55,16 @@ public class StreamTaskListenerFactory implements PluginBinder, TaskListenerFact
private List<QueueOperateListener> queueOperateListeners;
private List<SortOperateListener> sortOperateListeners;
private List<SinkOperateListener> sinkOperateListeners;
private List<ScheduleOperateListener> scheduleOperateListeners;

@Autowired
private StreamQueueResourceListener queueResourceListener;
@Autowired
private StreamSortConfigListener streamSortConfigListener;
@Autowired
private StreamSinkResourceListener sinkResourceListener;
@Autowired
private StreamScheduleResourceListener scheduleResourceListener;

@PostConstruct
public void init() {
Expand All @@ -70,6 +75,8 @@ public void init() {
sortOperateListeners.add(streamSortConfigListener);
sinkOperateListeners = new LinkedList<>();
sinkOperateListeners.add(sinkResourceListener);
scheduleOperateListeners = new LinkedList<>();
scheduleOperateListeners.add(scheduleResourceListener);
}

@Override
Expand All @@ -94,6 +101,10 @@ public void acceptPlugin(Plugin plugin) {
if (CollectionUtils.isNotEmpty(pluginSinkOperateListeners)) {
sinkOperateListeners.addAll(pluginSinkOperateListeners);
}
List<ScheduleOperateListener> pluginScheduleOperateListeners = processPlugin.createScheduleOperateListeners();
if (CollectionUtils.isNotEmpty(pluginScheduleOperateListeners)) {
scheduleOperateListeners.addAll(pluginScheduleOperateListeners);
}
}

@Override
Expand All @@ -118,6 +129,9 @@ public List<? extends TaskEventListener> get(WorkflowContext workflowContext, Se
case INIT_SINK:
List<SinkOperateListener> sinkOperateListeners = getSinkOperateListener(workflowContext);
return Lists.newArrayList(sinkOperateListeners);
case INIT_SCHEDULE:
List<ScheduleOperateListener> scheduleOperateListeners = getScheduleOperateListener(workflowContext);
return Lists.newArrayList(scheduleOperateListeners);
default:
throw new IllegalArgumentException(String.format("Unsupported ServiceTaskType %s", taskType));
}
Expand All @@ -131,6 +145,7 @@ public void clearListeners() {
queueOperateListeners = new LinkedList<>();
sortOperateListeners = new LinkedList<>();
sinkOperateListeners = new LinkedList<>();
scheduleOperateListeners = new LinkedList<>();
}

/**
Expand Down Expand Up @@ -185,4 +200,17 @@ private List<SinkOperateListener> getSinkOperateListener(WorkflowContext context
return listeners;
}

/**
* Get schedule operate listener list.
*/
private List<ScheduleOperateListener> getScheduleOperateListener(WorkflowContext context) {
List<ScheduleOperateListener> listeners = new ArrayList<>();
for (ScheduleOperateListener listener : scheduleOperateListeners) {
if (listener != null && listener.accept(context)) {
listeners.add(listener);
}
}
return listeners;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.inlong.manager.service.listener.schedule;

import org.apache.inlong.manager.common.consts.InlongConstants;
import org.apache.inlong.manager.common.enums.GroupOperateType;
import org.apache.inlong.manager.common.enums.TaskEvent;
import org.apache.inlong.manager.pojo.stream.InlongStreamExtInfo;
import org.apache.inlong.manager.pojo.stream.InlongStreamInfo;
import org.apache.inlong.manager.pojo.workflow.form.process.ProcessForm;
import org.apache.inlong.manager.pojo.workflow.form.process.StreamResourceProcessForm;
import org.apache.inlong.manager.workflow.WorkflowContext;
import org.apache.inlong.manager.workflow.event.ListenerResult;
import org.apache.inlong.manager.workflow.event.task.ScheduleOperateListener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StreamScheduleResourceListener implements ScheduleOperateListener {

@Override
public TaskEvent event() {
return TaskEvent.COMPLETE;
}

@Override
public boolean accept(WorkflowContext context) {
ProcessForm processForm = context.getProcessForm();
String groupId = processForm.getInlongGroupId();
if (!(processForm instanceof StreamResourceProcessForm)) {
log.info("not add schedule stream listener, not StreamResourceProcessForm for groupId [{}]", groupId);
return false;
}

StreamResourceProcessForm streamProcessForm = (StreamResourceProcessForm) processForm;
String streamId = streamProcessForm.getStreamInfo().getInlongStreamId();
if (streamProcessForm.getGroupOperateType() != GroupOperateType.INIT) {
log.info("not add schedule stream listener, as the operate was not INIT for groupId [{}] streamId [{}]",
groupId, streamId);
return false;
}

log.info("add startup stream listener for groupId [{}] streamId [{}]", groupId, streamId);
return InlongConstants.DATASYNC_OFFLINE_MODE.equals(streamProcessForm.getGroupInfo().getInlongGroupMode());
}

@Override
public ListenerResult listen(WorkflowContext context) throws Exception {
StreamResourceProcessForm form = (StreamResourceProcessForm) context.getProcessForm();
InlongStreamInfo streamInfo = form.getStreamInfo();
final String groupId = streamInfo.getInlongGroupId();
final String streamId = streamInfo.getInlongStreamId();
log.info("begin to register schedule info for groupId={}, streamId={}", groupId, streamId);

// todo: register schedule info to schedule service

// after register schedule info successfully, add ext property to stream info
saveInfo(streamInfo, InlongConstants.REGISTER_SCHEDULE_STATUS,
InlongConstants.REGISTERED, streamInfo.getExtList());
log.info("success to register schedule info for group [" + groupId + "] and stream [" + streamId + "]");
return ListenerResult.success();
}

/**
* Save stream ext info into list.
*/
private void saveInfo(InlongStreamInfo streamInfo, String keyName, String keyValue,
List<InlongStreamExtInfo> extInfoList) {
InlongStreamExtInfo extInfo = new InlongStreamExtInfo();
extInfo.setInlongGroupId(streamInfo.getInlongGroupId());
extInfo.setInlongStreamId(streamInfo.getInlongStreamId());
extInfo.setKeyName(keyName);
extInfo.setKeyValue(keyValue);
extInfoList.add(extInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.inlong.manager.common.exceptions.WorkflowListenerException;
import org.apache.inlong.manager.pojo.group.InlongGroupInfo;
import org.apache.inlong.manager.pojo.sink.StreamSink;
import org.apache.inlong.manager.pojo.sort.util.StreamParseUtils;
import org.apache.inlong.manager.pojo.stream.InlongStreamInfo;
import org.apache.inlong.manager.pojo.workflow.form.process.GroupResourceProcessForm;
import org.apache.inlong.manager.pojo.workflow.form.process.ProcessForm;
Expand Down Expand Up @@ -125,6 +126,13 @@ public ListenerResult listen(WorkflowContext context) throws WorkflowListenerExc

try {
for (InlongStreamInfo streamInfo : streamInfos) {
// do not build sort config if the group mode is offline and the stream is not config successfully
if (InlongConstants.DATASYNC_OFFLINE_MODE.equals(groupInfo.getInlongGroupMode())
&& !StreamParseUtils.isRegisterScheduleSuccess(streamInfo)) {
LOGGER.info("no need to build sort config for groupId={} streamId={} as the mode is offline "
+ "and the stream is not config successfully yet", groupId, streamInfo.getInlongStreamId());
continue;
}
List<StreamSink> sinkList = streamInfo.getSinkList();
if (CollectionUtils.isEmpty(sinkList)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,26 @@ public WorkflowProcess defineProcess() {
initSourceTask.setListenerFactory(streamTaskListenerFactory);
process.addTask(initSourceTask);

// Init Schedule info
ServiceTask initScheduleTask = new ServiceTask();
initScheduleTask.setName("InitSchedule");
initScheduleTask.setDisplayName("Stream-InitSchedule");
initScheduleTask.setServiceTaskType(ServiceTaskType.INIT_SCHEDULE);
initScheduleTask.setListenerFactory(streamTaskListenerFactory);
process.addTask(initScheduleTask);

// End node
EndEvent endEvent = new EndEvent();
process.setEndEvent(endEvent);

// Task dependency order: 1.MQ -> 2.Sink -> 3.Sort -> 4.Source
// Task dependency order: 1.MQ -> 2.Sink -> 3.Sort -> 4.Source -> 5.Schedule
// To ensure that after some tasks fail, data will not start to be collected by source or consumed by sort
startEvent.addNext(initMQTask);
initMQTask.addNext(initSinkTask);
initSinkTask.addNext(initSortTask);
initSortTask.addNext(initSourceTask);
initSourceTask.addNext(endEvent);
initSourceTask.addNext(initScheduleTask);
initScheduleTask.addNext(endEvent);

return process;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum ServiceTaskType {
INIT_MQ,
INIT_SORT,
INIT_SINK,
INIT_SCHEDULE,

STOP_SOURCE,
STOP_SORT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.inlong.manager.workflow.event.task;

import org.apache.inlong.manager.common.enums.TaskEvent;
import org.apache.inlong.manager.workflow.WorkflowContext;
import org.apache.inlong.manager.workflow.event.ListenerResult;

public interface ScheduleOperateListener extends TaskEventListener {

ScheduleOperateListener DEFAULT_SCHEDULE_OPERATE_LISTENER = new ScheduleOperateListener() {

@Override
public TaskEvent event() {
return TaskEvent.COMPLETE;
}

@Override
public ListenerResult listen(WorkflowContext context) {
return ListenerResult.success();
}
};
}

0 comments on commit 65d3a92

Please sign in to comment.