mneethiraj commented on code in PR #307: URL: https://github.com/apache/atlas/pull/307#discussion_r2011104270
########## intg/src/main/java/org/apache/atlas/model/impexp/AsyncImportStatus.java: ########## @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.model.impexp; + +public class AsyncImportStatus { Review Comment: Since instances of this class would be serialized, this shoud implement `Serialize`, and have appropriate @Json annotations. ########## intg/src/main/java/org/apache/atlas/model/impexp/AsyncImportStatus.java: ########## @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.model.impexp; + +public class AsyncImportStatus { + private String importId; + private AtlasAsyncImportRequest.ImportStatus status; + private String importRequestReceivedAt; Review Comment: should `importRequestReceivedAt` be of type `Date`? ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,413 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.impexp; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.AtlasBaseModelObject; +import org.apache.atlas.utils.AtlasEntityUtil; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TimeZone; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class AtlasAsyncImportRequest extends AtlasBaseModelObject implements Serializable { + private static final long serialVersionUID = 1L; + public static final String ASYNC_IMPORT_TYPE_NAME = "__AtlasAsyncImportRequest"; + public static final String ASYNC_IMPORT_TOPIC_PREFIX = "ATLAS_IMPORT_"; + public static final String REQUEST_ID_PREFIX_PROPERTY = "async_import_"; + + public enum ImportStatus { + STAGING("STAGING"), + WAITING("WAITING"), + PROCESSING("PROCESSING"), + SUCCESSFUL("SUCCESSFUL"), + PARTIAL_SUCCESS("PARTIAL_SUCCESS"), + ABORTED("ABORTED"), + FAILED("FAILED"); + + private final String status; + + ImportStatus(String status) { + this.status = status; + } + + public String getStatus() { + return status; + } + + @Override + public String toString() { + return status; + } + } + + private String importId; + private ImportStatus status; + private ImportDetails importDetails = new ImportDetails(); + private long receivedAt; + private long stagedAt; + private long startedProcessingAt; + private long completedAt; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private AtlasImportResult importResult; + + @JsonIgnore + private ImportTrackingInfo importTrackingInfo; + + public ImportTrackingInfo getImportTrackingInfo() { + return importTrackingInfo; + } + + public void setImportTrackingInfo(ImportTrackingInfo importTrackingInfo) { + this.importTrackingInfo = importTrackingInfo; + } + + public AtlasAsyncImportRequest() {} + + public AtlasAsyncImportRequest(String guid) { + setGuid(guid); + } + + public AtlasAsyncImportRequest(AtlasImportResult result) { + this.importResult = result; + this.status = ImportStatus.STAGING; + this.receivedAt = 0L; + this.stagedAt = 0L; + this.startedProcessingAt = 0L; + this.completedAt = 0L; + this.importDetails = new ImportDetails(); + this.importTrackingInfo = new ImportTrackingInfo(null, 0); + setGuid(getGuid()); + } + + public String getImportId() { + return importId; + } + + public void setImportId(String importId) { + this.importId = importId; + if (importTrackingInfo != null) { + importTrackingInfo.setRequestId(REQUEST_ID_PREFIX_PROPERTY + importId + "@" + AtlasEntityUtil.getMetadataNamespace()); + } + } + + public ImportStatus getStatus() { + return status; + } + + public void setStatus(ImportStatus status) { + this.status = status; + } + + public ImportDetails getImportDetails() { + return importDetails; + } + + public void setImportDetails(ImportDetails importDetails) { + this.importDetails = importDetails; + } + + @JsonIgnore + public String getTopicName() { + return ASYNC_IMPORT_TOPIC_PREFIX + importId; + } + + public AtlasImportResult getImportResult() { + return importResult; + } + + public void setImportResult(AtlasImportResult importResult) { + this.importResult = importResult; + } + + public long getReceivedAt() { + return receivedAt; + } + + public void setReceivedAt(long receivedAt) { + this.receivedAt = receivedAt; + } + + public long getStagedAt() { + return stagedAt; + } + + public void setStagedAt(long stagedAt) { + this.stagedAt = stagedAt; + } + + public long getStartedProcessingAt() { + return startedProcessingAt; + } + + public void setStartedProcessingAt(long startedProcessingAt) { + this.startedProcessingAt = startedProcessingAt; + } + + public long getCompletedAt() { + return completedAt; + } + + public void setCompletedAt(long completedAt) { + this.completedAt = completedAt; + } + + @JsonIgnore + public AsyncImportStatus toImportMinInfo() { + AsyncImportStatus asyncImportStatus = new AsyncImportStatus( + this.getImportId(), + status, + toIsoDate(new Date(this.receivedAt)), + importResult.getUserName()); + return asyncImportStatus; + } + + private String toIsoDate(Date value) { + final TimeZone tz = TimeZone.getTimeZone("UTC"); + final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + df.setTimeZone(tz); + return df.format(value); + } + + @Override + public String toString() { + return toString(new StringBuilder()).toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof AtlasAsyncImportRequest)) { + return false; + } + if (!super.equals(o)) { + return false; + } + AtlasAsyncImportRequest that = (AtlasAsyncImportRequest) o; + return Objects.equals(importResult, that.importResult) && + Objects.equals(importId, that.importId) && + Objects.equals(status, that.status) && + Objects.equals(importDetails, that.importDetails) && + Objects.equals(importTrackingInfo.getRequestId(), that.importTrackingInfo.getRequestId()) && Review Comment: `importTrackingInfo` could be null, right - looking at line 121 above? ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,383 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.impexp; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.AtlasBaseModelObject; +import org.apache.atlas.utils.AtlasEntityUtil; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TimeZone; +import java.util.concurrent.ConcurrentHashMap; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class AtlasAsyncImportRequest extends AtlasBaseModelObject implements Serializable { + private static final long serialVersionUID = 1L; + public static final String ASYNC_IMPORT_TYPE_NAME = "__AtlasAsyncImportRequest"; Review Comment: None of the classes in model package referenes `ASYNC_IMPORT_TYPE_NAME`. This is referenced only from `AsyncImportService` and `AtlasAsyncImportRequestDTO`. ########## intg/src/main/java/org/apache/atlas/model/impexp/AsyncImportStatus.java: ########## @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.model.impexp; + +public class AsyncImportStatus { + private String importId; + private AtlasAsyncImportRequest.ImportStatus status; + private String importRequestReceivedAt; + private String importRequestReceivedBy; Review Comment: What does field `importRequestReceivedBy` capture? Is it username or hostname or anything else? ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,413 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.impexp; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.AtlasBaseModelObject; +import org.apache.atlas.utils.AtlasEntityUtil; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TimeZone; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class AtlasAsyncImportRequest extends AtlasBaseModelObject implements Serializable { + private static final long serialVersionUID = 1L; + public static final String ASYNC_IMPORT_TYPE_NAME = "__AtlasAsyncImportRequest"; + public static final String ASYNC_IMPORT_TOPIC_PREFIX = "ATLAS_IMPORT_"; + public static final String REQUEST_ID_PREFIX_PROPERTY = "async_import_"; + + public enum ImportStatus { + STAGING("STAGING"), + WAITING("WAITING"), + PROCESSING("PROCESSING"), + SUCCESSFUL("SUCCESSFUL"), + PARTIAL_SUCCESS("PARTIAL_SUCCESS"), + ABORTED("ABORTED"), + FAILED("FAILED"); + + private final String status; + + ImportStatus(String status) { + this.status = status; + } + + public String getStatus() { + return status; + } + + @Override + public String toString() { + return status; + } + } + + private String importId; + private ImportStatus status; + private ImportDetails importDetails = new ImportDetails(); + private long receivedAt; + private long stagedAt; + private long startedProcessingAt; + private long completedAt; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private AtlasImportResult importResult; + + @JsonIgnore + private ImportTrackingInfo importTrackingInfo; + + public ImportTrackingInfo getImportTrackingInfo() { + return importTrackingInfo; + } + + public void setImportTrackingInfo(ImportTrackingInfo importTrackingInfo) { + this.importTrackingInfo = importTrackingInfo; + } + + public AtlasAsyncImportRequest() {} + + public AtlasAsyncImportRequest(String guid) { + setGuid(guid); + } + + public AtlasAsyncImportRequest(AtlasImportResult result) { + this.importResult = result; + this.status = ImportStatus.STAGING; + this.receivedAt = 0L; + this.stagedAt = 0L; + this.startedProcessingAt = 0L; + this.completedAt = 0L; + this.importDetails = new ImportDetails(); + this.importTrackingInfo = new ImportTrackingInfo(null, 0); + setGuid(getGuid()); + } + + public String getImportId() { + return importId; + } + + public void setImportId(String importId) { + this.importId = importId; + if (importTrackingInfo != null) { + importTrackingInfo.setRequestId(REQUEST_ID_PREFIX_PROPERTY + importId + "@" + AtlasEntityUtil.getMetadataNamespace()); + } + } + + public ImportStatus getStatus() { + return status; + } + + public void setStatus(ImportStatus status) { + this.status = status; + } + + public ImportDetails getImportDetails() { + return importDetails; + } + + public void setImportDetails(ImportDetails importDetails) { + this.importDetails = importDetails; + } + + @JsonIgnore + public String getTopicName() { + return ASYNC_IMPORT_TOPIC_PREFIX + importId; + } + + public AtlasImportResult getImportResult() { + return importResult; + } + + public void setImportResult(AtlasImportResult importResult) { + this.importResult = importResult; + } + + public long getReceivedAt() { + return receivedAt; + } + + public void setReceivedAt(long receivedAt) { + this.receivedAt = receivedAt; + } + + public long getStagedAt() { + return stagedAt; + } + + public void setStagedAt(long stagedAt) { + this.stagedAt = stagedAt; + } + + public long getStartedProcessingAt() { + return startedProcessingAt; + } + + public void setStartedProcessingAt(long startedProcessingAt) { + this.startedProcessingAt = startedProcessingAt; + } + + public long getCompletedAt() { + return completedAt; + } + + public void setCompletedAt(long completedAt) { + this.completedAt = completedAt; + } + + @JsonIgnore + public AsyncImportStatus toImportMinInfo() { + AsyncImportStatus asyncImportStatus = new AsyncImportStatus( + this.getImportId(), + status, + toIsoDate(new Date(this.receivedAt)), + importResult.getUserName()); + return asyncImportStatus; + } + + private String toIsoDate(Date value) { + final TimeZone tz = TimeZone.getTimeZone("UTC"); + final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + df.setTimeZone(tz); + return df.format(value); + } + + @Override Review Comment: Is this override necessary? ########## intg/src/main/java/org/apache/atlas/utils/AtlasAsyncImportTestUtil.java: ########## @@ -0,0 +1,64 @@ +package org.apache.atlas.utils; + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + */ + +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest; +import org.apache.atlas.model.impexp.AtlasImportRequest; +import org.apache.atlas.model.impexp.AtlasImportResult; + +import static org.apache.atlas.AtlasConfiguration.ATLAS_ASYNC_IMPORT_MIN_DURATION_OVERRIDE_TEST_AUTOMATION; + +public class AtlasAsyncImportTestUtil { + private AtlasAsyncImportTestUtil() { + // to block instantiation + } + + public static final String OPTION_KEY_MIN_ASYNC_IMPORT_COMPLETION_TIME = "asyncImportCompletionTime"; Review Comment: `asyncImportCompletionTime` => `asyncImportMinCompletionTime` ########## intg/src/test/java/org/apache/atlas/utils/TestAtlasAsyncImportTestUtil.java: ########## @@ -0,0 +1,108 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.atlas.utils; + +import org.apache.atlas.ApplicationProperties; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest; +import org.apache.atlas.model.impexp.AtlasImportRequest; +import org.apache.atlas.model.impexp.AtlasImportResult; +import org.apache.commons.configuration.Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static org.apache.atlas.AtlasConfiguration.ATLAS_ASYNC_IMPORT_MIN_DURATION_OVERRIDE_TEST_AUTOMATION; +import static org.apache.atlas.utils.AtlasAsyncImportTestUtil.OPTION_KEY_MIN_ASYNC_IMPORT_COMPLETION_TIME; +import static org.testng.Assert.assertTrue; + +public class TestAtlasAsyncImportTestUtil { + private Configuration conf; + + @BeforeClass + public void setup() throws Exception { + conf = ApplicationProperties.get(); + } + + @Test + public void testInterceptWaitsForRemainingTimeWhenOverrideEnabled() { + // Given + conf.setProperty(ATLAS_ASYNC_IMPORT_MIN_DURATION_OVERRIDE_TEST_AUTOMATION.getPropertyName(), true); + AtlasImportRequest importRequest = new AtlasImportRequest(); + importRequest.setOption(OPTION_KEY_MIN_ASYNC_IMPORT_COMPLETION_TIME, "3000"); + AtlasImportResult importResult = new AtlasImportResult(); + importResult.setRequest(importRequest); + + AtlasAsyncImportRequest asyncRequest = new AtlasAsyncImportRequest(importResult); + long now = System.currentTimeMillis(); + asyncRequest.setReceivedAt(now); + asyncRequest.setCompletedAt(now + 1000); + + long before = System.currentTimeMillis(); + // When + AtlasAsyncImportTestUtil.intercept(asyncRequest); + long after = System.currentTimeMillis(); + + // Then + long actualWait = after - before; + assertTrue(actualWait >= 1900, "Should have waited ~2000ms"); + } + + @Test + public void testInterceptSkipsSleepWhenDurationAlreadyMet() { + // Given + conf.setProperty(ATLAS_ASYNC_IMPORT_MIN_DURATION_OVERRIDE_TEST_AUTOMATION.getPropertyName(), true); + AtlasImportRequest importRequest = new AtlasImportRequest(); + importRequest.setOption(OPTION_KEY_MIN_ASYNC_IMPORT_COMPLETION_TIME, "3000"); + AtlasImportResult importResult = new AtlasImportResult(); + importResult.setRequest(importRequest); + + AtlasAsyncImportRequest asyncRequest = new AtlasAsyncImportRequest(); + long now = System.currentTimeMillis(); + asyncRequest.setReceivedAt(now); + asyncRequest.setCompletedAt(now + 4000); + + long before = System.currentTimeMillis(); + // When + AtlasAsyncImportTestUtil.intercept(asyncRequest); + long after = System.currentTimeMillis(); + + // Then + assertTrue(after - before < 200, "Should not sleep if already exceeded"); Review Comment: This test can fail if it took 200ms or more between line 79 and 82 - which can happen in a busy machine having only few CPU cycles. Redesign the test to avoid such flakiness. ########## intg/src/main/java/org/apache/atlas/model/notification/ImportNotification.java: ########## @@ -0,0 +1,167 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.notification; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; +import org.apache.atlas.model.typedef.AtlasTypesDef; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import java.io.Serializable; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +/** + * Class representing atlas import notification, extending HookNotification. + */ +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude +@JsonIgnoreProperties(ignoreUnknown = true) +@XmlRootElement +@XmlAccessorType(XmlAccessType.PROPERTY) +public class ImportNotification extends HookNotification implements Serializable { + private static final long serialVersionUID = 1L; + + @JsonProperty + private String importId; + + protected ImportNotification() { + } + + protected ImportNotification(HookNotificationType type, String user, String importId) { + super(type, user); + this.importId = importId; + } + + public String getImportId() { + return importId; + } + + public StringBuilder toString(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + + sb.append("ImportNotification{"); + super.toString(sb); + sb.append("type=").append(type); + sb.append(", user=").append(user); + sb.append(", importId=").append(importId); + sb.append("}"); + + return sb; + } + + /** + * Notification for type definitions import + */ + @JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIgnoreProperties(ignoreUnknown = true) + @XmlRootElement + @XmlAccessorType(XmlAccessType.PROPERTY) + public static class AtlasTypeDefImportNotification extends ImportNotification implements Serializable { + private static final long serialVersionUID = 1L; + + @JsonProperty + private AtlasTypesDef typeDefinitionMap; + + public AtlasTypeDefImportNotification() { + } + + public AtlasTypeDefImportNotification(String importId, String user, AtlasTypesDef typeDefinitionMap) { + super(HookNotificationType.IMPORT_TYPE_DEF, user, importId); + this.typeDefinitionMap = typeDefinitionMap; + } + + public AtlasTypesDef getTypeDefinitionMap() { + return typeDefinitionMap; + } + + @Override + public StringBuilder toString(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + + sb.append("AtlasTypeDefImportNotification{"); + super.toString(sb); + sb.append(", typeDefinitionMap=").append(typeDefinitionMap == null ? "null" : typeDefinitionMap.toString()); + sb.append("}"); + + return sb; + } + } + + /** + * Notification for entities import + */ + @JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) + @JsonInclude + @JsonIgnoreProperties(ignoreUnknown = true) + @XmlRootElement + @XmlAccessorType(XmlAccessType.PROPERTY) + public static class AtlasEntityImportNotification extends ImportNotification implements Serializable { + private static final long serialVersionUID = 1L; + + @JsonProperty + private AtlasEntityWithExtInfo entity; + + @JsonProperty + private int position; + + public AtlasEntityImportNotification() { + } + + public AtlasEntityImportNotification(String importId, String user, AtlasEntityWithExtInfo entity, int position) { + super(HookNotificationType.IMPORT_ENTITY, user, importId); + this.entity = entity; + this.position = position; + } + + public AtlasEntityWithExtInfo getEntity() { + return entity; + } + + public int getPosition() { + return position; + } + + @Override + public StringBuilder toString(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + + sb.append("AtlasEntityImportNotification{"); + super.toString(sb); + sb.append(", position=").append(position); + sb.append(", entities=").append(entity == null ? "null" : entity.toString()); Review Comment: line 161 can be replaced with : `sb.append(", entity=").append(entity);` ########## intg/src/main/java/org/apache/atlas/model/notification/ImportNotification.java: ########## @@ -0,0 +1,167 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.notification; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; +import org.apache.atlas.model.typedef.AtlasTypesDef; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import java.io.Serializable; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +/** + * Class representing atlas import notification, extending HookNotification. + */ +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude +@JsonIgnoreProperties(ignoreUnknown = true) +@XmlRootElement +@XmlAccessorType(XmlAccessType.PROPERTY) +public class ImportNotification extends HookNotification implements Serializable { + private static final long serialVersionUID = 1L; + + @JsonProperty + private String importId; + + protected ImportNotification() { + } + + protected ImportNotification(HookNotificationType type, String user, String importId) { + super(type, user); + this.importId = importId; + } + + public String getImportId() { + return importId; + } + + public StringBuilder toString(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + + sb.append("ImportNotification{"); + super.toString(sb); + sb.append("type=").append(type); + sb.append(", user=").append(user); + sb.append(", importId=").append(importId); + sb.append("}"); + + return sb; + } + + /** + * Notification for type definitions import + */ + @JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIgnoreProperties(ignoreUnknown = true) + @XmlRootElement + @XmlAccessorType(XmlAccessType.PROPERTY) + public static class AtlasTypeDefImportNotification extends ImportNotification implements Serializable { + private static final long serialVersionUID = 1L; + + @JsonProperty + private AtlasTypesDef typeDefinitionMap; + + public AtlasTypeDefImportNotification() { + } + + public AtlasTypeDefImportNotification(String importId, String user, AtlasTypesDef typeDefinitionMap) { + super(HookNotificationType.IMPORT_TYPE_DEF, user, importId); + this.typeDefinitionMap = typeDefinitionMap; + } + + public AtlasTypesDef getTypeDefinitionMap() { + return typeDefinitionMap; + } + + @Override + public StringBuilder toString(StringBuilder sb) { + if (sb == null) { + sb = new StringBuilder(); + } + + sb.append("AtlasTypeDefImportNotification{"); + super.toString(sb); + sb.append(", typeDefinitionMap=").append(typeDefinitionMap == null ? "null" : typeDefinitionMap.toString()); Review Comment: line 111 can be replaced with: `sb.append(", typeDefinitionMap=").append(typeDefinitionMap)` ########## repository/src/main/java/org/apache/atlas/repository/impexp/AsyncImportService.java: ########## @@ -0,0 +1,186 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.repository.impexp; + +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.SortOrder; +import org.apache.atlas.annotation.GraphTransaction; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.PList; +import org.apache.atlas.model.SearchFilter.SortType; +import org.apache.atlas.model.impexp.AsyncImportStatus; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest; +import org.apache.atlas.repository.ogm.DataAccess; +import org.apache.atlas.repository.ogm.impexp.AtlasAsyncImportRequestDTO; +import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ASYNC_IMPORT_TYPE_NAME; +import static org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus.ABORTED; +import static org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus.PROCESSING; +import static org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus.WAITING; + +@Service +public class AsyncImportService { + private static final Logger LOG = LoggerFactory.getLogger(AsyncImportService.class); + + private final DataAccess dataAccess; + + @Inject + public AsyncImportService(DataAccess dataAccess) { + this.dataAccess = dataAccess; + } + + public AtlasAsyncImportRequest fetchImportRequestByImportId(String importId) { + try { + AtlasAsyncImportRequest request = new AtlasAsyncImportRequest(); + request.setImportId(importId); + + return dataAccess.load(request); + } catch (Exception e) { + LOG.error("Error fetching request with importId: {}", importId, e); + return null; + } + } + + public void saveImportRequest(AtlasAsyncImportRequest importRequest) throws AtlasBaseException { + try { + dataAccess.save(importRequest); + LOG.debug("Save request ID: {} request: {}", importRequest.getImportId(), importRequest); + } catch (AtlasBaseException e) { + LOG.error("Failed to save import: {} with request: {}", importRequest.getImportId(), importRequest, e); + throw e; + } + } + + public void updateImportRequest(AtlasAsyncImportRequest importRequest) { + try { + saveImportRequest(importRequest); + } catch (AtlasBaseException abe) { + LOG.error("Failed to update import: {} with request: {}", importRequest.getImportId(), importRequest, abe); + } + } + + public List<String> fetchInProgressImportIds() { + return AtlasGraphUtilsV2.findEntityPropertyValuesByTypeAndPropertyName( Review Comment: `findEntityPropertyValuesByTypeAndPropertyName()` => `findEntityPropertyValuesByTypeAndAttributes()` ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,413 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.atlas.model.impexp; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.AtlasBaseModelObject; +import org.apache.atlas.utils.AtlasEntityUtil; + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TimeZone; + +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE; +import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.PUBLIC_ONLY; + +@JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class AtlasAsyncImportRequest extends AtlasBaseModelObject implements Serializable { + private static final long serialVersionUID = 1L; + public static final String ASYNC_IMPORT_TYPE_NAME = "__AtlasAsyncImportRequest"; + public static final String ASYNC_IMPORT_TOPIC_PREFIX = "ATLAS_IMPORT_"; + public static final String REQUEST_ID_PREFIX_PROPERTY = "async_import_"; + + public enum ImportStatus { + STAGING("STAGING"), + WAITING("WAITING"), + PROCESSING("PROCESSING"), + SUCCESSFUL("SUCCESSFUL"), + PARTIAL_SUCCESS("PARTIAL_SUCCESS"), + ABORTED("ABORTED"), + FAILED("FAILED"); + + private final String status; + + ImportStatus(String status) { + this.status = status; + } + + public String getStatus() { + return status; + } + + @Override + public String toString() { + return status; + } + } + + private String importId; + private ImportStatus status; + private ImportDetails importDetails = new ImportDetails(); + private long receivedAt; + private long stagedAt; + private long startedProcessingAt; + private long completedAt; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private AtlasImportResult importResult; + + @JsonIgnore + private ImportTrackingInfo importTrackingInfo; + + public ImportTrackingInfo getImportTrackingInfo() { + return importTrackingInfo; + } + + public void setImportTrackingInfo(ImportTrackingInfo importTrackingInfo) { + this.importTrackingInfo = importTrackingInfo; + } + + public AtlasAsyncImportRequest() {} + + public AtlasAsyncImportRequest(String guid) { + setGuid(guid); + } + + public AtlasAsyncImportRequest(AtlasImportResult result) { + this.importResult = result; + this.status = ImportStatus.STAGING; + this.receivedAt = 0L; + this.stagedAt = 0L; + this.startedProcessingAt = 0L; + this.completedAt = 0L; + this.importDetails = new ImportDetails(); + this.importTrackingInfo = new ImportTrackingInfo(null, 0); + setGuid(getGuid()); + } + + public String getImportId() { + return importId; + } + + public void setImportId(String importId) { + this.importId = importId; + if (importTrackingInfo != null) { + importTrackingInfo.setRequestId(REQUEST_ID_PREFIX_PROPERTY + importId + "@" + AtlasEntityUtil.getMetadataNamespace()); + } + } + + public ImportStatus getStatus() { + return status; + } + + public void setStatus(ImportStatus status) { + this.status = status; + } + + public ImportDetails getImportDetails() { + return importDetails; + } + + public void setImportDetails(ImportDetails importDetails) { + this.importDetails = importDetails; + } + + @JsonIgnore + public String getTopicName() { + return ASYNC_IMPORT_TOPIC_PREFIX + importId; + } + + public AtlasImportResult getImportResult() { + return importResult; + } + + public void setImportResult(AtlasImportResult importResult) { + this.importResult = importResult; + } + + public long getReceivedAt() { + return receivedAt; + } + + public void setReceivedAt(long receivedAt) { + this.receivedAt = receivedAt; + } + + public long getStagedAt() { + return stagedAt; + } + + public void setStagedAt(long stagedAt) { + this.stagedAt = stagedAt; + } + + public long getStartedProcessingAt() { + return startedProcessingAt; + } + + public void setStartedProcessingAt(long startedProcessingAt) { + this.startedProcessingAt = startedProcessingAt; + } + + public long getCompletedAt() { + return completedAt; + } + + public void setCompletedAt(long completedAt) { + this.completedAt = completedAt; + } + + @JsonIgnore + public AsyncImportStatus toImportMinInfo() { + AsyncImportStatus asyncImportStatus = new AsyncImportStatus( + this.getImportId(), + status, + toIsoDate(new Date(this.receivedAt)), + importResult.getUserName()); + return asyncImportStatus; + } + + private String toIsoDate(Date value) { + final TimeZone tz = TimeZone.getTimeZone("UTC"); + final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + df.setTimeZone(tz); + return df.format(value); + } + + @Override + public String toString() { + return toString(new StringBuilder()).toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof AtlasAsyncImportRequest)) { + return false; + } + if (!super.equals(o)) { + return false; + } + AtlasAsyncImportRequest that = (AtlasAsyncImportRequest) o; + return Objects.equals(importResult, that.importResult) && + Objects.equals(importId, that.importId) && + Objects.equals(status, that.status) && + Objects.equals(importDetails, that.importDetails) && + Objects.equals(importTrackingInfo.getRequestId(), that.importTrackingInfo.getRequestId()) && + Objects.equals(receivedAt, that.receivedAt) && + Objects.equals(stagedAt, that.stagedAt) && + Objects.equals(startedProcessingAt, that.startedProcessingAt) && + Objects.equals(completedAt, that.completedAt); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), importTrackingInfo.getRequestId(), importResult, + importId, status, importDetails, receivedAt, stagedAt, startedProcessingAt, completedAt); + } + + @Override + protected StringBuilder toString(StringBuilder sb) { + sb.append(", importResult="); + if (importResult == null) { + sb.append("null"); + } else { + sb.append(importResult); + } + sb.append(", requestId=").append(importTrackingInfo.getRequestId()); + sb.append(", importId=").append(importId); + sb.append(", status=").append(status); + sb.append(", receivedAt=").append(receivedAt); + sb.append(", stagedAt=").append(stagedAt); + sb.append(", startedProcessingAt=").append(startedProcessingAt); + sb.append(", completedAt=").append(completedAt); + sb.append(", importDetails=").append(importDetails); + + return sb; + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class ImportDetails { + private static final long serialVersionUID = 1L; + + private int publishedEntityCount; + private int totalEntitiesCount; + private int importedEntitiesCount; + private int failedEntitiesCount; + private List<String> failedEntities; + private float importProgress; + private Map<String, String> failures; + + @JsonIgnore + private List<String> creationOrder = new ArrayList<>(); + + public ImportDetails() { + this.failedEntities = new ArrayList<>(); + this.failures = new HashMap<>(); + } + + public int getPublishedEntityCount() { + return publishedEntityCount; + } + + public void setPublishedEntityCount(int count) { + this.publishedEntityCount = count; + } + + public int getTotalEntitiesCount() { + return totalEntitiesCount; + } + + public void setTotalEntitiesCount(int count) { + this.totalEntitiesCount = count; + } + + public int getImportedEntitiesCount() { + return importedEntitiesCount; + } + + public void setImportedEntitiesCount(int count) { + this.importedEntitiesCount = count; + } + + public int getFailedEntitiesCount() { + return failedEntitiesCount; + } + + public void setFailedEntitiesCount(int count) { + this.failedEntitiesCount = count; + } + + public float getImportProgress() { + return importProgress; + } + + public void setImportProgress(float progress) { + this.importProgress = progress; + } + + public Map<String, String> getFailures() { + return failures; + } + + public void addFailure(String guid, String message) { + this.failures.put(guid, message); + } + + public List<String> getFailedEntities() { + return failedEntities; + } + + public void setFailedEntities(List<String> failedEntities) { + this.failedEntities = failedEntities; + } + + public List<String> getCreationOrder() { + return creationOrder; + } + + public void setCreationOrder(List<String> creationOrder) { + this.creationOrder = creationOrder; + } + + @Override + public String toString() { + return "ImportDetails{" + + "publishedEntityCount=" + publishedEntityCount + + ", totalEntitiesCount=" + totalEntitiesCount + + ", importedEntitiesCount=" + importedEntitiesCount + + ", failedEntitiesCount=" + failedEntitiesCount + + ", importProgress=" + importProgress + + ", failures=" + failures + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof ImportDetails)) { + return false; + } + ImportDetails that = (ImportDetails) o; + return publishedEntityCount == that.publishedEntityCount && + totalEntitiesCount == that.totalEntitiesCount && + importedEntitiesCount == that.importedEntitiesCount && + failedEntitiesCount == that.failedEntitiesCount && + Float.compare(that.importProgress, importProgress) == 0 && + Objects.equals(failures, that.failures); + } + + @Override + public int hashCode() { + return Objects.hash(publishedEntityCount, totalEntitiesCount, importedEntitiesCount, failedEntitiesCount, importProgress, failures); + } + } + + public static class ImportTrackingInfo { Review Comment: - add `implements Serializable` - add @Json annotations -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@atlas.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org