Exemple #1
0
  @SuppressWarnings({"ThrowableInstanceNeverThrown", "ThrowableResultOfMethodCallIgnored"})
  protected void handleConnection(Socket socket, long reservation) {
    IndexProxyWorker worker = createWorker();

    ObjectInputStream input = null;
    ObjectOutputStream output = null;

    try {
      socket.setTcpNoDelay(true);
      long batchSize = 0;

      try {
        input = new ObjectInputStream(socket.getInputStream());
        batchSize = input.readLong();
        reservation = adjustReservation(reservation, batchSize);
        output = new ObjectOutputStream(socket.getOutputStream());
      } catch (EOFException eof) {
        // ignore immediate EOFs before we try to process request - these are typically LB
        // connections
        return;
      } catch (QuotaTimeoutException qte) {
        s_logger.error("Timeout processing adjustment of reservation", qte);
        suicide(); // signal supervisor to restart this service
      }

      m_proxyStats.onProxyWorkerStarted(this);

      worker.fulfillRequest(input, output, batchSize);

      updateStats(worker);
    } catch (Exception e) {
      s_logger.error("Exception attempting to service proxy request on " + socket + ":", e);

      try {
        worker.sendExceptionResponse(
            input,
            output,
            new ProxyException(
                "Unable to service proxy request", SerialSafeException.makeSerializable(e)));
      } catch (IOException ex) {
        s_logger.error("Unable to notify caller of proxy exception", ex);
      }
    } finally {
      IOUtils.closeQuietly(output);
      IOUtils.closeQuietly(input);
      m_quota.release(reservation);
      m_workerSemaphore.release();
      IOUtils.closeQuietly(socket);
    }
  }
 /**
  * Doesn't write the file atomically. Logs, but doesn't throw upon failure.
  *
  * @param file can't be null.
  * @param userId only for logging.
  * @param content
  * @param errorLevel if true log an error level message; otherwise warning level.
  */
 private void touchFile(File file, byte[] content, int userId, boolean errorLevel) {
   OutputStream out = null;
   try {
     file.getParentFile().mkdirs();
     out = new FileOutputStream(file);
     out.write(content);
     out.close();
     out = null;
   } catch (IOException ioe) {
     LogMessageGen lmg = new LogMessageGen();
     lmg.setSubject("Unable to touch cache file");
     lmg.param(LoggingConsts.USER_ID, userId);
     lmg.param(LoggingConsts.FILENAME, file.getAbsolutePath());
     lmg.param(LoggingConsts.CONTENT, content);
     if (errorLevel) {
       m_logCategory.error(lmg.toString(), ioe);
     } else {
       m_logCategory.warn(lmg.toString(), ioe);
     }
   } finally {
     // normally closed above; just for leak prevention.
     IOUtils.safeClose(out, false, m_logCategory);
   }
 }