<commit_before>package com.google.devtools.build.remote;

import com.google.common.io.CharStreams;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.remote.RemoteOptions;
import com.google.devtools.build.lib.remote.RemoteWorkExecutor;
import com.google.devtools.build.lib.remote.MemcacheActionCache;
import com.google.devtools.build.lib.remote.MemcacheWorkExecutor;
import com.google.devtools.build.lib.remote.RemoteProtocol.RemoteWorkRequest;
import com.google.devtools.build.lib.remote.RemoteProtocol.RemoteWorkResponse;
import com.google.devtools.build.lib.util.CommandFailureUtils;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.cache.Cache;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BuildRequestServlet extends HttpServlet {
    private final Path workPath;
    private final RemoteOptions remoteOptions;
    private final BuildWorkerOptions options;
    private final Cache<String, byte[]> cache;
    private final ListeningExecutorService executorService;

    public BuildRequestServlet(Path workPath, RemoteOptions remoteOptions,
                               BuildWorkerOptions options, Cache<String, byte[]> cache) {
        this.workPath = workPath;
        this.remoteOptions = remoteOptions;
        this.options = options;
        this.cache = cache;
        this.executorService = MoreExecutors.newDirectExecutorService();
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("application/json");
      RemoteWorkResponse.Builder workResponse = RemoteWorkResponse.newBuilder();
      // TODO(alpha): Choose a better temp directory name.
      Path tempRoot = workPath.getRelative("build-" + System.currentTimeMillis());
      FileSystemUtils.createDirectoryAndParents(tempRoot);
      boolean deleteTree = true;
      try {
        String workJson = CharStreams.toString(request.getReader());
        RemoteWorkRequest.Builder workRequest = RemoteWorkRequest.newBuilder();
        JsonFormat.parser().merge(workJson, workRequest);
        RemoteWorkRequest work = workRequest.build();
        final MemcacheActionCache actionCache = new MemcacheActionCache(
            tempRoot,
            remoteOptions,
            cache);
        final MemcacheWorkExecutor workExecutor = new MemcacheWorkExecutor(<commit_msg>Use a random work path for remote build worker<commit_after>package com.google.devtools.build.remote;

import com.google.common.io.CharStreams;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.remote.RemoteOptions;
import com.google.devtools.build.lib.remote.RemoteWorkExecutor;
import com.google.devtools.build.lib.remote.MemcacheActionCache;
import com.google.devtools.build.lib.remote.MemcacheWorkExecutor;
import com.google.devtools.build.lib.remote.RemoteProtocol.RemoteWorkRequest;
import com.google.devtools.build.lib.remote.RemoteProtocol.RemoteWorkResponse;
import com.google.devtools.build.lib.util.CommandFailureUtils;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import javax.cache.Cache;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BuildRequestServlet extends HttpServlet {
    private final Path workPath;
    private final RemoteOptions remoteOptions;
    private final BuildWorkerOptions options;
    private final Cache<String, byte[]> cache;
    private final ListeningExecutorService executorService;

    public BuildRequestServlet(Path workPath, RemoteOptions remoteOptions,
                               BuildWorkerOptions options, Cache<String, byte[]> cache) {
        this.workPath = workPath;
        this.remoteOptions = remoteOptions;
        this.options = options;
        this.cache = cache;
        this.executorService = MoreExecutors.newDirectExecutorService();
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("application/json");
      RemoteWorkResponse.Builder workResponse = RemoteWorkResponse.newBuilder();
      // TODO(alpha): Choose a better temp directory name.
      Path tempRoot = workPath.getRelative("build-" + UUID.randomUUID().toString());
      FileSystemUtils.createDirectoryAndParents(tempRoot);
      boolean deleteTree = true;
      try {
        String workJson = CharStreams.toString(request.getReader());
        RemoteWorkRequest.Builder workRequest = RemoteWorkRequest.newBuilder();
        JsonFormat.parser().merge(workJson, workRequest);
        RemoteWorkRequest work = workRequest.build();
        final MemcacheActionCache actionCache = new MemcacheActionCache(
            tempRoot,
            remoteOptions,
            cache);
        final MemcacheWorkExecutor workExecutor = new MemcacheWorkExecutor(
{'fraction_non_alphanumeric': 0.08482142857142858, 'fraction_numerical': 0.0, 'mean_word_length': 4.219534050179211, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7325933', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1472, 'n_words': 297}