Wenjun Ruan
2 years ago
committed by
GitHub
34 changed files with 630 additions and 325 deletions
@ -0,0 +1,25 @@ |
|||||||
|
/* |
||||||
|
* 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.dolphinscheduler.tools.datasource.upgrader; |
||||||
|
|
||||||
|
public interface DolphinSchedulerUpgrader { |
||||||
|
|
||||||
|
void doUpgrade(); |
||||||
|
|
||||||
|
String getCurrentVersion(); |
||||||
|
} |
@ -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.dolphinscheduler.tools.datasource.upgrader.v320; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.Tenant; |
||||||
|
import org.apache.dolphinscheduler.dao.entity.User; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionLogMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.TenantMapper; |
||||||
|
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
||||||
|
import org.apache.dolphinscheduler.tools.datasource.upgrader.DolphinSchedulerUpgrader; |
||||||
|
|
||||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
@Component |
||||||
|
public class V320DolphinSchedulerUpgrader implements DolphinSchedulerUpgrader { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ProcessInstanceMapper processInstanceMapper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private ProcessDefinitionLogMapper processDefinitionLogMapper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private TenantMapper tenantMapper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private UserMapper userMapper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private TaskInstanceMapper taskInstanceMapper; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void doUpgrade() { |
||||||
|
upgradeWorkflowInstance(); |
||||||
|
upgradeTaskInstance(); |
||||||
|
} |
||||||
|
|
||||||
|
private void upgradeWorkflowInstance() { |
||||||
|
Map<Integer, String> tenantMap = tenantMapper.selectList(new QueryWrapper<>()) |
||||||
|
.stream() |
||||||
|
.collect(Collectors.toMap(Tenant::getId, Tenant::getTenantCode)); |
||||||
|
Map<Integer, String> userMap = userMapper.selectList(new QueryWrapper<>()) |
||||||
|
.stream() |
||||||
|
.collect(Collectors.toMap(User::getId, User::getUserName)); |
||||||
|
|
||||||
|
while (true) { |
||||||
|
LambdaQueryWrapper<ProcessInstance> wrapper = new QueryWrapper<>(new ProcessInstance()) |
||||||
|
.lambda() |
||||||
|
.eq(ProcessInstance::getProjectCode, null) |
||||||
|
.last("limit 1000"); |
||||||
|
List<ProcessInstance> needUpdateWorkflowInstance = processInstanceMapper.selectList(wrapper); |
||||||
|
if (CollectionUtils.isEmpty(needUpdateWorkflowInstance)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
for (ProcessInstance processInstance : needUpdateWorkflowInstance) { |
||||||
|
ProcessDefinitionLog processDefinitionLog = processDefinitionLogMapper.queryByDefinitionCodeAndVersion( |
||||||
|
processInstance.getProcessDefinitionCode(), processInstance.getProcessDefinitionVersion()); |
||||||
|
if (processDefinitionLog != null) { |
||||||
|
processInstance.setProjectCode(processDefinitionLog.getProjectCode()); |
||||||
|
processInstance.setTenantCode(tenantMap.get(processDefinitionLog.getTenantId())); |
||||||
|
processInstance.setExecutorName(userMap.get(processInstance.getExecutorId())); |
||||||
|
} else { |
||||||
|
processInstance.setProjectCode(-1L); |
||||||
|
} |
||||||
|
processInstanceMapper.updateById(processInstance); |
||||||
|
} |
||||||
|
log.info("Success upgrade workflow instance, current batch size: {}", needUpdateWorkflowInstance.size()); |
||||||
|
|
||||||
|
try { |
||||||
|
Thread.sleep(1000L); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
log.error("Upgrade workflow instance error", e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void upgradeTaskInstance() { |
||||||
|
while (true) { |
||||||
|
LambdaQueryWrapper<TaskInstance> wrapper = new QueryWrapper<>(new TaskInstance()) |
||||||
|
.lambda() |
||||||
|
.eq(TaskInstance::getProjectCode, null) |
||||||
|
.last("limit 1000"); |
||||||
|
List<TaskInstance> taskInstances = taskInstanceMapper.selectList(wrapper); |
||||||
|
if (CollectionUtils.isEmpty(taskInstances)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
for (TaskInstance taskInstance : taskInstances) { |
||||||
|
ProcessInstance processInstance = processInstanceMapper.selectById(taskInstance.getProcessInstanceId()); |
||||||
|
if (processInstance == null) { |
||||||
|
taskInstance.setProjectCode(-1L); |
||||||
|
} else { |
||||||
|
taskInstance.setProjectCode(processInstance.getProjectCode()); |
||||||
|
taskInstance.setProcessInstanceName(processInstance.getName()); |
||||||
|
taskInstance.setExecutorName(processInstance.getExecutorName()); |
||||||
|
} |
||||||
|
taskInstanceMapper.updateById(taskInstance); |
||||||
|
} |
||||||
|
|
||||||
|
log.info("Success upgrade task instance, current batch size: {}", taskInstances.size()); |
||||||
|
try { |
||||||
|
Thread.sleep(1000L); |
||||||
|
} catch (InterruptedException e) { |
||||||
|
log.error("Upgrade task instance error", e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String getCurrentVersion() { |
||||||
|
return "3.2.0"; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue