async fn register_node_id(etcd_client: &EtcdDelegate) -> anyhow::Result<u64> {
    let lock_key = etcd_client
        .lock(ETCD_NODE_ID_LOCK.as_bytes(), 10)
        .await
        .with_context(|| "lock fail while register node_id")?;

    let node_id: Option<Vec<u8>> = etcd_client
        .get_at_most_one_value(ETCD_NODE_ID_COUNTER_KEY)
        .await
        .with_context(|| format!("get {} from etcd fail", ETCD_NODE_ID_COUNTER_KEY))?;

    let node_id = match node_id {
        Some(current_id_str) => {
            let current_id = std::str::from_utf8(current_id_str.as_slice())?;
            let (new_id, overflow) = current_id
                .parse::<u64>()
                .unwrap_or_else(|_| {
                    panic!("{} can't be parsed as node id (integer)", current_id);
                })
                .overflowing_add(1);
            if overflow {
                panic!("node_id({}) is too large, it overflows", current_id);
            } else {
                new_id
            }
        }
        None => 0,
    };

    let node_id_string = node_id.to_string();

    etcd_client
        .write_or_update_kv(ETCD_NODE_ID_COUNTER_KEY, &node_id_string)
        .await
        .with_context(|| {
            format!(
                "update {} to value {} failed",
                ETCD_NODE_ID_COUNTER_KEY, node_id_string
            )
        })?;

    etcd_client
        .unlock(lock_key)
        .await
        .with_context(|| "unlock fail while register node_id")?;

    let ip = env::var(ENV_IP_ADDRESS).unwrap_or_else(|_| {
        panic!(
            "pod Ip address should be assigned via environment variable: {}!",
            ENV_IP_ADDRESS
        );
    });
    etcd_client
        .write_or_update_kv(
            &format!("{}{}", ETCD_NODE_ID_INFO_PREFIX, node_id_string),
            &ip,
        )
        .await
        .with_context(|| {
            format!(
                "Update Node Ip address failed, node_id:{}, ip: {}",
                node_id_string, ip
            )
        })?;

    Ok(node_id)
}
/// Register current node to etcd and get a dedicated node id.
/// The registered information contains IP.
Register current node to etcd and get a dedicated node id.
The registered information contains IP.
Register current node to etcd and get a dedicated node id . The registered information contains IP .
async fn register_node_id(etcd_client: &EtcdDelegate) -> anyhow::Result<u64> {
    let lock_key = etcd_client
        .lock(ETCD_NODE_ID_LOCK.as_bytes(), 10)
        .await
        .with_context(|| "lock fail while register node_id")?;
    let node_id: Option<Vec<u8>> = etcd_client
        .get_at_most_one_value(ETCD_NODE_ID_COUNTER_KEY)
        .await
        .with_context(|| format!("get {} from etcd fail", ETCD_NODE_ID_COUNTER_KEY))?;
    let node_id = match node_id {
        Some(current_id_str) => {
            let current_id = std::str::from_utf8(current_id_str.as_slice())?;
            let (new_id, overflow) = current_id
                .parse::<u64>()
                .unwrap_or_else(|_| {
                    panic!("{} can't be parsed as node id (integer)", current_id);
                })
                .overflowing_add(1);
            if overflow {
                panic!("node_id({}) is too large, it overflows", current_id);
            } else {
                new_id
            }
        }
        None => 0,
    };
    let node_id_string = node_id.to_string();
    etcd_client
        .write_or_update_kv(ETCD_NODE_ID_COUNTER_KEY, &node_id_string)
        .await
        .with_context(|| {
            format!(
                "update {} to value {} failed",
                ETCD_NODE_ID_COUNTER_KEY, node_id_string
            )
        })?;
    etcd_client
        .unlock(lock_key)
        .await
        .with_context(|| "unlock fail while register node_id")?;
    let ip = env::var(ENV_IP_ADDRESS).unwrap_or_else(|_| {
        panic!(
            "pod Ip address should be assigned via environment variable: {}!",
            ENV_IP_ADDRESS
        );
    });
    etcd_client
        .write_or_update_kv(
            &format!("{}{}", ETCD_NODE_ID_INFO_PREFIX, node_id_string),
            &ip,
        )
        .await
        .with_context(|| {
            format!(
                "Update Node Ip address failed, node_id:{}, ip: {}",
                node_id_string, ip
            )
        })?;
    Ok(node_id)
}
async fn register_node_id ( etcd_client : & EtcdDelegate ) -> anyhow :: Result < u64 > { let lock_key = etcd_client . lock ( ETCD_NODE_ID_LOCK . as_bytes ( ) , 10 ) . await . with_context ( | | " " ) ? ; let node_id : Option < Vec < u8 > > = etcd_client . get_at_most_one_value ( ETCD_NODE_ID_COUNTER_KEY ) . await . with_context ( | | format ! ( " " ETCD_NODE_ID_COUNTER_KEY ) ) ? ; let node_id = match node_id { Some ( current_id_str ) => { let current_id = std :: str :: from_utf8 ( current_id_str . as_slice ( ) ) ? ; let ( new_id , overflow ) = current_id . parse :: < u64 > ( ) . unwrap_or_else ( | _ | { panic ! ( " " current_id ) ; } ) . overflowing_add ( 1 ) ; if overflow { panic ! ( " " current_id ) ; } else { new_id } } None => 0 , } ; let node_id_string = node_id . to_string ( ) ; etcd_client . write_or_update_kv ( ETCD_NODE_ID_COUNTER_KEY , & node_id_string ) . await . with_context ( | | { format ! ( " " ETCD_NODE_ID_COUNTER_KEY node_id_string ) } ) ? ; etcd_client . unlock ( lock_key ) . await . with_context ( | | " " ) ? ; let ip = env :: var ( ENV_IP_ADDRESS ) . unwrap_or_else ( | _ | { panic ! ( " " ENV_IP_ADDRESS ) ; } ) ; etcd_client . write_or_update_kv ( & format ! ( " " ETCD_NODE_ID_INFO_PREFIX node_id_string ) , & ip , ) . await . with_context ( | | { format ! ( " " node_id_string ip ) } ) ? ; Ok ( node_id ) }
Register current node to etcd and get a dedicated node id.
Register current node to etcd and get a dedicated node id .
etcd_client
&EtcdDelegate
etcd_client
&EtcdDelegate