-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reindex): Implement Reindex status Endpoint (#641)
* feat(reindex): Implement Reindex status Endpoint - Implement DB schema - Implement status update trigger on processed items updates - Implement endpoint Implements: MSEARCH-797
- Loading branch information
1 parent
60647a5
commit baee716
Showing
20 changed files
with
603 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/org/folio/search/converter/ReindexStatusMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.folio.search.converter; | ||
|
||
import org.folio.search.domain.dto.ReindexStatusItem; | ||
import org.folio.search.model.reindex.ReindexStatusEntity; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface ReindexStatusMapper { | ||
|
||
ReindexStatusItem convert(ReindexStatusEntity entity); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/org/folio/search/model/reindex/ReindexStatusEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.folio.search.model.reindex; | ||
|
||
import java.sql.Timestamp; | ||
import lombok.Data; | ||
import org.folio.search.model.types.ReindexEntityType; | ||
import org.folio.search.model.types.ReindexStatus; | ||
|
||
@Data | ||
public class ReindexStatusEntity { | ||
|
||
public static final String REINDEX_STATUS_TABLE = "reindex_status"; | ||
public static final String ENTITY_TYPE_COLUMN = "entity_type"; | ||
public static final String STATUS_COLUMN = "status"; | ||
public static final String TOTAL_MERGE_RANGES_COLUMN = "total_merge_ranges"; | ||
public static final String PROCESSED_MERGE_RANGES_COLUMN = "processed_merge_ranges"; | ||
public static final String TOTAL_UPLOAD_RANGES_COLUMN = "total_upload_ranges"; | ||
public static final String PROCESSED_UPLOAD_RANGES_COLUMN = "processed_upload_ranges"; | ||
public static final String START_TIME_MERGE_COLUMN = "start_time_merge"; | ||
public static final String END_TIME_MERGE_COLUMN = "end_time_merge"; | ||
public static final String START_TIME_UPLOAD_COLUMN = "start_time_upload"; | ||
public static final String END_TIME_UPLOAD_COLUMN = "end_time_upload"; | ||
|
||
private final ReindexEntityType entityType; | ||
private final ReindexStatus status; | ||
private int totalMergeRanges; | ||
private int processedMergeRanges; | ||
private int totalUploadRanges; | ||
private int processedUploadRanges; | ||
private Timestamp startTimeMerge; | ||
private Timestamp endTimeMerge; | ||
private Timestamp startTimeUpload; | ||
private Timestamp endTimeUpload; | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/folio/search/model/types/ReindexStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.folio.search.model.types; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum ReindexStatus { | ||
|
||
MERGE_IN_PROGRESS("Merge In Progress"), | ||
MERGE_COMPLETED("Merge Completed"), | ||
UPLOAD_IN_PROGRESS("Upload In Progress"), | ||
UPLOAD_COMPLETED("Upload Completed"), | ||
UPLOAD_FAILED("Upload Failed"); | ||
|
||
private final String value; | ||
|
||
ReindexStatus(String value) { | ||
this.value = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/org/folio/search/service/reindex/jdbc/ReindexStatusRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.folio.search.service.reindex.jdbc; | ||
|
||
import static org.folio.search.model.reindex.ReindexStatusEntity.END_TIME_MERGE_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.END_TIME_UPLOAD_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.PROCESSED_MERGE_RANGES_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.PROCESSED_UPLOAD_RANGES_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.REINDEX_STATUS_TABLE; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.START_TIME_MERGE_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.START_TIME_UPLOAD_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.STATUS_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.TOTAL_MERGE_RANGES_COLUMN; | ||
import static org.folio.search.model.reindex.ReindexStatusEntity.TOTAL_UPLOAD_RANGES_COLUMN; | ||
import static org.folio.search.utils.JdbcUtils.getFullTableName; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.folio.search.model.reindex.ReindexStatusEntity; | ||
import org.folio.search.model.types.ReindexEntityType; | ||
import org.folio.search.model.types.ReindexStatus; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReindexStatusRepository { | ||
|
||
private static final String SELECT_REINDEX_STATUS_BY_REINDEX_ID_SQL = "SELECT * FROM %s;"; | ||
private static final String UPDATE_REINDEX_STATUS_SQL = """ | ||
UPDATE %s | ||
SET status = ?, %s = ? | ||
WHERE entity_type = ?; | ||
"""; | ||
private static final String UPDATE_REINDEX_COUNTS_SQL = """ | ||
UPDATE %s | ||
SET processed_merge_ranges = processed_merge_ranges + ?, | ||
processed_upload_ranges = processed_upload_ranges + ? | ||
WHERE entity_type = ?; | ||
"""; | ||
|
||
private final FolioExecutionContext context; | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
|
||
public List<ReindexStatusEntity> getReindexStatuses() { | ||
var fullTableName = getFullTableName(context, REINDEX_STATUS_TABLE); | ||
var sql = SELECT_REINDEX_STATUS_BY_REINDEX_ID_SQL.formatted(fullTableName); | ||
return jdbcTemplate.query(sql, reindexStatusRowMapper()); | ||
} | ||
|
||
public void setReindexUploadFailed(ReindexEntityType entityType) { | ||
var fullTableName = getFullTableName(context, REINDEX_STATUS_TABLE); | ||
var sql = UPDATE_REINDEX_STATUS_SQL.formatted(fullTableName, END_TIME_UPLOAD_COLUMN); | ||
|
||
jdbcTemplate.update(sql, ReindexStatus.UPLOAD_FAILED.name(), Timestamp.from(Instant.now()), entityType.name()); | ||
} | ||
|
||
public void addReindexCounts(ReindexEntityType entityType, int processedMergeRanges, | ||
int processedUploadRanges) { | ||
var fullTableName = getFullTableName(context, REINDEX_STATUS_TABLE); | ||
var sql = UPDATE_REINDEX_COUNTS_SQL.formatted(fullTableName); | ||
|
||
jdbcTemplate.update(sql, processedMergeRanges, processedUploadRanges, entityType.name()); | ||
} | ||
|
||
private RowMapper<ReindexStatusEntity> reindexStatusRowMapper() { | ||
return (rs, rowNum) -> { | ||
var reindexStatus = new ReindexStatusEntity( | ||
ReindexEntityType.valueOf(rs.getString(ReindexStatusEntity.ENTITY_TYPE_COLUMN)), | ||
ReindexStatus.valueOf(rs.getString(STATUS_COLUMN)) | ||
); | ||
reindexStatus.setTotalMergeRanges(rs.getInt(TOTAL_MERGE_RANGES_COLUMN)); | ||
reindexStatus.setProcessedMergeRanges(rs.getInt(PROCESSED_MERGE_RANGES_COLUMN)); | ||
reindexStatus.setTotalUploadRanges(rs.getInt(TOTAL_UPLOAD_RANGES_COLUMN)); | ||
reindexStatus.setProcessedUploadRanges(rs.getInt(PROCESSED_UPLOAD_RANGES_COLUMN)); | ||
reindexStatus.setStartTimeMerge(rs.getTimestamp(START_TIME_MERGE_COLUMN)); | ||
reindexStatus.setEndTimeMerge(rs.getTimestamp(END_TIME_MERGE_COLUMN)); | ||
reindexStatus.setStartTimeUpload(rs.getTimestamp(START_TIME_UPLOAD_COLUMN)); | ||
reindexStatus.setEndTimeUpload(rs.getTimestamp(END_TIME_UPLOAD_COLUMN)); | ||
return reindexStatus; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/resources/changelog/changes/v4.0/update-reindex-status-trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CREATE OR REPLACE FUNCTION update_reindex_status_trigger() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
-- update status and end time for merge | ||
IF OLD.status = 'MERGE_IN_PROGRESS' and NEW.total_merge_ranges = NEW.processed_merge_ranges | ||
THEN NEW.status = 'MERGE_COMPLETED'; NEW.end_time_merge = current_timestamp; | ||
ELSE | ||
-- update status and end time for upload | ||
IF OLD.status = 'UPLOAD_IN_PROGRESS' and NEW.total_upload_ranges = NEW.processed_upload_ranges | ||
THEN NEW.status = 'UPLOAD_COMPLETED'; NEW.end_time_upload = current_timestamp; | ||
END IF; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
DROP TRIGGER IF EXISTS reindex_status_updated_trigger ON reindex_status CASCADE; | ||
CREATE TRIGGER reindex_status_updated_trigger | ||
BEFORE UPDATE | ||
ON reindex_status | ||
FOR EACH ROW | ||
EXECUTE FUNCTION update_reindex_status_trigger(); |
Oops, something went wrong.