forked from mtusi2026/mtusi2026lab2
fix migrations
This commit is contained in:
+7
-3
@@ -9,7 +9,8 @@ COPY build.gradle settings.gradle ./
|
|||||||
|
|
||||||
COPY src src
|
COPY src src
|
||||||
|
|
||||||
RUN chmod +x gradlew && ./gradlew bootJar --no-daemon
|
RUN --mount=type=cache,target=/root/.gradle \
|
||||||
|
chmod +x gradlew && ./gradlew bootJar --no-daemon
|
||||||
|
|
||||||
|
|
||||||
# Stage 2: Runtime
|
# Stage 2: Runtime
|
||||||
@@ -19,13 +20,16 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY --from=build /app/build/libs/*.jar app.jar
|
COPY --from=build /app/build/libs/*.jar app.jar
|
||||||
|
|
||||||
RUN useradd --system --create-home messenger
|
RUN apt-get update \
|
||||||
|
&& apt-get install --yes --no-install-recommends curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& useradd --system --create-home messenger
|
||||||
|
|
||||||
USER messenger
|
USER messenger
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
|
||||||
CMD wget --spider --quiet http://localhost:8080/health/live || exit 1
|
CMD curl --fail --silent --show-error http://localhost:8080/health/live || exit 1
|
||||||
|
|
||||||
ENTRYPOINT ["java", "-jar", "app.jar"]
|
ENTRYPOINT ["java", "-jar", "app.jar"]
|
||||||
@@ -22,5 +22,9 @@ load:
|
|||||||
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
test-persistence:
|
test-persistence:
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
@if "$(ACTION_SCRIPT)"=="" (echo Set ACTION_SCRIPT to your implemented executable restart hook & exit /b 2)
|
||||||
|
else
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
||||||
|
endif
|
||||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ dependencies {
|
|||||||
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
|
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
|
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-flyway'
|
||||||
implementation 'org.flywaydb:flyway-core'
|
implementation 'org.flywaydb:flyway-core'
|
||||||
implementation 'org.flywaydb:flyway-database-postgresql'
|
implementation 'org.flywaydb:flyway-database-postgresql'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
# Restart every stateful component used by the application without deleting the
|
||||||
|
# named PostgreSQL volume. The persistence test waits for readiness afterwards.
|
||||||
|
docker compose --env-file .env restart postgres api
|
||||||
@@ -14,6 +14,7 @@ import java.security.MessageDigest;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ public class AuthenticationService {
|
|||||||
|
|
||||||
public IssuedSession issueSession(User user) {
|
public IssuedSession issueSession(User user) {
|
||||||
String token = newToken();
|
String token = newToken();
|
||||||
Instant createdAt = Instant.now();
|
Instant createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
|
||||||
Instant expiresAt = createdAt.plusSeconds(ttlSeconds);
|
Instant expiresAt = createdAt.plusSeconds(ttlSeconds);
|
||||||
sessions.save(new Session(
|
sessions.save(new Session(
|
||||||
UUID.randomUUID(),
|
UUID.randomUUID(),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.example.mtusi_messenger.exception.ApiExceptions;
|
|||||||
import com.example.mtusi_messenger.model.Chat;
|
import com.example.mtusi_messenger.model.Chat;
|
||||||
import com.example.mtusi_messenger.repository.ChatRepository;
|
import com.example.mtusi_messenger.repository.ChatRepository;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -16,7 +17,8 @@ public class ChatService {
|
|||||||
Set<UUID> ids = new LinkedHashSet<>(r.member_ids());
|
Set<UUID> ids = new LinkedHashSet<>(r.member_ids());
|
||||||
if (ids.size() != r.member_ids().size() || !ids.contains(caller)) throw ApiExceptions.invalid("Invalid member_ids");
|
if (ids.size() != r.member_ids().size() || !ids.contains(caller)) throw ApiExceptions.invalid("Invalid member_ids");
|
||||||
for (UUID id : ids) if (id == null || !users.exists(id)) throw ApiExceptions.invalid("Invalid member_ids");
|
for (UUID id : ids) if (id == null || !users.exists(id)) throw ApiExceptions.invalid("Invalid member_ids");
|
||||||
return chats.save(new Chat(UUID.randomUUID(), r.title(), List.copyOf(ids), Instant.now()));
|
return chats.save(new Chat(UUID.randomUUID(), r.title(), List.copyOf(ids),
|
||||||
|
Instant.now().truncatedTo(ChronoUnit.MICROS)));
|
||||||
}
|
}
|
||||||
public Chat getAccessible(UUID chatId, UUID userId) {
|
public Chat getAccessible(UUID chatId, UUID userId) {
|
||||||
users.get(userId);
|
users.get(userId);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.example.mtusi_messenger.dto.CreateMessageRequest;
|
|||||||
import com.example.mtusi_messenger.model.Message;
|
import com.example.mtusi_messenger.model.Message;
|
||||||
import com.example.mtusi_messenger.repository.MessageRepository;
|
import com.example.mtusi_messenger.repository.MessageRepository;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -16,7 +17,8 @@ public class MessageService {
|
|||||||
public Message create(UUID chatId, UUID senderId, CreateMessageRequest r) {
|
public Message create(UUID chatId, UUID senderId, CreateMessageRequest r) {
|
||||||
chats.getAccessible(chatId, senderId);
|
chats.getAccessible(chatId, senderId);
|
||||||
String text = r.text();
|
String text = r.text();
|
||||||
return messages.save(new Message(UuidCreator.getTimeOrderedEpochPlus1(), chatId, senderId, text, Instant.now()));
|
return messages.save(new Message(UuidCreator.getTimeOrderedEpochPlus1(), chatId, senderId, text,
|
||||||
|
Instant.now().truncatedTo(ChronoUnit.MICROS)));
|
||||||
}
|
}
|
||||||
public List<Message> page(UUID chatId, UUID userId, int limit, String cursor) {
|
public List<Message> page(UUID chatId, UUID userId, int limit, String cursor) {
|
||||||
chats.getAccessible(chatId, userId);
|
chats.getAccessible(chatId, userId);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.example.mtusi_messenger.exception.ApiExceptions;
|
|||||||
import com.example.mtusi_messenger.model.User;
|
import com.example.mtusi_messenger.model.User;
|
||||||
import com.example.mtusi_messenger.repository.UserRepository;
|
import com.example.mtusi_messenger.repository.UserRepository;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -23,7 +24,8 @@ public class UserService {
|
|||||||
if (users.findByUsername(username).isPresent()) throw ApiExceptions.conflict("Username is already taken");
|
if (users.findByUsername(username).isPresent()) throw ApiExceptions.conflict("Username is already taken");
|
||||||
String passwordHash = passwordEncoder.encode(r.password());
|
String passwordHash = passwordEncoder.encode(r.password());
|
||||||
try {
|
try {
|
||||||
return users.save(new User(UUID.randomUUID(), username, display, Instant.now()), passwordHash);
|
return users.save(new User(UUID.randomUUID(), username, display,
|
||||||
|
Instant.now().truncatedTo(ChronoUnit.MICROS)), passwordHash);
|
||||||
} catch (DuplicateKeyException e) {
|
} catch (DuplicateKeyException e) {
|
||||||
throw ApiExceptions.conflict("Username is already taken");
|
throw ApiExceptions.conflict("Username is already taken");
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-1
@@ -3,6 +3,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
@@ -55,7 +56,18 @@ def main():
|
|||||||
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
|
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
|
||||||
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
|
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
|
||||||
# No shell interpolation and no Docker commands in this test.
|
# No shell interpolation and no Docker commands in this test.
|
||||||
proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
command = [str(script)]
|
||||||
|
if os.name == 'nt' and script.suffix.lower() == '.sh':
|
||||||
|
interpreter = shutil.which('sh')
|
||||||
|
if interpreter is None:
|
||||||
|
git = shutil.which('git')
|
||||||
|
if git is not None:
|
||||||
|
candidate = Path(git).resolve().parents[1] / 'bin' / 'bash.exe'
|
||||||
|
if candidate.is_file():
|
||||||
|
interpreter = str(candidate)
|
||||||
|
check(interpreter is not None,'sh interpreter is required to run the action script on Windows')
|
||||||
|
command = [interpreter,str(script)]
|
||||||
|
proc = subprocess.Popen(command,env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
|
||||||
started = time.monotonic()
|
started = time.monotonic()
|
||||||
successes,failures,after_instances = [],0,[]
|
successes,failures,after_instances = [],0,[]
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user