Skip to content

Commit bdad019

Browse files
authored
fix(taskframework): fix retry log attribute (#1904)
1 parent 4be845d commit bdad019

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2023 OceanBase.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.oceanbase.odc.service.task.constants;
17+
18+
/**
19+
* @author yaobin
20+
* @date 2024-03-13
21+
* @since 4.2.4
22+
*/
23+
public class JobAttributeEntityColumn {
24+
25+
public static final String ID = "id";
26+
27+
public static final String ATTRIBUTE_KEY = "attributeKey";
28+
29+
public static final String ATTRIBUTE_VALUE = "attributeValue";
30+
31+
}

server/odc-service/src/main/java/com/oceanbase/odc/service/task/executor/task/BaseTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void start(JobContext context) {
5959
updateStatus(JobStatus.FAILED);
6060
}
6161
} catch (Throwable e) {
62-
log.info("Task failed, id={}.", getJobId(), e);
62+
log.warn("Task failed, id={}.", getJobId(), e);
6363
updateStatus(JobStatus.FAILED);
6464
} finally {
6565
try {

server/odc-service/src/main/java/com/oceanbase/odc/service/task/service/StdTaskFrameworkService.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oceanbase.odc.metadb.task.JobEntity;
5858
import com.oceanbase.odc.metadb.task.JobRepository;
5959
import com.oceanbase.odc.service.task.config.TaskFrameworkProperties;
60+
import com.oceanbase.odc.service.task.constants.JobAttributeEntityColumn;
6061
import com.oceanbase.odc.service.task.constants.JobEntityColumn;
6162
import com.oceanbase.odc.service.task.enums.JobStatus;
6263
import com.oceanbase.odc.service.task.enums.TaskRunMode;
@@ -315,14 +316,34 @@ private void updateJobScheduleEntity(TaskResult taskResult) {
315316
jobRepository.update(jse);
316317

317318
if (taskResult.getLogMetadata() != null && taskResult.getStatus().isTerminated()) {
318-
taskResult.getLogMetadata().forEach((k, v) -> {
319+
saveOrUpdateLogMetadata(taskResult, jse);
320+
}
321+
}
322+
323+
private void saveOrUpdateLogMetadata(TaskResult taskResult, JobEntity jse) {
324+
taskResult.getLogMetadata().forEach((k, v) -> {
325+
// log key may exist if job is retrying
326+
Optional<String> logValue = findByJobIdAndAttributeKey(jse.getId(), k);
327+
if (logValue.isPresent()) {
328+
updateJobAttributeValue(jse.getId(), k, v);
329+
} else {
319330
JobAttributeEntity jobAttribute = new JobAttributeEntity();
320331
jobAttribute.setJobId(jse.getId());
321332
jobAttribute.setAttributeKey(k);
322333
jobAttribute.setAttributeValue(v);
323334
jobAttributeRepository.save(jobAttribute);
324-
});
325-
}
335+
}
336+
});
337+
}
338+
339+
private void updateJobAttributeValue(Long id, String key, String value) {
340+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
341+
CriteriaUpdate<JobAttributeEntity> update = cb.createCriteriaUpdate(JobAttributeEntity.class);
342+
Root<JobAttributeEntity> e = update.from(JobAttributeEntity.class);
343+
update.set(JobAttributeEntityColumn.ATTRIBUTE_VALUE, value);
344+
update.where(cb.equal(e.get(JobAttributeEntityColumn.ID), id),
345+
cb.equal(e.get(JobAttributeEntityColumn.ATTRIBUTE_KEY), key));
346+
entityManager.createQuery(update).executeUpdate();
326347
}
327348

328349
@Transactional(rollbackFor = Exception.class)
@@ -467,4 +488,5 @@ public Optional<String> findByJobIdAndAttributeKey(Long jobId, String attributeK
467488
JobAttributeEntity attributeEntity = jobAttributeRepository.findByJobIdAndAttributeKey(jobId, attributeKey);
468489
return Objects.isNull(attributeEntity) ? Optional.empty() : Optional.of(attributeEntity.getAttributeValue());
469490
}
491+
470492
}

0 commit comments

Comments
 (0)