Skip to content

Commit

Permalink
Merge pull request #31 from hotungkhanh/kan-75/fix-persist-errors
Browse files Browse the repository at this point in the history
KAN 75 fix(database): fix persist timeout and connection lost errors
  • Loading branch information
FlyingPufferFish authored Oct 6, 2024
2 parents e844717 + 5a1ce7c commit 6250939
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
*.sw?

# env variables
.env
.env
**/logs/
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ nb-configuration.xml
/.quarkus/cli/plugins/
# TLS Certificates
.certs/

# logs
**/logs/
14 changes: 13 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<groupId>ai.timefold.solver</groupId>
<artifactId>timefold-solver-quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down Expand Up @@ -92,6 +96,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-narayana-jta</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -127,4 +139,4 @@
</properties>
</profile>
</profiles>
</project>
</project>
8 changes: 3 additions & 5 deletions backend/src/main/java/org/acme/TimetableResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public Unit handleUnit(Unit unit) {
public void findByCampusAndDelete(String campusName) {
List<Timetable> timetables = Timetable.listAll();
for (Timetable timetable : timetables) {
System.out.println("CHECKING NOW\n");
if (campusName.equals(timetable.campusName)) {
System.out.println("SMTH HAS BEEN DELETED WOOOO\n");
timetable.delete();
}
}
Expand All @@ -95,9 +93,9 @@ public Timetable solveExample() throws ExecutionException, InterruptedException
Student h = new Student("h");
Student i = new Student("i");

Room r1 = new Room("Room1", 2, true);
Room r2 = new Room("Room2", 4, false);
Room r3 = new Room("Room3", 4, false);
Room r1 = new Room("Room1", "building A", "campus A", 2, true);
Room r2 = new Room("Room2", "building B", "campus A", 4, false);
Room r3 = new Room("Room3", "building A", "campus B", 4, false);

Unit u1 = new Unit(1, "1", "Course A", Duration.ofHours(2), List.of(a, b), true);
Unit u2 = new Unit(2, "2", "Course A", Duration.ofHours(2), List.of(a, c, d, e), true);
Expand Down
14 changes: 10 additions & 4 deletions backend/src/main/java/org/acme/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Room extends PanacheEntity {

public String buildingId;

public String campus;

public int capacity;

public boolean isLab;
Expand All @@ -53,12 +55,16 @@ public Room() {
/**
* Creates a room with its ID and capacity.
*
* @param id The room’s id.
* @param capacity The room's capacity.
* @param isLab Whether the room is a laboratory.
* @param id The room’s id.
* @param buildingId The building that the room belongs to.
* @param buildingId The campus that the room belongs to.
* @param capacity The room's capacity.
* @param isLab Whether the room is a laboratory.
*/
public Room(String id, int capacity, boolean isLab) {
public Room(String id, String buildingId, String campus, int capacity, boolean isLab) {
this.roomCode = id;
this.buildingId = buildingId;
this.campus = campus;
this.capacity = capacity;
this.isLab = isLab;
}
Expand Down
9 changes: 8 additions & 1 deletion backend/src/main/java/org/acme/domain/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Transient;

import java.time.DayOfWeek;
import java.time.Duration;
Expand Down Expand Up @@ -45,10 +46,13 @@ public class Unit extends PanacheEntity {
public LocalTime startTime;

// TODO: change unit to be the owner, rather than the student being owner
@Transient
@JsonIgnoreProperties("units")
@ManyToMany(mappedBy = "units", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<Student> students;

public int studentSize;

/*
* currently each unit only has 1 'slot' on the timetable, so it can only
* be associated with one room, but in the final product, we would most
Expand Down Expand Up @@ -90,6 +94,7 @@ public Unit(int unitID, String name, String course, Duration duration, List<Stud
this.course = course;
this.duration = duration;
this.students = students;
this.studentSize = this.students.size();
this.setStudentsUnits();
}

Expand All @@ -109,6 +114,7 @@ public Unit(int unitID, String name, String course, Duration duration, List<Stud
this.course = course;
this.duration = duration;
this.students = students;
this.studentSize = this.students.size();
this.wantsLab = wantsLab;
this.setStudentsUnits();
}
Expand All @@ -132,6 +138,7 @@ public Unit(int unitID, String name, String course, DayOfWeek dayOfWeek, LocalTi
this.startTime = startTime;
this.duration = duration;
this.students = students;
this.studentSize = this.students.size();
this.wantsLab = wantsLab;
this.room = room;
}
Expand Down Expand Up @@ -206,7 +213,7 @@ public void setStudentsUnits() {
* @return An int representing the number of students.
*/
public int getStudentSize() {
return students.size();
return this.studentSize;
}

public Room getRoom() {
Expand Down
60 changes: 58 additions & 2 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ quarkus.http.port=${PORT:8080}

# The solver runs only for 5 seconds to avoid a HTTP timeout in this simple implementation.
# It's recommended to run for at least 5 minutes ("5m") otherwise.
quarkus.timefold.solver.termination.spent-limit=5s
quarkus.timefold.solver.termination.spent-limit=6s
quarkus.http.cors=true
quarkus.http.cors.origins=*
quarkus.http.cors.methods=GET,POST,PUT,DELETE,OPTIONS
Expand All @@ -15,4 +15,60 @@ quarkus.datasource.password = ${QUARKUS_DATASOURCE_PASSWORD}
quarkus.datasource.jdbc.url = ${QUARKUS_DATASOURCE_JDBC_URL}

# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation = create-drop
quarkus.hibernate-orm.database.generation = create-drop

# transaction timeout
quarkus.transaction-manager.default-transaction-timeout=320s

# set batch size to optimise db transactions
quarkus.hibernate-orm.jdbc.batch_size=100

# Agoral pooling config
quarkus.datasource.jdbc.max-size=20
quarkus.datasource.jdbc.min-size=5
quarkus.datasource.jdbc.initial-size=10
quarkus.datasource.jdbc.idle-removal-interval=5M
quarkus.datasource.jdbc.max-lifetime=30M


# -------------------------------------------
# Debuggers
# -------------------------------------------

# debugger for large/complex db transactions
quarkus.log.category."com.arjuna".level=DEBUG

# TimeFold solver debuggers
quarkus.log.category."ai.tim.sol.cor.imp.sol".level=DEBUG
quarkus.log.category."ai.tim.sol.cor.imp.con".level=DEBUG

# -------------------------------------------
# Loggers
# -------------------------------------------

# log datasource metrics
quarkus.datasource.metrics.enabled=true
quarkus.datasource.jdbc.enable-metrics=true

# log datasource leak
quarkus.datasource.jdbc.log-leak-detection=true

# log transactions
quarkus.log.category."org.hibernate.engine.transaction.internal.TransactionImpl".level=DEBUG
quarkus.log.category."org.hibernate.engine.transaction.spi.AbstractTransactionImpl".level=DEBUG
quarkus.log.category."org.hibernate.resource.jdbc.internal.LogicalConnectionImpl".level=DEBUG
quarkus.log.category."org.hibernate.SQL".level=DEBUG
quarkus.log.category."org.jboss.logging".level=DEBUG
quarkus.hibernate-orm.statistics=true
quarkus.hibernate-orm.log-session-metrics=true
quarkus.hibernate-orm.metrics.enabled=true

# log sql commands
# quarkus.hibernate-orm.log.sql=true
# quarkus.hibernate-orm.format-sql=true

# enable logfile
quarkus.log.file.enable=true
quarkus.log.file.path=logs/quarkus.log
quarkus.log.file.rotation.max-file-size=10M
quarkus.log.file.rotation.max-backup-index=10
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

@QuarkusTest
public class TimetableConstraintProviderTest {
private static final Room ROOM1 = new Room("1", 2, true);
private static final Room ROOM2 = new Room("2", 2, false);
private static final Room ROOM3 = new Room("3", 2, true);
private static final Room ROOM1 = new Room("1", "building A", "campus A", 2, true);
private static final Room ROOM2 = new Room("2", "building B", "campus A", 2, false);
private static final Room ROOM3 = new Room("3", "building A", "campus B", 2, true);
private static final Student STUDENT1 = new Student("student1");
private static final Student STUDENT2 = new Student("student2");
private static final Student STUDENT3 = new Student("student3");
Expand Down

0 comments on commit 6250939

Please sign in to comment.