1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
extern crate consul_rust;
#[macro_use]
extern crate log;
extern crate rand;
extern crate url;

use core::diplomat::{Diplomat, Strategy};
use std::collections::HashMap;
use std::collections::HashSet;
use std::time::{SystemTime, UNIX_EPOCH};

pub mod core;

/// Discovery for target app address, with load balancing and fail over.
///
///    1. The discovery will refresh the service list every 10 seconds by
///       default. The selection strategy is roundrobin, for keeping the
///       connections balanced.
///    2. Once a connection to a specified host failed, discard the host and
///       try the next. And loop the process until find a connectable host
///       or the retry time reaches the max time. If all the hosts are
///       discarded, refresh the host list.
///    3. The discovery also accepts an extra address pair, for fallback
///       when the discovery agent is down. If no backup address found, zone
///       will try to pick one from the discarded addresses.
///
///

pub struct Discovery {
    // target means which service should to be found.
    // eg. comment for Comment service.
    target: String,

    // a fallback choice if discovery agent is down.
    address: String,

    // ttl specified the interval for the discovery to refresh the service list.
    ttl: u64,

    // the real woker to fetch the service list.
    diplomat: Diplomat,

    // record the last time the address is acessed,
    // used for refreshing the service list after time expired.
    last_cleanup_time: u64,

    // record the unnormal address to prevent failure.
    discarded_addr: HashSet<String>,

//   todo mu sync.Mutex
}

impl Default for Discovery {
    fn default() -> Discovery {
        Discovery {
            target: String::new(),
            address: String::new(),
            ttl: 10,
            diplomat: Diplomat::new(),
            discarded_addr: HashSet::new(),
            last_cleanup_time: 0,
        }
    }
}

fn get_unix_timestamp() -> u64 {
    let start = SystemTime::now();
    let since_the_epoch = start.duration_since(UNIX_EPOCH)
        .expect("Time went backwards");
    since_the_epoch.as_secs() * 1000
}

impl Discovery {
    pub fn new_discovery(target: String, address: Option<String>) -> Discovery {
        // todo verify address ip and port
        let mut discovery = Discovery { ..Default::default() };
        let mut addr = String::new();
        match address {
            Some(x) => { addr = x; }
            _ => {}
        }
        if target.is_empty() {
            if addr.is_empty() {
                panic!("Target name and address can not be null together");
            } else {
                discovery.address = addr;
            }
        } else {
            discovery.target = target;
        }
        discovery
    }
    pub fn get_address(&mut self) -> String {
        // todo Mutex
        let mut address = self.address.clone();
        if self.target.is_empty() {
            if address.is_empty() {
                panic!("Target null, no address available")
            }
            return address;
        }
        self.cleanup();

        let max_rr_time = self.discarded_addr.len() + 1;
        let mut rr_time: u32 = 0;

        loop {
            let service_entry = self.diplomat.select(self.target.clone(), Strategy::Roundrobin, false);
            match service_entry {
                Some(val) => {
                    let ret = format!("{}:{}", val.address, val.port);
                    if !self.discarded_addr.contains(&ret) {
                        address = ret;
                        break;
                    }
                    rr_time += 1;
                    if rr_time == max_rr_time as u32 {
                        if !self.address.is_empty() {
                            info!("No healthy addresses available, fallback to the given host-port pair.");
                            address = self.address.clone();
                        } else if self.discarded_addr.len() > 0 {
                            // random choice
                            for d in self.discarded_addr.iter() {
                                warn!("Fallback to a discarded address: {}", d);
                                address = d.clone();
                            }
                        } else {
                            panic!("Iterated on all the addresses, no healthy address available.")
                        }
                        break;
                    }
                }
                _ => { panic!("Can not discover available address"); }
            }
        }
        address
    }
    /// Cleanup the discarded addresses, the expiration time is the TTL.
    fn cleanup(&mut self) {
        let now = get_unix_timestamp();
        if self.last_cleanup_time != 0 {
            if self.last_cleanup_time + self.ttl <= now {
                self.discarded_addr.clear();
                self.last_cleanup_time = now;
            }
        } else {
            self.last_cleanup_time = now;
        }
    }


    /// Discard the given address for a period. The TTL is the one passed in the initializer.
    pub fn discard_address(&mut self, address: String) -> bool {
        if self.last_cleanup_time == 0 {
            self.last_cleanup_time = get_unix_timestamp()
        }
        if !self.discarded_addr.contains(&address) {
            self.discarded_addr.insert(address);
            true
        } else {
            false
        }
    }
}