LOG.info(getLeaseHolderId() + " attempting to acquire exclusive lease to become the master"); String sql = getStatements().getLeaseObtainStatement(); LOG.debug(getLeaseHolderId() + " locking Query is "+sql); while (!isStopping()) { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); initTimeDiff(connection); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setLong(3, now); if (keepAlive()) { break; reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + " lease acquire failure: "+ e, e); if (isStopping()) { throw new Exception( "Cannot start broker as being asked to shut down. " close(statement); close(connection);
protected long initTimeDiff(Connection connection) throws SQLException { if (Long.MAX_VALUE == diffFromCurrentTime) { if (maxAllowableDiffFromDBTime > 0) { diffFromCurrentTime = determineTimeDifference(connection); } else { diffFromCurrentTime = 0l; } } return diffFromCurrentTime; }
public void doStop(ServiceStopper stopper) throws Exception { if (lockable.getBrokerService() != null && lockable.getBrokerService().isRestartRequested()) { // keep our lease for restart return; } releaseLease(); }
private void reportLeasOwnerShipAndDuration(Connection connection) throws SQLException { PreparedStatement statement = null; try { statement = connection.prepareStatement(getStatements().getLeaseOwnerStatement()); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { LOG.debug(getLeaseHolderId() + " Lease held by " + resultSet.getString(1) + " till " + new Date(resultSet.getLong(2))); } } finally { close(statement); } }
private void releaseLease() { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); statement = connection.prepareStatement(getStatements().getLeaseUpdateStatement()); statement.setString(1, null); statement.setLong(2, 0l); statement.setString(3, getLeaseHolderId()); if (statement.executeUpdate() == 1) { LOG.info(getLeaseHolderId() + ", released lease"); } } catch (Exception e) { LOG.error(getLeaseHolderId() + " failed to release lease: " + e, e); } finally { close(statement); close(connection); } }
protected long determineTimeDifference(Connection connection) throws SQLException { PreparedStatement statement = connection.prepareStatement(getStatements().getCurrentDateTime()); ResultSet resultSet = statement.executeQuery(); long result = 0l; if (resultSet.next()) { Timestamp timestamp = resultSet.getTimestamp(1); long diff = System.currentTimeMillis() - timestamp.getTime(); if (Math.abs(diff) > maxAllowableDiffFromDBTime) { // off by more than maxAllowableDiffFromDBTime so lets adjust result = (-diff); } LOG.info(getLeaseHolderId() + " diff adjust from db: " + result + ", db time: " + timestamp); } return result; }
@Override public boolean keepAlive() throws IOException { boolean result = false; final String sql = getStatements().getLeaseUpdateStatement(); LOG.debug(getLeaseHolderId() + ", lease keepAlive Query is " + sql); connection = getConnection(); initTimeDiff(connection); statement = connection.prepareStatement(sql); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setString(3, getLeaseHolderId()); reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + ", failed to update lease: " + e, e); IOException ioe = IOExceptionSupport.create(e); lockable.getBrokerService().handleIOException(ioe); throw ioe; } finally { close(statement); close(connection);
@Override public boolean keepAlive() throws IOException { boolean result = false; final String sql = getStatements().getLeaseUpdateStatement(); LOG.debug(getLeaseHolderId() + ", lease keepAlive Query is " + sql); connection = getConnection(); initTimeDiff(connection); statement = connection.prepareStatement(sql); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setString(3, getLeaseHolderId()); reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + ", failed to update lease: " + e, e); IOException ioe = IOExceptionSupport.create(e); lockable.getBrokerService().handleIOException(ioe); throw ioe; } finally { close(statement); close(connection);
private void releaseLease() { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); statement = connection.prepareStatement(getStatements().getLeaseUpdateStatement()); statement.setString(1, null); statement.setLong(2, 0l); statement.setString(3, getLeaseHolderId()); if (statement.executeUpdate() == 1) { LOG.info(getLeaseHolderId() + ", released lease"); } } catch (Exception e) { LOG.error(getLeaseHolderId() + " failed to release lease: " + e, e); } finally { close(statement); close(connection); } }
private void reportLeasOwnerShipAndDuration(Connection connection) throws SQLException { PreparedStatement statement = null; try { statement = connection.prepareStatement(getStatements().getLeaseOwnerStatement()); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { LOG.debug(getLeaseHolderId() + " Lease held by " + resultSet.getString(1) + " till " + new Date(resultSet.getLong(2))); } } finally { close(statement); } }
protected long determineTimeDifference(Connection connection) throws SQLException { PreparedStatement statement = connection.prepareStatement(getStatements().getCurrentDateTime()); ResultSet resultSet = statement.executeQuery(); long result = 0l; if (resultSet.next()) { Timestamp timestamp = resultSet.getTimestamp(1); long diff = System.currentTimeMillis() - timestamp.getTime(); if (Math.abs(diff) > maxAllowableDiffFromDBTime) { // off by more than maxAllowableDiffFromDBTime so lets adjust result = (-diff); } LOG.info(getLeaseHolderId() + " diff adjust from db: " + result + ", db time: " + timestamp); } return result; }
LOG.info(getLeaseHolderId() + " attempting to acquire exclusive lease to become the master"); String sql = getStatements().getLeaseObtainStatement(); LOG.debug(getLeaseHolderId() + " locking Query is "+sql); while (!isStopping()) { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); initTimeDiff(connection); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setLong(3, now); if (keepAlive()) { break; reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + " lease acquire failure: "+ e, e); if (isStopping()) { throw new Exception( "Cannot start broker as being asked to shut down. " close(statement); close(connection);
@Override public boolean keepAlive() throws IOException { boolean result = false; final String sql = getStatements().getLeaseUpdateStatement(); LOG.debug(getLeaseHolderId() + ", lease keepAlive Query is " + sql); connection = getConnection(); initTimeDiff(connection); statement = connection.prepareStatement(sql); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setString(3, getLeaseHolderId()); reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + ", failed to update lease: " + e, e); IOException ioe = IOExceptionSupport.create(e); lockable.getBrokerService().handleIOException(ioe); throw ioe; } finally { close(statement); close(connection);
private void releaseLease() { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); statement = connection.prepareStatement(getStatements().getLeaseUpdateStatement()); statement.setString(1, null); statement.setLong(2, 0l); statement.setString(3, getLeaseHolderId()); if (statement.executeUpdate() == 1) { LOG.info(getLeaseHolderId() + ", released lease"); } } catch (Exception e) { LOG.error(getLeaseHolderId() + " failed to release lease: " + e, e); } finally { close(statement); close(connection); } }
private void reportLeasOwnerShipAndDuration(Connection connection) throws SQLException { PreparedStatement statement = null; try { statement = connection.prepareStatement(getStatements().getLeaseOwnerStatement()); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { LOG.debug(getLeaseHolderId() + " Lease held by " + resultSet.getString(1) + " till " + new Date(resultSet.getLong(2))); } } finally { close(statement); } }
protected long determineTimeDifference(Connection connection) throws SQLException { PreparedStatement statement = connection.prepareStatement(getStatements().getCurrentDateTime()); ResultSet resultSet = statement.executeQuery(); long result = 0l; if (resultSet.next()) { Timestamp timestamp = resultSet.getTimestamp(1); long diff = System.currentTimeMillis() - timestamp.getTime(); if (Math.abs(diff) > maxAllowableDiffFromDBTime) { // off by more than maxAllowableDiffFromDBTime so lets adjust result = (-diff); } LOG.info(getLeaseHolderId() + " diff adjust from db: " + result + ", db time: " + timestamp); } return result; }
protected long initTimeDiff(Connection connection) throws SQLException { if (Long.MAX_VALUE == diffFromCurrentTime) { if (maxAllowableDiffFromDBTime > 0) { diffFromCurrentTime = determineTimeDifference(connection); } else { diffFromCurrentTime = 0l; } } return diffFromCurrentTime; }
public void doStop(ServiceStopper stopper) throws Exception { if (lockable.getBrokerService() != null && lockable.getBrokerService().isRestartRequested()) { // keep our lease for restart return; } releaseLease(); }
LOG.info(getLeaseHolderId() + " attempting to acquire exclusive lease to become the master"); String sql = getStatements().getLeaseObtainStatement(); LOG.debug(getLeaseHolderId() + " locking Query is "+sql); while (!isStopping()) { Connection connection = null; PreparedStatement statement = null; try { connection = getConnection(); initTimeDiff(connection); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setLong(3, now); if (keepAlive()) { break; reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + " lease acquire failure: "+ e, e); if (isStopping()) { throw new Exception( "Cannot start broker as being asked to shut down. " close(statement); close(connection);
@Override public boolean keepAlive() throws IOException { boolean result = false; final String sql = getStatements().getLeaseUpdateStatement(); LOG.debug(getLeaseHolderId() + ", lease keepAlive Query is " + sql); connection = getConnection(); initTimeDiff(connection); statement = connection.prepareStatement(sql); setQueryTimeout(statement); statement.setString(1, getLeaseHolderId()); statement.setLong(2, now + lockAcquireSleepInterval); statement.setString(3, getLeaseHolderId()); reportLeasOwnerShipAndDuration(connection); LOG.warn(getLeaseHolderId() + ", failed to update lease: " + e, e); IOException ioe = IOExceptionSupport.create(e); lockable.getBrokerService().handleIOException(ioe); throw ioe; } finally { close(statement); close(connection);