Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-23962 Db lock added to idgen batch ops #1377

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public interface UinRepository extends JpaRepository<UinEntity, String> {
public UinEntity findByUin(String uin);

@Modifying
@Query(value = "UPDATE kernel.uin SET uin_status=:status, upd_by=:contextUser, upd_dtimes=:uptimes where uin=:uin", nativeQuery = true)
@Query(value = "UPDATE kernel.uin SET uin_status=:status, upd_by=:contextUser, upd_dtimes=:uptimes where uin=:uin for update", nativeQuery = true)
public void updateStatus(@Param("status") String status, @Param("contextUser") String contextUser,
@Param("uptimes") LocalDateTime uptimes, @Param("uin") String uin);

@Query(value = "select uu.uin, uu.cr_by, uu.cr_dtimes, uu.del_dtimes, uu.is_deleted, uu.upd_by, uu.upd_dtimes, uu.uin_status from kernel.uin uu where uu.uin_status=?", nativeQuery = true)
public List<UinEntity> findByStatus(String status);
@Query(value = "select uu.uin, uu.cr_by, uu.cr_dtimes, uu.del_dtimes, uu.is_deleted, uu.upd_by, uu.upd_dtimes, uu.uin_status from kernel.uin uu where uu.uin_status=:status limit :limit ", nativeQuery = true)
public List<UinEntity> findByStatus(@Param("status") String status, @Param("limit") int limit);

long countByStatusAndIsDeletedFalse(String status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -41,9 +41,11 @@
*/
@Component
public class UinServiceImpl implements UinService {

private Logger LOGGER = LoggerFactory.getLogger(UinServiceImpl.class);

@Value("${mosip.kernel.uin-transfer-limit:20000}")
private int uinTransferLimit;
/**
* Field for {@link #uinRepository}
*/
Expand Down Expand Up @@ -128,7 +130,7 @@ public UinStatusUpdateReponseDto updateUinStatus(UinEntity uinAck, RoutingContex
@Transactional(transactionManager = "transactionManager")
@Override
public void transferUin() {
List<UinEntity> uinEntities=uinRepository.findByStatus(UinGeneratorConstant.ASSIGNED);
List<UinEntity> uinEntities=uinRepository.findByStatus(UinGeneratorConstant.ASSIGNED,uinTransferLimit);
List<UinEntityAssigned> uinEntitiesAssined = modelMapper.map(uinEntities, new TypeToken<List<UinEntityAssigned>>() {}.getType());
uinRepositoryAssigned.saveAll(uinEntitiesAssined);
uinRepository.deleteAll(uinEntities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import io.mosip.kernel.vidgenerator.entity.VidAssignedEntity;

public interface VidAssignedRepository extends JpaRepository<VidAssignedEntity, String> {

List<VidAssignedEntity> findByStatusAndIsDeletedFalse(String status);
@Query(value = "select v.vid, v.cr_by, v.cr_dtimes, v.del_dtimes, v.is_deleted, v.upd_by, v.upd_dtimes, v.vid_status from kernel.vid_assigned v where v.vid_status=:status limit :limit ", nativeQuery = true)
List<VidAssignedEntity> findByStatusAndIsDeletedFalse(@Param("status") String status, @Param("limit") int limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public interface VidRepository extends JpaRepository<VidEntity, String> {

long countByStatusAndIsDeletedFalse(String status);

List<VidEntity> findByStatusAndIsDeletedFalse(String status);
@Query(value = "select v.vid,v.vid_status,v.expiry_dtimes,v.cr_by, v.cr_dtimes, v.del_dtimes, v.is_deleted, v.upd_by, v.upd_dtimes from kernel.vid v where v.vid_status=:status limit :limit FOR UPDATE", nativeQuery = true)
List<VidEntity> findByStatusAndIsDeletedFalse(@Param("status") String status, @Param("limit") int limit);

@Modifying
@Query(value = "UPDATE kernel.vid SET vid_status=:status, upd_by=:contextUser, upd_dtimes=:uptimes where vid=:vid", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public class VidServiceImpl implements VidService {

@Value("${mosip.kernel.vid.time-to-release-after-expiry}")
private long timeToRelaseAfterExpiry;


@Value("${mosip.kernel.vid-expire-limit:20000}")
private int vidExpireLimit;

@Value("${mosip.kernel.vid-release-limit:20000}")
private int vidReleaseLimit;


@Value("${mosip.kernel.vid-isolate-limit:20000}")
private int vidIsolateLimit;

@Autowired
private VidRepository vidRepository;
Expand Down Expand Up @@ -122,14 +133,14 @@ public void expireAndRelease() {

private void expireEligibleVids() {
List<VidAssignedEntity> vidAssignedEntities = vidAssignedRepository
.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED);
.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,vidExpireLimit);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is about expiry and there is no sorting, if we add limit will it not create issues like taking more time to expire when there are large no of rows ? What is the frequency of this job ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it will take some time to expire. frequency is per day at 11 PM. But they can increase freq and limit or should we remove limit as we will scan and expire all in case eligible?

vidAssignedEntities.forEach(this::expireIfEligible);
vidAssignedRepository.saveAll(vidAssignedEntities);
}

private void releaseEligibleVids() {
List<VidAssignedEntity> vidExpiredEntities = vidAssignedRepository
.findByStatusAndIsDeletedFalse(VidLifecycleStatus.EXPIRED);
.findByStatusAndIsDeletedFalse(VidLifecycleStatus.EXPIRED,vidReleaseLimit);
List<VidAssignedEntity> releasableVidAssignedEntities = new ArrayList<VidAssignedEntity>();
vidExpiredEntities.forEach(entity -> {
if(isEligibleToRelease(entity)) {
Expand Down Expand Up @@ -186,7 +197,7 @@ public boolean saveVID(VidEntity vid) {
@Transactional(transactionManager = "transactionManager")
@Override
public void isolateAssignedVids() {
List<VidEntity> vidEntities = vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED);
List<VidEntity> vidEntities = vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,vidIsolateLimit);
LOGGER.info("isolateAssignedVids called for entity count {} ", vidEntities.size());
List<VidAssignedEntity> vidEntitiesAssined = modelMapper.map(vidEntities,
new TypeToken<List<VidAssignedEntity>>() {}.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,21 @@ public void fetchVidCountTest() {

@Test
public void expireOrReleaseDataAccessExceptionTest() {
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED))
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,20000))
.thenThrow(new DataRetrievalFailureException("DataBase error occur"));
vidService.expireAndRelease();
}

@Test
public void expireOrReleaseExceptionTest() {
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED))
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,20000))
.thenThrow(new RuntimeException("DataBase error occur"));
vidService.expireAndRelease();
}

@Test
public void expireOrReleaseTest() {
Mockito.when(vidAssignedRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED))
Mockito.when(vidAssignedRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,20000))
.thenReturn(Stream.of(expiredButAssignedStatusVAEntities, notExpiredButAssignedStatusVAEntities)
.flatMap(Collection::stream)
.collect(Collectors.toList()));
Expand All @@ -256,7 +256,7 @@ public void expireOrReleaseTest() {
(List<VidAssignedEntity>) i.getArguments()[0]).spliterator(), false)
.collect(Collectors.toList()));

Mockito.when(vidAssignedRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.EXPIRED))
Mockito.when(vidAssignedRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.EXPIRED,20000))
.thenReturn(Stream.of(releasableButExpiredStatusVAEntities, nonReleasableButExpiredStatusVAEntities)
.flatMap(Collection::stream)
.collect(Collectors.toList()));
Expand Down Expand Up @@ -326,7 +326,7 @@ public void isolateAssignedVidTest() {
Stream.of(nonExpiredButAssignedStatusEntities, expiredButAssignedStatusEntities)
.flatMap(Collection::stream)
.collect(Collectors.toList());
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED))
Mockito.when(vidRepository.findByStatusAndIsDeletedFalse(VidLifecycleStatus.ASSIGNED,20000))
.thenReturn(validTranserEntities);
Mockito.when(vidAssignedRepository.saveAll(vidAssignedEntityListCaptor.capture()))
.thenAnswer(i -> StreamSupport.stream((
Expand Down