Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.illuin.wombat.commons;

public enum ConfigSourceFactories
{
NODE_MODE(9001),
BACKEND_MODE(9000),
FEATURE_FLAG(9000),
;

private final int priority;

ConfigSourceFactories(int priority)
{
this.priority = priority;
}

public int priority()
{
return this.priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,47 @@
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.illuin.wombat.persistence.BackendMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;

import static tech.illuin.wombat.commons.ConfigSourceFactories.NODE_MODE;
import static tech.illuin.wombat.persistence.PersistenceBackendConfigSourceFactory.ACTIVE_BACKEND_CONFIG_KEY;
import static tech.illuin.wombat.persistence.PersistenceBackendConfigSourceFactory.PERSISTENCE_BACKEND_MODE_KEY;

@StaticInitSafe
public class NodeModeConfigSourceFactory implements ConfigSourceFactory
{
private static final String WORKER_DB_KIND = "sqlite";
private static final String WORKER_JDBC_URL = "jdbc:sqlite::memory:";

private static final Logger logger = LoggerFactory.getLogger(NodeModeConfigSourceFactory.class);

@Override
public Iterable<ConfigSource> getConfigSources(ConfigSourceContext context)
{
ConfigValue value = context.getValue("wombat.mode");
NodeMode mode = value.getValue() == null ? null : NodeMode.parse(value.getValue());
String value = context.getValue("wombat.mode").getValue();
NodeMode mode = value == null ? NodeMode.standalone : NodeMode.parse(value);

Map<String, String> properties = new HashMap<>();
if (mode == NodeMode.worker)
{
String backend = context.getValue(ACTIVE_BACKEND_CONFIG_KEY).getValue();
if (backend != null && !backend.equals(WORKER_DB_KIND))
logger.warn("Worker mode only runs on SQLite in-memory: overriding backend {} with {}", backend, WORKER_DB_KIND);

properties.put("quarkus.datasource.db-kind", WORKER_DB_KIND);
properties.put("quarkus.datasource.jdbc.url", WORKER_JDBC_URL);
properties.put("quarkus.datasource.jdbc.min-size", "1");
properties.put("quarkus.datasource.jdbc.max-size", "1");
properties.put(ACTIVE_BACKEND_CONFIG_KEY, WORKER_DB_KIND);
logger.debug("Overriding persistence backend for worker mode: set to in-memory sqlite");
properties.put(PERSISTENCE_BACKEND_MODE_KEY, BackendMode.sqlite.name());
properties.put("persistence.backend.sqlite.jdbc-url", "jdbc:sqlite::memory:");
}

logger.debug("Node mode established: {}", mode);
return List.of(new PropertiesConfigSource(
properties,
NodeModeConfigSourceFactory.class.getSimpleName(),
Integer.MAX_VALUE
NODE_MODE.priority()
));
}

@Override
public OptionalInt getPriority()
{
return OptionalInt.of(NODE_MODE.priority());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private AssetConfigAction reconcileAsset(String owner, String environmentId, Ass
private List<AssetEntity> deleteMissingAssets(String owner, Set<String> desiredAssetIds)
{
List<AssetEntity> deleted = new ArrayList<>();
for (AssetEntity entity : this.assetRepository.list("deletedAt is null"))
for (AssetEntity entity : this.assetRepository.findActive())
{
if (desiredAssetIds.contains(entity.id) || !Objects.equals(entity.owner, owner))
continue;
Expand All @@ -202,7 +202,7 @@ private void disableEmptyEnvironments(String owner, Set<String> desiredEnvironme
declared.addAll(this.monitoredEnvironments.environments().keySet());

Set<String> withAssets = new HashSet<>();
for (AssetEntity entity : this.assetRepository.list("deletedAt is null"))
for (AssetEntity entity : this.assetRepository.findActive())
{
if (entity.deletedAt == null)
withAssets.add(entity.environmentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -19,7 +18,7 @@
*/
@Entity
@Table(name = "asset_config_history")
public class AssetConfigHistory extends PanacheEntityBase
public class AssetConfigHistory
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import tech.illuin.wombat.persistence.backend.api.repository.Repository;

import java.util.List;

@ApplicationScoped
public class AssetConfigHistoryRepository implements PanacheRepositoryBase<AssetConfigHistory, Long>
public class AssetConfigHistoryRepository extends Repository<AssetConfigHistory, Long>
{
public AssetConfigHistoryRepository()
{
super(AssetConfigHistory.class);
}

public List<AssetConfigHistory> findByAsset(String assetId)
{
return this.list("assetId = ?1 order by changedAt", assetId);
return getEntityManager()
.createQuery("FROM AssetConfigHistory WHERE assetId = :assetId ORDER BY changedAt", AssetConfigHistory.class)
.setParameter("assetId", assetId)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
Expand All @@ -15,7 +14,7 @@

@Entity
@Table(name = "assets")
public class AssetEntity extends PanacheEntityBase
public class AssetEntity
{
@Id
@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import tech.illuin.wombat.persistence.backend.api.repository.Repository;

import java.util.List;

@ApplicationScoped
public class AssetRepository implements PanacheRepositoryBase<AssetEntity, String>
public class AssetRepository extends Repository<AssetEntity, String>
{
public AssetRepository()
{
super(AssetEntity.class);
}

public List<AssetEntity> findByEnvironment(String environmentId)
{
return this.list("environmentId = ?1 and deletedAt is null", environmentId);
return getEntityManager()
.createQuery("FROM AssetEntity WHERE environmentId = :environmentId AND deletedAt IS NULL", AssetEntity.class)
.setParameter("environmentId", environmentId)
.getResultList();
}

public List<AssetEntity> findActive()
{
return getEntityManager()
.createQuery("FROM AssetEntity WHERE deletedAt IS NULL", AssetEntity.class)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
Expand All @@ -14,7 +13,7 @@

@Entity
@Table(name = "environments")
public class EnvironmentEntity extends PanacheEntityBase
public class EnvironmentEntity
{
@Id @Column(nullable = false)
public String id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package tech.illuin.wombat.engine.context.persistence;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import tech.illuin.wombat.persistence.backend.api.repository.Repository;

import java.util.List;

@ApplicationScoped
public class EnvironmentRepository implements PanacheRepositoryBase<EnvironmentEntity, String>
public class EnvironmentRepository extends Repository<EnvironmentEntity, String>
{
public EnvironmentRepository()
{
super(EnvironmentEntity.class);
}

public List<EnvironmentEntity> findActive()
{
return this.list("disabledAt is null");
return getEntityManager()
.createQuery("FROM EnvironmentEntity WHERE disabledAt IS NULL", EnvironmentEntity.class)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tech.illuin.wombat.engine.impact.kubernetes;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import tech.illuin.wombat.persistence.backend.api.repository.Repository;
import tech.illuin.wombat.persistence.compaction.BucketCompactor;
import tech.illuin.wombat.core.activity.commons.TimeRange;
import tech.illuin.wombat.core.asset.AssetIdentity;
Expand All @@ -20,15 +21,22 @@
import static tech.illuin.wombat.core.activity.commons.TimeRange.toEpochMs;

@ApplicationScoped
public class KubernetesMetricRepository implements PanacheRepositoryBase<KubernetesMetricEntity, Long>, KubernetesMetricResolver, BucketCompactor
public class KubernetesMetricRepository extends Repository<KubernetesMetricEntity, Long> implements KubernetesMetricResolver, BucketCompactor
{
private static final String DATA = "data";
private static final String SERVICE_ID = "serviceId";

private final JsonPathDialect dialect;

public KubernetesMetricRepository()
{
this(null);
}

@Inject
public KubernetesMetricRepository(JsonPathDialect dialect)
{
super(KubernetesMetricEntity.class);
this.dialect = dialect;
}

Expand All @@ -44,10 +52,53 @@ public void save(Collection<KubernetesMetricEntity> entities)
persist(entities);
}

@Transactional
public void deleteByAssetId(String assetId)
{
this.getEntityManager()
.createNativeQuery("DELETE FROM server_metrics WHERE asset_id = ?1")
.setParameter(1, assetId)
.executeUpdate();
}

@Transactional
public void deleteByAssetIds(List<String> assetIds)
{
if (assetIds.isEmpty())
return;
this.getEntityManager()
.createQuery("DELETE FROM KubernetesMetricEntity WHERE assetId IN :ids")
.setParameter("ids", assetIds)
.executeUpdate();
}

@Transactional
public List<KubernetesMetricEntity> listByAssetId(String assetId)
{
return getEntityManager()
.createQuery("FROM KubernetesMetricEntity WHERE assetId = :assetId", KubernetesMetricEntity.class)
.setParameter("assetId", assetId)
.getResultList();
}

@Transactional
public long countByAssetId(String assetId)
{
return ((Number) getEntityManager()
.createQuery("SELECT COUNT(m) FROM KubernetesMetricEntity m WHERE m.assetId = :assetId")
.setParameter("assetId", assetId)
.getSingleResult()
).longValue();
}

@Transactional
public List<KubernetesMetricEntity> findByRange(long startMs, long endMs)
{
return find("instantMs >= ?1 AND instantMs <= ?2", startMs, endMs).list();
return getEntityManager()
.createQuery("FROM KubernetesMetricEntity WHERE instantMs >= :start AND instantMs <= :end", KubernetesMetricEntity.class)
.setParameter("start", startMs)
.setParameter("end", endMs)
.getResultList();
}

@Transactional
Expand Down Expand Up @@ -173,8 +224,11 @@ public List<Long> uncompactedBuckets(long stepMs, long beforeMs)
public int compactBucket(long bucketStartMs, long stepMs)
{
long bucketEndMs = bucketStartMs + stepMs;
List<KubernetesMetricEntity> rows = find(
"instantMs >= ?1 AND instantMs < ?2", bucketStartMs, bucketEndMs).list();
List<KubernetesMetricEntity> rows = this.getEntityManager()
.createQuery("FROM KubernetesMetricEntity WHERE instantMs >= :start AND instantMs < :end", KubernetesMetricEntity.class)
.setParameter("start", bucketStartMs)
.setParameter("end", bucketEndMs)
.getResultList();
if (rows.stream().allMatch(row -> row.compacted))
return 0;

Expand All @@ -196,7 +250,11 @@ public int compactBucket(long bucketStartMs, long stepMs)
folded.add(row);
});

delete("instantMs >= ?1 AND instantMs < ?2", bucketStartMs, bucketEndMs);
getEntityManager()
.createNativeQuery("DELETE FROM server_metrics WHERE instant_ms >= ?1 AND instant_ms < ?2")
.setParameter(1, bucketStartMs)
.setParameter(2, bucketEndMs)
.executeUpdate();
getEntityManager().clear();

folded.forEach(this::persist);
Expand Down
Loading
Loading