/** * Tries loading an existing token owned by a processor with given {@code processorName} and {@code segment}. If * such a token entry exists an attempt will be made to claim the token. If that succeeds the token will be * returned. If the token is already owned by another node an {@link UnableToClaimTokenException} will be thrown. * <p> * If no such token exists yet, a new token entry will be inserted with {@code null} token owned by this node and * return {@code null}. * * @param resultSet the updatable result set from a prior select for update query * @param processorName the name of the processor to load or insert a token entry for * @param segment the segment of the processor to load or insert a token entry for * @return the tracking token of the fetched entry or {@code null} if a new entry was inserted * * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while loading or inserting the entry */ protected TrackingToken loadOrInsertToken(ResultSet resultSet, String processorName, int segment) throws SQLException { if (!resultSet.next()) { return insertTokenEntry(resultSet, null, processorName, segment); } return claimToken(resultSet, readTokenEntry(resultSet)); }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException( "Could not initialize segments. Some segments were already present.", e )); } finally { closeQuietly(connection); } }
/** * 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 loading an existing token owned by a processor with given {@code processorName} and {@code segment}. If * such a token entry exists an attempt will be made to claim the token. If that succeeds the token will be * returned. If the token is already owned by another node an {@link UnableToClaimTokenException} will be thrown. * <p> * If no such token exists yet, a new token entry will be inserted with {@code null} token owned by this node and * return {@code null}. * * @param resultSet the updatable result set from a prior select for update query * @param processorName the name of the processor to load or insert a token entry for * @param segment the segment of the processor to load or insert a token entry for * @return the tracking token of the fetched entry or {@code null} if a new entry was inserted * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while loading or inserting the entry */ protected TrackingToken loadOrInsertToken(ResultSet resultSet, String processorName, int segment) throws SQLException { if (!resultSet.next()) { return insertTokenEntry(resultSet, null, processorName, segment); } return claimToken(resultSet, readTokenEntry(resultSet)); }
/** * Tries loading an existing token owned by a processor with given {@code processorName} and {@code segment}. If * such a token entry exists an attempt will be made to claim the token. If that succeeds the token will be * returned. If the token is already owned by another node an {@link UnableToClaimTokenException} will be thrown. * <p> * If no such token exists yet, a new token entry will be inserted with {@code null} token owned by this node and * return {@code null}. * * @param resultSet the updatable result set from a prior select for update query * @param processorName the name of the processor to load or insert a token entry for * @param segment the segment of the processor to load or insert a token entry for * @return the tracking token of the fetched entry or {@code null} if a new entry was inserted * * @throws UnableToClaimTokenException if the token cannot be claimed because another node currently owns the token * @throws SQLException when an exception occurs while loading or inserting the entry */ protected TrackingToken loadOrInsertToken(ResultSet resultSet, String processorName, int segment) throws SQLException { if (!resultSet.next()) { return insertTokenEntry(resultSet, null, processorName, segment); } return claimToken(resultSet, readTokenEntry(resultSet)); }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException("Could not initialize segments. Some segments were already present.", e)); } finally { closeQuietly(connection); } }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, 0), resultSet -> { for (int segment = 0; segment < segmentCount; segment++) { insertTokenEntry(resultSet, initialToken, processorName, segment); } if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new UnableToClaimTokenException( "Could not initialize segments. Some segments were already present.", e )); } finally { closeQuietly(connection); } }
/** * 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); } }
/** * 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); } }