Implements a BindableService that generates Out-Of-Band server metrics. Register the returned service to the server, then a client can request for periodic load reports.
| 43 | * Register the returned service to the server, then a client can request for periodic load reports. |
| 44 | */ |
| 45 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9006") |
| 46 | public final class OrcaServiceImpl implements BindableService { |
| 47 | private static final Logger logger = Logger.getLogger(OrcaServiceImpl.class.getName()); |
| 48 | |
| 49 | /** |
| 50 | * Empty or invalid (non-positive) minInterval config in will be treated to this default value. |
| 51 | */ |
| 52 | public static final long DEFAULT_MIN_REPORT_INTERVAL_NANOS = TimeUnit.SECONDS.toNanos(30); |
| 53 | |
| 54 | private final long minReportIntervalNanos; |
| 55 | private final ScheduledExecutorService timeService; |
| 56 | @VisibleForTesting |
| 57 | final AtomicInteger clientCount = new AtomicInteger(0); |
| 58 | private MetricRecorder metricRecorder; |
| 59 | private final RealOrcaServiceImpl delegate = new RealOrcaServiceImpl(); |
| 60 | |
| 61 | /** |
| 62 | * Constructs a service to report server metrics. Config the report interval lower bound, the |
| 63 | * executor to run the timer, and a {@link MetricRecorder} that contains metrics data. |
| 64 | * |
| 65 | * @param minInterval configures the minimum metrics reporting interval for the |
| 66 | * service. Bad configuration (non-positive) will be overridden to service default (30s). |
| 67 | * Minimum metrics reporting interval means, if the setting in the client's |
| 68 | * request is invalid (non-positive) or below this value, they will be treated |
| 69 | * as this value. |
| 70 | */ |
| 71 | public static BindableService createService(ScheduledExecutorService timeService, |
| 72 | MetricRecorder metricsRecorder, |
| 73 | long minInterval, TimeUnit timeUnit) { |
| 74 | return new OrcaServiceImpl(minInterval, timeUnit, timeService, metricsRecorder); |
| 75 | } |
| 76 | |
| 77 | public static BindableService createService(ScheduledExecutorService timeService, |
| 78 | MetricRecorder metricRecorder) { |
| 79 | return new OrcaServiceImpl(DEFAULT_MIN_REPORT_INTERVAL_NANOS, TimeUnit.NANOSECONDS, |
| 80 | timeService, metricRecorder); |
| 81 | } |
| 82 | |
| 83 | private OrcaServiceImpl(long minInterval, TimeUnit timeUnit, ScheduledExecutorService timeService, |
| 84 | MetricRecorder orcaMetrics) { |
| 85 | this.minReportIntervalNanos = minInterval > 0 ? timeUnit.toNanos(minInterval) |
| 86 | : DEFAULT_MIN_REPORT_INTERVAL_NANOS; |
| 87 | this.timeService = checkNotNull(timeService, "timeService"); |
| 88 | this.metricRecorder = checkNotNull(orcaMetrics, "orcaMetrics"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public ServerServiceDefinition bindService() { |
| 93 | return delegate.bindService(); |
| 94 | } |
| 95 | |
| 96 | private final class RealOrcaServiceImpl extends OpenRcaServiceGrpc.OpenRcaServiceImplBase { |
| 97 | @Override |
| 98 | public void streamCoreMetrics( |
| 99 | OrcaLoadReportRequest request, StreamObserver<OrcaLoadReport> responseObserver) { |
| 100 | OrcaClient client = new OrcaClient(request, responseObserver); |
| 101 | client.run(); |
| 102 | clientCount.getAndIncrement(); |