|
| 1 | +package org.skywalking.apm.collector.cluster.standalone; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import java.util.LinkedHashMap; |
| 5 | +import java.util.Map; |
| 6 | +import org.skywalking.apm.collector.client.h2.H2Client; |
| 7 | +import org.skywalking.apm.collector.client.zookeeper.util.PathUtils; |
| 8 | +import org.skywalking.apm.collector.core.CollectorException; |
| 9 | +import org.skywalking.apm.collector.core.client.Client; |
| 10 | +import org.skywalking.apm.collector.core.client.ClientException; |
| 11 | +import org.skywalking.apm.collector.core.client.DataMonitor; |
| 12 | +import org.skywalking.apm.collector.core.cluster.ClusterDataListener; |
| 13 | +import org.skywalking.apm.collector.core.module.ModuleRegistration; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author pengys5 |
| 19 | + */ |
| 20 | +public class ClusterStandaloneDataMonitor implements DataMonitor { |
| 21 | + |
| 22 | + private final Logger logger = LoggerFactory.getLogger(ClusterStandaloneDataMonitor.class); |
| 23 | + |
| 24 | + private H2Client client; |
| 25 | + |
| 26 | + private Map<String, ClusterDataListener> listeners; |
| 27 | + private Map<String, ModuleRegistration> registrations; |
| 28 | + |
| 29 | + public ClusterStandaloneDataMonitor() { |
| 30 | + listeners = new LinkedHashMap<>(); |
| 31 | + registrations = new LinkedHashMap<>(); |
| 32 | + } |
| 33 | + |
| 34 | + @Override public void setClient(Client client) { |
| 35 | + this.client = (H2Client)client; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void addListener(ClusterDataListener listener, ModuleRegistration registration) throws ClientException { |
| 40 | + String path = PathUtils.convertKey2Path(listener.path()); |
| 41 | + logger.info("listener path: {}", path); |
| 42 | + listeners.put(path, listener); |
| 43 | + registrations.put(path, registration); |
| 44 | + } |
| 45 | + |
| 46 | + @Override public ClusterDataListener getListener(String path) { |
| 47 | + path = PathUtils.convertKey2Path(path); |
| 48 | + return listeners.get(path); |
| 49 | + } |
| 50 | + |
| 51 | + @Override public void createPath(String path) throws ClientException { |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Override public void setData(String path, String value) throws ClientException { |
| 56 | + if (listeners.containsKey(path)) { |
| 57 | + listeners.get(path).addAddress(value); |
| 58 | + listeners.get(path).serverJoinNotify(value); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override public void start() throws CollectorException { |
| 63 | + Iterator<Map.Entry<String, ModuleRegistration>> entryIterator = registrations.entrySet().iterator(); |
| 64 | + while (entryIterator.hasNext()) { |
| 65 | + Map.Entry<String, ModuleRegistration> next = entryIterator.next(); |
| 66 | + ModuleRegistration.Value value = next.getValue().buildValue(); |
| 67 | + String contextPath = value.getContextPath() == null ? "" : value.getContextPath(); |
| 68 | + setData(next.getKey(), value.getHostPort() + contextPath); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments