Search in sources :

Example 1 with LoadBalancedService

use of com.facebook.buck.slb.LoadBalancedService in project buck by facebook.

the class PublicAnnouncementManager method getAndPostAnnouncements.

public void getAndPostAnnouncements() {
    final ListenableFuture<ImmutableList<Announcement>> message = service.submit(() -> {
        Optional<ClientSideSlb> slb = logConfig.getFrontendConfig().tryCreatingClientSideSlb(clock, eventBus, new CommandThreadFactory("PublicAnnouncement"));
        if (slb.isPresent()) {
            try (FrontendService frontendService = new FrontendService(ThriftOverHttpServiceConfig.of(new LoadBalancedService(slb.get(), logConfig.createOkHttpClient(), eventBus)))) {
                AnnouncementRequest announcementRequest = new AnnouncementRequest();
                announcementRequest.setBuckVersion(getBuckVersion());
                announcementRequest.setRepository(repository);
                FrontendRequest request = new FrontendRequest();
                request.setType(FrontendRequestType.ANNOUNCEMENT);
                request.setAnnouncementRequest(announcementRequest);
                FrontendResponse response = frontendService.makeRequest(request);
                return ImmutableList.copyOf(response.announcementResponse.announcements);
            } catch (IOException e) {
                throw new HumanReadableException("Failed to perform request", e);
            }
        } else {
            throw new HumanReadableException("Failed to establish connection to server.");
        }
    });
    Futures.addCallback(message, new FutureCallback<ImmutableList<Announcement>>() {

        @Override
        public void onSuccess(ImmutableList<Announcement> announcements) {
            LOG.info("Public announcements fetched successfully.");
            if (!announcements.isEmpty()) {
                String announcement = HEADER_MSG;
                for (Announcement entry : announcements) {
                    announcement = announcement.concat(String.format(ANNOUNCEMENT_TEMPLATE, entry.getErrorMessage(), entry.getSolutionMessage()));
                }
                consoleEventBusListener.setPublicAnnouncements(eventBus, Optional.of(announcement));
            }
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.warn("Failed to get public announcements. Reason: %s", t.getMessage());
        }
    });
}
Also used : Announcement(com.facebook.buck.distributed.thrift.Announcement) ImmutableList(com.google.common.collect.ImmutableList) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) IOException(java.io.IOException) ClientSideSlb(com.facebook.buck.slb.ClientSideSlb) HumanReadableException(com.facebook.buck.util.HumanReadableException) FrontendResponse(com.facebook.buck.distributed.thrift.FrontendResponse) FrontendService(com.facebook.buck.distributed.FrontendService) FrontendRequest(com.facebook.buck.distributed.thrift.FrontendRequest) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) AnnouncementRequest(com.facebook.buck.distributed.thrift.AnnouncementRequest)

Example 2 with LoadBalancedService

use of com.facebook.buck.slb.LoadBalancedService in project buck by facebook.

the class ArtifactCaches method createHttpArtifactCache.

private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
    // Setup the default client to use.
    OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
    storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
    int timeoutSeconds = cacheDescription.getTimeoutSeconds();
    setTimeouts(storeClientBuilder, timeoutSeconds);
    storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
    (int) config.getThreadPoolSize(), /* keepAliveDurationMs */
    config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
    // The artifact cache effectively only connects to a single host at a time. We should allow as
    // many concurrent connections to that host as we allow threads.
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
    storeClientBuilder.dispatcher(dispatcher);
    final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
    final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
    // If write headers are specified, add them to every default client request.
    if (!writeHeaders.isEmpty()) {
        storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
    }
    OkHttpClient storeClient = storeClientBuilder.build();
    // For fetches, use a client with a read timeout.
    OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
    setTimeouts(fetchClientBuilder, timeoutSeconds);
    // If read headers are specified, add them to every read client request.
    if (!readHeaders.isEmpty()) {
        fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
    }
    fetchClientBuilder.networkInterceptors().add((chain -> {
        Response originalResponse = chain.proceed(chain.request());
        return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
    }));
    OkHttpClient fetchClient = fetchClientBuilder.build();
    HttpService fetchService;
    HttpService storeService;
    switch(config.getLoadBalancingType()) {
        case CLIENT_SLB:
            HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
            fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
            storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
            break;
        case SINGLE_SERVER:
            URI url = cacheDescription.getUrl();
            fetchService = new SingleUriService(url, fetchClient);
            storeService = new SingleUriService(url, storeClient);
            break;
        default:
            throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
    }
    String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
    boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
    return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
Also used : ConnectionPool(okhttp3.ConnectionPool) BuckEventBus(com.facebook.buck.event.BuckEventBus) Okio(okio.Okio) Source(okio.Source) BytesReceivedEvent(com.facebook.buck.event.NetworkEvent.BytesReceivedEvent) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) Dispatcher(okhttp3.Dispatcher) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) CommonGroups(com.facebook.buck.randomizedtrial.CommonGroups) BuckConfig(com.facebook.buck.cli.BuckConfig) ImmutableList(com.google.common.collect.ImmutableList) DefaultClock(com.facebook.buck.timing.DefaultClock) Map(java.util.Map) ForwardingSource(okio.ForwardingSource) Response(okhttp3.Response) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer) URI(java.net.URI) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Logger(com.facebook.buck.log.Logger) Request(okhttp3.Request) Buffer(okio.Buffer) SingleUriService(com.facebook.buck.slb.SingleUriService) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) CharMatcher(com.google.common.base.CharMatcher) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) TimeUnit(java.util.concurrent.TimeUnit) BufferedSource(okio.BufferedSource) OkHttpClient(okhttp3.OkHttpClient) Optional(java.util.Optional) ConnectionPool(okhttp3.ConnectionPool) AsyncCloseable(com.facebook.buck.util.AsyncCloseable) RandomizedTrial(com.facebook.buck.randomizedtrial.RandomizedTrial) DirCacheExperimentEvent(com.facebook.buck.event.DirCacheExperimentEvent) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) OkHttpClient(okhttp3.OkHttpClient) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) Dispatcher(okhttp3.Dispatcher) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) URI(java.net.URI) Response(okhttp3.Response) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) DefaultClock(com.facebook.buck.timing.DefaultClock) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) SingleUriService(com.facebook.buck.slb.SingleUriService) HttpLoadBalancer(com.facebook.buck.slb.HttpLoadBalancer)

