/** * 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 TrackingToken fetchToken(String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { return executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { TrackingToken result = loadOrInsertToken(resultSet, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return result; }, e -> new JdbcException( format("Could not load token for processor [%s] and segment [%d]", processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void initializeTokenSegments(String processorName, int segmentCount) throws UnableToClaimTokenException { initializeTokenSegments(processorName, segmentCount, null); }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), 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); } }
@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 void releaseClaim(String processorName, int segment) { Connection connection = getConnection(); try { int[] result = executeUpdates(connection, e -> { throw new JdbcException( format("Could not load token for processor [%s] and segment " + "[%d]", processorName, segment), e); }, c -> releaseClaim(c, processorName, segment)); try { if (!connection.isClosed() && !connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { // ignore } if (result[0] < 1) { logger.warn( "Releasing claim of token {}/{} failed. It was owned by another node.", processorName, segment ); } } finally { closeQuietly(connection); } }
/** * Performs the DDL queries to create the schema necessary for this token store implementation. * * @param schemaFactory factory of the token entry schema */ public void createSchema(TokenTableFactory schemaFactory) { Connection c = getConnection(); try { executeUpdates(c, e -> { throw new JdbcException("Failed to create token tables", e); }, connection -> schemaFactory.createTable(connection, schema)); } finally { closeQuietly(c); } }
/** * Convert given {@code resultSet} to an {@link AbstractTokenEntry}. The result set contains a single token entry. * * @param resultSet the result set of a prior select statement containing a single token entry * @return an token entry with data extracted from the result set * * @throws SQLException if the result set cannot be converted to an entry */ protected AbstractTokenEntry<?> readTokenEntry(ResultSet resultSet) throws SQLException { return new GenericTokenEntry<>(readSerializedData(resultSet, schema.tokenColumn()), resultSet.getString(schema.tokenTypeColumn()), resultSet.getString(schema.timestampColumn()), resultSet.getString(schema.ownerColum()), resultSet.getString(schema.processorNameColumn()), resultSet.getInt(schema.segmentColumn()), contentType); }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), 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); } }
@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 void releaseClaim(String processorName, int segment) { Connection connection = getConnection(); try { int[] result = executeUpdates(connection, e -> { throw new JdbcException( format("Could not load token for processor [%s] and segment " + "[%d]", processorName, segment), e); }, c -> releaseClaim(c, processorName, segment)); try { if (!connection.isClosed() && !connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { // ignore } if (result[0] < 1) { logger.warn( "Releasing claim of token {}/{} failed. It was owned by another node.", processorName, segment ); } } finally { closeQuietly(connection); } }
/** * Performs the DDL queries to create the schema necessary for this token store implementation. * * @param schemaFactory factory of the token entry schema * @throws EventStoreException when an error occurs executing SQL statements */ public void createSchema(TokenTableFactory schemaFactory) { Connection c = getConnection(); try { executeUpdates(c, e -> { throw new JdbcException("Failed to create token tables", e); }, connection -> schemaFactory.createTable(connection, schema)); } finally { closeQuietly(c); } }
/** * Convert given {@code resultSet} to an {@link AbstractTokenEntry}. The result set contains a single token entry. * * @param resultSet the result set of a prior select statement containing a single token entry * @return an token entry with data extracted from the result set * @throws SQLException if the result set cannot be converted to an entry */ protected AbstractTokenEntry<?> readTokenEntry(ResultSet resultSet) throws SQLException { return new GenericTokenEntry<>(readSerializedData(resultSet, schema.tokenColumn()), resultSet.getString(schema.tokenTypeColumn()), resultSet.getString(schema.timestampColumn()), resultSet.getString(schema.ownerColum()), resultSet.getString(schema.processorNameColumn()), resultSet.getInt(schema.segmentColumn()), contentType); }
/** * 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); } }
@Override public TrackingToken fetchToken(String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { return executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { TrackingToken result = loadOrInsertToken(resultSet, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return result; }, e -> new JdbcException( format("Could not load token for processor [%s] and segment [%d]", processorName, segment), e)); } finally { closeQuietly(connection); } }
@Override public void storeToken(TrackingToken token, String processorName, int segment) throws UnableToClaimTokenException { Connection connection = getConnection(); try { executeQuery(connection, c -> selectForUpdate(c, processorName, segment), resultSet -> { insertOrUpdateToken(resultSet, token, processorName, segment); if (!connection.getAutoCommit()) { connection.commit(); } return null; }, e -> new JdbcException(format("Could not store token [%s] for processor [%s] and segment [%d]", token, processorName, segment), 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); } }
@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); } }