/** * Builds a new {@link TokenSchema} from builder values. * * @return TokenSchema from this builder */ public TokenSchema build() { return new TokenSchema(this); } }
/** * Initializes the default TokenSchema */ public TokenSchema() { this(builder()); }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + " BLOB NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); } }
/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying * storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * If the given {@code resultSet} has no items this method should insert a new token entry. If a token already * exists it should be attempted to replace the token in the entry with the given {@code token} and claim ownership. * * @param resultSet the updatable query result set of an executed {@link PreparedStatement} * @param token the token for the new or updated entry * @param processorName the name of the processor owning the token * @param segment the segment of the processor owning the token * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while updating the result set */ protected void insertOrUpdateToken(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { if (resultSet.next()) { AbstractTokenEntry<?> entry = readTokenEntry(resultSet); entry.updateToken(token, serializer); resultSet.updateObject(schema.tokenColumn(), entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); claimToken(resultSet, entry); } else { insertTokenEntry(resultSet, token, processorName, segment); } }
/** * Tries to claim the given token {@code entry}. If the claim fails an {@link UnableToClaimTokenException} should be * thrown. Otherwise the given {@code resultSet} should be updated to reflect the claim. * * @param resultSet the updatable query result of an executed {@link PreparedStatement} * @param entry the entry extracted from the given result set * @return the claimed tracking token * * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while claiming the token entry */ protected TrackingToken claimToken(ResultSet resultSet, AbstractTokenEntry<?> entry) throws SQLException { if (!entry.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", entry.getProcessorName(), entry.getSegment(), entry.getOwner())); } resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateRow(); return entry.getToken(serializer); }
@Override public int[] fetchSegments(String processorName) { Connection connection = getConnection(); try { List<Integer> integers = executeQuery(connection, c -> selectForSegments(c, processorName), listResults(rs -> rs.getInt(schema.segmentColumn())), e -> new JdbcException(format( "Could not load segments for processor [%s]", processorName ), e) ); return integers.stream().mapToInt(i -> i).toArray(); } finally { closeQuietly(connection); } }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * If the given {@code resultSet} has no items this method should insert a new token entry. If a token already * exists it should be attempted to replace the token in the entry with the given {@code token} and claim ownership. * * @param resultSet the updatable query result set of an executed {@link PreparedStatement} * @param token the token for the new or updated entry * @param processorName the name of the processor owning the token * @param segment the segment of the processor owning the token * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while updating the result set */ protected void insertOrUpdateToken(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { if (resultSet.next()) { AbstractTokenEntry<?> entry = readTokenEntry(resultSet); entry.updateToken(token, serializer); resultSet.updateObject(schema.tokenColumn(), entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); claimToken(resultSet, entry); } else { insertTokenEntry(resultSet, token, processorName, segment); } }
/** * Tries to claim the given token {@code entry}. If the claim fails an {@link UnableToClaimTokenException} should be * thrown. Otherwise the given {@code resultSet} should be updated to reflect the claim. * * @param resultSet the updatable query result of an executed {@link PreparedStatement} * @param entry the entry extracted from the given result set * @return the claimed tracking token * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while claiming the token entry */ protected TrackingToken claimToken(ResultSet resultSet, AbstractTokenEntry<?> entry) throws SQLException { if (!entry.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", entry.getProcessorName(), entry.getSegment(), entry.getOwner())); } resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateRow(); return entry.getToken(serializer); }
@Override public int[] fetchSegments(String processorName) { Connection connection = getConnection(); try { List<Integer> integers = executeQuery(connection, c -> selectForSegments(c, processorName), listResults(rs -> rs.getInt(schema.segmentColumn())), e -> new JdbcException( format("Could not load segments for processor [%s]", processorName), e)); return integers.stream().mapToInt(i -> i).toArray(); } finally { closeQuietly(connection); } }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + tokenType() + " NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying * storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * If the given {@code resultSet} has no items this method should insert a new token entry. If a token already * exists it should be attempted to replace the token in the entry with the given {@code token} and claim ownership. * * @param resultSet the updatable query result set of an executed {@link PreparedStatement} * @param token the token for the new or updated entry * @param processorName the name of the processor owning the token * @param segment the segment of the processor owning the token * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while updating the result set */ protected void insertOrUpdateToken(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { if (resultSet.next()) { AbstractTokenEntry<?> entry = readTokenEntry(resultSet); entry.updateToken(token, serializer); resultSet.updateObject(schema.tokenColumn(), entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); claimToken(resultSet, entry); } else { insertTokenEntry(resultSet, token, processorName, segment); } }
/** * Tries to claim the given token {@code entry}. If the claim fails an {@link UnableToClaimTokenException} should be * thrown. Otherwise the given {@code resultSet} should be updated to reflect the claim. * * @param resultSet the updatable query result of an executed {@link PreparedStatement} * @param entry the entry extracted from the given result set * @return the claimed tracking token * * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while claiming the token entry */ protected TrackingToken claimToken(ResultSet resultSet, AbstractTokenEntry<?> entry) throws SQLException { if (!entry.claim(nodeId, claimTimeout)) { throw new UnableToClaimTokenException( format("Unable to claim token '%s[%s]'. It is owned by '%s'", entry.getProcessorName(), entry.getSegment(), entry.getOwner())); } resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateRow(); return entry.getToken(serializer); }
/** * Builds a new {@link TokenSchema} from builder values. * * @return TokenSchema from this builder */ public TokenSchema build() { return new TokenSchema(this); } }
/** * Initializes the default TokenSchema */ public TokenSchema() { this(builder()); }