Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions e2e/bats/start.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ teardown() {
assert_command dfx canister call hello greet '("Alpha")'
assert_eq '("Hello, Alpha!")'

# ps "isn't a robust solution": https://dfinity.atlassian.net/browse/OPS-166
#
# I guess we could make dfx write the replica pid somewhere.
#
# Anyway, if this test ends up sometimes killing a replica other than
# the one created by dfx for this test, then some other test might fail.
#
# Also, this does not work on linux:
# ps -o "ppid, pid, comm"
REPLICA_PID=$(ps x | grep [/[:space:]]replica | awk '{print $1}')
REPLICA_PID=$(cat .dfx/replica-configuration/replica-pid)

echo "replica pid is $REPLICA_PID"

Expand Down
10 changes: 5 additions & 5 deletions e2e/bats/utils/_.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dfx_new() {
echo PWD: $(pwd) >&2
}

# Start the client in the background.
# Start the replica in the background.
dfx_start() {
if [ "$USE_IC_REF" ]
then
Expand Down Expand Up @@ -62,10 +62,10 @@ dfx_start() {
dfx start --background "$@" 3>&-
fi
local project_dir=${pwd}
local dfx_config_root=.dfx/client-configuration
local dfx_config_root=.dfx/replica-configuration
printf "Configuration Root for DFX: %s\n" "${dfx_config_root}"
test -f ${dfx_config_root}/client-1.port
local port=$(cat ${dfx_config_root}/client-1.port)
test -f ${dfx_config_root}/replica-1.port
local port=$(cat ${dfx_config_root}/replica-1.port)

# Overwrite the default networks.local.bind 127.0.0.1:8000 with allocated port
local webserver_port=$(cat .dfx/webserver-port)
Expand All @@ -80,7 +80,7 @@ dfx_start() {
|| (echo "could not connect to replica on port ${port}" && exit 1)
}

# Stop the client and verify it is very very stopped.
# Stop the replica and verify it is very very stopped.
dfx_stop() {
if [ "$USE_IC_REF" ]
then
Expand Down
2 changes: 1 addition & 1 deletion e2e/bats/utils/assertions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ assert_process_exits() {
# Asserts that `dfx start` and `replica` are no longer running
assert_no_dfx_start_or_replica_processes() {
! ( ps | grep "[/[:space:]]dfx start" )
! ( ps | grep "[/[:space:]]replica" )
! ( ps | cat .dfx/replica-configuration/replica-pid )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the right assertion. You need to grep the content of the file, not pipe into a cat (which is afaik no op)

}

assert_file_eventually_exists() {
Expand Down
8 changes: 8 additions & 0 deletions src/dfx/src/actors/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Config {
pub replica_path: PathBuf,
pub shutdown_controller: Addr<ShutdownController>,
pub logger: Option<Logger>,
pub replica_configuration_dir: PathBuf,
}

/// A replica actor. Starts the replica, can subscribe to a Ready signal and a
Expand Down Expand Up @@ -116,6 +117,7 @@ impl Replica {

// Create a replica config.
let config = &self.config.replica_config;
let replica_pid_path = self.config.replica_configuration_dir.join("replica-pid");

let port = config.http_handler.port;
let write_port_to = config.http_handler.write_port_to.clone();
Expand All @@ -131,6 +133,7 @@ impl Replica {
write_port_to,
ic_starter_path,
replica_path,
replica_pid_path,
addr,
receiver,
)?;
Expand Down Expand Up @@ -253,6 +256,7 @@ fn replica_start_thread(
write_port_to: Option<PathBuf>,
ic_starter_path: PathBuf,
replica_path: PathBuf,
replica_pid_path: PathBuf,
addr: Addr<Replica>,
receiver: Receiver<()>,
) -> DfxResult<std::thread::JoinHandle<()>> {
Expand Down Expand Up @@ -298,6 +302,10 @@ fn replica_start_thread(
debug!(logger, "Starting replica...");
let mut child = cmd.spawn().expect("Could not start replica.");

std::fs::write(&replica_pid_path, "").expect("Could not write to replica-pid file.");
std::fs::write(&replica_pid_path, child.id().to_string())
.expect("Could not write to replica-pid file.");

let port = port.unwrap_or_else(|| {
Replica::wait_for_port_file(write_port_to.as_ref().unwrap()).unwrap()
});
Expand Down
1 change: 1 addition & 0 deletions src/dfx/src/commands/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
replica_path: replica_pathbuf,
shutdown_controller,
logger: Some(env.get_logger().clone()),
replica_configuration_dir: env.get_temp_dir().join("replica-configuration"),
})
.start();

Expand Down
15 changes: 8 additions & 7 deletions src/dfx/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,26 @@ fn start_replica(
let ic_starter_path = env.get_cache().get_binary_command_path("ic-starter")?;

let temp_dir = env.get_temp_dir();
let client_configuration_dir = temp_dir.join("client-configuration");
fs::create_dir_all(&client_configuration_dir)?;
let replica_configuration_dir = temp_dir.join("replica-configuration");
fs::create_dir_all(&replica_configuration_dir)?;
let state_dir = temp_dir.join("state/replicated_state");
fs::create_dir_all(&state_dir)?;
let client_port_path = client_configuration_dir.join("client-1.port");
let replica_port_path = replica_configuration_dir.join("replica-1.port");

// Touch the client port file. This ensures it is empty prior to
// Touch the replica port file. This ensures it is empty prior to
// handing it over to the replica. If we read the file and it has
// contents we shall assume it is due to our spawned client
// contents we shall assume it is due to our spawned replica
// process.
std::fs::write(&client_port_path, "")?;
std::fs::write(&replica_port_path, "")?;

let replica_config = ReplicaConfig::new(state_root).with_random_port(&client_port_path);
let replica_config = ReplicaConfig::new(state_root).with_random_port(&replica_port_path);
let actor_config = actors::replica::Config {
ic_starter_path,
replica_config,
replica_path,
shutdown_controller,
logger: Some(env.get_logger().clone()),
replica_configuration_dir,
};
Ok(actors::replica::Replica::new(actor_config).start())
}
Expand Down