mneethiraj commented on code in PR #307: URL: https://github.com/apache/atlas/pull/307#discussion_r2013036164
########## webapp/src/main/java/org/apache/atlas/notification/NotificationHookConsumer.java: ########## @@ -492,8 +542,30 @@ private void startConsumers(ExecutorService executorService) { HookConsumer hookConsumer = new HookConsumer(hookConsumerName, consumer); - consumers.add(hookConsumer); - executors.submit(hookConsumer); + hookConsumers.add(hookConsumer); + } + startConsumers(executorService, hookConsumers); + } + + private void startConsumers(ExecutorService executorService, List<HookConsumer> hookConsumers) { + if (consumers == null) { + consumers = new ArrayList<>(); + } + + if (executorService == null) { + executorService = new ThreadPoolExecutor( + 0, // Core pool size + Integer.MAX_VALUE, // Maximum pool size (dynamic scaling) + 60L, TimeUnit.SECONDS, // Idle thread timeout + new SynchronousQueue<>(), // Direct handoff queue + new ThreadFactoryBuilder().setNameFormat(THREADNAME_PREFIX + " thread-%d").build()); + + executors = executorService; + } + + for (final HookConsumer consumer : hookConsumers) { + consumers.add(consumer); + executors.submit(consumer); Review Comment: `executors` or `executorService`? Conside the case of this method called with non-null value for `executorService`. ########## addons/models/0000-Area0/0010-base_model.json: ########## @@ -568,6 +568,24 @@ "serviceType": "atlas_core", "typeVersion": "1.0", "attributeDefs": [] + }, + { + "name": "__AtlasAsyncImportRequest", + "superTypes": [ "__internal" ], + "serviceType": "atlas_core", + "typeVersion": "1.0", + "attributeDefs": [ + { "name": "requestId", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": true, "isIndexable": true }, + { "name": "importId", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": false, "isIndexable": true }, + { "name": "status", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": false, "isIndexable": true }, + { "name": "importDetails", "typeName": "string", "isOptional": true, "cardinality": "SINGLE", "isUnique": false, "isIndexable": false }, + { "name": "skipTo", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": false, "isIndexable": false }, + { "name": "importResult", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": false, "isIndexable": false }, + { "name": "receivedAt", "typeName": "string", "isOptional": false, "cardinality": "SINGLE", "isUnique": false, "isIndexable": false }, Review Comment: - `skipTo` => `startEntityPos` - `receivedAt` => `receivedTime` - `stagedAt` => `stagedTime` - `startedProcessingAt` => `processingStartTime` - `completedAt` => `completedTime` ########## intg/src/main/java/org/apache/atlas/AtlasConfiguration.java: ########## @@ -111,7 +111,10 @@ public enum AtlasConfiguration { ATLAS_AUDIT_DEFAULT_AGEOUT_IGNORE_TTL("atlas.audit.default.ageout.ignore.ttl", false), ATLAS_AUDIT_AGING_TTL_TEST_AUTOMATION("atlas.audit.aging.ttl.test.automation", false), //Only for test automation RELATIONSHIP_SEARCH_ENABLED("atlas.relationship.search.enabled", false), - UI_TASKS_TAB_USE_ENABLED("atlas.tasks.ui.tab.enabled", false); + UI_TASKS_TAB_USE_ENABLED("atlas.tasks.ui.tab.enabled", false), + ATLAS_ASYNC_IMPORT_MIN_DURATION_OVERRIDE_TEST_AUTOMATION("atlas.async.import.min.duration.override.test.automation", false), + ASYNC_IMPORT_TOPIC_PREFIX("atlas.async.import.topic.prefix", "ATLAS_IMPORT_"), + REQUEST_ID_PREFIX_PROPERTY("atlas.async.import.request_id.prefix", "async_import_"); Review Comment: `REQUEST_ID_PREFIX_PROPERTY` => `ASYNC_IMPORT_REQUST_ID_PREFIX` ########## notification/src/main/java/org/apache/atlas/kafka/KafkaNotification.java: ########## @@ -224,16 +246,20 @@ public boolean isReady(NotificationType notificationType) { public <T> List<NotificationConsumer<T>> createConsumers(NotificationType notificationType, int numConsumers, boolean autoCommitEnabled) { LOG.info("==> KafkaNotification.createConsumers(notificationType={}, numConsumers={}, autoCommitEnabled={})", notificationType, numConsumers, autoCommitEnabled); - String[] topics = CONSUMER_TOPICS_MAP.get(notificationType); + if (!autoCommitEnabled && notificationType.equals(NotificationType.ASYNC_IMPORT)) { + autoCommitEnabled = true; + } + + List<String> topics = CONSUMER_TOPICS_MAP.get(notificationType); Review Comment: It looks like `topics` can be null when `notificationType` is `ASYNC_IMPORT`. If true, I suggest adding following lines to avoid `NullPointerException`. ``` if (topics == null) { return Collections.emptyList(); } ``` ########## repository/src/main/java/org/apache/atlas/repository/impexp/ZipSource.java: ########## @@ -328,4 +344,13 @@ private AtlasEntity getEntity(String guid) throws AtlasBaseException { return null; } + + public void setMd5Hash(String md5Hash) { Review Comment: Is public method `setMd5Hash()` necessary? If not, consider removing this method and replace the caller at line 284 with the following: ``` this.md5Hash = md5Hash.toString(); ``` ########## webapp/src/main/java/org/apache/atlas/notification/NotificationHookConsumer.java: ########## @@ -170,9 +177,9 @@ public class NotificationHookConsumer implements Service, ActiveStateChangeHandl private static final int KAFKA_CONSUMER_SHUTDOWN_WAIT = 30000; private static final String ATLAS_HOOK_CONSUMER_THREAD_NAME = "atlas-hook-consumer-thread"; private static final String ATLAS_HOOK_UNSORTED_CONSUMER_THREAD_NAME = "atlas-hook-unsorted-consumer-thread"; + private static final String ATLAS_IMPORT_CONSUMER_THREAD_PREFIX = "atlas-import-consumer-thread-"; Review Comment: Is `ATLAS_IMPORT_CONSUMER_THREAD_PREFIX` the prefix of thread names that run import notification consumers? It seems `executors` is used to submit consumers and `executors' is initialized with thread prefix `THREADNAME_PREFIX + " thread-%d"`. Please review and update. ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,406 @@ +/** + * 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.AtlasConfiguration; +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 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; Review Comment: - `receivedAt` => `receivedTime` - `stagedAt` => `stagedTime` - `startedProcessingAt` => `processingStartTime` - `completedAt` => `completedTime` ########## intg/src/main/java/org/apache/atlas/model/notification/HookNotification.java: ########## @@ -105,7 +105,8 @@ public StringBuilder toString(StringBuilder sb) { */ public enum HookNotificationType { TYPE_CREATE, TYPE_UPDATE, ENTITY_CREATE, ENTITY_PARTIAL_UPDATE, ENTITY_FULL_UPDATE, ENTITY_DELETE, - ENTITY_CREATE_V2, ENTITY_PARTIAL_UPDATE_V2, ENTITY_FULL_UPDATE_V2, ENTITY_DELETE_V2 + ENTITY_CREATE_V2, ENTITY_PARTIAL_UPDATE_V2, ENTITY_FULL_UPDATE_V2, ENTITY_DELETE_V2, + IMPORT_TYPE_DEF, IMPORT_ENTITY Review Comment: `IMPORT_TYPE_DEF` => `IMPORT_TYPES_DEF` ########## intg/src/main/java/org/apache/atlas/model/impexp/AsyncImportStatus.java: ########## @@ -0,0 +1,66 @@ +/** + * 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; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus; + +import java.io.Serializable; + +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 AsyncImportStatus implements Serializable { + private static final long serialVersionUID = 1L; + + private String importId; + private ImportStatus status; + private String importRequestReceivedAt; Review Comment: `importRequestReceivedAt` => `importRequestReceivedTime` ########## client/client-v2/src/main/java/org/apache/atlas/AtlasClientV2.java: ########## @@ -140,6 +147,15 @@ public class AtlasClientV2 extends AtlasBaseClient { //IndexRecovery APIs private static final String INDEX_RECOVERY_URI = BASE_URI + "v2/indexrecovery"; + // Async Import APIs + private static final String ASYNC_IMPORT_URI = BASE_URI + "admin/async/import"; + private static final String ASYNC_IMPORT_STATUS_URI = BASE_URI + "admin/async/import/status"; + private static final String ASYNC_IMPORT_STATUS_BY_ID_URI = BASE_URI + "admin/async/import/status/"; Review Comment: - consider eliminating `ASYNC_IMPORT_STATUS_BY_ID_URI`, as its value is same as `ASYNC_IMPORT_URI` - consider eliminating `ASYNC_IMPORT_BY_ID_URI`, as its value is same as `ASYNC_IMPORT_URI` ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,406 @@ +/** + * 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.AtlasConfiguration; +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 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; + private AtlasImportResult importResult; + + @JsonIgnore + private 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(); Review Comment: `importDetails` is already initializd in line 75 above. ########## client/client-v2/src/main/java/org/apache/atlas/AtlasClientV2.java: ########## @@ -1039,6 +1055,22 @@ public API formatPathWithParameter(API api, String... params) { return formatPathParameters(api, params); } + public AtlasAsyncImportRequest importAsync(AtlasImportRequest request, InputStream stream) throws AtlasServiceException { + return performAsyncImport(getImportRequestBodyPart(request), new StreamDataBodyPart(IMPORT_DATA_PARAMETER, stream)); + } + + public PList<AsyncImportStatus> getAsyncImportStatus() throws AtlasServiceException { Review Comment: `getAsyncImportStatus()` should accept parameters like startPos, pageSize - to retrieve subsequent pages. ########## intg/src/main/java/org/apache/atlas/model/impexp/AsyncImportStatus.java: ########## @@ -0,0 +1,66 @@ +/** + * 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; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus; + +import java.io.Serializable; + +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 AsyncImportStatus implements Serializable { + private static final long serialVersionUID = 1L; + + private String importId; + private ImportStatus status; + private String importRequestReceivedAt; + private String importRequestReceivedBy; + + public AsyncImportStatus() {} + + public AsyncImportStatus(String importId, ImportStatus status, String importRequestReceivedAt, String importRequestReceivedBy) { + this.importId = importId; + this.status = status; + this.importRequestReceivedAt = importRequestReceivedAt; + this.importRequestReceivedBy = importRequestReceivedBy; Review Comment: `importRequestReceivedBy` - is this the username? If yes, consider renaming to `importRequestUser` ########## intg/src/main/java/org/apache/atlas/model/impexp/AtlasAsyncImportRequest.java: ########## @@ -0,0 +1,406 @@ +/** + * 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.AtlasConfiguration; +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 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; + private AtlasImportResult importResult; + + @JsonIgnore + private 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(AtlasConfiguration.REQUEST_ID_PREFIX_PROPERTY.getString() + 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; + } + + 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; + } + + @JsonIgnore + public String getTopicName() { + return AtlasConfiguration.ASYNC_IMPORT_TOPIC_PREFIX.getString() + importId; + } + + public AtlasImportResult getImportResult() { + return importResult; + } + + public void setImportResult(AtlasImportResult importResult) { + this.importResult = importResult; + } + + public long getCompletedAt() { + return completedAt; + } + + public void setCompletedAt(long completedAt) { + this.completedAt = completedAt; + } + + public ImportTrackingInfo getImportTrackingInfo() { + return importTrackingInfo; + } + + public void setImportTrackingInfo(ImportTrackingInfo importTrackingInfo) { + this.importTrackingInfo = importTrackingInfo; + } + + @JsonIgnore + public AsyncImportStatus toImportMinInfo() { + return new AsyncImportStatus(this.getImportId(), status, toIsoDate(new Date(this.receivedAt)), importResult.getUserName()); + } + + 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 boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof AtlasAsyncImportRequest)) { + return false; + } else 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) && + (importTrackingInfo == null ? that.importTrackingInfo == null : (that.importTrackingInfo != null && 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(), importResult, importId, status, importDetails, + importTrackingInfo == null ? null : importTrackingInfo.getRequestId(), receivedAt, stagedAt, startedProcessingAt, completedAt); + } + + @Override + protected StringBuilder toString(StringBuilder sb) { + sb.append(", importResult=").append(importResult); + sb.append(", requestId=").append(importTrackingInfo == null ? null : 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; + } + + @JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIgnoreProperties(ignoreUnknown = true) + public static class ImportDetails implements Serializable { + 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; + } else 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); + } + } + + @JsonAutoDetect(getterVisibility = PUBLIC_ONLY, setterVisibility = PUBLIC_ONLY, fieldVisibility = NONE) + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIgnoreProperties(ignoreUnknown = true) + public static class ImportTrackingInfo implements Serializable { + private static final long serialVersionUID = 1L; + + private String requestId; + private int skipTo; Review Comment: `skipTo` => `startEntityPosition` ########## intg/src/main/java/org/apache/atlas/model/notification/ImportNotification.java: ########## @@ -0,0 +1,170 @@ +/** + * 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; Review Comment: `typeDefinitionMap` => `typesDef` ########## notification/src/main/java/org/apache/atlas/kafka/KafkaNotification.java: ########## @@ -429,6 +453,48 @@ private KafkaProducer getOrCreateProducerByCriteria(Object producerCriteria, Map return ret; } + @Override + public void addTopicToNotificationType(NotificationType notificationType, String topic) throws AtlasBaseException { + try (AdminClient adminClient = AdminClient.create(this.properties)) { + // checking if a topic exists with the name before adding to consumers. + if (adminClient.listTopics().names().get().contains(topic)) { + CONSUMER_TOPICS_MAP.computeIfAbsent(notificationType, k -> new ArrayList<>()).add(topic); + return; + } + LOG.error("Error while adding topic: {}, topic not found", topic); Review Comment: `"Error while adding topic: {}, topic not found"` ==> `"Failed to add consumer for notificationType={}, topic={}: topic does not exist"` Review and update error messages in exceptions and line 467 below. ########## notification/src/main/java/org/apache/atlas/kafka/KafkaNotification.java: ########## @@ -322,8 +346,8 @@ public KafkaConsumer getOrCreateKafkaConsumer(KafkaConsumer existingConsumer, Pr try { if (ret == null || !isKafkaConsumerOpen(ret)) { - String[] topics = CONSUMER_TOPICS_MAP.get(notificationType); - String topic = topics[idxConsumer % topics.length]; + List<String> topics = CONSUMER_TOPICS_MAP.get(notificationType); + String topic = topics.get(idxConsumer % topics.size()); Review Comment: It looks like `topics` can be null when `notificationType` is `ASYNC_IMPORT`. Please review and update the following lines to avoid `NullPointerException`. ########## intg/src/main/java/org/apache/atlas/model/notification/ImportNotification.java: ########## @@ -0,0 +1,170 @@ +/** + * 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 { Review Comment: `AtlasTypeDefImportNotification` => `AtlasTypesDefImportNotification` ########## repository/src/main/java/org/apache/atlas/repository/impexp/ImportService.java: ########## @@ -306,4 +514,26 @@ private EntityImportStream getZipDirectEntityImportStream(AtlasImportRequest req private boolean isMigrationMode(AtlasImportRequest request) { return AtlasStringUtil.hasOption(request.getOptions(), AtlasImportRequest.OPTION_KEY_MIGRATION); } + + @VisibleForTesting + void addToImportOperationAudits(AtlasImportResult result) throws AtlasBaseException { + String params = AtlasJson.toJson(Collections.singletonMap(OPERATION_STATUS, result.getOperationStatus().name())); + + if (result.getExportResult().getRequest() == null) { + int resultCount = result.getProcessedEntities().size(); + + auditService.add(AtlasAuditEntry.AuditOperation.IMPORT, params, AtlasJson.toJson(result.getMetrics()), resultCount); + } else { + List<AtlasObjectId> objectIds = result.getExportResult().getRequest().getItemsToExport(); + + auditImportExportOperations(objectIds, AtlasAuditEntry.AuditOperation.IMPORT, params); + } + } + + private void auditImportExportOperations(List<AtlasObjectId> objectIds, AtlasAuditEntry.AuditOperation auditOperation, String params) throws AtlasBaseException { Review Comment: Private method `auditImportExportOperations()` is a small one, and has only one caller (line 529). Consider eliminating this method by replacing the caller with this method body. ########## repository/src/main/java/org/apache/atlas/repository/impexp/ZipSourceWithBackingDirectory.java: ########## @@ -425,4 +442,13 @@ private String moveNext() { return null; } + + public void setMd5Hash(String md5Hash) { Review Comment: Is public method `setMd5Hash()` necessary? If not, consider removing this method and replace the caller at line 318 with the following: ``` this.md5Hash = md5Hash.toString(); ``` ########## webapp/src/main/java/org/apache/atlas/notification/ImportTaskListenerImpl.java: ########## @@ -0,0 +1,387 @@ +/** + * 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.notification; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.apache.atlas.ApplicationProperties; +import org.apache.atlas.AtlasException; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.ha.HAConfiguration; +import org.apache.atlas.listener.ActiveStateChangeHandler; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest; +import org.apache.atlas.model.impexp.AtlasAsyncImportRequest.ImportStatus; +import org.apache.atlas.repository.impexp.AsyncImportService; +import org.apache.atlas.repository.store.graph.v2.asyncimport.ImportTaskListener; +import org.apache.atlas.service.Service; +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.ObjectUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.DependsOn; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import javax.annotation.PreDestroy; +import javax.inject.Inject; + +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import static org.apache.atlas.AtlasConfiguration.ASYNC_IMPORT_TOPIC_PREFIX; +import static org.apache.atlas.AtlasErrorCode.IMPORT_QUEUEING_FAILED; + +@Component +@Order(8) +@DependsOn(value = "notificationHookConsumer") +public class ImportTaskListenerImpl implements Service, ActiveStateChangeHandler, ImportTaskListener { + private static final Logger LOG = LoggerFactory.getLogger(ImportTaskListenerImpl.class); + + private static final String THREADNAME_PREFIX = ImportTaskListener.class.getSimpleName(); + private static final int ASYNC_IMPORT_PERMITS = 1; // Only one asynchronous import task is permitted + + private final BlockingQueue<String> requestQueue; // Blocking queue for requests + private final ExecutorService executorService; // Single-thread executor for sequential processing + private final AsyncImportService asyncImportService; + private final NotificationHookConsumer notificationHookConsumer; + private final Semaphore asyncImportSemaphore; + private final Configuration applicationProperties; + + @Inject + public ImportTaskListenerImpl(AsyncImportService asyncImportService, NotificationHookConsumer notificationHookConsumer) throws AtlasException { + this(asyncImportService, notificationHookConsumer, new LinkedBlockingQueue<>()); + } + + public ImportTaskListenerImpl(AsyncImportService asyncImportService, NotificationHookConsumer notificationHookConsumer, BlockingQueue<String> requestQueue) throws AtlasException { + this.asyncImportService = asyncImportService; + this.notificationHookConsumer = notificationHookConsumer; + this.requestQueue = requestQueue; + this.asyncImportSemaphore = new Semaphore(ASYNC_IMPORT_PERMITS); + this.applicationProperties = ApplicationProperties.get(); + this.executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat(THREADNAME_PREFIX + " thread-%d") + .setUncaughtExceptionHandler((thread, throwable) -> LOG.error("Uncaught exception in thread {}: {}", thread.getName(), throwable.getMessage(), throwable)).build()); + } + + @Override + public void start() throws AtlasException { + if (HAConfiguration.isHAEnabled(applicationProperties)) { + LOG.info("HA is enabled, not starting import consumers inline."); + + return; + } + + startInternal(); + } + + private void startInternal() { + CompletableFuture<Void> populateTask = CompletableFuture.runAsync(this::populateRequestQueue) + .exceptionally(ex -> { + LOG.error("Failed to populate request queue", ex); + return null; + }); + + CompletableFuture<Void> resumeTask = CompletableFuture.runAsync(this::resumeInProgressImports) + .exceptionally(ex -> { + LOG.error("Failed to resume in-progress imports", ex); + return null; + }); + + // Wait for both tasks to complete before proceeding + CompletableFuture.allOf(populateTask, resumeTask) + .thenRun(this::startNextImportInQueue) + .exceptionally(ex -> { + LOG.error("Failed to start next import in queue", ex); + return null; + }).join(); + } + + @Override + public void onReceiveImportRequest(AtlasAsyncImportRequest importRequest) throws AtlasBaseException { + try { + LOG.info("==> onReceiveImportRequest(atlasAsyncImportRequest={})", importRequest); + + importRequest.setStatus(ImportStatus.WAITING); + + asyncImportService.updateImportRequest(importRequest); + requestQueue.put(importRequest.getImportId()); + + startNextImportInQueue(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + + LOG.warn("Failed to add import request: {} to the queue", importRequest.getImportId()); + + throw new AtlasBaseException(IMPORT_QUEUEING_FAILED, e, importRequest.getImportId()); + } finally { + LOG.info("<== onReceiveImportRequest(atlasAsyncImportRequest={})", importRequest); + } + } + + @Override + public void onCompleteImportRequest(String importId) { + LOG.info("==> onCompleteImportRequest(importId={})", importId); + + try { + notificationHookConsumer.closeImportConsumer(importId, ASYNC_IMPORT_TOPIC_PREFIX.getString() + importId); + } finally { + releaseAsyncImportSemaphore(); + startNextImportInQueue(); + + LOG.info("<== onCompleteImportRequest(importId={})", importId); + } + } + + private void startNextImportInQueue() { + LOG.info("==> startNextImportInQueue()"); + + startAsyncImportIfAvailable(null); + + LOG.info("<== startNextImportInQueue()"); + } + + @VisibleForTesting + void startAsyncImportIfAvailable(String importId) { + LOG.info("==> startAsyncImportIfAvailable()"); + + try { + if (!asyncImportSemaphore.tryAcquire()) { + LOG.warn("An async import is in progress, import request is queued"); Review Comment: Is this log needed at WARN level? ########## webapp/src/main/java/org/apache/atlas/notification/NotificationHookConsumer.java: ########## @@ -443,6 +454,32 @@ public int getHandlerOrder() { return HandlerOrder.NOTIFICATION_HOOK_CONSUMER.getOrder(); } + public void closeImportConsumer(String importId, String topic) { + try { + LOG.info("==> closeImportConsumer(importId={}, topic={})", importId, topic); + + String consumerName = ATLAS_IMPORT_CONSUMER_THREAD_PREFIX + importId; + ListIterator<HookConsumer> consumersIterator = consumers.listIterator(); Review Comment: Can multiple threads call closeImportConsumer() simultaneously? If yes, iteration and removal of entries in `consumers` from multiple threads might cause failures. Review and add necessary synchoronized to prevent this. -- 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