<commit_before><?php


require_once('pudlMySqli.php');



class pudlGalera extends pudlMySqli {
	use pudlMySqlHelper;


	public function __construct($data, $autoconnect=true) {
		if (!is_array($data['server'])) {
			throw new pudlException(
				'Not a valid server pool, $data[server] must be ARRAY data type'
			);
		}

		//SET INITIAL VALUES
		$this->pool = $this->onlineServers($data['server']);

		//RANDOMIZE SERVER POOL ORDER
		//IF REMOTE_ADDR AVAILABLE, USE IT TO HASH ROUTE TO SAME NODE EACH TIME
		if (!empty($_SERVER['REMOTE_ADDR'])) {
			srand( crc32($_SERVER['REMOTE_ADDR']) );
			shuffle($this->pool);
			srand();
		} else {
			shuffle($this->pool);
		}

		//CONNECT TO THE SERVER CLUSTER
		parent::__construct($data, $autoconnect);
	}



	public static function instance($data, $autoconnect=true) {
		return new pudlGalera($data, $autoconnect);
	}



	public function connect() {
		$auth = $this->auth();

		foreach ($this->pool as $server) {
			$this->mysqli = mysqli_init();

			//SET CONNECTION TIMEOUT TO 1 SECOND IF WE'RE IN A CLSUTER, ELSE 10 SECONDS
			$this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, count($this->pool)>1 ? 1 : 10);

			//ATTEMPT TO CREATE A PERSISTANT CONNECTION
			$ok = @$this->mysqli->real_connect(
				"p:$server",
				$auth['username'],
				$auth['password'],
				$auth['database']
			);

			//ATTEMPT TO CREATE A NON-PERSISTANT CONNECTION
			if (empty($ok)) {
				$ok = @$this->mysqli->real_connect(
					$server,
					$auth['username'],
					$auth['password'],
					$auth['database']
				);
			}

			//ATTEMPT TO SET UTF-8 CHARACTER SET
			//WE'RE GOOD, RETURN A GOOD RESULT!
			if ($ok  &&  @$this->mysqli->set_charset('utf8')) {
				$this->connected = $server;
				return true;
			}

			//OKAY, MAYBE WE'RE NOT
			$this->offlineServer($server);
		}

		//CANNOT CONNECT - ERROR OUT
		$error  = "<br />\n";
		$error .= 'Unable to connect to galera cluster "';
		$error .= implode(', ', $this->pool);
		$error .= '" with the username: "' . $auth['username'];
		$error .= "\"<br />\nError " . $this->connectErrno() . ': ' . $this->connectError();
		if (self::$die) die($error);
		return false;
	}



	public function reconnect() {
		if (empty($this->pool)) return false;
<commit_msg>Prevent connections to donor nodes<commit_after><?php


require_once('pudlMySqli.php');



class pudlGalera extends pudlMySqli {
	use pudlMySqlHelper;


	public function __construct($data, $autoconnect=true) {
		if (!is_array($data['server'])) {
			throw new pudlException(
				'Not a valid server pool, $data[server] must be ARRAY data type'
			);
		}

		//SET INITIAL VALUES
		$this->pool = $this->onlineServers($data['server']);

		//RANDOMIZE SERVER POOL ORDER
		//IF REMOTE_ADDR AVAILABLE, USE IT TO HASH ROUTE TO SAME NODE EACH TIME
		if (!empty($_SERVER['REMOTE_ADDR'])) {
			srand( crc32($_SERVER['REMOTE_ADDR']) );
			shuffle($this->pool);
			srand();
		} else {
			shuffle($this->pool);
		}

		//CONNECT TO THE SERVER CLUSTER
		parent::__construct($data, $autoconnect);
	}



	public static function instance($data, $autoconnect=true) {
		return new pudlGalera($data, $autoconnect);
	}



	public function connect() {
		$auth = $this->auth();

		foreach ($this->pool as $server) {
			$this->mysqli = mysqli_init();

			//SET CONNECTION TIMEOUT TO 1 SECOND IF WE'RE IN A CLSUTER, ELSE 10 SECONDS
			$this->mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, count($this->pool)>1 ? 1 : 10);

			//ATTEMPT TO CREATE A PERSISTANT CONNECTION
			$ok = @$this->mysqli->real_connect(
				"p:$server",
				$auth['username'],
				$auth['password'],
				$auth['database']
			);

			//ATTEMPT TO CREATE A NON-PERSISTANT CONNECTION
			if (empty($ok)) {
				$ok = @$this->mysqli->real_connect(
					$server,
					$auth['username'],
					$auth['password'],
					$auth['database']
				);
			}

			//ATTEMPT TO SET UTF-8 CHARACTER SET
			if ($ok) $ok = @$this->mysqli->set_charset('utf8');

			//ATTEMPT TO GET THE CLUSTER SYNC STATE OF THIS NODE
			$status = $ok ? $this->globals('wsrep_local_state') : [];
			if (empty($status['wsrep_local_state'])) $status['wsrep_local_state'] = 0;

			/* NOTE: HERE ARE THE NUMERICAL STATE VALUES
			1 - Joining (requesting/receiving State Transfer) - node is joining the cluster
			2 - Donor/Desynced - node is the donor to the node joining the cluster
			3 - Joined - node has joined the cluster
			4 - Synced - node is synced with the cluster */

			//ONLY CONNECT IF NODE IS IN A 'JOINED' OR 'SYNCED' STATE
			if ( ((int)$status['wsrep_local_state']) > 2) {
				$this->connected = $server;
				return true;
			}

			//OKAY, MAYBE WE'RE NOT
			$this->offlineServer($server);
		}

		//CANNOT CONNECT - ERROR OUT
		$error  = "<br />\n";
		$error .= 'Unable to connect to galera cluster "';
		$error .= implode(', ', $this->pool);
		$error .= '" with the username: "' . $auth['username'];
		$error .= "\"<br />\nError " . $this->connectErrno() . ': ' . $this->connectError();
		if (self::$die) die($error);
		return false;
	}



	public function reconnect() {
		if (empty($this->pool)) return false;

{'fraction_non_alphanumeric': 0.1666007123070835, 'fraction_numerical': 0.00554016620498615, 'mean_word_length': 3.3994778067885116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, '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': '3165447', 'n_tokens_mistral': 2153, 'n_tokens_neox': 1777, 'n_words': 460}