private String buildModulesConfigJSONProperty(
     NativeModuleRegistry nativeModuleRegistry, JavaScriptModulesConfig jsModulesConfig) {
   // TODO(5300733): Serialize config using single json generator
   JsonFactory jsonFactory = new JsonFactory();
   StringWriter writer = new StringWriter();
   try {
     JsonGenerator jg = jsonFactory.createGenerator(writer);
     jg.writeStartObject();
     jg.writeFieldName("remoteModuleConfig");
     jg.writeRawValue(nativeModuleRegistry.moduleDescriptions());
     jg.writeFieldName("localModulesConfig");
     jg.writeRawValue(jsModulesConfig.moduleDescriptions());
     jg.writeEndObject();
     jg.close();
   } catch (IOException ioe) {
     throw new RuntimeException("Unable to serialize JavaScript module declaration", ioe);
   }
   return writer.getBuffer().toString();
 }
 @Override
 public void serialize(
     final Date date,
     final JsonGenerator jsonGenerator,
     final SerializerProvider serializerProvider)
     throws IOException {
   jsonGenerator.writeRawValue(
       String.valueOf(date.getTime() / 1000)
           + "."
           + Strings.padStart(Long.toString(date.getTime() % 1000), 3, '0'));
 }
 @Override
 public void serialize(TBase value, JsonGenerator gen, SerializerProvider provider)
     throws IOException {
   gen.writeRawValue(
       writeThriftObjectAsTText(
           protocol -> {
             try {
               value.write(protocol);
             } catch (TException ex) {
               throw new IllegalArgumentException(ex);
             }
           }));
 }
Beispiel #4
0
    @Override
    public void serialize(List<String> value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {

      StringBuilder sb = new StringBuilder();
      sb.append("[");
      if (value != null) {
        for (int i = 0; i < value.size(); i++) {
          if (i > 0) {
            sb.append(",");
          }
          sb.append(value.get(i));
        }
      }
      sb.append("]");
      jgen.writeRawValue(sb.toString());
    }
    @Override
    public void serialize(
        Collection<String> values, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {

      StringBuilder sb = new StringBuilder();
      sb.append("[");
      if (values != null) {
        for (String value : values) {
          if (sb.length() > 1) {
            sb.append(",");
          }
          sb.append(value);
        }
      }
      sb.append("]");
      jgen.writeRawValue(sb.toString());
    }
  @Override
  public String handleJsonRequest(
      Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager)
      throws Exception {
    try {
      if (jobManager != null) {
        // whether one task manager's metrics are requested, or all task manager, we
        // return them in an array. This avoids unnecessary code complexity.
        // If only one task manager is requested, we only fetch one task manager metrics.
        final List<Instance> instances = new ArrayList<>();
        if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
          try {
            InstanceID instanceID =
                new InstanceID(StringUtils.hexStringToByte(pathParams.get(TASK_MANAGER_ID_KEY)));
            Future<Object> future =
                jobManager.ask(
                    new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout);
            TaskManagerInstance instance = (TaskManagerInstance) Await.result(future, timeout);
            if (instance.instance().nonEmpty()) {
              instances.add(instance.instance().get());
            }
          }
          // this means the id string was invalid. Keep the list empty.
          catch (IllegalArgumentException e) {
            // do nothing.
          }
        } else {
          Future<Object> future =
              jobManager.ask(JobManagerMessages.getRequestRegisteredTaskManagers(), timeout);
          RegisteredTaskManagers taskManagers =
              (RegisteredTaskManagers) Await.result(future, timeout);
          instances.addAll(taskManagers.asJavaCollection());
        }

        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

        gen.writeStartObject();
        gen.writeArrayFieldStart("taskmanagers");

        for (Instance instance : instances) {
          gen.writeStartObject();
          gen.writeStringField("id", instance.getId().toString());
          gen.writeStringField("path", instance.getActorGateway().path());
          gen.writeNumberField("dataPort", instance.getTaskManagerLocation().dataPort());
          gen.writeNumberField("timeSinceLastHeartbeat", instance.getLastHeartBeat());
          gen.writeNumberField("slotsNumber", instance.getTotalNumberOfSlots());
          gen.writeNumberField("freeSlots", instance.getNumberOfAvailableSlots());
          gen.writeNumberField("cpuCores", instance.getResources().getNumberOfCPUCores());
          gen.writeNumberField("physicalMemory", instance.getResources().getSizeOfPhysicalMemory());
          gen.writeNumberField("freeMemory", instance.getResources().getSizeOfJvmHeap());
          gen.writeNumberField("managedMemory", instance.getResources().getSizeOfManagedMemory());

          // only send metrics when only one task manager requests them.
          if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
            byte[] report = instance.getLastMetricsReport();
            if (report != null) {
              gen.writeFieldName("metrics");
              gen.writeRawValue(new String(report, "utf-8"));
            }
          }

          gen.writeEndObject();
        }

        gen.writeEndArray();
        gen.writeEndObject();

        gen.close();
        return writer.toString();
      } else {
        throw new Exception("No connection to the leading JobManager.");
      }
    } catch (Exception e) {
      throw new RuntimeException("Failed to fetch list of all task managers: " + e.getMessage(), e);
    }
  }