github.com/m-lab/locate@v0.17.6/USAGE.md (about) 1 # Locate API Usage 2 3 ## Revision History 4 5 | Version | Date | Major Changes | 6 |----------|------------|-----------------| 7 | v2.0.0 | 2020-07-28 | initial version | 8 9 ## Introduction 10 11 The Locate API provides consistent, expected measurement quality for M-Lab 12 clients. The Locate API is a GCP hosted service that "locates" the best M-Lab 13 server for a user request. For different use cases, "best" could mean 14 different things. The sections below provide an overview of the locate API 15 operations and describe currently supported queries in more detail. 16 17 ### Locate Servers Near the Client 18 19 #### When best means "geographically close" 20 21 GCP automatically adds client latitude and longitude to incoming [HTTP request 22 headers][headers]. The locate API uses this location to search for nearby M-Lab 23 servers that satisfy the client query. For additional information about how 24 GCP identifies a client's location, please see the section below, [How GCP 25 identifies client location](#how-gcp-identifies-client-location). 26 27 [headers]: https://cloud.google.com/load-balancing/docs/user-defined-request-headers#how_user-defined_request_headers_work 28 29 The locate API also considers: 30 31 * is the target server online? (required) 32 * is the client request frequency within by our [acceptable use policy][aup]? 33 (required) 34 35 The locate API returns up to four results for the requested measurement 36 service. The locate API may return fewer results when too few servers are 37 healthy in a geographic region. The locate API may return an error when no 38 servers are available. 39 40 > PLANNED(v2): in all cases above, the locate API will return a recommended 41 [v2.NearestResult.NextRequest][nextRequest] time and signed URL for the client 42 to issue the next request. This will make retry logic in clients simpler and 43 encourages best practices for the API. See the [request priority 44 hierarchy][priority]. 45 46 [nextRequest]: https://godoc.org/github.com/m-lab/locate/api/v2#NearestResult 47 [priority]: https://godoc.org/github.com/m-lab/locate/api/v2 48 [aup]: https://www.measurementlab.net/aup 49 50 #### Query for Geographically Close Servers 51 52 The base URL for the locate API to query for geographically close 53 servers: 54 55 http://locate.measurementlab.net/v2/nearest/ 56 57 Well formed requests must specify a service name. For example: 58 59 * ndt/ndt5 - NDT5 protocol for the NDT measurement service. 60 * ndt/ndt7 - NDT7 protocol for the NDT measurement service. 61 * wehe/replay - Replay protocol for the Wehe measurement service. 62 * neubot/dash - Dash protocol for the Neubot measurement service. 63 64 > PLANNED(v2): to discover the list of available services, the locate API 65 will support queries to the base URL. Currently, only the named services are 66 supported. 67 68 A complete locate request with service name (e.g. ndt/ndt7) looks like: 69 70 http://locate.measurementlab.net/v2/nearest/ndt/ndt7 71 72 A successful response will include a list of results. Each result object 73 includes the machine name and a map of "urls"; the key is the measurement 74 service URL template and the value is the complete URL to the service running 75 on the target machine. The template keys are static so clients can extract 76 the value. 77 78 The complete URL includes the protocol scheme, e.g. `wss`, the machine name, 79 the resource path (e.g. `/ndt/v7/download`), and request parameters generated 80 by the locate API (e.g. `access_token=`). The `access_token=` request 81 parameter is validated by the target service (e.g. ndt-server). An invalid 82 access token will always be rejected. 83 84 { 85 "results": [ 86 { 87 "machine": "mlab3-lga05.mlab-oti.measurement-lab.org", 88 "urls": { 89 "ws:///ndt/v7/download": "ws://ndt-mlab3-lga05.mlab-oti.measurement-lab.org/ndt/v7/download?access_token={{TOKEN}}", 90 "ws:///ndt/v7/upload": "ws://ndt-mlab3-lga05.mlab-oti.measurement-lab.org/ndt/v7/upload?access_token={{TOKEN}}", 91 "wss:///ndt/v7/download": "wss://ndt-mlab3-lga05.mlab-oti.measurement-lab.org/ndt/v7/download?access_token={{TOKEN}}", 92 "wss:///ndt/v7/upload": "wss://ndt-mlab3-lga05.mlab-oti.measurement-lab.org/ndt/v7/upload?access_token={{TOKEN}}", 93 } 94 }, 95 ... 96 ] 97 } 98 99 > PLANNED(v2): to associate multiple measurements with the same session (e.g. 100 upload and download), the locate API will add additional request 101 parameters for `session=` with a random id that the target server saves with 102 the measurement results. 103 104 Once a client connects to a target service using a given URL, the target 105 server may accept or reject the connection based on local conditions (e.g. 106 sufficient network capacity). The goal is to preserve the expected 107 measurement quality for every client. Meeting this goal means that 108 occassionally some clients may be temporarily prevented from running a 109 measurement on a particular machine. 110 111 Therefore, the client should be resilient to transient rejections by continuing 112 with the next returned result. Clients must also gracefully handle the case 113 where all target servers reject the client request. This could happen when 114 the platform is under extreme load. Clients should report the outage to 115 users. 116 117 > PLANNED(v2): the Locate API `NextRequest` will provide clients with a wait 118 time before trying again. 119 120 ## How GCP Identifies Client Location 121 122 As mentioned above, the Locate service uses GCP to determine a client's 123 location and direct the client to the nearest M-Lab server. Using the Locate 124 service with no additional parameters will provide the Latitude & Longitude 125 from Appengine. For example, see the query and response below: 126 127 ``` 128 $ curl --head 'http://locate.measurementlab.net/v2/nearest/ndt/ndt7' 129 130 HTTP/1.1 200 OK 131 Date: Wed, 20 Jan 2021 16:05:12 GMT 132 Content-Type: application/json 133 Vary: Accept-Encoding 134 Access-Control-Allow-Origin: * 135 Cache-Control: no-store 136 X-Locate-Clientlatlon: 40.914821,-74.383763 137 X-Locate-Clientlatlon-Method: appengine-latlong 138 Via: 1.1 google 139 Transfer-Encoding: chunked 140 ``` 141 142 In this case, the Locate service used the Appengine provided latitude and 143 longitude values, derived from the [Appengine headers](headers), 144 `X-Appengine-Country` and `X-Appengine-Region`. 145 146 [headers]: https://cloud.google.com/appengine/docs/flexible/go/reference/request-headers 147 148 A client may also use Locate service and specify a `country` or `region` 149 parameter to select a server nearest to that country or region. For example 150 see the queries and responses below: 151 152 ``` 153 $ curl --head 'http://locate.measurementlab.net/v2/nearest/ndt/ndt7?country=IN 154 155 HTTP/1.1 200 OK 156 Date: Wed, 20 Jan 2021 18:26:05 GMT 157 Content-Type: application/json 158 Vary: Accept-Encoding 159 Access-Control-Allow-Origin: * 160 Cache-Control: no-store 161 X-Locate-Clientlatlon: 20.593684,78.96288 162 X-Locate-Clientlatlon-Method: user-country 163 Via: 1.1 google 164 Transfer-Encoding: chunked 165 166 $ curl --head 'http://locate.measurementlab.net/v2/nearest/ndt/ndt7?region=US-IL' 167 168 HTTP/1.1 200 OK 169 Date: Wed, 20 Jan 2021 18:22:54 GMT 170 Content-Type: application/json 171 Vary: Accept-Encoding 172 Access-Control-Allow-Origin: * 173 Cache-Control: no-store 174 X-Locate-Clientlatlon: 39.94600000,-89.1991000 175 X-Locate-Clientlatlon-Method: user-region 176 Via: 1.1 google 177 Transfer-Encoding: chunked 178 ``` 179 180 Additionally, the `country` parameter supports a `strict` option. 181 If `strict=true`, Locate will only return servers in the requested country. 182 183 Countries are specified using the [ISO 3166-1 alpha 2 country code][iso1] 184 (`country=IN`) or the [ISO 3166-2 region code][iso2] (`region=US-IL`). 185 186 [iso1]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 187 [iso2]: https://en.wikipedia.org/wiki/ISO_3166-2 188 189 If Appengine does not know a client location, and the user does not provide a 190 location parameter, then the Locate API falls back to using a current Maxmind 191 db. If that also fails, then the Locate API will return a 503 error, Service 192 Unavailable. If the client is connecting from an IP address with an unknown 193 location, the Locate service cannot direct it to a nearby server. 194 195 ## Additional Request Parameters 196 197 With the [Autojoin API][autojoin] and [autonode deployments][autonode], the 198 M-Lab platform consists of infrastructure managed by multiple organizations. The 199 Locate API allows clients to select the organization or site returned in 200 results. This can be helpful for creating test portals or experiments that only 201 target servers provided by a specific host organization. 202 203 To select servers in a single organization, include `org=<org>`: 204 * e.g. https://locate.measurementlab.net/v2/nearest/ndt/ndt7?org=mlab 205 206 To select servers in a single site, include `site=<site>`: 207 * e.g. https://locate.measurementlab.net/v2/nearest/ndt/ndt7?site=lga06 208 209 If there are no healthy servers associated with the named org or site, then 210 these queries may return an error. 211 212 [autojoin]: https://github.com/m-lab/autojoin 213 [autonode]: https://github.com/m-lab/autonode