Example 3 with LoadBalancedService

use of com.facebook.buck.slb.LoadBalancedService in project buck by facebook.

the class DistBuildFactory method newFrontendService.

public static FrontendService newFrontendService(CommandRunnerParams params) {
    DistBuildConfig config = new DistBuildConfig(params.getBuckConfig());
    ClientSideSlb slb = config.getFrontendConfig().createClientSideSlb(params.getClock(), params.getBuckEventBus(), new CommandThreadFactory("StampedeNetworkThreadPool", SLB_THREAD_PRIORITY));
    OkHttpClient client = config.createOkHttpClient();
    return new FrontendService(ThriftOverHttpServiceConfig.of(new LoadBalancedService(slb, client, params.getBuckEventBus())));
}
Also used : ClientSideSlb(com.facebook.buck.slb.ClientSideSlb) OkHttpClient(okhttp3.OkHttpClient) CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) FrontendService(com.facebook.buck.distributed.FrontendService) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) DistBuildConfig(com.facebook.buck.distributed.DistBuildConfig)

Example 4 with LoadBalancedService

use of com.facebook.buck.slb.LoadBalancedService in project buck by facebook.

the class DefaultDefectReporter method uploadReport.

private DefectSubmitResult uploadReport(final DefectReport defectReport, DefectSubmitResult.Builder defectSubmitResult, ClientSideSlb slb) throws IOException {
    long timeout = rageConfig.getHttpTimeout();
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).build();
    HttpService httpService = new RetryingHttpService(buckEventBus, new LoadBalancedService(slb, httpClient, buckEventBus), rageConfig.getMaxUploadRetries());
    try {
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.addHeader(REQUEST_PROTOCOL_VERSION, rageConfig.getProtocolVersion().name().toLowerCase());
        requestBuilder.post(new RequestBody() {

            @Override
            public MediaType contentType() {
                return MediaType.parse("application/x-www-form-urlencoded");
            }

            @Override
            public void writeTo(BufferedSink bufferedSink) throws IOException {
                writeReport(defectReport, bufferedSink.outputStream());
            }
        });
        HttpResponse response = httpService.makeRequest(rageConfig.getReportUploadPath(), requestBuilder);
        String responseBody;
        try (InputStream inputStream = response.getBody()) {
            responseBody = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
        }
        if (response.statusCode() == HTTP_SUCCESS_CODE) {
            defectSubmitResult.setIsRequestSuccessful(true);
            if (rageConfig.getProtocolVersion().equals(AbstractRageConfig.RageProtocolVersion.SIMPLE)) {
                return defectSubmitResult.setReportSubmitMessage(responseBody).setReportSubmitLocation(responseBody).build();
            } else {
                // Decode Json response.
                RageJsonResponse json = objectMapper.readValue(responseBody.getBytes(Charsets.UTF_8), RageJsonResponse.class);
                return defectSubmitResult.setIsRequestSuccessful(json.getRequestSuccessful()).setReportSubmitErrorMessage(json.getErrorMessage()).setReportSubmitMessage(json.getMessage()).setReportSubmitLocation(json.getRageUrl()).build();
            }
        } else {
            throw new IOException(String.format("Connection to %s returned code %d and message: %s", response.requestUrl(), response.statusCode(), responseBody));
        }
    } catch (IOException e) {
        throw new IOException(String.format("Failed uploading report because [%s].", e.getMessage()));
    } finally {
        httpService.close();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) RetryingHttpService(com.facebook.buck.slb.RetryingHttpService) HttpService(com.facebook.buck.slb.HttpService) MediaType(okhttp3.MediaType) LoadBalancedService(com.facebook.buck.slb.LoadBalancedService) RequestBody(okhttp3.RequestBody)

Aggregations

LoadBalancedService (com.facebook.buck.slb.LoadBalancedService)4 CommandThreadFactory (com.facebook.buck.log.CommandThreadFactory)3 IOException (java.io.IOException)3 OkHttpClient (okhttp3.OkHttpClient)3 FrontendService (com.facebook.buck.distributed.FrontendService)2 ClientSideSlb (com.facebook.buck.slb.ClientSideSlb)2 HttpService (com.facebook.buck.slb.HttpService)2 RetryingHttpService (com.facebook.buck.slb.RetryingHttpService)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 ImmutableList (com.google.common.collect.ImmutableList)2 MediaType (okhttp3.MediaType)2 Request (okhttp3.Request)2 BuckConfig (com.facebook.buck.cli.BuckConfig)1 DistBuildConfig (com.facebook.buck.distributed.DistBuildConfig)1 Announcement (com.facebook.buck.distributed.thrift.Announcement)1 AnnouncementRequest (com.facebook.buck.distributed.thrift.AnnouncementRequest)1 FrontendRequest (com.facebook.buck.distributed.thrift.FrontendRequest)1 FrontendResponse (com.facebook.buck.distributed.thrift.FrontendResponse)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 DirCacheExperimentEvent (com.facebook.buck.event.DirCacheExperimentEvent)1