Skip to content

Commit

Permalink
better logging, fix npe's
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Mar 16, 2024
1 parent 01db006 commit 5e00ea5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
7 changes: 5 additions & 2 deletions choam/src/main/java/com/salesforce/apollo/choam/CHOAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,8 @@ public Logger log() {
public void nextView(PendingView pendingView) {
var previous = CHOAM.this.pendingView.getAndSet(pendingView);
log.info("Pending view: {} size: {} previous: {} size: {} on: {}", pendingView.diadem,
pendingView.context.size(), previous.diadem, previous.context.size(), params.member().getId());
pendingView.context.size(), previous == null ? "<null>" : previous.diadem,
previous == null ? "<null>" : previous.context.size(), params.member().getId());
}

@Override
Expand Down Expand Up @@ -1371,7 +1372,9 @@ public void nextView(PendingView pendingView) {
params.context().setContext(pendingView.context);
CHOAM.this.diadem.set(pendingView.diadem);
CHOAM.this.pendingView.set(pendingView);
assembly.stop();
if (assembly != null) {
assembly.start();
}
transitions.nextView();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public GenesisAssembly(ViewContext vc, CommonCommunications<Terminal, ?> comms,
.stream()
.collect(Collectors.toMap(Member::getId, m -> m));
if (!Dag.validate(nextAssembly.size())) {
log.error("Invalid cardinality: {} for: {} on: {}", nextAssembly.size(), view.context().getId(),
params().member().getId());
throw new IllegalStateException("Invalid BFT cardinality: " + nextAssembly.size());
}
this.genesisMember = genesisMember;
Expand Down Expand Up @@ -151,13 +149,19 @@ public void gather() {

@Override
public void gather(List<ByteString> preblock, boolean last) {
preblock.stream().map(bs -> {
try {
return Join.parseFrom(bs);
} catch (InvalidProtocolBufferException e) {
return null;
}
}).filter(Objects::nonNull).filter(j -> !j.equals(Join.getDefaultInstance())).forEach(this::join);
preblock.stream()
.map(bs -> {
try {
return Join.parseFrom(bs);
} catch (InvalidProtocolBufferException e) {
return null;
}
})
.filter(Objects::nonNull)
.filter(j -> !j.equals(Join.getDefaultInstance()))
.peek(
j -> log.info("Gathering: {} on: {}", Digest.from(j.getMember().getId()), params().member().getId()))
.forEach(this::join);
}

@Override
Expand All @@ -170,6 +174,8 @@ public void nominate() {
.map(p -> view.generateValidation(p.join.getMember()))
.forEach(validations::addValidations);
ds.setValue(validations.build().toByteString());
log.info("Nominations of: {} validations: {} on: {}", params().context().getId(),
validations.getValidationsCount(), params().member().getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ByteString get() {
}

public void setValue(ByteString value) {
log.trace("resetting value: " + value);
log.trace("resetting value: " + (value == null ? "null" : String.valueOf(value.size())));
this.value = value;
latch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package com.salesforce.apollo.stereotomy.jks;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalCause;
import com.salesforce.apollo.cryptography.cert.BcX500NameDnImpl;
import com.salesforce.apollo.cryptography.cert.CertExtension;
import com.salesforce.apollo.cryptography.cert.Certificates;
Expand All @@ -18,6 +21,7 @@
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
Expand All @@ -33,14 +37,22 @@
* @author hal.hildebrand
*/
public class JksKeyStore implements StereotomyKeyStore {
private static final Logger log = LoggerFactory.getLogger(JksKeyStore.class);
protected final KeyStore keyStore;
protected final Supplier<char[]> passwordProvider;
private final Lock lock = new ReentrantLock();
private static final Logger log = LoggerFactory.getLogger(JksKeyStore.class);

protected final KeyStore keyStore;
protected final Supplier<char[]> passwordProvider;
private final Lock lock = new ReentrantLock();
private final Cache<String, KeyPair> cached;

public JksKeyStore(KeyStore keyStore, Supplier<char[]> passwordProvider) {
this.keyStore = keyStore;
this.passwordProvider = passwordProvider;
cached = Caffeine.newBuilder()
.maximumSize(4)
.expireAfterWrite(Duration.ofMinutes(10))
.removalListener(
(String alias, KeyPair ks, RemovalCause cause) -> log.trace("KeyPair was removed ({})", cause))
.build();
}

public static String coordinateOrdering(KeyCoordinates coords) {
Expand Down Expand Up @@ -134,23 +146,23 @@ public void storeNextKey(KeyCoordinates keyCoordinates, KeyPair keyPair) {
}
}

private Optional<KeyPair> get(String alias, KeyCoordinates keyCoordinates) {
private KeyPair fetch(String alias, KeyCoordinates keyCoordinates) {
try {
if (!keyStore.containsAlias(alias)) {
return Optional.empty();
return null;
}
} catch (KeyStoreException e) {
log.error("Unable to query keystore for: {} : {}", keyCoordinates != null ? keyCoordinates : alias,
e.getMessage());
return Optional.empty();
return null;
}
Certificate cert;
try {
cert = keyStore.getCertificate(alias);
} catch (KeyStoreException e) {
log.error("Unable to retrieve certificate for: {} : {}", keyCoordinates != null ? keyCoordinates : alias,
e.getMessage());
return Optional.empty();
return null;
}
var publicKey = cert.getPublicKey();
PrivateKey privateKey;
Expand All @@ -159,8 +171,12 @@ private Optional<KeyPair> get(String alias, KeyCoordinates keyCoordinates) {
} catch (UnrecoverableKeyException | KeyStoreException | NoSuchAlgorithmException e) {
log.error("Unable to retrieve certificate for: {} : {}", keyCoordinates != null ? keyCoordinates : alias,
e.getMessage());
return Optional.empty();
return null;
}
return Optional.of(new KeyPair(publicKey, privateKey));
return new KeyPair(publicKey, privateKey);
}

private Optional<KeyPair> get(String alias, KeyCoordinates keyCoordinates) {
return Optional.ofNullable(cached.get(alias, key -> fetch(key, keyCoordinates)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ private <T> void completeIt(CompletableFuture<T> result, HashMultiset<T> gathere
}

private boolean failedMajority(CompletableFuture<?> result, int maxAgree, String operation) {
log.error("Unable to achieve majority read: {}, max: {} required: {} on: {}", operation, maxAgree,
log.debug("Unable to achieve majority read: {}, max: {} required: {} on: {}", operation, maxAgree,
context.majority(true), member.getId());
return result.completeExceptionally(new CompletionException(
"Unable to achieve majority read: " + operation + ", max: " + maxAgree + " required: " + context.majority(true)
Expand Down

0 comments on commit 5e00ea5

Please sign in to comment.