예제 #1
0
 public static final Optional<String> httpPost(
     Entity framework, String subUrl, String dataUrl, Map<String, Object> substitutions) {
   String targetUrl =
       Urls.mergePaths(framework.sensors().get(MesosFramework.FRAMEWORK_URL), subUrl);
   String templateContents = ResourceUtils.create().getResourceAsString(dataUrl);
   String processedJson =
       TemplateProcessor.processTemplateContents(templateContents, substitutions);
   LOG.debug("Posting JSON to {}: {}", targetUrl, processedJson);
   URI postUri = URI.create(targetUrl);
   HttpToolResponse response =
       HttpTool.httpPost(
           MesosUtils.buildClient(framework),
           postUri,
           MutableMap.of(
               HttpHeaders.CONTENT_TYPE, "application/json",
               HttpHeaders.ACCEPT, "application/json"),
           processedJson.getBytes());
   LOG.debug("Response: " + response.getContentAsString());
   if (!HttpTool.isStatusCodeHealthy(response.getResponseCode())) {
     LOG.warn(
         "Invalid response code {}: {}", response.getResponseCode(), response.getReasonPhrase());
     return Optional.absent();
   } else {
     LOG.debug("Successfull call to {}: {}", targetUrl);
     return Optional.of(response.getContentAsString());
   }
 }
예제 #2
0
  @Override
  public void connectSensors() {
    super.connectSensors();

    final String id = sensors().get(MESOS_SLAVE_ID);

    HttpFeed.Builder httpFeedBuilder =
        HttpFeed.builder()
            .entity(this)
            .period(30, TimeUnit.SECONDS)
            .baseUri(getMesosCluster().sensors().get(Attributes.MAIN_URI))
            .credentialsIfNotNull(
                config().get(MesosCluster.MESOS_USERNAME),
                config().get(MesosCluster.MESOS_PASSWORD))
            .poll(
                new HttpPollConfig<Long>(MEMORY_AVAILABLE)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("resources", "mem"),
                            JsonFunctions.castM(Long.class)))
                    .onFailureOrException(Functions.constant(-1L)))
            .poll(
                new HttpPollConfig<Double>(CPU_AVAILABLE)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("resources", "cpus"),
                            JsonFunctions.castM(Double.class)))
                    .onFailureOrException(Functions.constant(-1d)))
            .poll(
                new HttpPollConfig<Long>(DISK_AVAILABLE)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("resources", "disk"),
                            JsonFunctions.castM(Long.class)))
                    .onFailureOrException(Functions.constant(-1L)))
            .poll(
                new HttpPollConfig<Long>(MEMORY_USED)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("used_resources", "mem"),
                            JsonFunctions.castM(Long.class)))
                    .onFailureOrException(Functions.constant(-1L)))
            .poll(
                new HttpPollConfig<Double>(CPU_USED)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("used_resources", "cpus"),
                            JsonFunctions.castM(Double.class)))
                    .onFailureOrException(Functions.constant(-1d)))
            .poll(
                new HttpPollConfig<Long>(DISK_USED)
                    .suburl("/master/state.json")
                    .onSuccess(
                        Functionals.chain(
                            HttpValueFunctions.jsonContents(),
                            Functions.compose(
                                MesosUtils.selectM(
                                    new Predicate<JsonElement>() {
                                      @Override
                                      public boolean apply(JsonElement input) {
                                        return input
                                            .getAsJsonObject()
                                            .get("id")
                                            .getAsString()
                                            .equals(id);
                                      }
                                    }),
                                JsonFunctions.walk("slaves")),
                            JsonFunctions.walkM("used_resources", "disk"),
                            JsonFunctions.castM(Long.class)))
                    .onFailureOrException(Functions.constant(-1L)));
    httpFeed = httpFeedBuilder.build();
  }