Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Quick fix for ping timeout issue, just skip the result. Next commit will
Browse files Browse the repository at this point in the history
try to provide an actual response.
  • Loading branch information
jshaughn committed Mar 27, 2015
1 parent 526394e commit ebc2f98
Showing 1 changed file with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
*/
package org.hawkular.component.pinger;

import org.hawkular.metrics.client.common.SingleMetric;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
Expand All @@ -29,15 +38,8 @@
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.hawkular.metrics.client.common.SingleMetric;

/**
* A SLSB that coordinates the pinging of resources
Expand All @@ -53,7 +55,6 @@ public class PingManager {
// How long do we wait between each round
private static final int WAIT_MILLIS = 500;


String tenantId = "test";

@EJB
Expand All @@ -69,22 +70,22 @@ public void startUp() {

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/hawkular/inventory/" + tenantId + "/resources");
target.queryParam("type","URL");
target.queryParam("type", "URL");
Response response = target.request().get();

if (response.getStatus()==200) {
if (response.getStatus() == 200) {

List list = response.readEntity(List.class);

for (Object o : list) {
for (Object o : list) {
if (o instanceof Map) {

Map<String,Object> m = (Map) o;
Map<String, Object> m = (Map) o;
String id = (String) m.get("id");
String type = (String) m.get("type");
Map<String,String> params = (Map<String, String>) m.get("parameters");
Map<String, String> params = (Map<String, String>) m.get("parameters");
String url = params.get("url");
destinations.add(new PingDestination(id,url));
destinations.add(new PingDestination(id, url));
}
}
}
Expand All @@ -93,7 +94,6 @@ public void startUp() {
}
}


/**
* This method triggers the actual work by starting pingers,
* collecting their return values and then publishing them.
Expand All @@ -102,7 +102,7 @@ public void startUp() {
@Schedule(minute = "*", hour = "*", second = "0,20,40", persistent = false)
public void scheduleWork() {

if (destinations.size()==0) {
if (destinations.size() == 0) {
return;
}

Expand All @@ -125,7 +125,6 @@ private void doThePing(Set<PingDestination> destinations) {
futures.add(result);
}


int round = 1;
while (!futures.isEmpty() && round < ROUNDS) {
Iterator<Future<PingStatus>> iterator = futures.iterator();
Expand Down Expand Up @@ -157,11 +156,17 @@ private void doThePing(Set<PingDestination> destinations) {
PingStatus ps = null;
try {
ps = f.get();
ps.timedOut = true;
ps.duration=10000;
results.add(ps);
// can be null if the future did not complete
// TODO this timeout logic needs to be fixed to actually provide a result
if (null != ps) {
ps.timedOut = true;
ps.duration = 10000;
results.add(ps);
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); // TODO: Customise this generated block
} catch ( CancellationException e ) {
// do nothing
}
}
}
Expand All @@ -171,19 +176,18 @@ private void doThePing(Set<PingDestination> destinations) {

private void reportResults(List<PingStatus> results) {

if (results.size()==0) {
if (results.size() == 0) {
return;
}

List<SingleMetric> singleMetrics = new ArrayList<>(results.size());
List<Map<String,Object>> mMetrics = new ArrayList<>();
List<Map<String, Object>> mMetrics = new ArrayList<>();

for (PingStatus status : results){
for (PingStatus status : results) {

addDataItem(mMetrics, status, status.duration, "duration");
addDataItem(mMetrics, status, status.code, "code");


// for the topic to alerting
SingleMetric singleMetric = new SingleMetric(status.destination.resourceId + ".status.duration",
status.getTimestamp(), (double) status.getDuration());
Expand All @@ -198,19 +202,17 @@ private void reportResults(List<PingStatus> results) {
metricPublisher.sendToMetricsViaRest(tenantId, mMetrics);
metricPublisher.publishToTopic(tenantId, singleMetrics);



}

private void addDataItem(List<Map<String, Object>> mMetrics, PingStatus status, Number value, String name) {
Map<String,Number> dataMap = new HashMap<>(2);
Map<String, Number> dataMap = new HashMap<>(2);
dataMap.put("timestamp", status.getTimestamp());
dataMap.put("value", value);
List<Map<String,Number>> data = new ArrayList<>(1);
List<Map<String, Number>> data = new ArrayList<>(1);
data.add(dataMap);
Map<String,Object> outer = new HashMap<>(2);
outer.put("id",status.destination.resourceId + ".status." + name);
outer.put("data",data);
Map<String, Object> outer = new HashMap<>(2);
outer.put("id", status.destination.resourceId + ".status." + name);
outer.put("data", data);
mMetrics.add(outer);
}

Expand Down

0 comments on commit ebc2f98

Please sign in to comment.