|
| 1 | +package com.linkedin.metadata.boot.steps; |
| 2 | + |
| 3 | +import com.linkedin.common.AuditStamp; |
| 4 | +import com.linkedin.common.urn.Urn; |
| 5 | +import com.linkedin.data.template.RecordTemplate; |
| 6 | +import com.linkedin.events.metadata.ChangeType; |
| 7 | +import com.linkedin.form.FormInfo; |
| 8 | +import com.linkedin.metadata.Constants; |
| 9 | +import com.linkedin.metadata.boot.UpgradeStep; |
| 10 | +import com.linkedin.metadata.entity.EntityService; |
| 11 | +import com.linkedin.metadata.entity.ListResult; |
| 12 | +import com.linkedin.metadata.models.AspectSpec; |
| 13 | +import com.linkedin.metadata.query.ExtraInfo; |
| 14 | +import io.datahubproject.metadata.context.OperationContext; |
| 15 | +import java.util.LinkedList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.concurrent.ExecutionException; |
| 19 | +import java.util.concurrent.Future; |
| 20 | +import javax.annotation.Nonnull; |
| 21 | +import lombok.extern.slf4j.Slf4j; |
| 22 | + |
| 23 | +@Slf4j |
| 24 | +public class RestoreFormInfoIndicesStep extends UpgradeStep { |
| 25 | + private static final String VERSION = "1"; |
| 26 | + private static final String UPGRADE_ID = "restore-form-info-indices"; |
| 27 | + private static final Integer BATCH_SIZE = 1000; |
| 28 | + |
| 29 | + public RestoreFormInfoIndicesStep(@Nonnull final EntityService<?> entityService) { |
| 30 | + super(entityService, VERSION, UPGRADE_ID); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void upgrade(@Nonnull OperationContext systemOperationContext) throws Exception { |
| 35 | + final AuditStamp auditStamp = |
| 36 | + new AuditStamp() |
| 37 | + .setActor(Urn.createFromString(Constants.SYSTEM_ACTOR)) |
| 38 | + .setTime(System.currentTimeMillis()); |
| 39 | + |
| 40 | + final int totalFormCount = getAndRestoreFormInfoIndices(systemOperationContext, 0, auditStamp); |
| 41 | + int formCount = BATCH_SIZE; |
| 42 | + while (formCount < totalFormCount) { |
| 43 | + getAndRestoreFormInfoIndices(systemOperationContext, formCount, auditStamp); |
| 44 | + formCount += BATCH_SIZE; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Nonnull |
| 49 | + @Override |
| 50 | + public ExecutionMode getExecutionMode() { |
| 51 | + return ExecutionMode.ASYNC; |
| 52 | + } |
| 53 | + |
| 54 | + private int getAndRestoreFormInfoIndices( |
| 55 | + @Nonnull OperationContext systemOperationContext, int start, AuditStamp auditStamp) { |
| 56 | + final AspectSpec formInfoAspectSpec = |
| 57 | + systemOperationContext |
| 58 | + .getEntityRegistry() |
| 59 | + .getEntitySpec(Constants.FORM_ENTITY_NAME) |
| 60 | + .getAspectSpec(Constants.FORM_INFO_ASPECT_NAME); |
| 61 | + |
| 62 | + final ListResult<RecordTemplate> latestAspects = |
| 63 | + entityService.listLatestAspects( |
| 64 | + systemOperationContext, |
| 65 | + Constants.FORM_ENTITY_NAME, |
| 66 | + Constants.FORM_INFO_ASPECT_NAME, |
| 67 | + start, |
| 68 | + BATCH_SIZE); |
| 69 | + |
| 70 | + if (latestAspects.getTotalCount() == 0 |
| 71 | + || latestAspects.getValues() == null |
| 72 | + || latestAspects.getMetadata() == null) { |
| 73 | + log.debug("Found 0 formInfo aspects for forms. Skipping migration."); |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + if (latestAspects.getValues().size() != latestAspects.getMetadata().getExtraInfos().size()) { |
| 78 | + // Bad result -- we should log that we cannot migrate this batch of formInfos. |
| 79 | + log.warn( |
| 80 | + "Failed to match formInfo aspects with corresponding urns. Found mismatched length between aspects ({})" |
| 81 | + + "and metadata ({}) for metadata {}", |
| 82 | + latestAspects.getValues().size(), |
| 83 | + latestAspects.getMetadata().getExtraInfos().size(), |
| 84 | + latestAspects.getMetadata()); |
| 85 | + return latestAspects.getTotalCount(); |
| 86 | + } |
| 87 | + |
| 88 | + List<Future<?>> futures = new LinkedList<>(); |
| 89 | + for (int i = 0; i < latestAspects.getValues().size(); i++) { |
| 90 | + ExtraInfo info = latestAspects.getMetadata().getExtraInfos().get(i); |
| 91 | + RecordTemplate formInfoRecord = latestAspects.getValues().get(i); |
| 92 | + Urn urn = info.getUrn(); |
| 93 | + FormInfo formInfo = (FormInfo) formInfoRecord; |
| 94 | + if (formInfo == null) { |
| 95 | + log.warn("Received null formInfo for urn {}", urn); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + futures.add( |
| 100 | + entityService |
| 101 | + .alwaysProduceMCLAsync( |
| 102 | + systemOperationContext, |
| 103 | + urn, |
| 104 | + Constants.FORM_ENTITY_NAME, |
| 105 | + Constants.FORM_INFO_ASPECT_NAME, |
| 106 | + formInfoAspectSpec, |
| 107 | + null, |
| 108 | + formInfo, |
| 109 | + null, |
| 110 | + null, |
| 111 | + auditStamp, |
| 112 | + ChangeType.RESTATE) |
| 113 | + .getFirst()); |
| 114 | + } |
| 115 | + |
| 116 | + futures.stream() |
| 117 | + .filter(Objects::nonNull) |
| 118 | + .forEach( |
| 119 | + f -> { |
| 120 | + try { |
| 121 | + f.get(); |
| 122 | + } catch (InterruptedException | ExecutionException e) { |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + return latestAspects.getTotalCount(); |
| 128 | + } |
| 129 | +} |
0 commit comments