Search in sources :

Example 1 with LogicalNode

use of com.datatorrent.bufferserver.internal.LogicalNode in project apex-core by apache.

the class Server method handleSubscriberRequest.

/**
   *
   * @param request
   * @param key
   */
private void handleSubscriberRequest(final SubscribeRequestTuple request, final SelectionKey key) {
    try {
        serverHelperExecutor.submit(new Runnable() {

            @Override
            public void run() {
                final String upstream_identifier = request.getUpstreamIdentifier();
                /*
           * if there is already a datalist registered for the type in which this client is interested,
           * then get a iterator on the data items of that data list. If the datalist is not registered,
           * then create one and register it. Hopefully this one would be used by future upstream nodes.
           */
                DataList dl = publisherBuffers.get(upstream_identifier);
                if (dl == null) {
                    dl = Tuple.FAST_VERSION.equals(request.getVersion()) ? new FastDataList(upstream_identifier, blockSize, numberOfCacheBlocks, BACK_PRESSURE_ENABLED) : new DataList(upstream_identifier, blockSize, numberOfCacheBlocks, BACK_PRESSURE_ENABLED);
                    DataList odl = publisherBuffers.putIfAbsent(upstream_identifier, dl);
                    if (odl != null) {
                        dl = odl;
                    }
                }
                final String identifier = request.getIdentifier();
                final String type = request.getStreamType();
                final long skipWindowId = (long) request.getBaseSeconds() << 32 | request.getWindowId();
                final LogicalNode ln = new LogicalNode(identifier, upstream_identifier, type, dl.newIterator(skipWindowId), skipWindowId, eventloop);
                int mask = request.getMask();
                if (mask != 0) {
                    for (Integer bs : request.getPartitions()) {
                        ln.addPartition(bs, mask);
                    }
                }
                final LogicalNode oln = subscriberGroups.put(type, ln);
                if (oln != null) {
                    oln.boot();
                }
                final Subscriber subscriber = new Subscriber(ln, request.getBufferSize());
                eventloop.submit(new Runnable() {

                    @Override
                    public void run() {
                        key.attach(subscriber);
                        subscriber.registered(key);
                        subscriber.connected();
                    }
                });
            }
        });
    } catch (RejectedExecutionException e) {
        logger.error("Received subscriber request {} after server {} termination. Disconnecting {}.", request, this, key.channel(), e);
        if (key.isValid()) {
            try {
                key.channel().close();
            } catch (IOException ioe) {
                logger.error("Failed to close channel {}", key.channel(), ioe);
            }
        }
    }
}
Also used : FastDataList(com.datatorrent.bufferserver.internal.FastDataList) DataList(com.datatorrent.bufferserver.internal.DataList) FastDataList(com.datatorrent.bufferserver.internal.FastDataList) IOException(java.io.IOException) LogicalNode(com.datatorrent.bufferserver.internal.LogicalNode) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

DataList (com.datatorrent.bufferserver.internal.DataList)1 FastDataList (com.datatorrent.bufferserver.internal.FastDataList)1 LogicalNode (com.datatorrent.bufferserver.internal.LogicalNode)1 IOException (java.io.IOException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1