<commit_before>package com.boxymoron.ha;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class implements a simple Partition-Tolerant Leader election algorithm (over UDP). The algorithm is essentially a state machine:<br><br>
 * The valid states are: MASTER, SLAVE, UNDEFINED<br><br>
 * <li>Each node in the cluster maintains a static list of all (other) cluster member IP addresses/hostnames.<br>
 * <li>Each node asynchronously sends a message with its own 'priority' (UDP), which is a an integer in the range 0 - 9999 every {@link ClusterMember#timeout_ms}/2 ms.<br>
 * <li>Each node listens to all other node's messages.<br>
 * <li>The node with the highest priority, in a majority partition, becomes the MASTER.<br><br>
 * For this mechanism to work correctly, each node should have a distinct priority. Currently, only odd numbers of nodes >= 3 are supported ;)<br><br>
 * 
 * The API provides a joinCluster({@link ClusterMember.Listener}) method to register a state changed listener. The listener can then
 * be used to control application specific behavior, such as starting/stopping services, replication, etc.
 * 
 * 
 * @author <NAME>
 *
 */
public class ClusterMember {
	
	private static Logger logger = LoggerFactory.getLogger(ClusterMember.class);

	private static Properties props = new Properties();

	/**
	 * Sets the UDP datagram's payload width (fixed).
	 * This sets a max value of 9999 for property 'priority'
	 */
	private static final int BUFF_SIZE = 4;
	
	/**
	 * The number of milliseconds to wait for a ping from *any* other member in the cluster.
	 */
	private static int timeout_ms = 30000;
	
	/**
	 * The port for UDP. This should be the same on all nodes.
	 */
	private static int port = 8888;
	
	/**<commit_msg>update javadoc<commit_after>package com.boxymoron.ha;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class implements a simple Partition-Tolerant Leader election algorithm (over UDP). The algorithm is essentially a state machine:<br><br>
 * The valid states are: MASTER, SLAVE, UNDEFINED<br><br>
 * <li>Each node in the cluster maintains a static list of all (other) cluster member IP addresses/hostnames.<br>
 * <li>Each node asynchronously sends a message with its own 'priority' (UDP), which is a an integer in the range 0 - 9999 every {@link ClusterMember#timeout_ms}/2 ms.<br>
 * <li>Each node listens to all other node's messages.<br>
 * <li>The node with the highest priority, in a majority partition, becomes the MASTER.<br><br>
 * For this mechanism to work correctly, each node should have a distinct priority. Currently, only odd numbers of nodes >= 3 are supported ;)<br><br>
 * 
 * The API provides a joinCluster({@link ClusterMember.Listener}) method to register a state changed listener. The listener can then
 * be used to control application specific behavior, such as starting/stopping services, replication, etc.
 * <br>
 * <br>
 * Notes/TODO:<br>
 * <li>Look into UDP multicast, so non-cluster members can monitor status?
 * <li>Look into TCP, so network connection issues can be detected more rapidly (at the expense of TCP/network congestion issues).
 * <br>
 * <br>
 * @author <NAME>
 *
 */
public class ClusterMember {
	
	private static Logger logger = LoggerFactory.getLogger(ClusterMember.class);

	private static Properties props = new Properties();

	/**
	 * Sets the UDP datagram's payload width (fixed).
	 * This sets a max value of 9999 for property 'priority'
	 */
	private static final int BUFF_SIZE = 4;
	
	/**
	 * The number of milliseconds to wait for a ping from *any* other member in the cluster.
	 */
	private static int timeout_ms = 30000;
	
	/**
	 * The port for UDP. This should be the same on all nodes.
	 */
	private static int port = 8888;
	
	/**
{'fraction_non_alphanumeric': 0.096780375622844, 'fraction_numerical': 0.008815638175546186, 'mean_word_length': 4.575854700854701, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268103', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1463, 'n_words': 569}