Example #1
0
  public void inputReady(NHttpServerConnection conn, ContentDecoder decoder) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);

      if (protocolState != ProtocolState.REQUEST_HEAD
          && protocolState != ProtocolState.REQUEST_BODY) {
        handleInvalidState(conn, "Request message body data received");
        return;
      }

      SourceContext.updateState(conn, ProtocolState.REQUEST_BODY);

      SourceRequest request = SourceContext.getRequest(conn);

      int readBytes = request.read(conn, decoder);
      if (readBytes > 0) {
        metrics.incrementBytesReceived(readBytes);
      }
    } catch (IOException e) {
      logIOException(conn, e);

      informReaderError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #2
0
    @Override
    public void run(SourceContext<Integer> ctx) throws Exception {
      final Object lock = ctx.getCheckpointLock();
      final int subtaskIndex = getRuntimeContext().getIndexOfThisSubtask();

      while (running) {

        if (counter < numberElements) {
          synchronized (lock) {
            for (int value = subtaskIndex;
                value < numberKeys;
                value += getRuntimeContext().getNumberOfParallelSubtasks()) {

              ctx.collect(value);
            }

            counter++;
          }
        } else {
          if (terminateAfterEmission) {
            running = false;
          } else {
            Thread.sleep(100);
          }
        }
      }
    }
Example #3
0
  public void exception(NHttpServerConnection conn, Exception ex) {
    if (ex instanceof IOException) {
      logIOException(conn, (IOException) ex);

      metrics.incrementFaultsReceiving();

      ProtocolState state = SourceContext.getState(conn);
      if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
        informReaderError(conn);
      } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
        informWriterError(conn);
      } else if (state == ProtocolState.REQUEST_DONE) {
        informWriterError(conn);
      } else if (state == ProtocolState.RESPONSE_DONE) {
        informWriterError(conn);
      }

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } else if (ex instanceof HttpException) {
      try {
        if (conn.isResponseSubmitted()) {
          sourceConfiguration.getSourceConnections().shutDownConnection(conn);
          return;
        }
        HttpContext httpContext = conn.getContext();

        HttpResponse response =
            new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request");
        response.setParams(
            new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams()));
        response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

        // Pre-process HTTP request
        httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
        httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

        sourceConfiguration.getHttpProcessor().process(response, httpContext);

        conn.submitResponse(response);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        conn.close();
      } catch (Exception ex1) {
        log.error(ex.getMessage(), ex);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
      }
    } else {
      log.error("Unexoected error: " + ex.getMessage(), ex);
      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #4
0
  public void connected(NHttpServerConnection conn) {
    // we have to have these two operations in order
    sourceConfiguration.getSourceConnections().addConnection(conn);
    SourceContext.create(conn, ProtocolState.REQUEST_READY, sourceConfiguration);

    metrics.connected();
  }
    @Override
    public void run(SourceContext<String> ctx) throws Exception {
      final Object lockingObject = ctx.getCheckpointLock();

      while (isRunning && index.value() < numElements) {
        char first = (char) ((index.value() % 40) + 40);

        stringBuilder.setLength(0);
        stringBuilder.append(first);

        String result = randomString(stringBuilder, rnd);

        synchronized (lockingObject) {
          index.update(index.value() + step);
          ctx.collect(result);
        }
      }
    }
Example #6
0
  private void informWriterError(NHttpServerConnection conn) {
    Pipe writer = SourceContext.get(conn).getWriter();

    metrics.incrementFaultsSending();

    if (writer != null) {
      writer.consumerError();
    }
  }
Example #7
0
  private void informReaderError(NHttpServerConnection conn) {
    Pipe reader = SourceContext.get(conn).getReader();

    metrics.incrementFaultsReceiving();

    if (reader != null) {
      reader.producerError();
    }
  }
Example #8
0
    @Override
    public void run(SourceContext<Integer> ctx) throws Exception {
      final Object lock = ctx.getCheckpointLock();

      while (running) {
        synchronized (lock) {
          ++counter;
          ctx.collect(1);
        }

        Thread.sleep(2);
        if (counter == 10) {
          workStartedLatch.countDown();
        }

        if (counter >= 500) {
          break;
        }
      }
    }
  @Override
  public void run(SourceContext<Long> ctx) throws Exception {
    final Object checkpointLock = ctx.getCheckpointLock();

    RuntimeContext context = getRuntimeContext();

    final long stepSize = context.getNumberOfParallelSubtasks();
    final long congruence = start + context.getIndexOfThisSubtask();

    final long toCollect =
        ((end - start + 1) % stepSize > (congruence - start))
            ? ((end - start + 1) / stepSize + 1)
            : ((end - start + 1) / stepSize);

    while (isRunning && collected < toCollect) {
      synchronized (checkpointLock) {
        ctx.collect(collected * stepSize + congruence);
        collected++;
      }
    }
  }
Example #10
0
  public void closed(NHttpServerConnection conn) {
    ProtocolState state = SourceContext.getState(conn);

    if (state == ProtocolState.REQUEST_READY || state == ProtocolState.RESPONSE_DONE) {
      if (log.isDebugEnabled()) {
        log.debug(conn + ": Keep-Alive connection was closed: " + conn);
      }
    } else if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
      informReaderError(conn);
      log.warn("Connection closed while reading the request: " + conn);
    } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
      informWriterError(conn);
      log.warn("Connection closed while writing the response: " + conn);
    } else if (state == ProtocolState.REQUEST_DONE) {
      log.warn("Connection closed by the client after request is read: " + conn);
    }

    metrics.disconnected();

    SourceContext.updateState(conn, ProtocolState.CLOSED);
    sourceConfiguration.getSourceConnections().shutDownConnection(conn);
  }
