/** * 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 updateToken(TrackingToken token, Serializer serializer) { updateToken(token, serializer, contentType); }
/** * Inserts a new token entry via the given updatable {@code resultSet}. * * @param resultSet the updatable result set to add the entry to * @param token the token of the entry to insert * @param processorName the name of the processor to insert a token for * @param segment the segment of the processor to insert a token for * @return the tracking token of the inserted entry * * @throws SQLException when an exception occurs while inserting a token entry */ protected TrackingToken insertTokenEntry(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { AbstractTokenEntry<?> entry = new GenericTokenEntry<>(token, serializer, contentType, processorName, segment); entry.claim(nodeId, claimTimeout); resultSet.moveToInsertRow(); resultSet.updateObject(schema.tokenColumn(), token == null ? null : entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), token == null ? null : entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.processorNameColumn(), processorName); resultSet.updateInt(schema.segmentColumn(), segment); resultSet.insertRow(); return token; }
@Override public void updateToken(TrackingToken token, Serializer serializer) { updateToken(token, serializer, contentType); }
@Override public void initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { if (fetchSegments(processorName).length > 0) { throw new UnableToClaimTokenException("Unable to initialize segments. Some tokens were already present for the given processor."); } List<Document> entries = IntStream.range(0, segmentCount) .mapToObj(segment -> new GenericTokenEntry<>(initialToken, serializer, contentType, processorName, segment)) .map(this::tokenEntryToDocument) .collect(Collectors.toList()); mongoTemplate.trackingTokensCollection() .insertMany(entries, new InsertManyOptions().ordered(false)); }
@Override public void updateToken(TrackingToken token, Serializer serializer) { updateToken(token, serializer, contentType); }
/** * 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 initializeTokenSegments(String processorName, int segmentCount, TrackingToken initialToken) throws UnableToClaimTokenException { if (fetchSegments(processorName).length > 0) { throw new UnableToClaimTokenException( "Unable to initialize segments. Some tokens were already present for the given processor." ); } List<Document> entries = IntStream.range(0, segmentCount) .mapToObj(segment -> new GenericTokenEntry<>(initialToken, serializer, contentType, processorName, segment)) .map(this::tokenEntryToDocument) .collect(Collectors.toList()); mongoTemplate.trackingTokensCollection() .insertMany(entries, new InsertManyOptions().ordered(false)); }
/** * 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); }
private AbstractTokenEntry<?> documentToTokenEntry(Document document) { return new GenericTokenEntry<>( readSerializedData(document), document.getString("tokenType"), Instant.ofEpochMilli(document.getLong("timestamp")).toString(), document.getString("owner"), document.getString("processorName"), document.getInteger("segment"), contentType); }
private AbstractTokenEntry<?> documentToTokenEntry(Document document) { return new GenericTokenEntry<>( readSerializedData(document), document.getString("tokenType"), Instant.ofEpochMilli(document.getLong("timestamp")).toString(), document.getString("owner"), document.getString("processorName"), document.getInteger("segment"), contentType); }
private void updateOrInsertTokenEntry(TrackingToken token, String processorName, int segment) { AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(token, serializer, contentType, processorName, segment); tokenEntry.claim(nodeId, claimTimeout); Bson update = combine(set("owner", nodeId), set("timestamp", tokenEntry.timestamp().toEpochMilli()), set("token", tokenEntry.getSerializedToken().getData()), set("tokenType", tokenEntry.getSerializedToken().getType().getName())); UpdateResult updateResult = mongoTemplate.trackingTokensCollection() .updateOne(claimableTokenEntryFilter(processorName, segment), update); if (updateResult.getModifiedCount() == 0) { try { mongoTemplate.trackingTokensCollection() .insertOne(tokenEntryToDocument(tokenEntry)); } catch (MongoWriteException exception) { if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) { throw new UnableToClaimTokenException(format("Unable to claim token '%s[%s]'", processorName, segment)); } } } }
private void updateOrInsertTokenEntry(TrackingToken token, String processorName, int segment) { AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(token, serializer, contentType, processorName, segment); tokenEntry.claim(nodeId, claimTimeout); Bson update = combine(set("owner", nodeId), set("timestamp", tokenEntry.timestamp().toEpochMilli()), set("token", tokenEntry.getSerializedToken().getData()), set("tokenType", tokenEntry.getSerializedToken().getType().getName())); UpdateResult updateResult = mongoTemplate.trackingTokensCollection() .updateOne(claimableTokenEntryFilter(processorName, segment), update); if (updateResult.getModifiedCount() == 0) { try { mongoTemplate.trackingTokensCollection() .insertOne(tokenEntryToDocument(tokenEntry)); } catch (MongoWriteException exception) { if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) { throw new UnableToClaimTokenException(format("Unable to claim token '%s[%s]'", processorName, segment)); } } } }
private AbstractTokenEntry<?> loadOrInsertTokenEntry(String processorName, int segment) { Document document = mongoTemplate.trackingTokensCollection() .findOneAndUpdate(claimableTokenEntryFilter(processorName, segment), combine(set("owner", nodeId), set("timestamp", clock.millis())), new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER)); if (document == null) { try { AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(null, serializer, contentType, processorName, segment); tokenEntry.claim(nodeId, claimTimeout); mongoTemplate.trackingTokensCollection() .insertOne(tokenEntryToDocument(tokenEntry)); return tokenEntry; } catch (MongoWriteException exception) { if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) { throw new UnableToClaimTokenException(format("Unable to claim token '%s[%s]'", processorName, segment)); } } } return documentToTokenEntry(document); }
private AbstractTokenEntry<?> loadOrInsertTokenEntry(String processorName, int segment) { Document document = mongoTemplate.trackingTokensCollection() .findOneAndUpdate(claimableTokenEntryFilter(processorName, segment), combine(set("owner", nodeId), set("timestamp", clock.millis())), new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER)); if (document == null) { try { AbstractTokenEntry<?> tokenEntry = new GenericTokenEntry<>(null, serializer, contentType, processorName, segment); tokenEntry.claim(nodeId, claimTimeout); mongoTemplate.trackingTokensCollection() .insertOne(tokenEntryToDocument(tokenEntry)); return tokenEntry; } catch (MongoWriteException exception) { if (ErrorCategory.fromErrorCode(exception.getError().getCode()) == ErrorCategory.DUPLICATE_KEY) { throw new UnableToClaimTokenException(format("Unable to claim token '%s[%s]'", processorName, segment)); } } } return documentToTokenEntry(document); }
/** * Inserts a new token entry via the given updatable {@code resultSet}. * * @param resultSet the updatable result set to add the entry to * @param token the token of the entry to insert * @param processorName the name of the processor to insert a token for * @param segment the segment of the processor to insert a token for * @return the tracking token of the inserted entry * @throws SQLException when an exception occurs while inserting a token entry */ protected TrackingToken insertTokenEntry(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { AbstractTokenEntry<?> entry = new GenericTokenEntry<>(token, serializer, contentType, processorName, segment); entry.claim(nodeId, claimTimeout); resultSet.moveToInsertRow(); resultSet.updateObject(schema.tokenColumn(), token == null ? null : entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), token == null ? null : entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.processorNameColumn(), processorName); resultSet.updateInt(schema.segmentColumn(), segment); resultSet.insertRow(); return token; }
/** * Inserts a new token entry via the given updatable {@code resultSet}. * * @param resultSet the updatable result set to add the entry to * @param token the token of the entry to insert * @param processorName the name of the processor to insert a token for * @param segment the segment of the processor to insert a token for * @return the tracking token of the inserted entry * * @throws SQLException when an exception occurs while inserting a token entry */ protected TrackingToken insertTokenEntry(ResultSet resultSet, TrackingToken token, String processorName, int segment) throws SQLException { AbstractTokenEntry<?> entry = new GenericTokenEntry<>(token, serializer, contentType, processorName, segment); entry.claim(nodeId, claimTimeout); resultSet.moveToInsertRow(); resultSet.updateObject(schema.tokenColumn(), token == null ? null : entry.getSerializedToken().getData()); resultSet.updateString(schema.tokenTypeColumn(), token == null ? null : entry.getSerializedToken().getType().getName()); resultSet.updateString(schema.timestampColumn(), entry.timestampAsString()); resultSet.updateString(schema.ownerColum(), entry.getOwner()); resultSet.updateString(schema.processorNameColumn(), processorName); resultSet.updateInt(schema.segmentColumn(), segment); resultSet.insertRow(); return token; }