Skip to content

Commit

Permalink
[INLONG-8960][Manager] Automatically assign sort cluster after creati…
Browse files Browse the repository at this point in the history
…ng stream sinks (apache#9001)
  • Loading branch information
vernedeng authored and liaosunny123 committed Oct 11, 2023
1 parent 0b3ef6a commit c3319b3
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ List<InlongClusterEntity> selectByKey(@Param("clusterTag") String clusterTag, @P

int deleteByPrimaryKey(Integer id);

List<InlongClusterEntity> selectStandaloneClusterByType(@Param("sinkType") String sinkType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,6 @@ List<String> selectExistsStreamId(@Param("groupId") String groupId, @Param("sink
*/
int deleteByInlongGroupIds(@Param("groupIdList") List<String> groupIdList);

String selectAssignedCluster(@Param("dataNodeName") String dataNodeName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@
from inlong_cluster
where is_deleted = 0
</select>
<select id="selectStandaloneClusterByType" resultType="org.apache.inlong.manager.dao.entity.InlongClusterEntity">
select
<include refid="Base_Column_List"/>
from inlong_cluster
<where>
type = 'SORTSTANDALONE'
and find_in_set(#{sinkType, jdbcType=VARCHAR}, ext_tag)
and is_deleted = 0
</where>
</select>

<update id="updateById" parameterType="org.apache.inlong.manager.dao.entity.InlongClusterEntity">
update inlong_cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
sink.inlong_group_id,
sink.inlong_stream_id,
sink.sink_type,
sink.inlong_cluster_name,
sink.description,
sink.enable_create_resource,
sink.ext_params,
Expand Down Expand Up @@ -389,6 +390,17 @@
from stream_sink
where is_deleted = 0
</select>
<select id="selectAssignedCluster" resultType="java.lang.String">
select inlong_cluster_name
from stream_sink
<where>
data_node_name = #{dataNodeName, jdbcType=VARCHAR}
and is_deleted = 0
</where>
group by inlong_cluster_name
order by count(*) asc
limit 1
</select>
<update id="updateByIdSelective" parameterType="org.apache.inlong.manager.dao.entity.StreamSinkEntity">
update stream_sink
<set>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SinkInfo {
private String inlongGroupId;
private String inlongStreamId;
private String sinkType;
private String inlongClusterName;
private String sinkName;
private String dataNodeName;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,38 @@

package org.apache.inlong.manager.service.cluster;

import org.apache.inlong.manager.common.consts.InlongConstants;
import org.apache.inlong.manager.common.enums.ClusterType;
import org.apache.inlong.manager.common.enums.ErrorCodeEnum;
import org.apache.inlong.manager.common.exceptions.BusinessException;
import org.apache.inlong.manager.common.util.CommonBeanUtils;
import org.apache.inlong.manager.dao.entity.InlongClusterEntity;
import org.apache.inlong.manager.pojo.cluster.ClusterInfo;
import org.apache.inlong.manager.pojo.cluster.ClusterRequest;
import org.apache.inlong.manager.pojo.cluster.sortstandalone.SortStandaloneClusterDTO;
import org.apache.inlong.manager.pojo.cluster.sortstandalone.SortStandaloneClusterInfo;
import org.apache.inlong.manager.pojo.cluster.sortstandalone.SortStandaloneClusterRequest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@Service
public class SortStandaloneClusterOperator extends AbstractClusterOperator {

@Autowired
private ObjectMapper objectMapper;

@Override
protected void setTargetEntity(ClusterRequest request, InlongClusterEntity targetEntity) {
SortStandaloneClusterRequest standaloneRequest = (SortStandaloneClusterRequest) request;
CommonBeanUtils.copyProperties(standaloneRequest, targetEntity, true);
try {
SortStandaloneClusterDTO dto = SortStandaloneClusterDTO.getFromRequest(standaloneRequest,
targetEntity.getExtParams());
targetEntity.setExtParams(objectMapper.writeValueAsString(dto));
log.debug("success to set entity for SortStandalone cluster");
} catch (Exception e) {
throw new BusinessException(ErrorCodeEnum.CLUSTER_INFO_INCORRECT,
String.format("serialize extParams of SortStandalone Cluster failure: %s", e.getMessage()));
Set<String> supportedTypes = standaloneRequest.getSupportedSinkTypes();
if (CollectionUtils.isNotEmpty(supportedTypes)) {
String extTag = Joiner.on(InlongConstants.COMMA).join(supportedTypes);
targetEntity.setExtTag(extTag);
}
}

Expand All @@ -74,9 +70,10 @@ public ClusterInfo getFromEntity(InlongClusterEntity entity) {

SortStandaloneClusterInfo clusterInfo = new SortStandaloneClusterInfo();
CommonBeanUtils.copyProperties(entity, clusterInfo);
if (StringUtils.isNotBlank(entity.getExtParams())) {
SortStandaloneClusterDTO dto = SortStandaloneClusterDTO.getFromJson(entity.getExtParams());
CommonBeanUtils.copyProperties(dto, clusterInfo);
String extTag = entity.getExtTag();
if (StringUtils.isNotBlank(extTag)) {
Set<String> supportedTypes = Sets.newHashSet(extTag.split(InlongConstants.COMMA));
clusterInfo.setSupportedSinkTypes(supportedTypes);
}
return clusterInfo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.resource.sink;

import org.apache.inlong.manager.common.consts.InlongConstants;
import org.apache.inlong.manager.common.util.Preconditions;
import org.apache.inlong.manager.dao.entity.InlongClusterEntity;
import org.apache.inlong.manager.dao.entity.InlongGroupEntity;
import org.apache.inlong.manager.dao.entity.StreamSinkEntity;
import org.apache.inlong.manager.dao.mapper.InlongClusterEntityMapper;
import org.apache.inlong.manager.dao.mapper.InlongGroupEntityMapper;
import org.apache.inlong.manager.dao.mapper.StreamSinkEntityMapper;
import org.apache.inlong.manager.pojo.sink.SinkInfo;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public abstract class AbstractStandaloneSinkResourceOperator implements SinkResourceOperator {

@Autowired
private InlongClusterEntityMapper clusterEntityMapper;
@Autowired
private StreamSinkEntityMapper sinkEntityMapper;
@Autowired
private InlongGroupEntityMapper groupEntityMapper;

private Random rand = new Random();

@VisibleForTesting
protected void assignCluster(SinkInfo sinkInfo) {
if (StringUtils.isNotBlank(sinkInfo.getInlongClusterName())) {
return;
}

String targetCluster = assignOneCluster(sinkInfo);
Preconditions.expectNotBlank(targetCluster,
String.format("find no proper cluster assign to group=%s, stream=%s, sink type=%s, data node=%s ",
sinkInfo.getInlongGroupId(), sinkInfo.getInlongStreamId(), sinkInfo.getSinkType(),
sinkInfo.getDataNodeName()));

StreamSinkEntity sink = sinkEntityMapper.selectByPrimaryKey(sinkInfo.getId());
sink.setInlongClusterName(targetCluster);
sinkEntityMapper.updateByIdSelective(sink);
}

private String assignOneCluster(SinkInfo sinkInfo) {
return StringUtils
.firstNonBlank(assignFromExist(sinkInfo.getDataNodeName()),
assignFromRelated(sinkInfo.getSinkType(), sinkInfo.getInlongGroupId()));
}

private String assignFromExist(String dataNodeName) {
return sinkEntityMapper.selectAssignedCluster(dataNodeName);
}

private String assignFromRelated(String sinkType, String groupId) {
InlongGroupEntity group = groupEntityMapper.selectByGroupId(groupId);
List<InlongClusterEntity> clusters = clusterEntityMapper
.selectStandaloneClusterByType(sinkType).stream()
.filter(cluster -> checkCluster(cluster.getClusterTags(), group.getInlongClusterTag()))
.collect(Collectors.toList());

return CollectionUtils.isEmpty(clusters) ? null : clusters.get(rand.nextInt(clusters.size())).getName();

}

private boolean checkCluster(String clusterTags, String targetTag) {
return StringUtils.isBlank(clusterTags)
|| Sets.newHashSet(clusterTags.split(InlongConstants.COMMA)).contains(targetTag);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.inlong.manager.pojo.node.cls.ClsDataNodeDTO;
import org.apache.inlong.manager.pojo.sink.SinkInfo;
import org.apache.inlong.manager.pojo.sink.cls.ClsSinkDTO;
import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
import org.apache.inlong.manager.service.resource.sink.AbstractStandaloneSinkResourceOperator;
import org.apache.inlong.manager.service.sink.StreamSinkService;

import com.tencentcloudapi.cls.v20201016.ClsClient;
Expand All @@ -52,7 +52,7 @@
import org.springframework.stereotype.Service;

@Service
public class ClsResourceOperator implements SinkResourceOperator {
public class ClsResourceOperator extends AbstractStandaloneSinkResourceOperator {

private static final Logger LOG = LoggerFactory.getLogger(ClsResourceOperator.class);

Expand All @@ -79,6 +79,7 @@ public void createSinkResource(SinkInfo sinkInfo) {
return;
}
this.createTopicID(sinkInfo);
this.assignCluster(sinkInfo);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.apache.inlong.manager.pojo.sink.es.ElasticsearchFieldInfo;
import org.apache.inlong.manager.pojo.sink.es.ElasticsearchSinkDTO;
import org.apache.inlong.manager.service.node.DataNodeOperateHelper;
import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
import org.apache.inlong.manager.service.resource.sink.AbstractStandaloneSinkResourceOperator;
import org.apache.inlong.manager.service.sink.StreamSinkService;

import org.apache.commons.collections.CollectionUtils;
Expand All @@ -45,7 +45,7 @@
* Elasticsearch's resource operator
*/
@Service
public class ElasticsearchResourceOperator implements SinkResourceOperator {
public class ElasticsearchResourceOperator extends AbstractStandaloneSinkResourceOperator {

private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchResourceOperator.class);
@Autowired
Expand Down Expand Up @@ -76,6 +76,7 @@ public void createSinkResource(SinkInfo sinkInfo) {
}

this.createIndex(sinkInfo);
this.assignCluster(sinkInfo);
}

private void createIndex(SinkInfo sinkInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.inlong.manager.service.node.DataNodeOperateHelper;
import org.apache.inlong.manager.service.resource.queue.pulsar.PulsarOperator;
import org.apache.inlong.manager.service.resource.queue.pulsar.PulsarUtils;
import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
import org.apache.inlong.manager.service.resource.sink.AbstractStandaloneSinkResourceOperator;
import org.apache.inlong.manager.service.sink.StreamSinkService;

import org.apache.pulsar.client.admin.PulsarAdmin;
Expand All @@ -49,7 +49,7 @@
* Pulsar resource operate for creating pulsar resource
*/
@Service
public class PulsarResourceOperator implements SinkResourceOperator {
public class PulsarResourceOperator extends AbstractStandaloneSinkResourceOperator {

private static final Logger LOG = LoggerFactory.getLogger(PulsarResourceOperator.class);

Expand All @@ -76,6 +76,7 @@ public void createSinkResource(SinkInfo sinkInfo) {
return;
}
this.createTopic(sinkInfo);
this.assignCluster(sinkInfo);
}

private void createTopic(SinkInfo sinkInfo) {
Expand Down
Loading

0 comments on commit c3319b3

Please sign in to comment.