Example usage for com.google.common.cache CacheLoader asyncReloading

List of usage examples for com.google.common.cache CacheLoader asyncReloading

Introduction

In this page you can find the example usage for com.google.common.cache CacheLoader asyncReloading.

Prototype

@GwtIncompatible("Executor + Futures")
public static <K, V> CacheLoader<K, V> asyncReloading(final CacheLoader<K, V> loader, final Executor executor) 

Source Link

Document

Returns a CacheLoader which wraps loader , executing calls to CacheLoader#reload using executor .

Usage

From source file:com.qubole.rubix.hadoop2.hadoop2CM.Hadoop2ClusterManager.java

@Override
public void initialize(Configuration conf) {
    super.initialize(conf);
    yconf = new YarnConfiguration();
    this.address = yconf.get(addressConf, address);
    this.serverAddress = address.substring(0, address.indexOf(":"));
    this.serverPort = Integer.parseInt(address.substring(address.indexOf(":") + 1));

    ExecutorService executor = Executors.newSingleThreadExecutor();
    nodesCache = CacheBuilder.newBuilder().refreshAfterWrite(getNodeRefreshTime(), TimeUnit.SECONDS)
            .build(CacheLoader.asyncReloading(new CacheLoader<String, List<String>>() {
                @Override//  www  .j a  v  a  2s  .  c om
                public List<String> load(String s) throws Exception {
                    if (!isMaster) {
                        // First time all nodes start assuming themselves as master and down the line figure out their role
                        // Next time onwards, only master will be fetching the list of nodes
                        return ImmutableList.of();
                    }
                    try {
                        StringBuffer response = new StringBuffer();
                        URL obj = getNodeURL();
                        HttpURLConnection httpcon = (HttpURLConnection) obj.openConnection();
                        httpcon.setRequestMethod("GET");
                        log.debug("Sending 'GET' request to URL: " + obj.toString());
                        int responseCode = httpcon.getResponseCode();
                        if (responseCode == HttpURLConnection.HTTP_OK) {
                            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(httpcon.getInputStream()));
                            String inputLine;
                            while ((inputLine = in.readLine()) != null) {
                                response.append(inputLine);
                            }
                            in.close();
                            httpcon.disconnect();
                        } else {
                            log.info("/ws/v1/cluster/nodes failed due to " + responseCode
                                    + ". Setting this node as worker.");
                            isMaster = false;
                            httpcon.disconnect();
                            return ImmutableList.of();
                        }
                        Gson gson = new Gson();
                        Type type = new TypeToken<Nodes>() {
                        }.getType();
                        Nodes nodes = gson.fromJson(response.toString(), type);
                        List<Elements> allNodes = nodes.getNodes().getNode();
                        Set<String> hosts = new HashSet<>();

                        for (Elements node : allNodes) {
                            String state = node.getState();
                            log.debug("Hostname: " + node.getNodeHostName() + "State: " + state);
                            //keep only healthy data nodes
                            if (state.equalsIgnoreCase("Running") || state.equalsIgnoreCase("New")
                                    || state.equalsIgnoreCase("Rebooted")) {
                                hosts.add(node.getNodeHostName());
                            }
                        }

                        if (hosts.isEmpty()) {
                            throw new Exception("No healthy data nodes found.");
                        }

                        List<String> hostList = Lists.newArrayList(hosts.toArray(new String[0]));
                        Collections.sort(hostList);
                        log.debug("Hostlist: " + hostList.toString());
                        return hostList;
                    } catch (Exception e) {
                        throw Throwables.propagate(e);
                    }
                }
            }, executor));
}