All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cubrid.jdbc.jci.BrokerHealthCheck Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution. 
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met: 
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer. 
 *
 * - Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution. 
 *
 * - Neither the name of the  nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software without 
 *   specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE. 
 *
 */

package cubrid.jdbc.jci;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import cubrid.jdbc.driver.CUBRIDDriver;
import cubrid.jdbc.net.BrokerHandler;

public class BrokerHealthCheck extends Thread{

	private static final String HEALTH_CHECK_DUMMY_DB = "___health_check_dummy_db___";
	private static final int BROKER_HEALTH_CHECK_TIMEOUT = 5000;
	private static final int CAS_INFO_SIZE = 4;
	public static final int MONITORING_INTERVAL = 60000;

	public void run() {
		String ip;
		int port;
		long startTime, elapseTime;
		
		while (true) {
			startTime = System.currentTimeMillis();
			
			if (CUBRIDDriver.unreachableHosts == null) {
				return;
			}
			if (CUBRIDDriver.unreachableHosts.size() > 0) {
				for (String host : CUBRIDDriver.unreachableHosts) {
					ip = host.split(":")[0];
					port = Integer.parseInt(host.split(":")[1]); 
					try {
						checkBrokerAlive(ip, port, BROKER_HEALTH_CHECK_TIMEOUT);
						CUBRIDDriver.unreachableHosts.remove(host);
					} catch (UJciException e) {
						// do nothing
					} catch (IOException e) {
						// do nothing
					}
				}
			}
			elapseTime = System.currentTimeMillis() - startTime;
			
			if (elapseTime < MONITORING_INTERVAL) {
				try {
					Thread.sleep(MONITORING_INTERVAL - elapseTime);
				} catch (InterruptedException e) {
					// do nothing
				}
			}
		}
	}
	
	private void checkBrokerAlive(String ip, int port, int timeout) throws IOException, UJciException {
		Socket toBroker = null;
		byte[] serverInfo;
		byte[] casInfo;
		String dummyUrl = "jdbc:cubrid:" + ip + ":" + port + ":" + HEALTH_CHECK_DUMMY_DB + "::********:";
		UTimedDataInputStream is = null;
		DataOutputStream os = null;
		
		long startTime = System.currentTimeMillis();
		
		try {
			toBroker = BrokerHandler.connectBroker(ip, port, timeout);
			if (timeout > 0) {
				timeout -= (System.currentTimeMillis() - startTime);
				if (timeout <= 0) {
					throw new UJciException(UErrorCode.ER_TIMEOUT);
				}
			}
			
			is = new UTimedDataInputStream(toBroker.getInputStream(), ip, port, timeout);
			os = new DataOutputStream(toBroker.getOutputStream());
			serverInfo = UConnection.createDBInfo(HEALTH_CHECK_DUMMY_DB, "", "", dummyUrl);
			
			// send db info
			os.write(serverInfo);
			os.flush();
			
			// receive header
			int dataLength = is.readInt();
			casInfo = new byte[CAS_INFO_SIZE];
			is.readFully(casInfo);
			if (dataLength < 0) {
				throw new UJciException(UErrorCode.ER_ILLEGAL_DATA_SIZE);
			}
			
		} finally {
			if (is != null) is.close();
			if (os != null) os.close();
			if (toBroker != null) toBroker.close();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy