|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.gateway.ha.router; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableMap; |
| 17 | +import com.google.errorprone.annotations.concurrent.GuardedBy; |
| 18 | +import com.google.inject.Inject; |
| 19 | +import io.trino.gateway.ha.clustermonitor.ClusterStats; |
| 20 | +import jakarta.annotation.PostConstruct; |
| 21 | +import jakarta.annotation.PreDestroy; |
| 22 | +import org.weakref.jmx.MBeanExport; |
| 23 | +import org.weakref.jmx.MBeanExporter; |
| 24 | +import org.weakref.jmx.Managed; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static java.util.Objects.requireNonNull; |
| 32 | + |
| 33 | +public class BackendStateMBeanExporter |
| 34 | +{ |
| 35 | + @GuardedBy("this") |
| 36 | + private final List<MBeanExport> mbeanExports = new ArrayList<>(); |
| 37 | + |
| 38 | + private final MBeanExporter exporter; |
| 39 | + private final BackendStateManager backendStateManager; |
| 40 | + private final Map<String, ClusterStatsJMX> backendStates = new HashMap<>(); |
| 41 | + |
| 42 | + @Inject |
| 43 | + public BackendStateMBeanExporter(MBeanExporter exporter, BackendStateManager backendStateManager) |
| 44 | + { |
| 45 | + this.exporter = requireNonNull(exporter, "exporter is null"); |
| 46 | + this.backendStateManager = requireNonNull(backendStateManager, "backendStateManager is null"); |
| 47 | + } |
| 48 | + |
| 49 | + @PostConstruct |
| 50 | + public synchronized void updateExport() |
| 51 | + { |
| 52 | + for (ClusterStats clusterStats : backendStateManager.getAllBackendStates().values()) { |
| 53 | + String clusterId = clusterStats.clusterId(); |
| 54 | + |
| 55 | + if (backendStates.containsKey(clusterId)) { |
| 56 | + ClusterStatsJMX clusterStatsJMX = backendStates.get(clusterId); |
| 57 | + clusterStatsJMX.setFrom(clusterStats); |
| 58 | + } |
| 59 | + else { |
| 60 | + ClusterStatsJMX clusterStatsJMX = new ClusterStatsJMX(); |
| 61 | + clusterStatsJMX.setFrom(clusterStats); |
| 62 | + mbeanExports.add(exporter.exportWithGeneratedName( |
| 63 | + clusterStatsJMX, |
| 64 | + ClusterStatsJMX.class, |
| 65 | + ImmutableMap.<String, String>builder() |
| 66 | + .put("name", "ClusterStats") |
| 67 | + .put("backend", clusterId) |
| 68 | + .build())); |
| 69 | + backendStates.put(clusterId, clusterStatsJMX); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @PreDestroy |
| 75 | + public synchronized void unexport() |
| 76 | + { |
| 77 | + for (MBeanExport mbeanExport : mbeanExports) { |
| 78 | + mbeanExport.unexport(); |
| 79 | + } |
| 80 | + mbeanExports.clear(); |
| 81 | + } |
| 82 | + |
| 83 | + public static class ClusterStatsJMX |
| 84 | + { |
| 85 | + private int numWorkerNodes; |
| 86 | + private boolean healthy; |
| 87 | + private String proxyTo; |
| 88 | + private String externalUrl; |
| 89 | + private String routingGroup; |
| 90 | + |
| 91 | + public void setFrom(ClusterStats clusterStats) |
| 92 | + { |
| 93 | + numWorkerNodes = clusterStats.numWorkerNodes(); |
| 94 | + healthy = clusterStats.healthy(); |
| 95 | + proxyTo = clusterStats.proxyTo(); |
| 96 | + externalUrl = clusterStats.externalUrl(); |
| 97 | + routingGroup = clusterStats.routingGroup(); |
| 98 | + } |
| 99 | + |
| 100 | + @Managed |
| 101 | + public int getNumWorkerNodes() |
| 102 | + { |
| 103 | + return numWorkerNodes; |
| 104 | + } |
| 105 | + |
| 106 | + @Managed |
| 107 | + public boolean isHealthy() |
| 108 | + { |
| 109 | + return healthy; |
| 110 | + } |
| 111 | + |
| 112 | + @Managed |
| 113 | + public String getProxyTo() |
| 114 | + { |
| 115 | + return proxyTo; |
| 116 | + } |
| 117 | + |
| 118 | + @Managed |
| 119 | + public String getExternalUrl() |
| 120 | + { |
| 121 | + return externalUrl; |
| 122 | + } |
| 123 | + |
| 124 | + @Managed |
| 125 | + public String getRoutingGroup() |
| 126 | + { |
| 127 | + return routingGroup; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments