2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ****************************************************************************/
21 import java.io.IOException;
22 import java.util.HashSet;
23 import java.util.Iterator;
26 public class DNSMembershipManager {
28 * Uses Failure Detector (FD) to keep track of the DNS servers currently active.
29 * @param port: the port that the FD service listens for incoming UDP packets
30 * @param timeout: how often the FD checks the status of client processes
31 * @param threshold: number of missing ping messages before declaring a client dead
32 * @param debug: debug mode on/off
35 static Set<String> active = new HashSet<String>();
37 @SuppressWarnings("static-access")
38 public static void main(String[] args) throws IOException, InterruptedException {
39 if(args.length != 5) {
40 System.out.println("Missing input parameters");
41 System.out.println("Usage:");
42 System.out.print("\t- java FDServer [public IP address] [port] [timeout (sec)] [threshold] [debug]\n");
46 // Input parameters: PORT, TIMEOUT, THRESHOLD
47 String IPADDR = args[0];
48 int PORT = Integer.parseInt(args[1]);
49 long TIMEOUT = Long.parseLong(args[2]) * 1000; // convert the FD timeout to milliseconds
50 int THRESHOLD = Integer.parseInt(args[3]);
51 int debug = Integer.parseInt(args[4]);
58 // Start Failure Detector
59 FDServer fd = new FDServer(IPADDR, PORT, TIMEOUT, THRESHOLD, DEBUG);
61 // Check the status of client processes periodically. We use the same timeout value as FD
62 Set<String> active = new HashSet<String>();
64 Set<String> alive_this_round = fd.getAliveProcesses();
65 Iterator<String> iter = alive_this_round.iterator();
68 // Check if there is some new DNS active
69 while(iter.hasNext()) {
71 if(!active.contains(pid)) {
73 // Add the new vDNS to the set of vDNS servers
74 String script = new String("bash /opt/FDserver/add_dns.sh " + pid);
75 Runtime.getRuntime().exec(script);
76 if(DEBUG) System.out.println("Adding process " + pid + " to the list of DNSs");
80 // Remove possible dead DNSs
81 iter = active.iterator();
82 while(iter.hasNext()) {
84 if(!alive_this_round.contains(pid)) {
85 iter.remove(); // remove element from the iterator to avoid ConcurrentModificationException
86 // Remove the new vDNS from the set of vDNS servers
87 String script = new String("bash /opt/FDserver/remove_dns.sh " + pid);
88 Runtime.getRuntime().exec(script);
89 if(DEBUG) System.out.println("Removing process " + pid + " from the list of DNSs");
92 Thread.currentThread().sleep(TIMEOUT);