blob: 8737d915bcf6a45273b01b3696fc8a1696f1b1f3 (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.monitor;
import com.yahoo.config.model.api.ApplicationInfo;
import com.yahoo.config.model.api.HostInfo;
import com.yahoo.config.model.api.PortInfo;
import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.jrt.Spec;
import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.jrt.slobrok.api.SlobrokList;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Class to manage Slobrok
*/
public class SlobrokMonitor2 implements AutoCloseable {
public static final String SLOBROK_SERVICE_TYPE = "slobrok";
public static final String SLOBROK_RPC_PORT_TAG = "rpc";
private final SlobrokList slobrokList;
private final Mirror mirror;
SlobrokMonitor2() {
this(new SlobrokList());
}
// Package-private for testing.
SlobrokMonitor2(SlobrokList slobrokList, Mirror mirror) {
this.slobrokList = slobrokList;
this.mirror = mirror;
}
private SlobrokMonitor2(SlobrokList slobrokList) {
this(slobrokList, new Mirror(new Supervisor(new Transport()), slobrokList));
}
void updateSlobrokList(ApplicationInfo application) {
List<String> slobrokSpecs = getSlobrokSpecs(application);
slobrokList.setup(slobrokSpecs.toArray(new String[0]));
}
List<String> getSlobrokSpecs(ApplicationInfo applicationInfo) {
List<String> slobrokSpecs = new ArrayList<>();
for (HostInfo host : applicationInfo.getModel().getHosts()) {
for (ServiceInfo service : host.getServices()) {
if (!Objects.equals(service.getServiceType(), SLOBROK_SERVICE_TYPE)) {
continue;
}
for (PortInfo port : service.getPorts()) {
if (port.getTags().contains(SLOBROK_RPC_PORT_TAG)) {
Spec spec = new Spec(host.getHostname(), port.getPort());
slobrokSpecs.add(spec.toString());
}
}
}
}
return slobrokSpecs;
}
@Override
public void close() {
// TODO: Make sure registeredInSlobrok returns DOWN from now on (concurrently)
mirror.shutdown();
}
boolean registeredInSlobrok(String slobrokServiceName) {
return mirror.lookup(slobrokServiceName).length > 0;
}
}
|