Пример #1
0
public class ClientConfigActor extends UntypedActor {
  private final SettingsImpl settings = Settings.SettingsProvider.get(getContext().system());
  private LoggingAdapter log = Logging.getLogger(getContext().system(), this);

  public static Props props() {
    return Props.create(ClientConfigActor.class);
  }

  @Override
  public void onReceive(Object message) throws Exception {
    if (message instanceof ClientInfo) {
      ClientInfo clientInfo = (ClientInfo) message;
      final ClientConfig configFor = getConfigFor(clientInfo);
      log.debug(
          "config for client {} {} {}",
          clientInfo.id(),
          clientInfo.metrics().metadata().get("host-hostname"),
          configFor);
      getSender().tell(configFor, getSelf());
    } else {
      unhandled(message);
    }
  }

  private ClientConfig getConfigFor(ClientInfo clientInfo) {
    // take the first executor defined in the config
    return settings
        .getExecutors()
        .stream()
        .filter(ex -> clientInfo.executors().containsKey(ex))
        .findFirst()
        .map(executor -> getConfig(executor, clientInfo))
        .orElseGet(
            () -> {
              log.error("client has reported unsupported executors: {} ", clientInfo.executors());
              return ClientConfig.empty();
            });
  }

  private ClientConfig getConfig(String executor, ClientInfo clientInfo) {
    int executorCpuCount = clientInfo.executors().get(executor);
    int cpuSetSize = Math.min(executorCpuCount, settings.getCpuSetSize());
    // this happens if the config says use 2 cpus but the machine has only 1.
    if (executorCpuCount < settings.getCpuSetSize()) {
      log.warning(
          "Client reported less cpus ({}) than CPU_SET_SIZE ({})",
          executorCpuCount,
          settings.getCpuSetSize());
    }
    int numberOfExecutors = executorCpuCount / cpuSetSize;

    return ClientConfig.builder()
        .cpuSetSize(cpuSetSize)
        .executor(executor)
        .numberOfExecutors(numberOfExecutors)
        .build();
  }
}