You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/02/23 11:54:09 UTC

[GitHub] [incubator-inlong] healchow commented on a change in pull request #2671: [INLONG-2669][Manager] Optimizing inlong-manager source module code s…

healchow commented on a change in pull request #2671:
URL: https://github.com/apache/incubator-inlong/pull/2671#discussion_r812814253



##########
File path: inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/source/AbstractStreamSourceOperation.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.inlong.manager.service.source;
+
+import org.apache.inlong.manager.common.enums.Constant;
+import org.apache.inlong.manager.common.enums.EntityStatus;
+import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
+import org.apache.inlong.manager.common.pojo.source.SourceRequest;
+import org.apache.inlong.manager.common.pojo.source.SourceResponse;
+import org.apache.inlong.manager.common.pojo.source.kafka.KafkaSourceDTO;
+import org.apache.inlong.manager.common.util.CommonBeanUtils;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.dao.entity.StreamSourceEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSourceEntityMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.function.Supplier;
+
+public abstract class AbstractStreamSourceOperation implements StreamSourceOperation {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStreamSourceOperation.class);
+    @Autowired
+    protected StreamSourceEntityMapper sourceMapper;
+
+    /**
+     * Setting the parameters of the latest entity.
+     *
+     * @param request source request
+     * @param originEntity entity object which will set the new parameters.
+     */
+    protected abstract void setUpdatedEntity(SourceRequest request, StreamSourceEntity originEntity);
+
+    /**
+     * Getting the source type.
+     *
+     * @return source type string.
+     */
+    protected abstract String constantSourceType();

Review comment:
       It's better to name it `getSourceType`.

##########
File path: inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/source/AbstractStreamSourceOperation.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.inlong.manager.service.source;
+
+import org.apache.inlong.manager.common.enums.Constant;
+import org.apache.inlong.manager.common.enums.EntityStatus;
+import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
+import org.apache.inlong.manager.common.pojo.source.SourceRequest;
+import org.apache.inlong.manager.common.pojo.source.SourceResponse;
+import org.apache.inlong.manager.common.pojo.source.kafka.KafkaSourceDTO;
+import org.apache.inlong.manager.common.util.CommonBeanUtils;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.dao.entity.StreamSourceEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSourceEntityMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.function.Supplier;
+
+public abstract class AbstractStreamSourceOperation implements StreamSourceOperation {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStreamSourceOperation.class);
+    @Autowired
+    protected StreamSourceEntityMapper sourceMapper;
+
+    /**
+     * Setting the parameters of the latest entity.
+     *
+     * @param request source request
+     * @param originEntity entity object which will set the new parameters.
+     */
+    protected abstract void setUpdatedEntity(SourceRequest request, StreamSourceEntity originEntity);

Review comment:
       It's better to name it `setTargetEntity`.

