-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jdbc): refactor queue to use jooq
- Loading branch information
1 parent
06337f5
commit 746bcbd
Showing
3 changed files
with
132 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,75 @@ | ||
package io.kestra.runner.h2; | ||
|
||
import io.kestra.jdbc.repository.AbstractRepository; | ||
import io.kestra.jdbc.runner.JdbcQueue; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.core.annotation.NonNull; | ||
import org.jooq.DSLContext; | ||
import org.jooq.Record; | ||
import org.jooq.Result; | ||
import org.jooq.*; | ||
import org.jooq.impl.DSL; | ||
import org.jooq.impl.SQLDataType; | ||
import org.jooq.util.h2.H2DataType; | ||
|
||
import java.sql.Types; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class H2Queue<T> extends JdbcQueue<T> { | ||
public H2Queue(Class<T> cls, ApplicationContext applicationContext) { | ||
super(cls, applicationContext); | ||
} | ||
|
||
protected Result<Record> receiveFetch(DSLContext ctx, @NonNull Integer offset) { | ||
return ctx | ||
.resultQuery( | ||
"SELECT" + "\n" + | ||
" \"value\"," + "\n" + | ||
" \"offset\"" + "\n" + | ||
"FROM " + table.getName() + "\n" + | ||
"WHERE 1 = 1" + "\n" + | ||
(offset != 0 ? "AND \"offset\" > ?" + "\n" : "") + | ||
"AND \"type\" = ? " + "\n" + | ||
"ORDER BY \"offset\" ASC" + "\n" + | ||
"LIMIT 10" + "\n" + | ||
"FOR UPDATE", | ||
offset != 0 ? offset : this.cls.getName(), | ||
this.cls.getName() | ||
@Override | ||
protected Result<Record> receiveFetch(DSLContext ctx, Integer offset) { | ||
SelectConditionStep<Record2<Object, Object>> select = ctx | ||
.select( | ||
AbstractRepository.field("value"), | ||
AbstractRepository.field("offset") | ||
) | ||
.fetch(); | ||
.from(this.table) | ||
.where(AbstractRepository.field("type").eq(this.cls.getName())); | ||
|
||
if (offset != 0) { | ||
select = select.and(AbstractRepository.field("offset").gt(offset)); | ||
} | ||
|
||
return select | ||
.orderBy(AbstractRepository.field("offset").asc()) | ||
.limit(10) | ||
.forUpdate() | ||
.fetchMany() | ||
.get(0); | ||
} | ||
|
||
protected Result<Record> receiveFetch(DSLContext ctx, String consumerGroup) { | ||
return ctx | ||
.resultQuery( | ||
"SELECT" + "\n" + | ||
" \"value\"," + "\n" + | ||
" \"offset\"" + "\n" + | ||
"FROM " + table.getName() + "\n" + | ||
"WHERE (" + | ||
" \"consumers\" IS NULL" + "\n" + | ||
" OR NOT(ARRAY_CONTAINS(\"consumers\", ?))" + "\n" + | ||
")" + "\n" + | ||
"AND \"type\" = ?" + "\n" + | ||
"ORDER BY \"offset\" ASC" + "\n" + | ||
"LIMIT 10" + "\n" + | ||
"FOR UPDATE", | ||
consumerGroup, | ||
this.cls.getName() | ||
.select( | ||
AbstractRepository.field("value"), | ||
AbstractRepository.field("offset") | ||
) | ||
.fetch(); | ||
.from(this.table) | ||
.where(AbstractRepository.field("type").eq(this.cls.getName())) | ||
.and(DSL.or(List.of( | ||
AbstractRepository.field("consumers").isNull(), | ||
DSL.condition("NOT(ARRAY_CONTAINS(\"consumers\", ?))", consumerGroup) | ||
))) | ||
.orderBy(AbstractRepository.field("offset").asc()) | ||
.limit(10) | ||
.forUpdate() | ||
.fetchMany() | ||
.get(0); | ||
} | ||
|
||
@Override | ||
protected void updateGroupOffsets(DSLContext ctx, String consumerGroup, List<Integer> offsets) { | ||
ctx | ||
.query( | ||
"UPDATE " + table.getName() + "\n" + | ||
"SET \"consumers\" = COALESCE(\"consumers\", ARRAY[]) || ARRAY['" + consumerGroup + "']\n" + | ||
"WHERE \"offset\" IN (" + | ||
offsets | ||
.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining(",")) + | ||
")", | ||
consumerGroup | ||
.update(DSL.table(table.getName())) | ||
.set( | ||
AbstractRepository.field("consumers"), | ||
DSL.field( | ||
"ARRAY_APPEND(COALESCE(\"consumers\", ARRAY[]), ?)", | ||
SQLDataType.VARCHAR(50).getArrayType(), | ||
(Object) new String[]{consumerGroup} | ||
) | ||
) | ||
.where(AbstractRepository.field("offset").in(offsets.toArray(Integer[]::new))) | ||
.execute(); | ||
} | ||
} |
87 changes: 41 additions & 46 deletions
87
jdbc-mysql/src/main/java/io/kestra/runner/mysql/MysqlQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,70 @@ | ||
package io.kestra.runner.mysql; | ||
|
||
import io.kestra.jdbc.repository.AbstractRepository; | ||
import io.kestra.jdbc.runner.JdbcQueue; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.core.annotation.NonNull; | ||
import lombok.SneakyThrows; | ||
import org.jooq.*; | ||
import org.jooq.impl.DSL; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class MysqlQueue<T> extends JdbcQueue<T> { | ||
public MysqlQueue(Class<T> cls, ApplicationContext applicationContext) { | ||
super(cls, applicationContext); | ||
} | ||
|
||
protected Result<Record> receiveFetch(DSLContext ctx, @NonNull Integer offset) { | ||
return ctx | ||
.resultQuery( | ||
"SELECT" + "\n" + | ||
" value," + "\n" + | ||
" offset" + "\n" + | ||
"FROM " + table.getName() + "\n" + | ||
"WHERE 1 = 1" + "\n" + | ||
(offset != 0 ? "AND offset > ?" + "\n" : "") + | ||
"AND type = ? " + "\n" + | ||
"ORDER BY offset ASC" + "\n" + | ||
"LIMIT 10" + "\n" + | ||
"FOR UPDATE SKIP LOCKED", | ||
offset != 0 ? offset : this.cls.getName(), | ||
this.cls.getName() | ||
@Override | ||
protected Result<Record> receiveFetch(DSLContext ctx, Integer offset) { | ||
SelectConditionStep<Record2<Object, Object>> select = ctx | ||
.select( | ||
AbstractRepository.field("value"), | ||
AbstractRepository.field("offset") | ||
) | ||
.fetch(); | ||
.from(this.table) | ||
.where(AbstractRepository.field("type").eq(this.cls.getName())); | ||
|
||
if (offset != 0) { | ||
select = select.and(AbstractRepository.field("offset").gt(offset)); | ||
} | ||
|
||
return select | ||
.orderBy(AbstractRepository.field("offset").asc()) | ||
.limit(10) | ||
.forUpdate() | ||
.skipLocked() | ||
.fetchMany() | ||
.get(0); | ||
} | ||
|
||
protected Result<Record> receiveFetch(DSLContext ctx, String consumerGroup) { | ||
return ctx | ||
.resultQuery( | ||
"SELECT" + "\n" + | ||
" value," + "\n" + | ||
" offset" + "\n" + | ||
"FROM " + table.getName() + "\n" + | ||
"WHERE (" + | ||
" consumers IS NULL" + "\n" + | ||
" OR NOT(FIND_IN_SET(?, consumers) > 0)" + "\n" + | ||
")" + "\n" + | ||
"AND type = ?" + "\n" + | ||
"ORDER BY offset ASC" + "\n" + | ||
"LIMIT 10" + "\n" + | ||
"FOR UPDATE SKIP LOCKED", | ||
consumerGroup, | ||
this.cls.getName() | ||
.select( | ||
AbstractRepository.field("value"), | ||
AbstractRepository.field("offset") | ||
) | ||
.fetch(); | ||
.from(this.table) | ||
.where(AbstractRepository.field("type").eq(this.cls.getName())) | ||
.and(DSL.or(List.of( | ||
AbstractRepository.field("consumers").isNull(), | ||
DSL.condition("NOT(FIND_IN_SET(?, consumers) > 0)", consumerGroup) | ||
))) | ||
.orderBy(AbstractRepository.field("offset").asc()) | ||
.limit(10) | ||
.forUpdate() | ||
.skipLocked() | ||
.fetchMany() | ||
.get(0); | ||
} | ||
|
||
@Override | ||
protected void updateGroupOffsets(DSLContext ctx, String consumerGroup, List<Integer> offsets) { | ||
ctx | ||
.query( | ||
"UPDATE " + table.getName() + "\n" + | ||
"SET consumers = CONCAT_WS(',', consumers, '" + consumerGroup + "')\n" + | ||
"WHERE offset IN (" + | ||
offsets | ||
.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.joining(",")) + | ||
")", | ||
consumerGroup | ||
.update(DSL.table(table.getName())) | ||
.set( | ||
AbstractRepository.field("consumers"), | ||
DSL.field("CONCAT_WS(',', consumers, ?)", String.class, consumerGroup) | ||
) | ||
.where(AbstractRepository.field("offset").in(offsets.toArray(Integer[]::new))) | ||
.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters