Skip to content

Commit 073187a

Browse files
committed
- started adding some arbitrary logging
- started adding javadocs - created some integration tests - fixed bug when creating already existing message in persistence layer
1 parent 759d785 commit 073187a

File tree

11 files changed

+358
-28
lines changed

11 files changed

+358
-28
lines changed

sampling-message-server/src/it/java/de/dhbw/ravensburg/verteiltesysteme/server/samplingmessageserver/SamplingMessageServerIT.java

Lines changed: 201 additions & 1 deletion
Large diffs are not rendered by default.

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/Main.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,38 @@
33
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
44
import lombok.extern.slf4j.Slf4j;
55

6+
/**
7+
* Class providing the application entry point.
8+
*/
69
@Slf4j
710
public class Main {
811

9-
//TODO; implement Apache CLI https://stackoverflow.com/a/367714/876724
12+
/**
13+
* Application Entry Point
14+
* Parsing the CLI arguments and initializing a {@see service.ServiceEndpoint}
15+
*
16+
* @param args CLI arguments
17+
*/
1018
public static void main(String[] args) {
1119

1220
final ServiceConfig serviceConfig = new ServiceConfig(255, 32, 32, 8080);
21+
1322
final ServiceEndpoint serviceEndpoint = new ServiceEndpoint(serviceConfig);
1423

24+
/*
25+
Graceful shutdown on shutdown hook
26+
*/
1527
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
1628
log.info("Shutting Hook received.");
1729
serviceEndpoint.shutdown();
1830
log.info("Bye bye...");
1931
}));
2032

2133
serviceEndpoint.init();
34+
try {
35+
serviceEndpoint.awaitTermination();
36+
} catch (InterruptedException e) {
37+
log.error("Interrupted while running: ", e);
38+
}
2239
}
2340
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/ServiceEndpoint.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.dhbw.ravensburg.verteiltesysteme.server;
22

3+
import de.dhbw.ravensburg.verteiltesysteme.server.persistence.DatabaseAccessObject;
34
import de.dhbw.ravensburg.verteiltesysteme.server.persistence.DatabaseAccessObjectImpl;
45
import de.dhbw.ravensburg.verteiltesysteme.server.persistence.FakePersistence;
56
import de.dhbw.ravensburg.verteiltesysteme.server.rpc.RpcService;
67
import de.dhbw.ravensburg.verteiltesysteme.server.service.ContractValidator;
8+
import de.dhbw.ravensburg.verteiltesysteme.server.service.SamplingMessageService;
79
import de.dhbw.ravensburg.verteiltesysteme.server.service.SamplingMessageServiceImpl;
810
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
911
import io.grpc.*;
@@ -12,16 +14,31 @@
1214
import java.io.IOException;
1315
import java.net.SocketAddress;
1416

