/
SuggestHttpWorker.java
117 lines (95 loc) · 3.55 KB
/
SuggestHttpWorker.java
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
/**
* Copyright 2015-2016 The Splicer Query Engine Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.turn.splicer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Takes a api/suggest query string and runs it against a random TSD node,
* returns the result of the query
*
* TODO: needs to be refactored with HttpWorker, there is a lot of duplicate logic
* TODO: error reporting differs from regular TSDB - no error message for invalid type
*/
public class SuggestHttpWorker implements Callable<String> {
private static final Logger LOG = LoggerFactory.getLogger(SuggestHttpWorker.class);
private final String suggestQuery;
private final String[] hosts;
private static final Random RANDOM_GENERATOR = new Random();
public SuggestHttpWorker(String queryString) {
this.suggestQuery = queryString;
Set<String> hosts = HttpWorker.TSDMap.keySet();
if (hosts.size() == 0) {
throw new NullPointerException("No Query Hosts. TSDMap.size = 0");
}
this.hosts = new String[hosts.size()];
int i=0;
for (String h: hosts) {
this.hosts[i] = h;
i++;
}
}
@Override
public String call() throws Exception {
LinkedBlockingQueue<String> TSDs;
//TODO: have it implement its own RegionChecker to get hbase locality looking for metric names
//lets have it just pick a random host
String hostname = getRandomHost();
TSDs = HttpWorker.TSDMap.get(hostname);
if (TSDs == null) {
LOG.error("We are not running TSDs on regionserver={}. Choosing a random host failed", hostname);
return "{'error': 'Choice of hostname=" + hostname + " failed.'}";
}
String server = TSDs.take();
String uri = "http://" + server + "/api/suggest?" + suggestQuery;
CloseableHttpClient postman = HttpClientBuilder.create().build();
try {
HttpGet getRequest = new HttpGet(uri);
LOG.info("Sending query=" + uri + " to TSD running on host=" + hostname);
HttpResponse response = postman.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
List<String> dl = IOUtils.readLines(response.getEntity().getContent());
String result = StringUtils.join(dl, "");
LOG.info("Result={}", result);
return result;
} finally {
IOUtils.closeQuietly(postman);
TSDs.put(server);
LOG.info("Returned {} into the available queue", server);
}
}
/**
* Chooses a random number between 0 - (host.length-1) inclusive
* returns that index in the hosts list
* @return
*/
private String getRandomHost() {
return hosts[RANDOM_GENERATOR.nextInt(hosts.length)];
}
}