public Connection connect(String url, Properties info) throws SQLException { if (acceptsURL(url)) { String fileName = getFileNameFromUrl(url); try { Configuration configuration = configLoader.load(fileName); List<String> drivers = configuration.getDrivers(); for (String classname : drivers) { Class<? extends Driver> driverClass = (Class<? extends Driver>) Class.forName(classname); Driver driver = driverClass.newInstance(); java.sql.DriverManager.registerDriver(driver); logger.debug("Registering driver " + classname); } List<ConnectionInfo> connections = configuration.getConnections(); ConnectionsHolder connectionsHolder = new ConnectionsHolder(); for (ConnectionInfo connectionInfo : connections) { Properties properties = new Properties(info); properties.setProperty("user", connectionInfo.getUser()); properties.setProperty("password", connectionInfo.getPassword()); Connection connection = DriverManager.getConnection(connectionInfo.getUrl(), properties); connectionsHolder.add(connectionInfo.getName(), connection); } return new ShardsConnection(connectionsHolder, configuration); } catch (Exception e) { throw new SQLException("Cannot load configuration file", e); } } else { return null; } }
private boolean isMasterAddressChanged(ConnectionInfo currentMasterConnectionInfo) { if (previousMasterConnectionInfo == null) { return true; } return !previousMasterConnectionInfo .getHostAndPort() .equals(currentMasterConnectionInfo.getHostAndPort()); }
@Override protected ConnectionQueryServices getConnectionQueryServices(String url, Properties info) throws SQLException { try { closeLock.readLock().lock(); checkClosed(); ConnectionInfo connInfo = ConnectionInfo.create(url); QueryServices services = getQueryServices(); ConnectionInfo normalizedConnInfo = connInfo.normalize(services.getProps()); ConnectionQueryServices connectionQueryServices = connectionQueryServicesMap.get(normalizedConnInfo); if (connectionQueryServices == null) { if (normalizedConnInfo.isConnectionless()) { connectionQueryServices = new ConnectionlessQueryServicesImpl(services, normalizedConnInfo); } else { connectionQueryServices = new ConnectionQueryServicesImpl(services, normalizedConnInfo, info); } ConnectionQueryServices prevValue = connectionQueryServicesMap.putIfAbsent(normalizedConnInfo, connectionQueryServices); if (prevValue != null) { connectionQueryServices = prevValue; } } boolean success = false; SQLException sqlE = null; try { connectionQueryServices.init(url, info); success = true; } catch (SQLException e) { sqlE = e; } finally { if (!success) { try { connectionQueryServices.close(); } catch (SQLException e) { if (sqlE == null) { sqlE = e; } else { sqlE.setNextException(e); } } finally { // Remove from map, as initialization failed connectionQueryServicesMap.remove(normalizedConnInfo); if (sqlE != null) { throw sqlE; } } } } return connectionQueryServices; } finally { closeLock.readLock().unlock(); } }
@Override protected Connection createConnection() { if (connectionInfoName == null) throw new PropertyNotSetException("connectionInfoName"); Map cim = AppData.getConnections().get(connectionInfoName); if (cim == null) throw new RuntimeException("connection " + connectionInfoName + " not found"); ConnectionInfo ci = new ConnectionInfo(cim); return ci.createConnection(); }
/** * Removes all ConnectionInfos from tracking where for the key & passed in Connection * * @param key the connection key * @param con the connection to remove */ private static synchronized void remove(String key, Connection con) { final Collection<ConnectionInfo> conInfos = getConnectionInfos(key); for (Iterator<ConnectionInfo> i = conInfos.iterator(); i.hasNext(); ) { final ConnectionInfo conInfo = i.next(); if (conInfo.getConnection() == con) { i.remove(); } } OPEN_CONNECTIONS.get().put(key, conInfos); }
/** Ping the jedis instance, return true if the result is PONG. */ private boolean ping(ConnectionInfo connectionInfo) { Jedis jedis = new Jedis(connectionInfo.getHost(), connectionInfo.getPort()); try { String result = jedis.ping(); return (result != null) && result.equals("PONG"); } catch (JedisException e) { return false; } finally { JedisUtils.closeJedis(jedis); } }
/** Pickup the first available sentinel, if all sentinel down, return false. */ private boolean selectSentinel() { for (ConnectionInfo info : sentinelInfos) { if (ping(info)) { sentinelInfo = info; sentinelJedis = new Jedis(sentinelInfo.getHost(), sentinelInfo.getPort()); return true; } } return false; }
private Session createSessionAndValidate(ConnectionInfo ci) { try { boolean ifExists = ci.getProperty("IFEXISTS", false); Session session; for (int i = 0; ; i++) { session = createSession(ci, ifExists); if (session != null) { break; } // we found a database that is currently closing // wait a bit to avoid a busy loop (the method is synchronized) if (i > 60 * 1000) { // retry at most 1 minute throw DbException.get( ErrorCode.DATABASE_ALREADY_OPEN_1, "Waited for database closing longer than 1 minute"); } try { Thread.sleep(1); } catch (InterruptedException e) { // ignore } } initSession(session, ci); validateUserAndPassword(true); return session; } catch (DbException e) { if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) { validateUserAndPassword(false); } throw e; } }
@Override public void onConnectClick(ConnectionInfo ci) { OPDSBrowserFragment frag = ((OPDSBrowserFragment) tab_opds.getFragment()); if (mCalibreService != null) { mCalibreService.connectToServer(ci); } if (frag != null) { frag.loadURL(ci.getContentServerURL(), false); } }
@Override public void onMessage(String channel, String message) { // message example: +switch-master: mymaster 127.0.0.1 6379 127.0.0.1 6380 // +redirect-to-master mymaster 127.0.0.1 6380 127.0.0.1 6381 (if slave-master fail-over // quick enough) logger.info("Sentinel " + sentinelInfo.getHostAndPort() + " published: " + message); String[] switchMasterMsg = message.split(" "); // if the switeched master name equals my master name, destroy the old pool and init a new // pool if (masterName.equals(switchMasterMsg[0])) { destroyInternalPool(); ConnectionInfo masterConnectionInfo = buildMasterConnectionInfo(switchMasterMsg[3], switchMasterMsg[4]); logger.info("Switch master to " + masterConnectionInfo.getHostAndPort()); initInternalPool(masterConnectionInfo); previousMasterConnectionInfo = masterConnectionInfo; } }
private void sendOKAndDataToRequest(ConnectionInfo info, int id) { ByteBuffer buf = ByteBuffer.allocate(Byte.SIZE + Integer.SIZE + Integer.SIZE + Integer.SIZE); buf.put(RESPONCECODES.OK); buf.putInt(info.udpConnection.socket().getLocalPort()); buf.putInt(id); buf.putInt(info.sharedSecret); buf.flip(); try { info.tcpConnection.write(buf); } catch (IOException e) { info.closeOpenSockets(); } }
@Override protected ConnectionQueryServices getConnectionQueryServices(String url, Properties info) throws SQLException { ConnectionInfo connInfo = ConnectionInfo.create(url); ConnectionInfo normalizedConnInfo = connInfo.normalize(getQueryServices().getProps()); ConnectionQueryServices connectionQueryServices = connectionQueryServicesMap.get(normalizedConnInfo); if (connectionQueryServices == null) { if (normalizedConnInfo.isConnectionless()) { connectionQueryServices = new ConnectionlessQueryServicesImpl(getQueryServices()); } else { connectionQueryServices = new ConnectionQueryServicesImpl(getQueryServices(), normalizedConnInfo); } connectionQueryServices.init(url, info); ConnectionQueryServices prevValue = connectionQueryServicesMap.putIfAbsent(normalizedConnInfo, connectionQueryServices); if (prevValue != null) { connectionQueryServices = prevValue; } } return connectionQueryServices; }
/** * logs a message related to multiple connections being open. * * @param key the connection key */ private static synchronized void logMultipleConnections(String key) { final Collection<ConnectionInfo> conInfos = OPEN_CONNECTIONS.get().get(key); if (conInfos != null && !conInfos.isEmpty()) { final StringBuilder stacks = new StringBuilder(); for (ConnectionInfo conInfo : conInfos) { stacks.append(conInfo.getEstablishedStack()); stacks.append("\n\n"); } final StringBuilder msg = new StringBuilder(); msg.append("There are "); msg.append(conInfos.size()); msg.append(" connection(s) on this thread "); msg.append(Thread.currentThread().getName()); msg.append(" to "); msg.append(key); msg.append(" that have not been closed."); msg.append(" The following stacktraces show where these connections were established: \n"); msg.append(stacks); LOG.debug(msg); } }
private void initSession(Session session, ConnectionInfo ci) { boolean ignoreUnknownSetting = ci.getProperty("IGNORE_UNKNOWN_SETTINGS", false); String init = ci.getProperty("INIT", null); session.setAllowLiterals(true); CommandInterface command; for (String setting : ci.getKeys()) { if (SetTypes.contains(setting)) { String value = ci.getProperty(setting); try { command = session.prepareCommandLocal( "SET " + session.getDatabase().quoteIdentifier(setting) + " " + value); command.executeUpdate(); } catch (DbException e) { if (!ignoreUnknownSetting) { session.close(); throw e; } } } } if (init != null) { try { command = session.prepareCommand(init, Integer.MAX_VALUE); command.executeUpdate(); } catch (DbException e) { if (!ignoreUnknownSetting) { session.close(); throw e; } } } session.setAllowLiterals(false); session.commit(true); }
public JedisPool(ConnectionInfo connectionInfo, JedisPoolConfig config) { this.hostAndPort = connectionInfo.getHostAndPort(); JedisFactory factory = new JedisFactory( connectionInfo.getHost(), connectionInfo.getPort(), connectionInfo.getTimeout(), connectionInfo.getPassword(), connectionInfo.getDatabase(), connectionInfo.getClientName()); internalPool = new GenericObjectPool(factory, config); }
private void initInternalPool(ConnectionInfo masterConnectionInfo) { JedisFactory factory = new JedisFactory( masterConnectionInfo.getHost(), masterConnectionInfo.getPort(), masterConnectionInfo.getTimeout(), masterConnectionInfo.getPassword(), masterConnectionInfo.getDatabase(), masterConnectionInfo.getClientName()); internalPool = new GenericObjectPool(factory, masterPoolConfig); }
@Override public void run() { while (running.get()) { try { boolean avalibleSentinelExist = selectSentinel(); if (avalibleSentinelExist) { try { ConnectionInfo masterConnectionInfo = queryMasterAddress(); if ((internalPool != null) && isMasterAddressChanged(masterConnectionInfo)) { logger.info( "The internalPool {} had changed, destroy it now.", previousMasterConnectionInfo.getHostAndPort()); destroyInternalPool(); } if ((internalPool == null) || isMasterAddressChanged(masterConnectionInfo)) { logger.info( "The internalPool {} is not init or the address had changed, init it now.", masterConnectionInfo.getHostAndPort()); initInternalPool(masterConnectionInfo); } previousMasterConnectionInfo = masterConnectionInfo; // blocking listen master switch message until exception happen. subscriber = new MasterSwitchSubscriber(); sentinelJedis.subscribe(subscriber, "+switch-master", "+redirect-to-master"); } catch (JedisConnectionException e) { JedisUtils.closeJedis(sentinelJedis); if (running.get()) { logger.error( "Lost connection with Sentinel " + sentinelInfo.getHostAndPort() + ", sleep 1000ms and try to connect another one."); sleep(RETRY_WAIT_TIME_MILLS); } } catch (Exception e) { JedisUtils.closeJedis(sentinelJedis); if (running.get()) { logger.error( "Unexpected Exception happen, current Sentinel is" + sentinelInfo.getHostAndPort() + ", sleep 1000ms and try again.", e); sleep(RETRY_WAIT_TIME_MILLS); } } } else { logger.info("All sentinels down, sleep 1000ms and try to select again."); // when the system startup but the sentinels not yet, init an ugly address to prevent // null point // exception. if (internalPool == null) { ConnectionInfo masterConnectionInfo = new ConnectionInfo(UNAVAILABLE_MASTER_ADDRESS); initInternalPool(masterConnectionInfo); previousMasterConnectionInfo = masterConnectionInfo; } sleep(RETRY_WAIT_TIME_MILLS); } } catch (Exception e) { logger.error("Unexpected exception happen", e); sleep(RETRY_WAIT_TIME_MILLS); } } }
protected void sendErrorCodeToRequestAndClose(byte code, ConnectionInfo info) { sendErrorCodeToRequest(code, info.tcpConnection); info.closeOpenSockets(); }
@Override protected ConnectionQueryServices getConnectionQueryServices(String url, Properties info) throws SQLException { ConnectionInfo connInfo = getConnectionInfo(url); String zookeeperQuorum = connInfo.getZookeeperQuorum(); Integer port = connInfo.getPort(); String rootNode = connInfo.getRootNode(); boolean isConnectionless = false; // Normalize connInfo so that a url explicitly specifying versus implicitly inheriting // the default values will both share the same ConnectionQueryServices. Configuration globalConfig = getQueryServices().getConfig(); if (zookeeperQuorum == null) { zookeeperQuorum = globalConfig.get(ZOOKEEPER_QUARUM_ATTRIB); if (zookeeperQuorum == null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL) .setMessage(url) .build() .buildException(); } } isConnectionless = PhoenixRuntime.CONNECTIONLESS.equals(zookeeperQuorum); if (port == null) { if (!isConnectionless) { String portStr = globalConfig.get(ZOOKEEPER_PORT_ATTRIB); if (portStr != null) { try { port = Integer.parseInt(portStr); } catch (NumberFormatException e) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL) .setMessage(url) .build() .buildException(); } } } } else if (isConnectionless) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL) .setMessage("Port may not be specified when using the connectionless url \"" + url + "\"") .build() .buildException(); } if (rootNode == null) { if (!isConnectionless) { rootNode = globalConfig.get(ZOOKEEPER_ROOT_NODE_ATTRIB); } } else if (isConnectionless) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL) .setMessage( "Root node may not be specified when using the connectionless url \"" + url + "\"") .build() .buildException(); } ConnectionInfo normalizedConnInfo = new ConnectionInfo(zookeeperQuorum, port, rootNode); ConnectionQueryServices connectionQueryServices = connectionQueryServicesMap.get(normalizedConnInfo); if (connectionQueryServices == null) { if (isConnectionless) { connectionQueryServices = new ConnectionlessQueryServicesImpl(getQueryServices()); } else { Configuration childConfig = HBaseConfiguration.create(globalConfig); if (connInfo.getZookeeperQuorum() != null) { childConfig.set(ZOOKEEPER_QUARUM_ATTRIB, connInfo.getZookeeperQuorum()); } else if (childConfig.get(ZOOKEEPER_QUARUM_ATTRIB) == null) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.MALFORMED_CONNECTION_URL) .setMessage(url) .build() .buildException(); } if (connInfo.getPort() != null) { childConfig.setInt(ZOOKEEPER_PORT_ATTRIB, connInfo.getPort()); } if (connInfo.getRootNode() != null) { childConfig.set(ZOOKEEPER_ROOT_NODE_ATTRIB, connInfo.getRootNode()); } connectionQueryServices = new ConnectionQueryServicesImpl(getQueryServices(), childConfig); } connectionQueryServices.init(url, info); ConnectionQueryServices prevValue = connectionQueryServicesMap.putIfAbsent(normalizedConnInfo, connectionQueryServices); if (prevValue != null) { connectionQueryServices = prevValue; } } return connectionQueryServices; }
private Session createSession(ConnectionInfo ci, boolean ifExists) { String name = ci.getDatabaseName(); name = Database.parseDatabaseShortName(ci.getDbSettings(), name); Database database; boolean openNew = ci.getProperty("OPEN_NEW", false); if (openNew) { database = null; } else { database = DATABASES.get(name); } User user = null; boolean opened = false; if (database == null) { if (ifExists && !SystemDatabase.exists(name)) { throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, name); } database = createDatabase(ci.isPersistent()); database.init(ci, name); opened = true; if (database.getAllUsers().isEmpty()) { // users is the last thing we add, so if no user is around, // the database is new (or not initialized correctly) user = new User(database, database.allocateObjectId(), ci.getUserName(), false); user.setAdmin(true); user.setUserPasswordHash(ci.getUserPasswordHash()); database.setMasterUser(user); } DATABASES.put(name, database); if (database.isPersistent()) SystemDatabase.addDatabase(database.getShortName(), database.getStorageEngineName()); } else { if (!database.isInitialized()) database.init(ci, name); } synchronized (database) { if (opened) { // start the thread when already synchronizing on the database // otherwise a deadlock can occur when the writer thread // opens a new database (as in recovery testing) database.opened(); } if (database.isClosing()) { return null; } if (user == null) { if (database.validateFilePasswordHash( ci.getProperty("CIPHER", null), ci.getFilePasswordHash())) { user = database.findUser(ci.getUserName()); if (user != null) { if (!user.validateUserPasswordHash(ci.getUserPasswordHash())) { user = null; } } } if (opened && (user == null || !user.isAdmin())) { // reset - because the user is not an admin, and has no // right to listen to exceptions database.setEventListener(null); } } if (user == null) { database.removeSession(null); throw DbException.get(ErrorCode.WRONG_USER_OR_PASSWORD); } Session session = database.createSession(user); session.setConnectionInfo(ci); return session; } }