17+
/**
18+
* This class represents a high level application service.
19+
* It will initialize a gRPC server with a respective gRPC service implementation.
20+
*/
1521
@Slf4j
1622
public class ServiceEndpoint {
1723
private final Server server;
1824
private final ServiceConfig serviceConfig;
1925

20-
ServiceEndpoint(final ServiceConfig serviceConfig) {
26+
27+
/**
28+
* Instantiate a ServiceEndpoint object with the given {@see service.ServiceConfig}
29+
* Prepares the gRPC server to be initialized afterwards.
30+
*
31+
* @param serviceConfig Basic service configuration
32+
*/
33+
public ServiceEndpoint(final ServiceConfig serviceConfig) {
2134
log.info("Preparing Service Endpoint");
2235
this.serviceConfig = serviceConfig;
2336

24-
final RpcService rpcService = new RpcService(new SamplingMessageServiceImpl(new DatabaseAccessObjectImpl(new FakePersistence<>()), new ContractValidator(this.serviceConfig)));
37+
final DatabaseAccessObject databaseAccessObject = new DatabaseAccessObjectImpl(new FakePersistence<>());
38+
final ContractValidator contractValidator = new ContractValidator(this.serviceConfig);
39+
final SamplingMessageService samplingMessageService = new SamplingMessageServiceImpl(databaseAccessObject, contractValidator);
40+
41+
final RpcService rpcService = new RpcService(samplingMessageService);
2542

2643
this.server = ServerBuilder
2744
.forPort(this.serviceConfig.getServiceEndpointListeningPort())
@@ -41,19 +58,27 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
4158
};
4259
}
4360

61+
/**
62+
* Initialize the ServiceEndpoint after instantiation.
63+
*/
4464
public void init() {
4565
try {
4666
log.info("Starting Service Endpoint");
4767
this.server.start();
4868
log.info(String.format("Server listening on TCP port: %s", this.serviceConfig.getServiceEndpointListeningPort()));
49-
this.server.awaitTermination();
5069
} catch (IOException e) {
5170
log.error(e.getMessage());
52-
} catch (InterruptedException e) {
53-
log.error(e.getMessage());
71+
this.shutdown();
5472
}
5573
}
5674

75+
public void awaitTermination() throws InterruptedException {
76+
this.server.awaitTermination();
77+
}
78+
79+
/**
80+
* Perform graceful shutdown of the endpoint.
81+
*/
5782
public void shutdown() {
5883
log.info("Shutting down gRPC Server");
5984
this.server.shutdownNow();

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/persistence/DatabaseAccessObject.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,49 @@
66
import java.time.Instant;
77
import java.util.Optional;
88

9+
/**
10+
* Basic interface of the persistence layer.
11+
* Provides general purpose storage operations.
12+
*/
913
public interface DatabaseAccessObject {
14+
/**
15+
* Gets an already existing {@see DatabaseSamplingMessage}
16+
*
17+
* @param messageName the desired sampling message name
18+
* @return returns a {@see java.util.Optional} eventually containing a {@see DatabaseSamplingMessage}, if there was already an existing one. An empty {@see java.util.Optional} is beeing returned, if none was existing.
19+
*/
1020
Optional<DatabaseSamplingMessage> getSamplingMessage(@NonNull String messageName);
1121

22+
/**
23+
* Stores a {@link DatabaseSamplingMessage}
24+
*
25+
* @param messageName the unique identifier of the object to be stored
26+
* @param databaseSamplingMessage the object to be stored
27+
* @return true if there was no previous {@see DatabaseSamplingMessage} with the same unique identifier. false if the desired messageName is already taken.
28+
*/
1229
boolean createSamplingMessage(@NonNull String messageName, @NonNull DatabaseSamplingMessage databaseSamplingMessage);
1330

31+
/**
32+
* Stores additional information to an already existing {@link DatabaseSamplingMessage}
33+
*
34+
* @param messageName the unique identifier of the object to be modified
35+
* @param messageContent the new content of the object
36+
* @param updateTimestamp the timestamp of the last write
37+
* @return true if the object was found, false if there was no object associated with the provided unique key
38+
*/
1439
boolean writeSamplingMessageContentAndTimestamp(@NonNull final String messageName, @NonNull final String messageContent, @NonNull final Instant updateTimestamp);
1540

41+
/**
42+
* Delete an already existing object and free the unique identifier respectively
43+
* @param messageName the unique key of the object
44+
* @return true if the object was found, false if there was no object associated with the provided unique key
45+
*/
1646
boolean deleteSamplingMessage(@NonNull String messageName);
1747

48+
49+
/**
50+
* Get the total count of persistent stored messages.
51+
* @return currently stored messages
52+
*/
1853
long getTotalMessageCount();
1954
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/persistence/DatabaseAccessObjectImpl.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,39 @@ public DatabaseAccessObjectImpl(final @NonNull FakePersistence<String, DatabaseS
1818

1919
@Override
2020
public Optional<DatabaseSamplingMessage> getSamplingMessage(@NonNull final String messageName) {
21+
log.info(String.format("Getting DatabaseSamplingMessage with the messageName: %s.", messageName));
22+
2123
final DatabaseSamplingMessage databaseSamplingMessage = fakePersistence.get(messageName);
24+
2225
if (databaseSamplingMessage == null) {
2326
log.info(String.format("DatabaseSamplingMessage with the messageName: %s not found.", messageName));
27+
return Optional.empty();
28+
} else {
29+
log.info(String.format("DatabaseSamplingMessage with the messageName: %s successfully found.", messageName));
30+
31+
return Optional.of(databaseSamplingMessage);
2432
}
25-
return Optional.of(databaseSamplingMessage);
2633
}
2734

2835
@Override
2936
public boolean createSamplingMessage(@NonNull final String messageName, @NonNull final DatabaseSamplingMessage databaseSamplingMessage) {
37+
log.info(String.format("Creating DatabaseSamplingMessage with the messageName: %s.", messageName));
38+
3039
if (fakePersistence.putIfAbsent(messageName, databaseSamplingMessage) == null) {
40+
log.info(String.format("DatabaseSamplingMessage with the messageName: %s created successfully.", messageName));
41+
3142
return true;
3243
}
3344
log.info(String.format("DatabaseSamplingMessage with the messageName: %s already exists.", messageName));
3445
return false;
3546
}
3647

3748
public boolean writeSamplingMessageContentAndTimestamp(@NonNull final String messageName, @NonNull final String messageContent, @NonNull final Instant updateTimestamp) {
49+
log.info(String.format("Writing DatabaseSamplingMessage with the messageName: %s.", messageName));
50+
3851
if (fakePersistence.computeIfPresent(messageName, (key, value) -> value.setMessageContent(messageContent).setMessageUpdateTimestamp(updateTimestamp)) != null) {
52+
log.info(String.format("DatabaseSamplingMessage with the messageName: %s written successfully", messageName));
53+
3954
return true;
4055
}
4156
log.info(String.format("DatabaseSamplingMessage with the messageName: %s not found.", messageName));
@@ -44,7 +59,10 @@ public boolean writeSamplingMessageContentAndTimestamp(@NonNull final String mes
4459

4560
@Override
4661
public boolean deleteSamplingMessage(@NonNull final String messageName) {
62+
log.info(String.format("Deleting DatabaseSamplingMessage with the messageName: %s.", messageName));
63+
4764
if (fakePersistence.remove(messageName) != null) {
65+
log.info(String.format("DatabaseSamplingMessage with the messageName: %s deleted successfully.", messageName));
4866
return true;
4967
}
5068
log.info(String.format("DatabaseSamplingMessage with the messageName: %s not found.", messageName));
@@ -53,6 +71,8 @@ public boolean deleteSamplingMessage(@NonNull final String messageName) {
5371

5472
@Override
5573
public long getTotalMessageCount() {
56-
return fakePersistence.size();
74+
final long messageCount = fakePersistence.size();
75+
log.info(String.format("Getting the Current count of DatabaseSamplingMessages: %d.", messageCount));
76+
return messageCount;
5777
}
5878
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/rpc/RpcService.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ private static SamplingMessageGrpcService.StatusCode fromServiceResultStatus(fin
3939
@Override
4040
public void createSamplingMessage(SamplingMessageGrpcService.CreateSamplingMessageRequest request,
4141
StreamObserver<SamplingMessageGrpcService.CreateSamplingMessageResponse> responseObserver) {
42+
log.info(String.format("Received WriteSamplingMessageRequest for messageName: %s with lifetime in sec: %d", request.getMessageName(), request.getLifetimeInSec()));
43+
4244
log.info("createSamplingMessage: " + request.getMessageName() + "\t" + request.getLifetimeInSec());
4345
final ServiceResult serviceResult = samplingMessageService.createSamplingMessage(request.getMessageName(), request.getLifetimeInSec());
4446
SamplingMessageGrpcService.CreateSamplingMessageResponse createSamplingMessageResponse = SamplingMessageGrpcService.CreateSamplingMessageResponse
@@ -53,7 +55,7 @@ public void createSamplingMessage(SamplingMessageGrpcService.CreateSamplingMessa
5355
@Override
5456
public void writeSamplingMessage(SamplingMessageGrpcService.WriteSamplingMessageRequest request,
5557
StreamObserver<SamplingMessageGrpcService.WriteSamplingMessageResponse> responseObserver) {
56-
log.info("writeSamplingMessage: " + request.getMessageContent());
58+
log.info(String.format("Received WriteSamplingMessageRequest for messageName: %s ", request.getMessageName()));
5759

5860
final ServiceResult serviceResult = samplingMessageService.writeSamplingMessage(request.getMessageName(), request.getMessageContent());
5961
SamplingMessageGrpcService.WriteSamplingMessageResponse writeSamplingMessageResponse = SamplingMessageGrpcService.WriteSamplingMessageResponse
@@ -68,7 +70,8 @@ public void writeSamplingMessage(SamplingMessageGrpcService.WriteSamplingMessage
6870
@Override
6971
public void clearSamplingMessage(SamplingMessageGrpcService.ClearSamplingMessageRequest request,
7072
StreamObserver<SamplingMessageGrpcService.ClearSamplingMessageResponse> responseObserver) {
71-
log.info("clearSamplingMessage");
73+
log.info(String.format("Received ClearSamplingMessageRequest for messageName: %s ", request.getMessageName()));
74+
7275
final ServiceResult serviceResult = samplingMessageService.clearSamplingMessage(request.getMessageName());
7376
SamplingMessageGrpcService.ClearSamplingMessageResponse clearSamplingMessageResponse = SamplingMessageGrpcService.ClearSamplingMessageResponse
7477
.newBuilder()
@@ -82,7 +85,7 @@ public void clearSamplingMessage(SamplingMessageGrpcService.ClearSamplingMessage
8285
@Override
8386
public void readSamplingMessage(SamplingMessageGrpcService.ReadSamplingMessageRequest request,
8487
StreamObserver<SamplingMessageGrpcService.ReadSamplingMessageResponse> responseObserver) {
85-
log.info("readSamplingMessage: " + request.getMessageName());
88+
log.info(String.format("Received ReadSamplingMessageRequest for messageName: %s ", request.getMessageName()));
8689

8790
final ServiceResult<SamplingMessage> samplingMessageServiceResult = samplingMessageService.readSamplingMessage(request.getMessageName());
8891
final SamplingMessageGrpcService.StatusCode statusCode = fromServiceResultStatus(samplingMessageServiceResult.getStatus());
@@ -109,7 +112,7 @@ public void readSamplingMessage(SamplingMessageGrpcService.ReadSamplingMessageRe
109112
@Override
110113
public void getSamplingMessageStatus(SamplingMessageGrpcService.GetSamplingMessageStatusRequest request,
111114
StreamObserver<SamplingMessageGrpcService.GetSamplingMessageStatusResponse> responseObserver) {
112-
log.info("getSamplingMessageStatus");
115+
log.info(String.format("Received GetSamplingMessageStatusRequest for messageName: %s ", request.getMessageName()));
113116

114117
final ServiceResult<SamplingMessageStatus> samplingMessageServiceResult = samplingMessageService.getSamplingMessageStatus(request.getMessageName());
115118
final SamplingMessageGrpcService.StatusCode statusCode = fromServiceResultStatus(samplingMessageServiceResult.getStatus());
@@ -136,7 +139,7 @@ public void getSamplingMessageStatus(SamplingMessageGrpcService.GetSamplingMessa
136139
@Override
137140
public void deleteSamplingMessage(SamplingMessageGrpcService.DeleteSamplingMessageRequest request,
138141
StreamObserver<SamplingMessageGrpcService.DeleteSamplingMessageResponse> responseObserver) {
139-
log.info("deleteSamplingMessage");
142+
log.info(String.format("Received DeleteSamplingMessageRequest for messageName: %s ", request.getMessageName()));
140143

141144
final ServiceResult serviceResult = samplingMessageService.deleteSamplingMessage(request.getMessageName());
142145
SamplingMessageGrpcService.DeleteSamplingMessageResponse deleteSamplingMessageResponse = SamplingMessageGrpcService.DeleteSamplingMessageResponse

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/service/ContractValidator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.dhbw.ravensburg.verteiltesysteme.server.service;
22

3+
import lombok.Data;
34
import lombok.NonNull;
45

56
import java.time.Duration;
@@ -10,10 +11,11 @@
1011
* Utilizes ServiceConfig
1112
* Provides validation patterns for incoming transit data
1213
*/
14+
@Data
1315
public class ContractValidator {
1416
private ServiceConfig serviceConfig;
1517

16-
public ContractValidator(ServiceConfig serviceConfig) {
18+
public ContractValidator(final ServiceConfig serviceConfig) {
1719
this.serviceConfig = serviceConfig;
1820
}
1921

@@ -26,10 +28,15 @@ public boolean isInvalidMessageContent(@NonNull final String messageContent) {
2628
}
2729

2830
public boolean isMessageCountExceeded(final Long totalMessageCount) {
29-
return totalMessageCount >= ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_COUNT;
31+
return totalMessageCount >= serviceConfig.getMaximumSamplingMessageContentSize();
3032
}
3133

3234
public boolean isValid(@NonNull final Instant creationTime, @NonNull final Duration lifetime) {
33-
return Instant.now().isBefore(creationTime.plus(lifetime));
35+
//TODO; prevent overflow
36+
return Instant.now().minus(lifetime).isBefore(creationTime.plus(lifetime));
37+
}
38+
39+
public int getCurrentSamplingMaximumMessageCount() {
40+
return this.serviceConfig.getMaximumSamplingMessageCount();
3441
}
3542
}

0 commit comments

Comments
 (0)