feat: 어댑터/인터페이스/클라이언트 유량제어 메트릭 API 추가
InflowTargetMetricController / InflowTargetMetricService / TargetMetricDTO 추가. 어댑터·인터페이스·클라이언트별 허용/초당거절/임계거절 카운터 조회 및 초기화 API 제공. 단위테스트: InflowTargetMetricServiceTest(26건), InflowTargetMetricControllerTest(16건). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
package com.eactive.eai.manage.inflow;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 어댑터/인터페이스/클라이언트 유량제어 메트릭 API.
|
||||
*
|
||||
* DualInflowControlManager 가 활성화된 환경에서만 정상 응답한다.
|
||||
*
|
||||
* GET /manage/inflow/adapter/metric → 전체 어댑터 메트릭
|
||||
* GET /manage/inflow/adapter/{adapterId}/metric → 특정 어댑터 메트릭
|
||||
* POST /manage/inflow/adapter/metric/reset → 전체 어댑터 메트릭 초기화
|
||||
* POST /manage/inflow/adapter/{adapterId}/metric/reset → 특정 어댑터 메트릭 초기화
|
||||
*
|
||||
* GET /manage/inflow/interface/metric → 전체 인터페이스 메트릭
|
||||
* GET /manage/inflow/interface/{interfaceId}/metric → 특정 인터페이스 메트릭
|
||||
* POST /manage/inflow/interface/metric/reset → 전체 인터페이스 메트릭 초기화
|
||||
* POST /manage/inflow/interface/{interfaceId}/metric/reset → 특정 인터페이스 메트릭 초기화
|
||||
*
|
||||
* GET /manage/inflow/client/metric → 전체 클라이언트 메트릭
|
||||
* GET /manage/inflow/client/{clientId}/metric → 특정 클라이언트 메트릭
|
||||
* POST /manage/inflow/client/metric/reset → 전체 클라이언트 메트릭 초기화
|
||||
* POST /manage/inflow/client/{clientId}/metric/reset → 특정 클라이언트 메트릭 초기화
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/manage/inflow")
|
||||
public class InflowTargetMetricController {
|
||||
|
||||
@Autowired
|
||||
private InflowTargetMetricService metricService;
|
||||
|
||||
// =========================================================================
|
||||
// 어댑터
|
||||
// =========================================================================
|
||||
|
||||
@GetMapping("/adapter/metric")
|
||||
public ResponseEntity<?> getAllAdapterMetrics() {
|
||||
List<TargetMetricDTO> list = metricService.getAllAdapterMetrics();
|
||||
return okList(list);
|
||||
}
|
||||
|
||||
@GetMapping("/adapter/{adapterId}/metric")
|
||||
public ResponseEntity<?> getAdapterMetric(@PathVariable String adapterId) {
|
||||
TargetMetricDTO dto = metricService.getAdapterMetric(adapterId);
|
||||
return okSingle(dto, "어댑터를 찾을 수 없습니다: " + adapterId);
|
||||
}
|
||||
|
||||
@PostMapping("/adapter/metric/reset")
|
||||
public ResponseEntity<?> resetAllAdapterMetrics() {
|
||||
metricService.resetAllAdapterMetrics();
|
||||
return okReset("전체 어댑터 메트릭 초기화 완료");
|
||||
}
|
||||
|
||||
@PostMapping("/adapter/{adapterId}/metric/reset")
|
||||
public ResponseEntity<?> resetAdapterMetric(@PathVariable String adapterId) {
|
||||
boolean ok = metricService.resetAdapterMetric(adapterId);
|
||||
return okResetSingle(ok, adapterId, "어댑터");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 인터페이스
|
||||
// =========================================================================
|
||||
|
||||
@GetMapping("/interface/metric")
|
||||
public ResponseEntity<?> getAllInterfaceMetrics() {
|
||||
List<TargetMetricDTO> list = metricService.getAllInterfaceMetrics();
|
||||
return okList(list);
|
||||
}
|
||||
|
||||
@GetMapping("/interface/{interfaceId}/metric")
|
||||
public ResponseEntity<?> getInterfaceMetric(@PathVariable String interfaceId) {
|
||||
TargetMetricDTO dto = metricService.getInterfaceMetric(interfaceId);
|
||||
return okSingle(dto, "인터페이스를 찾을 수 없습니다: " + interfaceId);
|
||||
}
|
||||
|
||||
@PostMapping("/interface/metric/reset")
|
||||
public ResponseEntity<?> resetAllInterfaceMetrics() {
|
||||
metricService.resetAllInterfaceMetrics();
|
||||
return okReset("전체 인터페이스 메트릭 초기화 완료");
|
||||
}
|
||||
|
||||
@PostMapping("/interface/{interfaceId}/metric/reset")
|
||||
public ResponseEntity<?> resetInterfaceMetric(@PathVariable String interfaceId) {
|
||||
boolean ok = metricService.resetInterfaceMetric(interfaceId);
|
||||
return okResetSingle(ok, interfaceId, "인터페이스");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 클라이언트
|
||||
// =========================================================================
|
||||
|
||||
@GetMapping("/client/metric")
|
||||
public ResponseEntity<?> getAllClientMetrics() {
|
||||
List<TargetMetricDTO> list = metricService.getAllClientMetrics();
|
||||
return okList(list);
|
||||
}
|
||||
|
||||
@GetMapping("/client/{clientId}/metric")
|
||||
public ResponseEntity<?> getClientMetric(@PathVariable String clientId) {
|
||||
TargetMetricDTO dto = metricService.getClientMetric(clientId);
|
||||
return okSingle(dto, "클라이언트를 찾을 수 없습니다: " + clientId);
|
||||
}
|
||||
|
||||
@PostMapping("/client/metric/reset")
|
||||
public ResponseEntity<?> resetAllClientMetrics() {
|
||||
metricService.resetAllClientMetrics();
|
||||
return okReset("전체 클라이언트 메트릭 초기화 완료");
|
||||
}
|
||||
|
||||
@PostMapping("/client/{clientId}/metric/reset")
|
||||
public ResponseEntity<?> resetClientMetric(@PathVariable String clientId) {
|
||||
boolean ok = metricService.resetClientMetric(clientId);
|
||||
return okResetSingle(ok, clientId, "클라이언트");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Helpers
|
||||
// =========================================================================
|
||||
|
||||
private ResponseEntity<?> okList(List<TargetMetricDTO> list) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("data", list);
|
||||
result.put("count", list.size());
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
private ResponseEntity<?> okSingle(TargetMetricDTO dto, String notFoundMsg) {
|
||||
if (dto == null) {
|
||||
Map<String, Object> error = new HashMap<>();
|
||||
error.put("success", false);
|
||||
error.put("message", notFoundMsg);
|
||||
return ResponseEntity.ok(error);
|
||||
}
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("data", dto);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
private ResponseEntity<?> okReset(String message) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("message", message);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
private ResponseEntity<?> okResetSingle(boolean ok, String targetId, String targetLabel) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", ok);
|
||||
result.put("message", ok
|
||||
? "초기화 완료: " + targetId
|
||||
: targetLabel + "를 찾을 수 없습니다: " + targetId);
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.eactive.eai.manage.inflow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.eactive.eai.common.inflow.AbstractInflowControlManager;
|
||||
import com.eactive.eai.common.inflow.InflowControlUtil;
|
||||
import com.eactive.eai.common.inflow.InflowTargetVO;
|
||||
import com.eactive.eai.common.inflow.dual.DualInflowControlManager;
|
||||
|
||||
@Service
|
||||
public class InflowTargetMetricService {
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 어댑터
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public TargetMetricDTO getAdapterMetric(String adapterId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return null;
|
||||
InflowTargetVO vo = manager.getAdapterInflowThreashold(adapterId);
|
||||
if (vo == null) return null;
|
||||
return buildDTO(adapterId, "ADAPTER", vo, manager.getAdapterMetrics(adapterId));
|
||||
}
|
||||
|
||||
public List<TargetMetricDTO> getAllAdapterMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return new ArrayList<>();
|
||||
List<TargetMetricDTO> result = new ArrayList<>();
|
||||
for (String id : manager.getAdapterAllKeys()) {
|
||||
TargetMetricDTO dto = getAdapterMetric(id);
|
||||
if (dto != null) result.add(dto);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean resetAdapterMetric(String adapterId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return false;
|
||||
if (manager.getAdapterInflowThreashold(adapterId) == null) return false;
|
||||
manager.resetAdapterMetrics(adapterId);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void resetAllAdapterMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager != null) manager.resetAllAdapterMetrics();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 인터페이스
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public TargetMetricDTO getInterfaceMetric(String interfaceId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return null;
|
||||
InflowTargetVO vo = manager.getInterfaceInflowThreashold(interfaceId);
|
||||
if (vo == null) return null;
|
||||
return buildDTO(interfaceId, "INTERFACE", vo, manager.getInterfaceMetrics(interfaceId));
|
||||
}
|
||||
|
||||
public List<TargetMetricDTO> getAllInterfaceMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return new ArrayList<>();
|
||||
List<TargetMetricDTO> result = new ArrayList<>();
|
||||
for (String id : manager.getInterfaceAllKeys()) {
|
||||
TargetMetricDTO dto = getInterfaceMetric(id);
|
||||
if (dto != null) result.add(dto);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean resetInterfaceMetric(String interfaceId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return false;
|
||||
if (manager.getInterfaceInflowThreashold(interfaceId) == null) return false;
|
||||
manager.resetInterfaceMetrics(interfaceId);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void resetAllInterfaceMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager != null) manager.resetAllInterfaceMetrics();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 클라이언트
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
public TargetMetricDTO getClientMetric(String clientId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return null;
|
||||
InflowTargetVO vo = manager.getClientInflowThreshold(clientId);
|
||||
if (vo == null) return null;
|
||||
return buildDTO(clientId, "CLIENT", vo, manager.getClientMetrics(clientId));
|
||||
}
|
||||
|
||||
public List<TargetMetricDTO> getAllClientMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return new ArrayList<>();
|
||||
List<TargetMetricDTO> result = new ArrayList<>();
|
||||
for (String id : manager.getClientBucketMap().keySet()) {
|
||||
TargetMetricDTO dto = getClientMetric(id);
|
||||
if (dto != null) result.add(dto);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean resetClientMetric(String clientId) {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager == null) return false;
|
||||
if (manager.getClientInflowThreshold(clientId) == null) return false;
|
||||
manager.resetClientMetrics(clientId);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void resetAllClientMetrics() {
|
||||
DualInflowControlManager manager = getDualManager();
|
||||
if (manager != null) manager.resetAllClientMetrics();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Private helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
protected DualInflowControlManager getDualManager() {
|
||||
AbstractInflowControlManager base = InflowControlUtil.getInflowControlManager();
|
||||
return (base instanceof DualInflowControlManager) ? (DualInflowControlManager) base : null;
|
||||
}
|
||||
|
||||
private TargetMetricDTO buildDTO(String targetId, String targetType,
|
||||
InflowTargetVO vo,
|
||||
DualInflowControlManager.TargetMetrics m) {
|
||||
TargetMetricDTO dto = new TargetMetricDTO();
|
||||
dto.setTargetId(targetId);
|
||||
dto.setTargetType(targetType);
|
||||
dto.setActivate(vo.isActivate());
|
||||
|
||||
if (m != null) {
|
||||
long allowed = m.allowed.get();
|
||||
long rejPs = m.rejectedPerSecond.get();
|
||||
long rejTh = m.rejectedThreshold.get();
|
||||
long total = allowed + rejPs + rejTh;
|
||||
dto.setAllowed(allowed);
|
||||
dto.setRejectedPerSecond(rejPs);
|
||||
dto.setRejectedThreshold(rejTh);
|
||||
dto.setTotalRequests(total);
|
||||
dto.setRejectRatio(total > 0
|
||||
? Math.round((double)(rejPs + rejTh) / total * 10000) / 100.0
|
||||
: 0.0);
|
||||
dto.setLastResetTime(m.lastResetTime);
|
||||
}
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.eactive.eai.manage.inflow;
|
||||
|
||||
/**
|
||||
* 어댑터/인터페이스/클라이언트 메트릭 DTO.
|
||||
*/
|
||||
public class TargetMetricDTO {
|
||||
|
||||
private String targetId;
|
||||
private String targetType; // "ADAPTER" | "INTERFACE" | "CLIENT"
|
||||
private boolean activate;
|
||||
private long allowed;
|
||||
private long rejectedPerSecond;
|
||||
private long rejectedThreshold;
|
||||
private long totalRequests;
|
||||
private double rejectRatio;
|
||||
private long lastResetTime;
|
||||
|
||||
public String getTargetId() { return targetId; }
|
||||
public void setTargetId(String targetId) { this.targetId = targetId; }
|
||||
|
||||
public String getTargetType() { return targetType; }
|
||||
public void setTargetType(String targetType) { this.targetType = targetType; }
|
||||
|
||||
public boolean isActivate() { return activate; }
|
||||
public void setActivate(boolean activate) { this.activate = activate; }
|
||||
|
||||
public long getAllowed() { return allowed; }
|
||||
public void setAllowed(long allowed) { this.allowed = allowed; }
|
||||
|
||||
public long getRejectedPerSecond() { return rejectedPerSecond; }
|
||||
public void setRejectedPerSecond(long rejectedPerSecond) { this.rejectedPerSecond = rejectedPerSecond; }
|
||||
|
||||
public long getRejectedThreshold() { return rejectedThreshold; }
|
||||
public void setRejectedThreshold(long rejectedThreshold) { this.rejectedThreshold = rejectedThreshold; }
|
||||
|
||||
public long getTotalRequests() { return totalRequests; }
|
||||
public void setTotalRequests(long totalRequests) { this.totalRequests = totalRequests; }
|
||||
|
||||
public double getRejectRatio() { return rejectRatio; }
|
||||
public void setRejectRatio(double rejectRatio) { this.rejectRatio = rejectRatio; }
|
||||
|
||||
public long getLastResetTime() { return lastResetTime; }
|
||||
public void setLastResetTime(long lastResetTime) { this.lastResetTime = lastResetTime; }
|
||||
}
|
||||
Reference in New Issue
Block a user