Skip to content

Commit

Permalink
rename service to reduce the cyclic dependency possible
Browse files Browse the repository at this point in the history
  • Loading branch information
anda-ren committed Nov 13, 2023
1 parent 26ca316 commit 79785ff
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import ai.starwhale.mlops.api.protocol.ft.FineTuneSpaceCreateRequest;
import ai.starwhale.mlops.api.protocol.ft.FineTuneSpaceVo;
import ai.starwhale.mlops.common.IdConverter;
import ai.starwhale.mlops.domain.ft.FineTuneService;
import ai.starwhale.mlops.domain.ft.FineTuneAppService;
import ai.starwhale.mlops.domain.ft.FineTuneSpaceService;
import ai.starwhale.mlops.domain.ft.vo.FineTuneVo;
import ai.starwhale.mlops.domain.project.ProjectService;
Expand Down Expand Up @@ -56,18 +56,18 @@ public class FineTuneController {
final UserService userService;
final FineTuneSpaceService fineTuneSpaceService;

final FineTuneService fineTuneService;
final FineTuneAppService fineTuneAppService;

public FineTuneController(
ProjectService projectService,
UserService userService,
FineTuneSpaceService fineTuneSpaceService,
FineTuneService fineTuneService
FineTuneAppService fineTuneAppService
) {
this.projectService = projectService;
this.userService = userService;
this.fineTuneSpaceService = fineTuneSpaceService;
this.fineTuneService = fineTuneService;
this.fineTuneAppService = fineTuneAppService;
}

@Operation(summary = "Get the list of fine-tune spaces")
Expand Down Expand Up @@ -124,7 +124,7 @@ public ResponseEntity<ResponseMessage<String>> createFineTune(
@Valid @RequestBody FineTuneCreateRequest request
) {

fineTuneService.createFineTune(
fineTuneAppService.createFineTune(
spaceId,
projectService.findProject(projectId),
request,
Expand All @@ -144,7 +144,7 @@ public ResponseEntity<ResponseMessage<PageInfo<FineTuneVo>>> listFineTune(
@RequestParam(required = false, defaultValue = "10") Integer pageSize
) {

PageInfo<FineTuneVo> pageInfo = fineTuneService.list(spaceId, pageNum, pageSize);
PageInfo<FineTuneVo> pageInfo = fineTuneAppService.list(spaceId, pageNum, pageSize);
return ResponseEntity.ok(Code.success.asResponse(pageInfo));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import org.springframework.util.StringUtils;

@Service
public class FineTuneService {
public class FineTuneAppService {

final JobCreator jobCreator;

Expand All @@ -71,7 +71,7 @@ public class FineTuneService {

final String instanceUri;

public FineTuneService(
public FineTuneAppService(
JobCreator jobCreator, FineTuneMapper fineTuneMapper, JobMapper jobMapper, JobSpecParser jobSpecParser,
ModelDao modelDao,
@Value("${sw.instance-uri}") String instanceUri,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2022 Starwhale, Inc. All Rights Reserved.
*
* Licensed 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 ai.starwhale.mlops.domain.ft;

import ai.starwhale.mlops.api.protocol.ft.FineTuneCreateRequest;
import ai.starwhale.mlops.common.Constants;
import ai.starwhale.mlops.domain.dataset.DatasetDao;
import ai.starwhale.mlops.domain.dataset.bo.DatasetVersion;
import ai.starwhale.mlops.domain.ft.mapper.FineTuneMapper;
import ai.starwhale.mlops.domain.ft.po.FineTuneEntity;
import ai.starwhale.mlops.domain.ft.vo.FineTuneVo;
import ai.starwhale.mlops.domain.job.JobCreator;
import ai.starwhale.mlops.domain.job.JobType;
import ai.starwhale.mlops.domain.job.bo.Job;
import ai.starwhale.mlops.domain.job.bo.UserJobCreateRequest;
import ai.starwhale.mlops.domain.job.mapper.JobMapper;
import ai.starwhale.mlops.domain.job.po.JobEntity;
import ai.starwhale.mlops.domain.job.spec.Env;
import ai.starwhale.mlops.domain.job.spec.JobSpecParser;
import ai.starwhale.mlops.domain.job.spec.StepSpec;
import ai.starwhale.mlops.domain.model.ModelDao;
import ai.starwhale.mlops.domain.model.bo.ModelVersion;
import ai.starwhale.mlops.domain.model.po.ModelVersionEntity;
import ai.starwhale.mlops.domain.project.bo.Project;
import ai.starwhale.mlops.domain.user.bo.User;
import ai.starwhale.mlops.exception.SwValidationException;
import ai.starwhale.mlops.exception.SwValidationException.ValidSubject;
import ai.starwhale.mlops.exception.api.StarwhaleApiException;
import ai.starwhale.mlops.schedule.impl.container.ContainerSpecification;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Service
public class FineTuneDomainService {

final FineTuneMapper fineTuneMapper;

public FineTuneDomainService(FineTuneMapper fineTuneMapper) {
this.fineTuneMapper = fineTuneMapper;
}

public void attachTargetModel(Long id, ModelVersionEntity modelVersionEntity) {
fineTuneMapper.updateTargetModel(id, modelVersionEntity.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import ai.starwhale.mlops.domain.bundle.revert.RevertManager;
import ai.starwhale.mlops.domain.bundle.tag.BundleVersionTagDao;
import ai.starwhale.mlops.domain.bundle.tag.po.BundleVersionTagEntity;
import ai.starwhale.mlops.domain.ft.FineTuneService;
import ai.starwhale.mlops.domain.ft.FineTuneDomainService;
import ai.starwhale.mlops.domain.job.cache.HotJobHolder;
import ai.starwhale.mlops.domain.job.spec.JobSpecParser;
import ai.starwhale.mlops.domain.job.status.JobStatus;
Expand Down Expand Up @@ -110,7 +110,6 @@
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4SafeDecompressor;
import org.apache.commons.codec.binary.Hex;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -141,7 +140,7 @@ public class ModelService {
@Setter
private BundleManager bundleManager;

private final FineTuneService fineTuneService;
private final FineTuneDomainService fineTuneService;

public ModelService(
ModelMapper modelMapper,
Expand All @@ -158,7 +157,7 @@ public ModelService(
TrashService trashService,
JobSpecParser jobSpecParser,
BlobService blobService,
@Lazy FineTuneService fineTuneService
FineTuneDomainService fineTuneService
) {
this.modelMapper = modelMapper;
this.modelVersionMapper = modelVersionMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

class FineTuneServiceTest {
class FineTuneAppServiceTest {

JobCreator jobCreator;

Expand All @@ -59,7 +59,7 @@ class FineTuneServiceTest {

DatasetDao datasetDao;

FineTuneService fineTuneService;
FineTuneAppService fineTuneAppService;

@BeforeEach
public void setup() {
Expand All @@ -69,7 +69,7 @@ public void setup() {
jobSpecParser = mock(JobSpecParser.class);
modelDao = mock(ModelDao.class);
datasetDao = mock(DatasetDao.class);
fineTuneService = new FineTuneService(
fineTuneAppService = new FineTuneAppService(
jobCreator,
fineTuneMapper,
jobMapper,
Expand Down Expand Up @@ -97,7 +97,7 @@ public Object answer(InvocationOnMock invocation) {
when(datasetDao.getDatasetVersion(anyLong())).thenReturn(DatasetVersion.builder().projectId(22L).datasetName(
"dsn").versionName("dsv").build());
when(jobSpecParser.parseAndFlattenStepFromYaml(any())).thenReturn(List.of(StepSpec.builder().build()));
fineTuneService.createFineTune(1L, Project.builder().build(), request, User.builder().build());
fineTuneAppService.createFineTune(1L, Project.builder().build(), request, User.builder().build());

verify(fineTuneMapper).updateJobId(123L, 22L);

Expand All @@ -107,7 +107,7 @@ public Object answer(InvocationOnMock invocation) {
void listFt() {
when(fineTuneMapper.list(anyLong())).thenReturn(List.of(FineTuneEntity.builder().jobId(1L).build()));
when(jobMapper.findJobById(1L)).thenReturn(JobEntity.builder().build());
Assertions.assertEquals(1, fineTuneService.list(1L, 1, 1).getSize());
Assertions.assertEquals(1, fineTuneAppService.list(1L, 1, 1).getSize());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import ai.starwhale.mlops.domain.MySqlContainerHolder;
import ai.starwhale.mlops.domain.blob.BlobService;
import ai.starwhale.mlops.domain.bundle.tag.BundleVersionTagDao;
import ai.starwhale.mlops.domain.ft.FineTuneService;
import ai.starwhale.mlops.domain.ft.FineTuneAppService;
import ai.starwhale.mlops.domain.job.ModelServingService;
import ai.starwhale.mlops.domain.job.cache.HotJobHolder;
import ai.starwhale.mlops.domain.job.spec.JobSpecParser;
Expand Down Expand Up @@ -231,7 +231,7 @@ RunExecutor runExecutor() {
private StorageAccessService storageAccessService;

@Autowired
private FineTuneService fineTuneService;
private FineTuneAppService fineTuneAppService;

private static final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();

Expand Down Expand Up @@ -781,7 +781,8 @@ public void testShareModelVersion() {
mock(TrashService.class),
mock(JobSpecParser.class),
mock(BlobService.class),
fineTuneService);
fineTuneAppService
);

// public project
when(projectService.getProjectVo("pub")).thenReturn(ProjectVo.builder().id("1").privacy("PUBLIC").build());
Expand Down Expand Up @@ -986,7 +987,8 @@ public void testGetFileDataReleaseResource() throws Exception {
mock(TrashService.class),
mock(JobSpecParser.class),
blobService,
fineTuneService);
fineTuneAppService
);
var file = ModelPackageStorage.File.newBuilder()
.setSize(1L)
.addBlobIds("1")
Expand Down

0 comments on commit 79785ff

Please sign in to comment.