我正在开展一个项目,其中我有三个数据中心 - DC1
,DC2
和DC3
。
在DC1
我有2台机器(machineA and machineB)
,在DC2
我有两台机器(machineC and machineD)
,在DC3
我又有两台机器{{1} }。
每个数据中心的每个机器URL都是这样的,它返回字符串作为响应 -
(machineE and machineF)
对于DC1 -
http://machineName:8080/textbeat
对于DC2 -
http://machineA:8080/textbeat
http://machineB:8080/textbeat
对于DC3 -
http://machineC:8080/textbeat
http://machineD:8080/textbeat
这是我在点击任何特定机器的网址后一般看到的响应字符串 -
http://machineE:8080/textbeat
http://machineF:8080/textbeat
问题陈述: -
现在我需要迭代每个数据中心的所有计算机并执行URL,然后从中提取state: READY server_uptime: 12462125 data_syncs: 29
。这必须每1分钟完成一次。
现在如果data_syncs
machineA
在5分钟内始终为零,那么我想打印data_syncs
和DC1
。同样适用于machineB和其他数据中心。
我在想的逻辑 -
machineA
值,将计数器增加1,下面是我的地图,如果他们符合上述条件我将把数据中心及其机器放在哪里 -
data_syncs
此处key是数据中心名称,value是满足条件的数据中心的计算机列表。
下面是我提出的用于解决上述问题的代码,但它不能像我想的那样工作。在这里,我的final Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>();
对于我认为不是我想要的所有机器是相同的。
counter
这是我的public class MachineTest {
private static int counter = 0;
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
public static void main(String[] args) {
final ScheduledFuture<?> taskUtility = scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
generalUtility();
} catch (Exception ex) {
// log an exception
}
}
}, 0, 1L, TimeUnit.MINUTES);
}
protected static void generalUtility() {
try {
final Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>();
List<String> datacenters = Arrays.asList("DC1", "DC2", "DC3");
for (String datacenter : datacenters) {
LinkedList<String> machines = new LinkedList<String>();
List<String> childrenInEachDatacenter = getMachinesInEachDatacenter(datacenter);
for (String hosts : childrenInEachDatacenter) {
String host_name = hosts;
String url = "http://" + host_name + ":8080/textbeat";
MachineMetrics metrics = GeneralUtilities.getMetricsOfMachine(host_name, url); // execute the url and populate the MachineMetrics object
if (metrics.getDataSyncs().equalsIgnoreCase("0")) {
counter++;
if (counter == 5) {
machines.add(hosts);
}
}
}
if(!machines.isEmpty()) {
holder.put(datacenter, machines);
}
}
if (!holder.isEmpty()) {
// log the datacenter and its machine as our criteria is met
System.out.println(holder);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Below method will return list of machines given the name of datacenter
private static List<String> getMachinesInEachDatacenter(String datacenter) {
// this will return list of machines for a given datacenter
}
}
课程 -
MachineMetrics
这是否可以使用ScheduledExecutorService,因为这不是一次性过程?它必须重复完成
对于每台计算机,如果public class MachineMetrics {
private String machineName;
private String dataSyncs;
// getters and setters
}
在data_syncs
连续期间为0,那么我需要记录该数据中心及其计算机。
答案 0 :(得分:0)
public class Machine{
private String dataCenter;
private String machineName;
private String hostname;
private int zeroCount = 0;
//getters setters, except for zeroCount
// constructor with datacenter,machine as args
private boolean isEligibleForLogging(String dataSyncs){
if(dataSyncs.equals("0")){
zeroCount++;
}else{
zeroCount = 0;
}
if(zeroCount > 5){
zeroCount = 0;
return true;
}
return false;
}
}
static List<Machine> machines = new ArrayList<Machine>();
static{
Machine machine1 = new Machine("DC1", "name1","hostname1");
machines.add(machine1);
//repeat the above two lines per each machine.
}
protected static void generalUtility() {
try {
for (Machine machine : machines) {
String host_name = machine.getHostName();
String url = "http://" + host_name + ":8080/textbeat";
String dataSyncs = //execute url and get datasyncs
if(machine.isEligibleForLogging()){
System.out.println(machine.getName() + ... +machine.getDataCenter() + ... + dataSyncs......);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}