inflow group 추가
This commit is contained in:
@@ -6,4 +6,9 @@ public interface Bucket {
|
||||
public InflowTargetVO getAdapterInflowThreashold(String adapter);
|
||||
public InflowTargetVO getInterfaceInflowThreashold(String inter);
|
||||
public InflowTargetVO getInflowThreashold(String inter);
|
||||
|
||||
// 그룹 관련 메서드
|
||||
public boolean isGroupPass(String groupId);
|
||||
public InflowGroupVO getGroupInflowThreshold(String groupId);
|
||||
public String getGroupIdByInterface(String interfaceId);
|
||||
}
|
||||
|
||||
@@ -12,10 +12,14 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import com.eactive.eai.common.dao.BaseDAO;
|
||||
import com.eactive.eai.common.dao.DAOException;
|
||||
import com.eactive.eai.common.exception.ExceptionUtil;
|
||||
import com.eactive.eai.common.inflow.loader.InflowControlGroupLoader;
|
||||
import com.eactive.eai.common.inflow.loader.InflowControlGroupMappingLoader;
|
||||
import com.eactive.eai.common.inflow.loader.InflowControlLoader;
|
||||
import com.eactive.eai.common.inflow.loader.InflowControlLogLogger;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
import com.eactive.eai.data.entity.onl.inflow.InflowControl;
|
||||
import com.eactive.eai.data.entity.onl.inflow.InflowControlGroup;
|
||||
import com.eactive.eai.data.entity.onl.inflow.InflowControlGroupMapping;
|
||||
import com.eactive.eai.data.entity.onl.inflow.InflowControlLog;
|
||||
import com.eactive.eai.data.entity.onl.inflow.InflowControlLogId;
|
||||
|
||||
@@ -30,6 +34,12 @@ public class InflowControlDAO extends BaseDAO {
|
||||
@Autowired
|
||||
private InflowControlLogLogger inflowControlLogLogger;
|
||||
|
||||
@Autowired
|
||||
private InflowControlGroupLoader inflowControlGroupLoader;
|
||||
|
||||
@Autowired
|
||||
private InflowControlGroupMappingLoader inflowControlGroupMappingLoader;
|
||||
|
||||
public List<InflowTargetVO> getInflowAdapterList() throws DAOException {
|
||||
return getInflowList("01");
|
||||
}
|
||||
@@ -132,4 +142,89 @@ public class InflowControlDAO extends BaseDAO {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 그룹 관련 메서드 ====================
|
||||
|
||||
/**
|
||||
* 활성화된 유량제어 그룹 목록 조회
|
||||
*/
|
||||
public List<InflowGroupVO> getInflowGroupList() throws DAOException {
|
||||
try {
|
||||
Iterable<InflowControlGroup> groups = inflowControlGroupLoader.findActiveGroups();
|
||||
List<InflowGroupVO> groupList = new ArrayList<>();
|
||||
logger.info("[ @@ Load InflowControlGroup Configuration - starting >>>>>>>>>>>>>>>>> ]");
|
||||
|
||||
for (InflowControlGroup group : groups) {
|
||||
InflowGroupVO vo = convertToGroupVO(group);
|
||||
// 그룹에 속한 인터페이스 목록 조회
|
||||
Iterable<InflowControlGroupMapping> mappings = inflowControlGroupMappingLoader.findActiveByGroupId(group.getGroupId());
|
||||
for (InflowControlGroupMapping mapping : mappings) {
|
||||
vo.addInterface(mapping.getInterfaceId());
|
||||
}
|
||||
groupList.add(vo);
|
||||
}
|
||||
logger.info("[>>Load InflowControlGroup Configuration - ended. count=" + groupList.size() + " ]");
|
||||
return groupList;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 인터페이스ID → 그룹ID 매핑 조회
|
||||
*/
|
||||
public Map<String, String> getInterfaceToGroupMap() throws DAOException {
|
||||
try {
|
||||
Iterable<InflowControlGroupMapping> mappings = inflowControlGroupMappingLoader.findActiveMappings();
|
||||
Map<String, String> interfaceToGroupMap = new HashMap<>();
|
||||
logger.info("[ @@ Load InterfaceToGroupMap Configuration - starting >>>>>>>>>>>>>>>>> ]");
|
||||
|
||||
for (InflowControlGroupMapping mapping : mappings) {
|
||||
interfaceToGroupMap.put(mapping.getInterfaceId(), mapping.getGroupId());
|
||||
}
|
||||
logger.info("[>>Load InterfaceToGroupMap Configuration - ended. count=" + interfaceToGroupMap.size() + " ]");
|
||||
return interfaceToGroupMap;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 그룹 조회
|
||||
*/
|
||||
public Map<String, InflowGroupVO> getInflowTargetByGroup(String groupId) throws DAOException {
|
||||
try {
|
||||
Iterable<InflowControlGroup> groups = inflowControlGroupLoader.findByConditions(groupId);
|
||||
Map<String, InflowGroupVO> groupMap = new HashMap<>();
|
||||
logger.info("[ @@ Load InflowControlGroup getInflowTargetByGroup Configuration - starting >>>>>>>>>>>>>>>>> ]");
|
||||
|
||||
for (InflowControlGroup group : groups) {
|
||||
InflowGroupVO vo = convertToGroupVO(group);
|
||||
// 그룹에 속한 인터페이스 목록 조회
|
||||
Iterable<InflowControlGroupMapping> mappings = inflowControlGroupMappingLoader.findActiveByGroupId(group.getGroupId());
|
||||
for (InflowControlGroupMapping mapping : mappings) {
|
||||
vo.addInterface(mapping.getInterfaceId());
|
||||
}
|
||||
groupMap.put(vo.getGroupId(), vo);
|
||||
}
|
||||
logger.info("[>>Load InflowControlGroup getInflowTargetByGroup Configuration - ended ]");
|
||||
return groupMap;
|
||||
} catch (Exception e) {
|
||||
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Entity → VO 변환
|
||||
*/
|
||||
private InflowGroupVO convertToGroupVO(InflowControlGroup group) {
|
||||
InflowGroupVO vo = new InflowGroupVO();
|
||||
vo.setGroupId(group.getGroupId());
|
||||
vo.setGroupName(group.getGroupName());
|
||||
vo.setThreshold(group.getThreshold() != null ? group.getThreshold() : 0);
|
||||
vo.setThresholdPerSecond(group.getThresholdPerSecond() != null ? group.getThresholdPerSecond() : 0);
|
||||
vo.setThresholdTimeUnit(group.getThresholdTimeUnit());
|
||||
vo.setActivate(!"0".equals(group.getUseYn()));
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
private Map<String, CustomBucket> adapterBucketList = new HashMap<>();
|
||||
private Map<String, CustomBucket> interfaceBucketList = new HashMap<>();
|
||||
private Map<String, CustomBucket> groupBucketList = new HashMap<>();
|
||||
private Map<String, String> interfaceToGroupMap = new HashMap<>();
|
||||
private Map<String, InflowGroupVO> groupVoMap = new HashMap<>();
|
||||
private boolean started;
|
||||
private LifecycleSupport lifecycle = new LifecycleSupport(this);
|
||||
|
||||
@@ -91,6 +94,7 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
private void init() throws Exception {
|
||||
initAdapter();
|
||||
initInterface();
|
||||
initGroup();
|
||||
}
|
||||
|
||||
private void initAdapter() throws Exception {
|
||||
@@ -121,6 +125,33 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
}
|
||||
}
|
||||
|
||||
private void initGroup() throws Exception {
|
||||
groupBucketList = new HashMap<>();
|
||||
interfaceToGroupMap = new HashMap<>();
|
||||
groupVoMap = new HashMap<>();
|
||||
|
||||
List<InflowGroupVO> inflowGroupVoList = inflowControlDAO.getInflowGroupList();
|
||||
|
||||
for (InflowGroupVO groupVo : inflowGroupVoList) {
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
CustomBucket bucket = makeBucketUsingGroupVO(groupVo);
|
||||
if (bucket != null) {
|
||||
groupBucketList.put(groupVo.getGroupId(), bucket);
|
||||
groupVoMap.put(groupVo.getGroupId(), groupVo);
|
||||
// 인터페이스 → 그룹 매핑 등록
|
||||
for (String interfaceId : groupVo.getInterfaceList()) {
|
||||
interfaceToGroupMap.put(interfaceId, groupVo.getGroupId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isInfo()) {
|
||||
logger.info("InflowControlManager] initGroup completed. groups=" + groupBucketList.size()
|
||||
+ ", mappings=" + interfaceToGroupMap.size());
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadAdapter() throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reload all Started ...");
|
||||
@@ -200,6 +231,61 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadGroup() throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup all Started ...");
|
||||
}
|
||||
initGroup();
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup all finished ...");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void reloadGroup(String groupId) throws Exception {
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup Started ... groupId=" + groupId);
|
||||
}
|
||||
Map<String, InflowGroupVO> map = inflowControlDAO.getInflowTargetByGroup(groupId);
|
||||
if (map != null) {
|
||||
// 기존 그룹에 속한 인터페이스 매핑 제거
|
||||
Iterator<Map.Entry<String, String>> mappingIt = interfaceToGroupMap.entrySet().iterator();
|
||||
while (mappingIt.hasNext()) {
|
||||
Map.Entry<String, String> entry = mappingIt.next();
|
||||
if (groupId.equals(entry.getValue())) {
|
||||
mappingIt.remove();
|
||||
}
|
||||
}
|
||||
|
||||
Iterator<String> it = map.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
InflowGroupVO groupVo = map.get(key);
|
||||
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
CustomBucket bucket = makeBucketUsingGroupVO(groupVo);
|
||||
if (bucket != null) {
|
||||
groupBucketList.put(groupVo.getGroupId(), bucket);
|
||||
groupVoMap.put(groupVo.getGroupId(), groupVo);
|
||||
// 인터페이스 → 그룹 매핑 재등록
|
||||
for (String interfaceId : groupVo.getInterfaceList()) {
|
||||
interfaceToGroupMap.put(interfaceId, groupVo.getGroupId());
|
||||
}
|
||||
} else {
|
||||
groupBucketList.remove(groupVo.getGroupId());
|
||||
groupVoMap.remove(groupVo.getGroupId());
|
||||
}
|
||||
} else {
|
||||
groupBucketList.remove(groupVo.getGroupId());
|
||||
groupVoMap.remove(groupVo.getGroupId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isWarn()) {
|
||||
logger.warn("InflowControlManager] reloadGroup finished ...");
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() throws LifecycleException {
|
||||
// Validate and update our current component state
|
||||
if (!started)
|
||||
@@ -210,6 +296,9 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
adapterBucketList.clear();
|
||||
interfaceBucketList.clear();
|
||||
groupBucketList.clear();
|
||||
interfaceToGroupMap.clear();
|
||||
groupVoMap.clear();
|
||||
|
||||
started = false;
|
||||
// Notify our interested LifecycleListeners
|
||||
@@ -260,6 +349,29 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
interfaceBucketList.remove(inter);
|
||||
}
|
||||
|
||||
public void removeGroup(String groupId) {
|
||||
groupBucketList.remove(groupId);
|
||||
groupVoMap.remove(groupId);
|
||||
// 해당 그룹에 속한 인터페이스 매핑도 제거
|
||||
Iterator<Map.Entry<String, String>> it = interfaceToGroupMap.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<String, String> entry = it.next();
|
||||
if (groupId.equals(entry.getValue())) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getGroupAllKeys() {
|
||||
Iterator<String> it = this.groupBucketList.keySet().iterator();
|
||||
String[] groupIds = new String[this.groupBucketList.size()];
|
||||
for (int i = 0; it.hasNext(); i++) {
|
||||
groupIds[i] = it.next();
|
||||
}
|
||||
Arrays.sort(groupIds);
|
||||
return groupIds;
|
||||
}
|
||||
|
||||
private CustomBucket makeBucketUsingInflowVO(InflowTargetVO inflowTarget) {
|
||||
if (InflowControlManager.isInflowTarget(inflowTarget)) {
|
||||
LocalBucketBuilder builder = Bucket4j.builder();
|
||||
@@ -278,6 +390,32 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
return null;
|
||||
}
|
||||
|
||||
private CustomBucket makeBucketUsingGroupVO(InflowGroupVO groupVo) {
|
||||
if (isInflowGroupTarget(groupVo)) {
|
||||
LocalBucketBuilder builder = Bucket4j.builder();
|
||||
if (groupVo.getThresholdPerSecond() > 0) {
|
||||
builder.addLimit(Bandwidth.simple(groupVo.getThresholdPerSecond(), Duration.ofSeconds(1)));
|
||||
}
|
||||
|
||||
if (groupVo.getThreshold() > 0 && StringUtils.isNotBlank(groupVo.getThresholdTimeUnit())) {
|
||||
builder.addLimit(Bandwidth.simple(groupVo.getThreshold(),
|
||||
InflowControlManager.getPeriod(groupVo.getThresholdTimeUnit())));
|
||||
}
|
||||
|
||||
// InflowTargetVO로 변환하여 CustomBucket 생성
|
||||
InflowTargetVO targetVo = new InflowTargetVO();
|
||||
targetVo.setName(groupVo.getGroupId());
|
||||
targetVo.setThreshold(groupVo.getThreshold());
|
||||
targetVo.setThresholdPerSecond(groupVo.getThresholdPerSecond());
|
||||
targetVo.setThresholdTimeUnit(groupVo.getThresholdTimeUnit());
|
||||
targetVo.setActivate(groupVo.isActivate());
|
||||
|
||||
return new CustomBucket(builder.build(), targetVo);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAdapterPass(String adapter) {
|
||||
CustomBucket b = adapterBucketList.get(adapter);
|
||||
@@ -327,6 +465,30 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
return getInterfaceInflowThreashold(inter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGroupPass(String groupId) {
|
||||
CustomBucket b = groupBucketList.get(groupId);
|
||||
|
||||
if (b == null)
|
||||
return true;
|
||||
|
||||
return b.getLocalBucket().tryConsume(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InflowGroupVO getGroupInflowThreshold(String groupId) {
|
||||
return groupVoMap.get(groupId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGroupIdByInterface(String interfaceId) {
|
||||
return interfaceToGroupMap.get(interfaceId);
|
||||
}
|
||||
|
||||
public InflowGroupVO getGroupInflow(String groupId) {
|
||||
return getGroupInflowThreshold(groupId);
|
||||
}
|
||||
|
||||
public static boolean isInflowTarget(InflowTargetVO inflowTarget) {
|
||||
if (inflowTarget == null || !inflowTarget.isActivate()) {
|
||||
return false;
|
||||
@@ -338,6 +500,16 @@ public class InflowControlManager implements Lifecycle, Bucket {
|
||||
|
||||
}
|
||||
|
||||
public static boolean isInflowGroupTarget(InflowGroupVO groupVo) {
|
||||
if (groupVo == null || !groupVo.isActivate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return groupVo.getThresholdPerSecond() > 0 || (groupVo.getThreshold() > 0 && StringUtils
|
||||
.equalsAnyIgnoreCase(groupVo.getThresholdTimeUnit(), QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_SECOND,
|
||||
QUOTA_TIMEUNIT_MINUTE, QUOTA_TIMEUNIT_HOUR, QUOTA_TIMEUNIT_DAY, QUOTA_TIMEUNIT_MONTH));
|
||||
}
|
||||
|
||||
public static Duration getPeriod(String timeUnit) {
|
||||
if (StringUtils.equalsIgnoreCase(timeUnit, QUOTA_TIMEUNIT_SECOND)) {
|
||||
return Duration.ofSeconds(1);
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.eactive.eai.common.inflow;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class InflowGroupVO implements Serializable {
|
||||
private String groupId;
|
||||
private String groupName;
|
||||
private long thresholdPerSecond;
|
||||
private long threshold;
|
||||
private String thresholdTimeUnit;
|
||||
private boolean activate;
|
||||
private List<String> interfaceList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof InflowGroupVO) {
|
||||
return groupId.equals(((InflowGroupVO) obj).getGroupId());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return groupId != null ? groupId.hashCode() : 0;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
public long getThresholdPerSecond() {
|
||||
return thresholdPerSecond;
|
||||
}
|
||||
|
||||
public void setThresholdPerSecond(long thresholdPerSecond) {
|
||||
this.thresholdPerSecond = thresholdPerSecond;
|
||||
}
|
||||
|
||||
public long getThreshold() {
|
||||
return threshold;
|
||||
}
|
||||
|
||||
public void setThreshold(long threshold) {
|
||||
this.threshold = threshold;
|
||||
}
|
||||
|
||||
public String getThresholdTimeUnit() {
|
||||
return thresholdTimeUnit;
|
||||
}
|
||||
|
||||
public void setThresholdTimeUnit(String thresholdTimeUnit) {
|
||||
this.thresholdTimeUnit = thresholdTimeUnit;
|
||||
}
|
||||
|
||||
public boolean isActivate() {
|
||||
return activate;
|
||||
}
|
||||
|
||||
public void setActivate(boolean activate) {
|
||||
this.activate = activate;
|
||||
}
|
||||
|
||||
public List<String> getInterfaceList() {
|
||||
return interfaceList;
|
||||
}
|
||||
|
||||
public void setInterfaceList(List<String> interfaceList) {
|
||||
this.interfaceList = interfaceList;
|
||||
}
|
||||
|
||||
public void addInterface(String interfaceId) {
|
||||
if (this.interfaceList == null) {
|
||||
this.interfaceList = new ArrayList<>();
|
||||
}
|
||||
this.interfaceList.add(interfaceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InflowGroupVO [groupId=" + groupId + ", groupName=" + groupName
|
||||
+ ", thresholdPerSecond=" + thresholdPerSecond
|
||||
+ ", threshold=" + threshold
|
||||
+ ", thresholdTimeUnit=" + thresholdTimeUnit
|
||||
+ ", activate=" + activate
|
||||
+ ", interfaceCount=" + (interfaceList != null ? interfaceList.size() : 0) + "]";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user