##########
File path: inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/source/AbstractStreamSourceOperation.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.inlong.manager.service.source;
+
+import org.apache.inlong.manager.common.enums.Constant;
+import org.apache.inlong.manager.common.enums.EntityStatus;
+import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
+import org.apache.inlong.manager.common.pojo.source.SourceRequest;
+import org.apache.inlong.manager.common.pojo.source.SourceResponse;
+import org.apache.inlong.manager.common.pojo.source.kafka.KafkaSourceDTO;
+import org.apache.inlong.manager.common.util.CommonBeanUtils;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.dao.entity.StreamSourceEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSourceEntityMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.function.Supplier;
+
+public abstract class AbstractStreamSourceOperation implements StreamSourceOperation {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStreamSourceOperation.class);
+    @Autowired
+    protected StreamSourceEntityMapper sourceMapper;
+
+    /**
+     * Setting the parameters of the latest entity.
+     *
+     * @param request source request
+     * @param originEntity entity object which will set the new parameters.
+     */
+    protected abstract void setUpdatedEntity(SourceRequest request, StreamSourceEntity originEntity);
+
+    /**
+     * Getting the source type.
+     *
+     * @return source type string.
+     */
+    protected abstract String constantSourceType();
+
+    /**
+     * Creating source response object.
+     *
+     * @return response object.
+     */
+    protected abstract SourceResponse responseObject();
+
+    @Override
+    public SourceResponse getById(@NotNull Integer id) {
+        StreamSourceEntity entity = sourceMapper.selectByPrimaryKey(id);
+        Preconditions.checkNotNull(entity, ErrorCodeEnum.SOURCE_INFO_NOT_FOUND.getMessage());
+        String existType = entity.getSourceType();
+        Preconditions.checkTrue(constantSourceType().equals(existType),
+                String.format(Constant.SOURCE_TYPE_NOT_SAME, constantSourceType(), existType));
+        return this.getFromEntity(entity, this::responseObject);
+    }
+
+    @Override
+    public void updateOpt(SourceRequest request, String operator) {
+        StreamSourceEntity entity = sourceMapper.selectByPrimaryKey(request.getId());
+        Preconditions.checkNotNull(entity, ErrorCodeEnum.SOURCE_INFO_NOT_FOUND.getMessage());
+        // Setting updated parameters of stream source entity.
+        setUpdatedEntity(request, entity);
+        entity.setPreviousStatus(entity.getStatus());
+        entity.setStatus(EntityStatus.GROUP_CONFIG_ING.getCode());
+        entity.setModifier(operator);
+        entity.setModifyTime(new Date());
+        sourceMapper.updateByPrimaryKeySelective(entity);
+        LOGGER.info("success to update source of type={}", request.getSourceType());
+    }
+
+    @Override
+    public Integer saveOpt(SourceRequest request, String operator) {
+        StreamSourceEntity entity = CommonBeanUtils.copyProperties(request, StreamSourceEntity::new);
+        entity.setStatus(EntityStatus.SOURCE_NEW.getCode());
+        entity.setIsDeleted(EntityStatus.UN_DELETED.getCode());
+        entity.setCreator(operator);
+        entity.setModifier(operator);
+        Date now = new Date();
+        entity.setCreateTime(now);
+        entity.setModifyTime(now);
+        // get the ext params
+        setUpdatedEntity(request, entity);
+        sourceMapper.insert(entity);
+        Integer sourceId = entity.getId();
+        request.setId(sourceId);
+        return sourceId;
+    }
+
+    @Override
+    public <T> T getFromEntity(StreamSourceEntity entity, Supplier<T> target) {

Review comment:
       This method may be abstract, and all sub-classes override it.

##########
File path: inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/source/AbstractStreamSourceOperation.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.inlong.manager.service.source;
+
+import org.apache.inlong.manager.common.enums.Constant;
+import org.apache.inlong.manager.common.enums.EntityStatus;
+import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
+import org.apache.inlong.manager.common.pojo.source.SourceRequest;
+import org.apache.inlong.manager.common.pojo.source.SourceResponse;
+import org.apache.inlong.manager.common.pojo.source.kafka.KafkaSourceDTO;
+import org.apache.inlong.manager.common.util.CommonBeanUtils;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.dao.entity.StreamSourceEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSourceEntityMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.function.Supplier;
+
+public abstract class AbstractStreamSourceOperation implements StreamSourceOperation {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStreamSourceOperation.class);
+    @Autowired
+    protected StreamSourceEntityMapper sourceMapper;
+
+    /**
+     * Setting the parameters of the latest entity.
+     *
+     * @param request source request
+     * @param originEntity entity object which will set the new parameters.
+     */
+    protected abstract void setUpdatedEntity(SourceRequest request, StreamSourceEntity originEntity);
+
+    /**
+     * Getting the source type.
+     *
+     * @return source type string.
+     */
+    protected abstract String constantSourceType();
+
+    /**
+     * Creating source response object.
+     *
+     * @return response object.
+     */
+    protected abstract SourceResponse responseObject();
+
+    @Override
+    public SourceResponse getById(@NotNull Integer id) {
+        StreamSourceEntity entity = sourceMapper.selectByPrimaryKey(id);
+        Preconditions.checkNotNull(entity, ErrorCodeEnum.SOURCE_INFO_NOT_FOUND.getMessage());
+        String existType = entity.getSourceType();
+        Preconditions.checkTrue(constantSourceType().equals(existType),
+                String.format(Constant.SOURCE_TYPE_NOT_SAME, constantSourceType(), existType));
+        return this.getFromEntity(entity, this::responseObject);
+    }
+
+    @Override
+    public void updateOpt(SourceRequest request, String operator) {
+        StreamSourceEntity entity = sourceMapper.selectByPrimaryKey(request.getId());
+        Preconditions.checkNotNull(entity, ErrorCodeEnum.SOURCE_INFO_NOT_FOUND.getMessage());
+        // Setting updated parameters of stream source entity.
+        setUpdatedEntity(request, entity);
+        entity.setPreviousStatus(entity.getStatus());
+        entity.setStatus(EntityStatus.GROUP_CONFIG_ING.getCode());
+        entity.setModifier(operator);
+        entity.setModifyTime(new Date());
+        sourceMapper.updateByPrimaryKeySelective(entity);
+        LOGGER.info("success to update source of type={}", request.getSourceType());
+    }
+
+    @Override
+    public Integer saveOpt(SourceRequest request, String operator) {
+        StreamSourceEntity entity = CommonBeanUtils.copyProperties(request, StreamSourceEntity::new);
+        entity.setStatus(EntityStatus.SOURCE_NEW.getCode());
+        entity.setIsDeleted(EntityStatus.UN_DELETED.getCode());
+        entity.setCreator(operator);
+        entity.setModifier(operator);
+        Date now = new Date();
+        entity.setCreateTime(now);
+        entity.setModifyTime(now);
+        // get the ext params
+        setUpdatedEntity(request, entity);
+        sourceMapper.insert(entity);
+        Integer sourceId = entity.getId();
+        request.setId(sourceId);

Review comment:
       Remove this unused line.




-- 
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: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org