/** * Adds command to the command queue. Does not block. Called by the command object. * * @param ccmd non-null object containing the command to be committed. */ public void add(CoreCmd ccmd) throws GondolaException { // Commits are not allowed in slave mode if (cmember.masterId >= 0) { throw new GondolaException(GondolaException.Code.SLAVE_MODE, cmember.memberId); } commandQueue.add(ccmd); }
} catch (GondolaException e) { logger.info("[{}-{}] Error while get gondola command, appliedIndex={}, error={}", hostId, memberId, appliedIndex, e.getMessage()); try { Thread.sleep(1000);
double random(int index) throws GondolaException { double r = Math.random(); if (enabled && r < .001) { throw new GondolaException(GondolaException.Code.ERROR, "Nasty exception for index=" + index); } return r; }
@Override public void delete(int memberId, int index) throws GondolaException { try { String sql = "DELETE FROM logs WHERE memberId=? AND index=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, index); preparedStatement.execute(); } catch (SQLException e) { throw new GondolaException(e); } }
@Override public String getAddress(int memberId) throws GondolaException { String sql = "SELECT address FROM MemberInfo WHERE memberId=" + memberId; try { Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getString("address"); } } catch (SQLException e) { throw new GondolaException(e); } return null; }
@Override public int getVotedFor(int memberId) throws GondolaException { try { String sql = "SELECT votedFor FROM MemberInfo WHERE memberId=" + memberId; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getInt("votedFor"); } } catch (SQLException e) { throw new GondolaException(e); } return -1; }
@Override public int getCurrentTerm(int memberId) throws GondolaException { try { String sql = "SELECT term FROM MemberInfo WHERE memberId=" + memberId; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getInt("term"); } } catch (SQLException e) { throw new GondolaException(e); } return 1; }
@Override public int getMaxGap(int memberId) throws GondolaException { try { String sql = "SELECT maxGap FROM MemberInfo WHERE memberId=" + memberId; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getInt("maxGap"); } } catch (SQLException e) { throw new GondolaException(e); } return 0; }
@Override public String getPid(int memberId) throws GondolaException { try { String sql = "SELECT pid FROM MemberInfo WHERE memberId=" + memberId; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getString("pid"); } } catch (SQLException e) { throw new GondolaException(e); } return null; }
@Override public int count(int memberId) throws GondolaException { try { String sql = "SELECT count(*) FROM logs WHERE memberId=" + memberId; Statement statement = c.createStatement(); ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { return resultSet.getInt(1); } } catch (SQLException e) { throw new GondolaException(e); } return 0; }
@Override public boolean hasLogEntry(int memberId, int term, int index) throws GondolaException { try { String sql = "SELECT 1 FROM logs WHERE memberId=? AND term=? AND index=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, term); preparedStatement.setInt(3, index); ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.next(); } catch (SQLException e) { throw new GondolaException(e); } }
@Override public void setAddress(int memberId, String address) throws GondolaException { try { String sql = "MERGE INTO MemberInfo(memberId,address) KEY(memberId) VALUES(?,?)"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setString(2, address); int i = preparedStatement.executeUpdate(); if (i != 1) { throw new SQLException("setAddress() failed"); } } catch (SQLException e) { throw new GondolaException(e); } }
@Override public void setPid(int memberId, String pid) throws GondolaException { try { String sql = "MERGE INTO MemberInfo(memberId,pid) KEY(memberId) VALUES(?,?)"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setString(2, pid); int i = preparedStatement.executeUpdate(); if (i != 1) { throw new SQLException("setPid() failed"); } } catch (SQLException e) { throw new GondolaException(e); } }
@Override public void setMaxGap(int memberId, int maxGap) throws GondolaException { try { String sql = "MERGE INTO MemberInfo(memberId,maxGap) KEY(memberId) VALUES(?,?)"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, maxGap); int i = preparedStatement.executeUpdate(); if (i != 1) { throw new SQLException("setMaxGap() failed"); } } catch (SQLException e) { throw new GondolaException(e); } }
public void execute(Runnable runnable) throws GondolaException, InterruptedException { FutureTask<Void> task = new FutureTask<>(runnable, null); queue.add(new Action(task)); try { task.get(); } catch (ExecutionException e) { throw new GondolaException(e); } } }
@Override public void delete(int memberId, int index) throws GondolaException { try (Connection c = ds.getConnection()) { String sql = "DELETE FROM logs WHERE member_id=? AND indx=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, index); preparedStatement.execute(); } catch (SQLException e) { throw new GondolaException(e); } }
@Override public void saveVote(int memberId, int currentTerm, int votedFor) throws GondolaException { try { String sql = "MERGE INTO MemberInfo(memberId,term,votedFor) KEY(memberId) VALUES(?,?,?)"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, currentTerm); preparedStatement.setInt(3, votedFor); int i = preparedStatement.executeUpdate(); if (i < 1) { throw new SQLException(String.format("save(memberId=%d, currentTerm=%d, votedFor=%d) failed", memberId, currentTerm, votedFor)); } } catch (SQLException e) { throw new GondolaException(e); } }
@Override public boolean hasLogEntry(int memberId, int term, int index) throws GondolaException { try (Connection c = ds.getConnection()) { String sql = "SELECT 1 FROM logs WHERE member_id=? AND term=? AND indx=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); preparedStatement.setInt(2, term); preparedStatement.setInt(3, index); ResultSet resultSet = preparedStatement.executeQuery(); return resultSet.next(); } catch (SQLException e) { throw new GondolaException(e); } }
@Override public String getPid(int memberId) throws GondolaException { try (Connection c = ds.getConnection()) { String sql = "SELECT pid FROM member_info WHERE member_id=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getString("pid"); } return null; } catch (SQLException e) { throw new GondolaException(e); } }
@Override public int count(int memberId) throws GondolaException { try (Connection c = ds.getConnection()) { String sql = "SELECT count(*) FROM logs WHERE member_id=?"; PreparedStatement preparedStatement = c.prepareStatement(sql); preparedStatement.setInt(1, memberId); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getInt(1); } return 0; } catch (SQLException e) { throw new GondolaException(e); } }