Example #11
0
  public void responseReady(NHttpServerConnection conn) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);
      if (protocolState.compareTo(ProtocolState.REQUEST_DONE) < 0) {
        return;
      }

      if (protocolState.compareTo(ProtocolState.CLOSING) >= 0) {
        return;
      }

      if (protocolState != ProtocolState.REQUEST_DONE) {
        handleInvalidState(conn, "Writing a response");
        return;
      }

      // because the duplex nature of http core we can reach hear without a actual response
      SourceResponse response = SourceContext.getResponse(conn);
      if (response != null) {
        response.start(conn);

        metrics.incrementMessagesSent();
      }
    } catch (IOException e) {
      logIOException(conn, e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } catch (HttpException e) {
      log.error(e.getMessage(), e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #12
0
  @Override
  public void run(SourceContext<OUT> ctx) throws Exception {
    if (iteratorToRead == null) {
      throw new IllegalStateException("Kafka iterator not initialized properly.");
    }

    final Object checkpointLock = ctx.getCheckpointLock();

    while (running && iteratorToRead.hasNext()) {
      MessageAndMetadata<byte[], byte[]> message = iteratorToRead.next();
      if (lastOffsets.getState()[message.partition()] >= message.offset()) {
        LOG.info(
            "Skipping message with offset {} from partition {}",
            message.offset(),
            message.partition());
        continue;
      }
      OUT next = deserializationSchema.deserialize(message.message());

      if (deserializationSchema.isEndOfStream(next)) {
        LOG.info("DeserializationSchema signaled end of stream for this source");
        break;
      }

      // make the state update and the element emission atomic
      synchronized (checkpointLock) {
        lastOffsets.getState()[message.partition()] = message.offset();
        ctx.collect(next);
      }

      if (LOG.isTraceEnabled()) {
        LOG.trace(
            "Processed record with offset {} from partition {}",
            message.offset(),
            message.partition());
      }
    }
  }
Example #13
0
  public void requestReceived(NHttpServerConnection conn) {
    try {

      HttpContext _context = conn.getContext();
      _context.setAttribute(PassThroughConstants.REQ_ARRIVAL_TIME, System.currentTimeMillis());

      if (!SourceContext.assertState(conn, ProtocolState.REQUEST_READY)
          && !SourceContext.assertState(conn, ProtocolState.WSDL_RESPONSE_DONE)) {
        handleInvalidState(conn, "Request received");
        return;
      }
      // we have received a message over this connection. So we must inform the pool
      sourceConfiguration.getSourceConnections().useConnection(conn);

      // at this point we have read the HTTP Headers
      SourceContext.updateState(conn, ProtocolState.REQUEST_HEAD);

      SourceRequest request = new SourceRequest(sourceConfiguration, conn.getHttpRequest(), conn);

      SourceContext.setRequest(conn, request);

      request.start(conn);

      metrics.incrementMessagesReceived();

      /** *** */
      String method =
          request.getRequest() != null
              ? request.getRequest().getRequestLine().getMethod().toUpperCase()
              : "";
      OutputStream os = null;
      if ("GET".equals(method) || "HEAD".equals(method)) {
        HttpContext context = request.getConnection().getContext();
        ContentOutputBuffer outputBuffer =
            new SimpleOutputBuffer(8192, new HeapByteBufferAllocator());
        // ContentOutputBuffer outputBuffer
        // = new SharedOutputBuffer(8192, conn, new
        // HeapByteBufferAllocator());
        context.setAttribute("synapse.response-source-buffer", outputBuffer);
        os = new ContentOutputStream(outputBuffer);
      }

      sourceConfiguration
          .getWorkerPool()
          .execute(new ServerWorker(request, sourceConfiguration, os));
    } catch (HttpException e) {
      log.error(e.getMessage(), e);

      informReaderError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } catch (IOException e) {
      logIOException(conn, e);

      informReaderError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #14
0
 private void handleInvalidState(NHttpServerConnection conn, String action) {
   log.warn(
       action + " while the handler is in an inconsistent state " + SourceContext.getState(conn));
   SourceContext.updateState(conn, ProtocolState.CLOSED);
   sourceConfiguration.getSourceConnections().shutDownConnection(conn);
 }
Example #15
0
  public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);

      // special case to handle WSDLs
      if (protocolState == ProtocolState.WSDL_RESPONSE_DONE) {
        // we need to shut down if the shutdown flag is set
        HttpContext context = conn.getContext();
        ContentOutputBuffer outBuf =
            (ContentOutputBuffer) context.getAttribute("synapse.response-source-buffer");
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null && bytesWritten > 0) {
          metrics.incrementBytesSent(bytesWritten);
        }

        conn.requestInput();
        if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
          sourceConfiguration.getSourceConnections().releaseConnection(conn);
        }

        return;
      }

      if (protocolState != ProtocolState.RESPONSE_HEAD
          && protocolState != ProtocolState.RESPONSE_BODY) {
        log.warn(
            "Illegal incoming connection state: "
                + protocolState
                + " . Possibly two send backs "
                + "are happening for the same request");

        handleInvalidState(conn, "Trying to write response body");
        return;
      }

      SourceContext.updateState(conn, ProtocolState.RESPONSE_BODY);

      SourceResponse response = SourceContext.getResponse(conn);

      int bytesSent = response.write(conn, encoder);

      if (encoder.isCompleted()) {
        HttpContext context = conn.getContext();
        if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null
            && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null
            && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) {

          if (latencyView != null) {
            latencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          } else if (s2sLatencyView != null) {
            s2sLatencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          }
        }

        context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME);
        context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
        context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);
      }

      metrics.incrementBytesSent(bytesSent);
    } catch (IOException e) {
      logIOException(conn, e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
 @Override
 public void run(SourceContext<String> ctx) throws Exception {
   while (running) {
     ctx.collect("hello");
   }
 }