private void startGrpcServer(Configuration monitoringConfig) { MonitoringServerInterceptor interceptor = MonitoringServerInterceptor.create( monitoringConfig.withCollectorRegistry(collectorRegistry)); grpcPort = Utils.pickUnusedPort(); grpcServer = ServerBuilder.forPort(grpcPort) .addService(ServerInterceptors.intercept(new HelloServiceImpl().bindService(), interceptor)) .build(); try { grpcServer.start(); } catch (IOException e) { throw new RuntimeException("Exception while running grpc server", e); } }
private void startServer() { grpcPort = Utils.pickUnusedPort(); grpcServer = ServerBuilder.forPort(grpcPort) .addService(new HelloServiceImpl().bindService()) .build(); try { grpcServer.start(); } catch (IOException e) { throw new RuntimeException("Exception while running grpc server", e); } }
@Override protected void setServerValue(ServerConfiguration config, String value) { SocketAddress address = Utils.parseSocketAddress(value); if (address instanceof InetSocketAddress) { InetSocketAddress addr = (InetSocketAddress) address; int port = addr.getPort() == 0 ? Utils.pickUnusedPort() : addr.getPort(); // Re-create the address so that the server is available on all local addresses. address = new InetSocketAddress(port); } config.address = address; }
@Override public StreamObserver<Messages.SimpleRequest> streamingCall( final StreamObserver<Messages.SimpleResponse> observer) { final ServerCallStreamObserver<Messages.SimpleResponse> responseObserver = (ServerCallStreamObserver<Messages.SimpleResponse>) observer; // TODO(spencerfang): flow control to stop reading when !responseObserver.isReady return new StreamObserver<Messages.SimpleRequest>() { @Override public void onNext(Messages.SimpleRequest value) { if (shutdown.get()) { responseObserver.onCompleted(); return; } responseObserver.onNext(Utils.makeResponse(value)); } @Override public void onError(Throwable t) { // other side closed with non OK responseObserver.onError(t); } @Override public void onCompleted() { // other side closed with OK responseObserver.onCompleted(); } }; }
@Override public StreamObserver<Messages.SimpleRequest> streamingFromClient( final StreamObserver<Messages.SimpleResponse> responseObserver) { return new StreamObserver<Messages.SimpleRequest>() { Messages.SimpleRequest lastSeen = null; @Override public void onNext(Messages.SimpleRequest value) { if (shutdown.get()) { responseObserver.onCompleted(); return; } lastSeen = value; } @Override public void onError(Throwable t) { // other side closed with non OK responseObserver.onError(t); } @Override public void onCompleted() { if (lastSeen != null) { responseObserver.onNext(Utils.makeResponse(lastSeen)); responseObserver.onCompleted(); } else { responseObserver.onError( Status.FAILED_PRECONDITION .withDescription("never received any requests").asException()); } } }; }
@Override public void streamingFromServer( final Messages.SimpleRequest request, final StreamObserver<Messages.SimpleResponse> observer) { // send forever, until the client cancels or we shut down final Messages.SimpleResponse response = Utils.makeResponse(request); final ServerCallStreamObserver<Messages.SimpleResponse> responseObserver = (ServerCallStreamObserver<Messages.SimpleResponse>) observer; // If the client cancels, copyWithFlowControl takes care of calling // responseObserver.onCompleted() for us StreamObservers.copyWithFlowControl( new Iterator<Messages.SimpleResponse>() { @Override public boolean hasNext() { return !shutdown.get() && !responseObserver.isCancelled(); } @Override public Messages.SimpleResponse next() { return response; } @Override public void remove() { throw new UnsupportedOperationException(); } }, responseObserver); }
LoadServer(Control.ServerConfig config) throws Exception { log.log(Level.INFO, "Server Config \n" + config.toString()); port = config.getPort() == 0 ? Utils.pickUnusedPort() : config.getPort(); ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port); int asyncThreads = config.getAsyncServerThreads() == 0 ? Runtime.getRuntime().availableProcessors() : config.getAsyncServerThreads(); // The concepts of sync & async server are quite different in the C impl and the names // chosen for the enum are based on that implementation. We use 'sync' to mean // the direct executor case in Java even though the service implementations are always // fully async. switch (config.getServerType()) { case ASYNC_SERVER: { serverBuilder.executor(getExecutor(asyncThreads)); break; } case SYNC_SERVER: { serverBuilder.directExecutor(); break; } case ASYNC_GENERIC_SERVER: { serverBuilder.executor(getExecutor(asyncThreads)); // Create buffers for the generic service PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT; genericResponse = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize()); if (genericResponse.capacity() > 0) { genericResponse.writerIndex(genericResponse.capacity() - 1); } break; } default: { throw new IllegalArgumentException(); } } if (config.hasSecurityParams()) { File cert = TestUtils.loadCert("server1.pem"); File key = TestUtils.loadCert("server1.key"); serverBuilder.useTransportSecurity(cert, key); } benchmarkService = new AsyncServer.BenchmarkServiceImpl(); if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) { serverBuilder.addService( ServerServiceDefinition .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME, GENERIC_STREAMING_PING_PONG_METHOD)) .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler()) .build()); } else { serverBuilder.addService(benchmarkService); } server = serverBuilder.build(); List<OperatingSystemMXBean> beans = ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class); if (!beans.isEmpty()) { osBean = beans.get(0); } else { osBean = null; } }
@Before public void setup() throws Exception { int port = Utils.pickUnusedPort(); worker = new LoadWorker(port, 0); worker.start(); channel = NettyChannelBuilder.forAddress("localhost", port).usePlaintext(true).build(); workerServiceStub = WorkerServiceGrpc.newStub(channel); marksQueue = new LinkedBlockingQueue<Stats.ClientStats>(); }
public ManagedChannel newChannel() throws IOException { return Utils.newClientChannel(transport, address, tls, testca, authorityOverride, useDefaultCiphers, flowControlWindow, directExecutor); }
public Messages.SimpleRequest newRequest() { return Utils.makeRequest(payloadType, clientPayload, serverPayload); }
@Override protected void setClientValue(ClientConfiguration config, String value) { config.address = Utils.parseSocketAddress(value); }
@Override public void unaryCall(Messages.SimpleRequest request, StreamObserver<Messages.SimpleResponse> responseObserver) { responseObserver.onNext(Utils.makeResponse(request)); responseObserver.onCompleted(); }