Skip to content

Commit

Permalink
WIP: add tracing to debug the CI issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Pfeil committed Jan 9, 2025
1 parent 04bff17 commit 885c42e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import java.util.Optional;
import java.util.stream.Stream;

import edu.kit.datamanager.pit.common.PidAlreadyExistsException;
import edu.kit.datamanager.pit.common.PidNotFoundException;
import edu.kit.datamanager.pit.common.*;
import edu.kit.datamanager.pit.configuration.ApplicationProperties;
import edu.kit.datamanager.pit.configuration.PidGenerationProperties;
import edu.kit.datamanager.pit.common.RecordValidationException;
import edu.kit.datamanager.pit.domain.PIDRecord;
import edu.kit.datamanager.pit.elasticsearch.PidRecordElasticRepository;
import edu.kit.datamanager.pit.elasticsearch.PidRecordElasticWrapper;
Expand Down Expand Up @@ -97,25 +95,29 @@ public ResponseEntity<PIDRecord> createPID(
setPid(pidRecord);
}

LOG.trace("Start validation of PID record.");
this.typingService.validate(pidRecord);

if (dryrun) {
// dryrun only does validation. Stop now and return as we would later on.
return ResponseEntity.status(HttpStatus.OK).eTag(quotedEtag(pidRecord)).body(pidRecord);
}

LOG.trace("Registering PID.");
String pid = this.typingService.registerPid(pidRecord);
pidRecord.setPid(pid);

if (applicationProps.getStorageStrategy().storesModified()) {
LOG.trace("Storing PID locally.");
storeLocally(pid, true);
}
PidRecordMessage message = PidRecordMessage.creation(
pid,
"", // TODO parameter is depricated and will be removed soon.
"", // TODO parameter is deprecated and will be removed soon.
AuthenticationHelper.getPrincipal(),
ControllerUtils.getLocalHostname());
try {
LOG.trace("Sending message to messaging service.");
this.messagingService.send(message);
} catch (Exception e) {
LOG.error("Could not notify messaging service about the following message: {}", message);
Expand All @@ -133,27 +135,39 @@ private void setPid(PIDRecord pidRecord) throws IOException {
boolean allowsCustomPids = pidGenerationProperties.isCustomClientPidsEnabled();

if (allowsCustomPids && hasCustomPid) {
// in this only case, we do not have to generate a PID
LOG.trace("Custom PID is allowed and given. No need to generate a PID.");
// in this only case, we do not have to generate a PID,
// but we have to check if the PID is already registered and return an error if so
String prefix = this.typingService.getPrefix().orElseThrow(() -> new IOException("No prefix configured."));
String prefix = this.typingService.getPrefix()
.orElseThrow(() -> new InvalidConfigException("No prefix configured."));
String maybeSuffix = pidRecord.getPid();
String pid = PidSuffix.asPrefixedChecked(maybeSuffix, prefix);
LOG.trace("Prefix: {}, Suffix: {}, PID: {}", prefix, maybeSuffix, pid);
boolean isRegisteredPid = this.typingService.isPidRegistered(pid);
if (isRegisteredPid) {
throw new PidAlreadyExistsException(pidRecord.getPid());
}
} else {
LOG.trace("Custom PID is not allowed or not given. Generating PID.");
// In all other (usual) cases, we have to generate a PID.
// We store only the suffix in the pid field.
// The registration at the PID service will preprend the prefix.

// The registration at the PID service will prepend the prefix.
long maximumGeneratedSuffixes = 100;
Stream<PidSuffix> suffixStream = suffixGenerator.infiniteStream();
Optional<PidSuffix> maybeSuffix = Streams.failableStream(suffixStream)
// With failible streams, we can throw exceptions.
.filter(suffix -> !this.typingService.isPidRegistered(suffix))
.filter(suffix -> {
LOG.trace("Checking if PID suffix is registered: {}", suffix);
return !this.typingService.isPidRegistered(suffix);
})
.stream() // back to normal java streams
.findFirst(); // as the stream is infinite, we should always find a prefix.
PidSuffix suffix = maybeSuffix.orElseThrow(() -> new IOException("Could not generate PID suffix."));
.limit(maximumGeneratedSuffixes)
.findFirst();
PidSuffix suffix = maybeSuffix
.orElseThrow(() -> new ExternalServiceException(
"Could not generate PID suffix which did not exist yet. Tried "
+ maximumGeneratedSuffixes
+ " times."
));
pidRecord.setPid(suffix.get());
}
}
Expand Down Expand Up @@ -263,6 +277,7 @@ public ResponseEntity<PIDRecord> getRecord(
}

private void saveToElastic(PIDRecord rec) {
LOG.trace("Saving PID record to elastic search.");
this.elastic.ifPresent(
database -> database.save(
new PidRecordElasticWrapper(rec, typingService.getOperations())
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/test/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ management.endpoints.web.exposure.include: *
#logging.level.root: ERROR
#logging.level.edu.kit.datamanager.doip:TRACE
logging.level.edu.kit: DEBUG
logging.edu.kit.datamanager.pit.web.impl: TRACE
#logging.level.org.springframework.transaction: TRACE
logging.level.org.springframework: WARN
logging.level.org.springframework.amqp: WARN
Expand Down

0 comments on commit 885c42e

Please sign in to comment.