1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| @Component @Slf4j public class VIPManager { private final NetworkInterfaceManager networkManager; private final KeepaliveConfig keepaliveConfig; private final MeterRegistry meterRegistry; public VIPManager(NetworkInterfaceManager networkManager, KeepaliveConfig keepaliveConfig, MeterRegistry meterRegistry) { this.networkManager = networkManager; this.keepaliveConfig = keepaliveConfig; this.meterRegistry = meterRegistry; } public void allocateVIP(String vip, String interfaceName) { log.info("Allocating VIP: {} on interface: {}", vip, interfaceName); try { if (isVIPAllocated(vip)) { throw new RuntimeException("VIP already allocated: " + vip); } addVIPToInterface(vip, interfaceName); updateRoutingTable(vip, interfaceName); recordVIPAllocation(vip, interfaceName); meterRegistry.counter("vip.allocated").increment(); log.info("VIP allocated successfully: {} on {}", vip, interfaceName); } catch (Exception e) { log.error("Error allocating VIP: {}", vip, e); meterRegistry.counter("vip.allocation_error").increment(); throw new RuntimeException("Failed to allocate VIP", e); } } public void releaseVIP(String vip, String interfaceName) { log.info("Releasing VIP: {} from interface: {}", vip, interfaceName); try { removeVIPFromInterface(vip, interfaceName); removeFromRoutingTable(vip, interfaceName); recordVIPRelease(vip, interfaceName); meterRegistry.counter("vip.released").increment(); log.info("VIP released successfully: {} from {}", vip, interfaceName); } catch (Exception e) { log.error("Error releasing VIP: {}", vip, e); meterRegistry.counter("vip.release_error").increment(); throw new RuntimeException("Failed to release VIP", e); } } private boolean isVIPAllocated(String vip) { try { return networkManager.isIPAddressActive(vip); } catch (Exception e) { log.error("Error checking VIP allocation: {}", vip, e); return false; } } private void addVIPToInterface(String vip, String interfaceName) { try { ProcessBuilder pb = new ProcessBuilder("ip", "addr", "add", vip + "/32", "dev", interfaceName); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { throw new RuntimeException("Failed to add VIP to interface"); } } catch (Exception e) { log.error("Error adding VIP to interface", e); throw new RuntimeException("Failed to add VIP to interface", e); } } private void removeVIPFromInterface(String vip, String interfaceName) { try { ProcessBuilder pb = new ProcessBuilder("ip", "addr", "del", vip + "/32", "dev", interfaceName); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { throw new RuntimeException("Failed to remove VIP from interface"); } } catch (Exception e) { log.error("Error removing VIP from interface", e); throw new RuntimeException("Failed to remove VIP from interface", e); } } private void updateRoutingTable(String vip, String interfaceName) { try { ProcessBuilder pb = new ProcessBuilder("ip", "route", "add", vip + "/32", "dev", interfaceName); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { log.warn("Failed to add route for VIP: {}", vip); } } catch (Exception e) { log.error("Error updating routing table", e); } } private void removeFromRoutingTable(String vip, String interfaceName) { try { ProcessBuilder pb = new ProcessBuilder("ip", "route", "del", vip + "/32", "dev", interfaceName); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode != 0) { log.warn("Failed to remove route for VIP: {}", vip); } } catch (Exception e) { log.error("Error removing from routing table", e); } } private void recordVIPAllocation(String vip, String interfaceName) { try { VIPAllocationRecord record = VIPAllocationRecord.builder() .vip(vip) .interfaceName(interfaceName) .allocatedAt(LocalDateTime.now()) .status(VIPStatus.ALLOCATED) .build(); log.info("VIP allocation recorded: {}", record); } catch (Exception e) { log.error("Error recording VIP allocation", e); } } private void recordVIPRelease(String vip, String interfaceName) { try { VIPAllocationRecord record = VIPAllocationRecord.builder() .vip(vip) .interfaceName(interfaceName) .releasedAt(LocalDateTime.now()) .status(VIPStatus.RELEASED) .build(); log.info("VIP release recorded: {}", record); } catch (Exception e) { log.error("Error recording VIP release", e); } } public VIPStatusInfo getVIPStatus(String vip) { try { boolean isActive = networkManager.isIPAddressActive(vip); String interfaceName = networkManager.getInterfaceForIP(vip); return VIPStatusInfo.builder() .vip(vip) .active(isActive) .interfaceName(interfaceName) .timestamp(LocalDateTime.now()) .build(); } catch (Exception e) { log.error("Error getting VIP status: {}", vip, e); return VIPStatusInfo.builder() .vip(vip) .active(false) .timestamp(LocalDateTime.now()) .error(e.getMessage()) .build(); } } public List<VIPStatusInfo> getAllVIPStatus() { List<VIPStatusInfo> statusList = new ArrayList<>(); try { List<String> configuredVIPs = keepaliveConfig.getConfiguredVIPs(); for (String vip : configuredVIPs) { VIPStatusInfo status = getVIPStatus(vip); statusList.add(status); } } catch (Exception e) { log.error("Error getting all VIP status", e); } return statusList; } }
|