Skip to content

Commit

Permalink
works again; maps can be scaled down by factor; tested tested tested;…
Browse files Browse the repository at this point in the history
… try to configure second level entity cache to disk ....
  • Loading branch information
svencc committed Apr 10, 2024
1 parent 0a98642 commit a95576d
Show file tree
Hide file tree
Showing 34 changed files with 234 additions and 75 deletions.
2 changes: 1 addition & 1 deletion services/recom-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
<classifier>jakarta</classifier>
<scope>runtime</scope>
<scope>compile</scope>
</dependency>
<!-- CACHE -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ResponseEntity<MapExistsResponseDto> mapExists(
Thread.sleep(Duration.ofSeconds(1).toMillis());
mapTopographyChunkScanRequestNotificationService.requestMapTopographyChunkScan(gameMap);
mapStructureChunkScanRequestNotificationService.requestMapStructureChunkScan(gameMap);
} catch (Throwable t) {
} catch (final Throwable t) {
log.error("Failed to notify map scan.", t);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public AsyncExceptionHandler getAsyncUncaughtExceptionHandler() {
}

// Additional Async Executor(s)
@Bean("AsyncMapTransactionExecutor")
@Qualifier(value = "AsyncMapTransactionExecutor")
@Bean("AsyncMapStructureTransactionExecutor")
@Qualifier(value = "AsyncMapStructureTransactionExecutor")
public ThreadPoolTaskExecutor getAsyncMapTransactionExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Table(indexes = {
@Index(name = "IDX_name", columnList = "name", unique = false)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class GameMap implements Persistable<Long>, Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(indexes = {})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MapDimensions implements Persistable<Long>, Serializable, MapRelatedEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(indexes = {})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SquareKilometerStructureChunk implements Persistable<Long>, Serializable, MapRelatedEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Table(indexes = {})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SquareKilometerTopographyChunk implements Persistable<Long>, Serializable, MapRelatedEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Table(indexes = {
@Index(name = "IDX_name", columnList = "name", unique = true)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ClassNameEntity implements Persistable<Long>, Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Table(indexes = {
@Index(name = "IDX_name", columnList = "name", unique = true)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MapDescriptorTypeEntity implements Persistable<Long>, Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Index(name = "IDX_gameMap_mapDescriptorType", columnList = "game_map_id, map_descriptor_type_id", unique = false),
@Index(name = "IDX_squareKilometerStructureChunk", columnList = "square_kilometer_structure_chunk_id", unique = false)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MapStructureEntity implements Persistable<Long>, Serializable, MapLocatedEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Table(indexes = {
@Index(name = "IDX_name", columnList = "name", unique = true)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class PrefabNameEntity implements Persistable<Long>, Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Table(indexes = {
@Index(name = "IDX_name", columnList = "name", unique = true)
})
@Cacheable
//@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ResourceNameEntity implements Persistable<Long>, Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

@Slf4j
@Component
public class MapStructureEntityScannerTransactionEventListener extends TransactionalMapLocatedPackageEventListenerTemplate<TransactionalMapStructurePackageDto, MapStructureEntity, MapStructureDto> {
public class MapStructureScannerTransactionEventListener extends TransactionalMapLocatedPackageEventListenerTemplate<TransactionalMapStructurePackageDto, MapStructureEntity, MapStructureDto> {

@NonNull
private final MapStructureChunkScanRequestNotificationService mapStructureChunkScanRequestNotificationService;


public MapStructureEntityScannerTransactionEventListener(
public MapStructureScannerTransactionEventListener(
@NonNull final EntityManager entityManager,
@NonNull final TransactionTemplate transactionTemplate,
@NonNull final MapStructurePersistenceLayer entityPersistenceLayer,
Expand All @@ -45,21 +45,21 @@ public MapStructureEntityScannerTransactionEventListener(
this.mapStructureChunkScanRequestNotificationService = mapStructureChunkScanRequestNotificationService;
}

@Async("AsyncMapTransactionExecutor")
@Async("AsyncMapStructureTransactionExecutor")
@EventListener(classes = OpenMapTransactionAsyncEvent.class)
public void handleOpenTransactionEvent(@NonNull final OpenMapTransactionAsyncEvent event) {
infoEvent(event);
handleOpenTransaction(event.getTransactionIdentifierDto());
}

@Async("AsyncMapTransactionExecutor")
@Async("AsyncMapStructureTransactionExecutor")
@EventListener(classes = AddMapPackageAsyncEvent.class)
public void handleAddMapPackageEvent(@NonNull final AddMapPackageAsyncEvent event) {
traceEvent(event);
handleAddMapPackage(event.getTransactionalMapEntityPackage());
}

@Async("AsyncMapTransactionExecutor")
@Async("AsyncMapStructureTransactionExecutor")
@EventListener(classes = CommitMapTransactionAsyncEvent.class)
public void handleCommitTransactionEvent(@NonNull final CommitMapTransactionAsyncEvent event) {
infoEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import com.recom.event.event.async.map.commit.CommitMapTopographyTransactionAsyncEvent;
import com.recom.event.event.async.map.open.OpenMapTopographyTransactionAsyncEvent;
import com.recom.event.listener.generic.maprelated.TransactionalMapRelatedPackageEventListenerTemplate;
import com.recom.event.listener.topography.ChunkCoordinate;
import com.recom.event.listener.topography.ChunkDimensions;
import com.recom.event.listener.topography.ChunkHelper;
import com.recom.event.listener.util.ChunkCoordinate;
import com.recom.event.listener.util.ChunkDimensions;
import com.recom.event.listener.util.ChunkHelper;
import com.recom.persistence.map.GameMapPersistenceLayer;
import com.recom.persistence.map.chunk.topography.MapTopographyChunkPersistenceLayer;
import com.recom.service.SerializationService;
Expand All @@ -32,7 +32,7 @@

@Slf4j
@Component
public class MapTopographyEntityScannerTransactionEventListener extends TransactionalMapRelatedPackageEventListenerTemplate<TransactionalMapTopographyPackageDto, SquareKilometerTopographyChunk, MapTopographyDto> {
public class MapTopographyScannerTransactionEventListener extends TransactionalMapRelatedPackageEventListenerTemplate<TransactionalMapTopographyPackageDto, SquareKilometerTopographyChunk, MapTopographyDto> {

@NonNull
private final SerializationService serializationService;
Expand All @@ -42,7 +42,7 @@ public class MapTopographyEntityScannerTransactionEventListener extends Transact
private final MapTopographyChunkPersistenceLayer mapTopographyChunkPersistenceLayer;


public MapTopographyEntityScannerTransactionEventListener(
public MapTopographyScannerTransactionEventListener(
@NonNull final TransactionTemplate transactionTemplate,
@NonNull final MapTopographyChunkPersistenceLayer entityPersistenceLayer,
@NonNull final MapTransactionValidatorService<MapTopographyDto, TransactionalMapTopographyPackageDto> mapTransactionValidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.recom.event.BaseRecomEntityScannerEventListener;
import com.recom.event.listener.generic.generic.MapLocatedEntityPersistable;
import com.recom.event.listener.generic.generic.TransactionalMapEntityPackable;
import com.recom.event.listener.topography.ChunkCoordinate;
import com.recom.event.listener.topography.ChunkHelper;
import com.recom.event.listener.topography.MapScanSessionIdentifierData;
import com.recom.event.listener.util.ChunkCoordinate;
import com.recom.event.listener.util.ChunkHelper;
import com.recom.event.listener.util.MapScanSessionIdentifierData;
import com.recom.model.map.MapTransaction;
import com.recom.persistence.map.GameMapPersistenceLayer;
import com.recom.persistence.map.chunk.structure.MapStructureChunkPersistenceLayer;
Expand Down Expand Up @@ -88,12 +88,18 @@ protected boolean processTransaction(@NonNull final String sessionIdentifier) {

// open new transaction
final Boolean transactionExecuted = transactionTemplate.execute(status -> {
final long startTransaction = System.currentTimeMillis();
log.debug("... execute transaction {}!", sessionIdentifier);
log.debug("... merge gameMap");
final GameMap gameMap = entityManager.merge(maybeGameMap.get());
log.debug("... merge mapChunk");
final SquareKilometerStructureChunk mapChunk = entityManager.merge(maybeChunk.get());

log.debug("... init entity mapper!");
entityMapper.init();
mapChunk.setStatus(ChunkStatus.CLOSED);

log.debug("... set gameMap and mapChunk to entities!");
final List<ENTITY_TYPE> distinctEntities = existingTransaction.getPackages().stream()
.flatMap(packageDto -> packageDto.getEntities().stream())
.distinct()
Expand All @@ -106,7 +112,7 @@ protected boolean processTransaction(@NonNull final String sessionIdentifier) {

entityPersistenceLayer.saveAll(distinctEntities);
mapStructureChunkPersistenceLayer.save(mapChunk);
log.info("Transaction named {} persisted!", sessionIdentifier);
log.info("Transaction named {} persisted in {}ms!", sessionIdentifier, System.currentTimeMillis() - startTransaction);

return true;
});
Expand All @@ -121,11 +127,14 @@ protected boolean processTransaction(@NonNull final String sessionIdentifier) {
log.warn("No map meta found for transaction named {}!", sessionIdentifier);
return false;
}
} else {
log.warn("Invalid Transaction {}!", sessionIdentifier);
return false;
}
} else {
log.warn("No transaction named {} found to process!", sessionIdentifier);
return false;
}

log.warn("No transaction named {} found to process!", sessionIdentifier);
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.recom.event.BaseRecomEntityScannerEventListener;
import com.recom.event.listener.generic.generic.MapRelatedEntityPersistable;
import com.recom.event.listener.generic.generic.TransactionalMapEntityPackable;
import com.recom.event.listener.topography.ChunkHelper;
import com.recom.event.listener.util.ChunkHelper;
import com.recom.model.map.MapTransaction;
import com.recom.persistence.map.GameMapPersistenceLayer;
import com.recom.service.map.MapTransactionValidatorService;
Expand Down Expand Up @@ -60,11 +60,14 @@ protected boolean processTransaction(@NonNull final String sessionIdentifier) {
final Optional<GameMap> maybeGameMap = gameMapPersistenceLayer.findByName(mapName);

if (maybeGameMap.isPresent()) {
log.info("... found existing chunk for transaction named {}!", sessionIdentifier);
final ENTITY_TYPE entity = mapTransactionToEntity(sessionIdentifier, maybeGameMap.get(), existingTransaction.getPackages());

final Boolean transactionExecuted = transactionTemplate.execute(status -> {
final long startTransaction = System.currentTimeMillis();
log.debug("... execute transaction {}!", sessionIdentifier);
entityPersistenceLayer.save(entity);
log.info("Transaction named {} persisted!", sessionIdentifier);
log.info("Transaction named {} persisted in {}ms!", sessionIdentifier, System.currentTimeMillis() - startTransaction);

return true;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.event.listener.topography;
package com.recom.event.listener.util;

public record ChunkCoordinate(long x, long z) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.event.listener.topography;
package com.recom.event.listener.util;

public record ChunkDimensions(int x, int z) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.event.listener.topography;
package com.recom.event.listener.util;

import com.recom.dto.map.scanner.topography.MapTopographyDto;
import com.recom.dto.map.scanner.topography.TransactionalMapTopographyPackageDto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.event.listener.topography;
package com.recom.event.listener.util;

public record MapScanSessionIdentifierData(String mapName ,ChunkCoordinate chunkCoordinate) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.recom.entity.map.GameMap;
import com.recom.entity.map.SquareKilometerStructureChunk;
import com.recom.event.listener.generic.generic.MapRelatedEntityPersistable;
import com.recom.event.listener.topography.ChunkCoordinate;
import com.recom.event.listener.util.ChunkCoordinate;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.recom.entity.map.GameMap;
import com.recom.entity.map.SquareKilometerTopographyChunk;
import com.recom.event.listener.generic.generic.MapRelatedEntityPersistable;
import com.recom.event.listener.topography.ChunkCoordinate;
import com.recom.event.listener.util.ChunkCoordinate;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public GameMap create(@NonNull final MapCreateRequestDto mapCreateRequestDto) {

final GameMap persistedGameMap = gameMapPersistenceLayer.save(newGameMap);

mapTopographyChunkScanRequestNotificationService.requestMapTopographyChunkScan(persistedGameMap);
mapStructureChunkScanRequestNotificationService.requestMapStructureChunkScan(persistedGameMap);
// mapTopographyChunkScanRequestNotificationService.requestMapTopographyChunkScan(persistedGameMap);
// mapStructureChunkScanRequestNotificationService.requestMapStructureChunkScan(persistedGameMap);

return persistedGameMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public boolean isValidTransaction(@NonNull final MapTransaction<DTO_TYPE, PACKAG
final Integer commitPackageNumber = transaction.getCommitTransactionIdentifier().getPackageOrder();
final Integer expectedPackageSize = commitPackageNumber - 1;

if (transaction.getPackages().isEmpty()) {
log.warn("Packages are empty!");
return false;
}
// if (transaction.getPackages().isEmpty()) {
// log.warn("Packages are empty!");
// return false;
// }

if (transaction.getPackages().size() != expectedPackageSize) {
log.warn("Package size does not match expected size {} != {}", transaction.getPackages().size(), expectedPackageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.recom.entity.map.ChunkStatus;
import com.recom.entity.map.GameMap;
import com.recom.entity.map.SquareKilometerStructureChunk;
import com.recom.event.listener.topography.ChunkHelper;
import com.recom.event.listener.util.ChunkHelper;
import com.recom.model.message.MessageContainer;
import com.recom.model.message.SingleMessage;
import com.recom.persistence.map.GameMapPersistenceLayer;
Expand All @@ -17,6 +17,7 @@
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
Expand Down Expand Up @@ -71,6 +72,7 @@ private Optional<SquareKilometerStructureChunk> getChunkToScanNext(@NonNull fina
}

@Synchronized
@Transactional(readOnly = false)
public void requestMapStructureChunkScan(@NonNull final GameMap gameMap) {
getChunkToScanNext(gameMap)
.ifPresent(nextChunk -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.recom.entity.map.ChunkStatus;
import com.recom.entity.map.GameMap;
import com.recom.entity.map.SquareKilometerTopographyChunk;
import com.recom.event.listener.topography.ChunkHelper;
import com.recom.event.listener.util.ChunkHelper;
import com.recom.model.message.MessageContainer;
import com.recom.model.message.SingleMessage;
import com.recom.persistence.map.GameMapPersistenceLayer;
Expand All @@ -17,6 +17,7 @@
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
Expand Down Expand Up @@ -71,6 +72,7 @@ private Optional<SquareKilometerTopographyChunk> getChunkToScanNext(@NonNull fin
}

@Synchronized
@Transactional(readOnly = false)
public void requestMapTopographyChunkScan(@NonNull final GameMap gameMap) {
getChunkToScanNext(gameMap)
.ifPresent(nextChunk -> {
Expand Down
Loading

0 comments on commit a95576d

Please sign in to comment.