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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
@Component public class TenantResourceManager { private final MultiTenantProperties properties; private final RedisTemplate<String, Object> redisTemplate; private final Map<String, ResourcePool> tenantResourcePools = new ConcurrentHashMap<>(); public TenantResourceManager(MultiTenantProperties properties) { this.properties = properties; this.redisTemplate = null; }
public boolean allocateTenantResource(String tenantId, String resourceType, int resourceCount) { try { if (!checkTenantResourceLimit(tenantId, resourceType, resourceCount)) { log.warn("租户资源分配超限: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount); return false; } ResourcePool resourcePool = getTenantResourcePool(tenantId); boolean allocated = resourcePool.allocateResource(resourceType, resourceCount); if (allocated) { recordResourceAllocation(tenantId, resourceType, resourceCount); log.info("租户资源分配成功: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount); } return allocated; } catch (Exception e) { log.error("分配租户资源失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); return false; } }
public boolean releaseTenantResource(String tenantId, String resourceType, int resourceCount) { try { ResourcePool resourcePool = getTenantResourcePool(tenantId); boolean released = resourcePool.releaseResource(resourceType, resourceCount); if (released) { recordResourceRelease(tenantId, resourceType, resourceCount); log.info("租户资源释放成功: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount); } return released; } catch (Exception e) { log.error("释放租户资源失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); return false; } }
public Map<String, ResourceUsage> getTenantResourceUsage(String tenantId) { try { ResourcePool resourcePool = getTenantResourcePool(tenantId); return resourcePool.getResourceUsage(); } catch (Exception e) { log.error("获取租户资源使用情况失败: tenantId={}", tenantId, e); return new HashMap<>(); } }
private boolean checkTenantResourceLimit(String tenantId, String resourceType, int resourceCount) { try { Map<String, Integer> resourceLimits = getTenantResourceLimits(tenantId); Integer limit = resourceLimits.get(resourceType); if (limit != null) { Map<String, ResourceUsage> currentUsage = getTenantResourceUsage(tenantId); ResourceUsage usage = currentUsage.get(resourceType); int currentCount = usage != null ? usage.getUsedCount() : 0; if (currentCount + resourceCount > limit) { return false; } } return true; } catch (Exception e) { log.error("检查租户资源限制失败: tenantId={}, resourceType={}", tenantId, resourceType, e); return false; } }
private ResourcePool getTenantResourcePool(String tenantId) { return tenantResourcePools.computeIfAbsent(tenantId, k -> new ResourcePool(tenantId)); }
private Map<String, Integer> getTenantResourceLimits(String tenantId) { try { String cacheKey = "tenant_resource_limits:" + tenantId; Map<String, Integer> limits = (Map<String, Integer>) redisTemplate.opsForValue().get(cacheKey); if (limits == null) { limits = getDefaultResourceLimits(); redisTemplate.opsForValue().set(cacheKey, limits, Duration.ofHours(1)); } return limits; } catch (Exception e) { log.error("获取租户资源限制失败: tenantId={}", tenantId, e); return getDefaultResourceLimits(); } }
private Map<String, Integer> getDefaultResourceLimits() { Map<String, Integer> limits = new HashMap<>(); limits.put("CPU", 1000); limits.put("MEMORY", 8192); limits.put("STORAGE", 100000); limits.put("NETWORK", 1000); limits.put("CONNECTIONS", 100); return limits; }
private void recordResourceAllocation(String tenantId, String resourceType, int resourceCount) { try { String cacheKey = "tenant_resource_allocation:" + tenantId; Map<String, Integer> allocations = (Map<String, Integer>) redisTemplate.opsForValue().get(cacheKey); if (allocations == null) { allocations = new HashMap<>(); } allocations.put(resourceType, allocations.getOrDefault(resourceType, 0) + resourceCount); redisTemplate.opsForValue().set(cacheKey, allocations, Duration.ofHours(1)); } catch (Exception e) { log.error("记录资源分配失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); } }
private void recordResourceRelease(String tenantId, String resourceType, int resourceCount) { try { String cacheKey = "tenant_resource_allocation:" + tenantId; Map<String, Integer> allocations = (Map<String, Integer>) redisTemplate.opsForValue().get(cacheKey); if (allocations != null) { int currentCount = allocations.getOrDefault(resourceType, 0); int newCount = Math.max(0, currentCount - resourceCount); if (newCount == 0) { allocations.remove(resourceType); } else { allocations.put(resourceType, newCount); } redisTemplate.opsForValue().set(cacheKey, allocations, Duration.ofHours(1)); } } catch (Exception e) { log.error("记录资源释放失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); } } }
public class ResourcePool { private final String tenantId; private final Map<String, ResourceUsage> resourceUsage = new ConcurrentHashMap<>(); public ResourcePool(String tenantId) { this.tenantId = tenantId; }
public synchronized boolean allocateResource(String resourceType, int resourceCount) { try { ResourceUsage usage = resourceUsage.computeIfAbsent(resourceType, k -> new ResourceUsage(resourceType, 0, 0)); if (usage.getUsedCount() + resourceCount > usage.getMaxCount()) { return false; } usage.setUsedCount(usage.getUsedCount() + resourceCount); return true; } catch (Exception e) { log.error("分配资源失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); return false; } }
public synchronized boolean releaseResource(String resourceType, int resourceCount) { try { ResourceUsage usage = resourceUsage.get(resourceType); if (usage == null) { return false; } int newCount = Math.max(0, usage.getUsedCount() - resourceCount); usage.setUsedCount(newCount); return true; } catch (Exception e) { log.error("释放资源失败: tenantId={}, resourceType={}, count={}", tenantId, resourceType, resourceCount, e); return false; } }
public Map<String, ResourceUsage> getResourceUsage() { return new HashMap<>(resourceUsage); } }
@Data @NoArgsConstructor @AllArgsConstructor public class ResourceUsage { private String resourceType; private int usedCount; private int maxCount; }
|