Skip to content

Commit

Permalink
Edits around peer shutdown process
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 19, 2024
1 parent 24d4cd3 commit 4e7a3c7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
5 changes: 4 additions & 1 deletion convex-cli/src/main/java/convex/cli/ACommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public String prompt(String message) {
}

public char[] readPassword(String prompt) {
// For some reason using this stops CTRL-C from being subsequently handled :-(
Console c = System.console();
if (c == null) {
if (verbose()>=3) {
Expand All @@ -125,7 +126,9 @@ public char[] readPassword(String prompt) {
}

if (isColoured()) prompt = Coloured.blue(prompt);
return c.readPassword(prompt);
char[] pass= c.readPassword(prompt);
c.flush();
return pass;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions convex-cli/src/main/java/convex/cli/local/LocalStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ public void execute() throws InterruptedException{
}

}



public List<Server> launchLocalPeers(List<AKeyPair> keyPairList, int peerPorts[]) throws InterruptedException {
List<AccountKey> keyList=keyPairList.stream().map(kp->kp.getAccountKey()).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RemotePeerMixin extends AMixin {

@Option(names={"--port"},
defaultValue="${env:CONVEX_PORT:-"+Constants.DEFAULT_PEER_PORT+"}",
description="Port number to connect to a peer. Defaulting to: ${DEFAULT-VALUE}")
description="Port number to connect to host peer. Defaulting to: ${DEFAULT-VALUE}")
private Integer port;

@Option(names={"--host"},
Expand All @@ -37,7 +37,7 @@ public Convex connect() {

return c;
} catch (ConnectException ce) {
throw new CLIError("Cannot connect to: "+sa,ce);
throw new CLIError("Cannot connect to host: "+sa,ce);
} catch (TimeoutException e) {
throw new CLIError("Timeout while attempting to connect to peer: "+hostname,e);
} catch (IOException e) {
Expand Down
24 changes: 11 additions & 13 deletions convex-cli/src/main/java/convex/cli/peer/PeerCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import convex.api.Convex;
import convex.cli.CLIError;
import convex.cli.Main;
import convex.cli.mixins.RemotePeerMixin;
import convex.cli.output.RecordOutput;
import convex.core.Result;
Expand All @@ -31,14 +30,13 @@
*
* convex.peer.create
*
* This creates an account and provides enougth funds, for a new peer account
* This creates an peer on an existing network
*
*
*/

@Command(name="create",
mixinStandardHelpOptions = true,
description="Configures and creates a peer ready to join a Convex network.")
description="Configures and creates a peer on a Convex network. Needs an esisting peer as --host and a valid peer controller account. Will generate a new peer key if not otherwise specified.")
public class PeerCreate extends APeerCommand {

private static final Logger log = LoggerFactory.getLogger(PeerCreate.class);
Expand All @@ -51,8 +49,6 @@ public class PeerCreate extends APeerCommand {
@Override
public void execute() throws InterruptedException {

Main mainParent = cli();

long peerStake = convex.core.Constants.MINIMUM_EFFECTIVE_STAKE;

AKeyPair keyPair = null;
Expand All @@ -67,14 +63,16 @@ public void execute() throws InterruptedException {
}

try {
// connect using the default first user
Convex convex = peerMixin.connect();

keyPair = AKeyPair.generate();

// save the new keypair in the keystore
PFXTools.setKeyPair(keyStore, keyPair, keyMixin.getKeyPassword());
storeMixin.saveKeyStore();
inform("Created new peer key: "+keyPair.getAccountKey());

// connect using the default first user
Convex convex = peerMixin.connect();
// create an account
Address address = convex.createAccountSync(keyPair.getAccountKey());
convex.transferSync(address, peerStake);
Expand All @@ -94,10 +92,10 @@ public void execute() throws InterruptedException {
long currentBalance = convex.getBalance(address);

String shortAccountKey = accountKeyString.substring(0, 6);
RecordOutput output=new RecordOutput();


RecordOutput output=new RecordOutput();
output.addField("Public Peer Key", keyPair.getAccountKey().toString());
output.addField("Address", address.longValue());
output.addField("Controller Address", address.longValue());
output.addField("Balance", currentBalance);
output.addField("Inital stake amount", stakeAmount);
// System.out.println("You can now start this peer by executing the following line:\n");
Expand All @@ -107,12 +105,12 @@ public void execute() throws InterruptedException {

output.addField("Peer start line",
String.format(
"./convex peer start --password=xx --address=%d --public-key=%s",
"./convex peer start --address=%d --peer-key=%s",
address.longValue(),
shortAccountKey
)
);
output.writeToStream(mainParent.commandLine.getOut());
output.writeToStream(cli().commandLine().getOut());
} catch (IOException | GeneralSecurityException | ResultException t) {
throw new CLIError("Error creating Peer",t);
}
Expand Down
1 change: 1 addition & 0 deletions convex-cli/src/main/java/convex/cli/peer/PeerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private AKeyPair findPeerKey(EtchStore store) {

@Override
public void execute() throws InterruptedException {

storeMixin.ensureKeyStore();
try (EtchStore store = etchMixin.getEtchStore()) {

Expand Down
23 changes: 17 additions & 6 deletions convex-core/src/main/java/convex/core/util/Shutdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Shutdown {

public static final int CLIENTHTTP = 60;
public static final int SERVER = 80;
public static final int CONNECTION = 90;
public static final int ETCH = 100;
public static final int EXECUTOR = 110;
public static final int CLI = 120;
Expand All @@ -32,19 +33,26 @@ public void run() {
Shutdown.runHooks();
}
},"Convex Shutdown"));
} catch(IllegalStateException e) {
} catch(Exception e) {
// Ignore, already shutting down
}
}

private static class Group {
protected static class Group {
protected final int level;

public Group(int level) {
this.level=level;
}

private final IdentityHashMap<Runnable, Runnable> hookSet=new IdentityHashMap<>();

public synchronized void addHook(Runnable r) {
hookSet.put(r, r);
}

public synchronized void runHooks() {
// System.out.println("Running shutdown hooks at level: "+level);
Collection<Runnable> hooks=hookSet.keySet();
hooks.stream().forEach(r->{
log.trace("Running shutdown hook: "+Utils.getClassName(r));
Expand All @@ -69,10 +77,10 @@ public synchronized void runHooks() {
* @param priority Priority number for shutdown hook
* @param shutdownTask Runnable instance to execute on shutdown
*/
public static synchronized void addHook(int priority,Runnable shutdownTask) {
public static void addHook(int priority,Runnable shutdownTask) {
Group g=order.get(priority);
if (g==null) {
g=new Group();
g=new Group(priority);
order.put(priority, g);
}
g.addHook(shutdownTask);
Expand All @@ -81,13 +89,16 @@ public static synchronized void addHook(int priority,Runnable shutdownTask) {
/**
* Execute all hooks. Called by standard Java shutdown process.
*/
private synchronized static void runHooks() {
private static void runHooks() {
for (Map.Entry<Integer,Group> me: order.entrySet()) {
log.debug("Running shutdown hooks at level: "+me.getKey());
me.getValue().runHooks();
}
order.clear();
log.debug("Convex shutdown hooks complete");

}

public void shoutDownNow() {
runHooks();
}
}
4 changes: 4 additions & 0 deletions convex-peer/src/main/java/convex/net/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import convex.core.transactions.ATransaction;
import convex.core.util.Counters;
import convex.core.util.Utils;
import convex.core.util.Shutdown;
import convex.net.impl.HandlerException;
import convex.peer.Config;

Expand Down Expand Up @@ -535,6 +536,9 @@ private static void ensureSelectorLoop() {
// make this a daemon thread so it shuts down if everything else exits
selectorThread.setDaemon(true);
selectorThread.start();

// shut down connection loop at end of shutdown process
Shutdown.addHook(Shutdown.CONNECTION,()->{selectorThread.interrupt();});
}
}
}
Expand Down

0 comments on commit 4e7a3c7

Please sign in to comment.