streams-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfranklin <...@git.apache.org>
Subject [GitHub] incubator-streams pull request: Streams 190
Date Fri, 10 Oct 2014 21:14:16 GMT
Github user mfranklin commented on a diff in the pull request:

    https://github.com/apache/incubator-streams/pull/99#discussion_r18731291
  
    --- Diff: streams-runtimes/streams-runtime-local/src/main/java/org/apache/streams/local/queues/ThroughputQueue.java
---
    @@ -0,0 +1,498 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.streams.local.queues;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import sun.reflect.generics.reflectiveObjects.NotImplementedException;
    +
    +import javax.management.*;
    +import java.lang.management.ManagementFactory;
    +import java.util.Collection;
    +import java.util.Iterator;
    +import java.util.concurrent.BlockingQueue;
    +import java.util.concurrent.LinkedBlockingQueue;
    +import java.util.concurrent.TimeUnit;
    +import java.util.concurrent.atomic.AtomicLong;
    +import java.util.concurrent.locks.ReadWriteLock;
    +import java.util.concurrent.locks.ReentrantReadWriteLock;
    +
    +/**
    + * A {@link java.util.concurrent.BlockingQueue} implementation that allows the measure
measurement of how
    + * data flows through the queue.  Is also a {@code MBean} so the flow statistics can
be viewed through
    + * JMX. Registration of the bean happens whenever a constructor receives a non-null id.
    + * <p/>
    + * !!! Warning !!!
    + * Only the necessary methods for the local streams runtime are implemented.  All other
methods throw a
    + * {@link sun.reflect.generics.reflectiveObjects.NotImplementedException}.
    + */
    +public class ThroughputQueue<E> implements BlockingQueue<E>, ThroughputQueueMXBean
{
    +
    +    public static final String NAME_TEMPLATE = "org.apache.streams.local:type=ThroughputQueue,name=%s";
    +
    +    private static final Logger LOGGER = LoggerFactory.getLogger(ThroughputQueue.class);
    +
    +    private BlockingQueue<ThroughputElement<E>> underlyingQueue;
    +    private AtomicLong elementsAdded;
    +    private AtomicLong elementsRemoved;
    +    private AtomicLong startTime;
    +    private AtomicLong totalQueueTime;
    +    private long maxQueuedTime;
    +    private volatile boolean active;
    +    private ReadWriteLock maxQueueTimeLock;
    +
    +    /**
    +     * Creates an unbounded, unregistered {@code ThroughputQueue}
    +     */
    +    public ThroughputQueue() {
    +        this(-1, null);
    +    }
    +
    +    /**
    +     * Creates a bounded, unregistered {@code ThroughputQueue}
    +     *
    +     * @param maxSize maximum capacity of queue, if maxSize < 1 then unbounded
    +     */
    +    public ThroughputQueue(int maxSize) {
    +        this(maxSize, null);
    +    }
    +
    +    /**
    +     * Creates an unbounded, registered {@code ThroughputQueue}
    +     *
    +     * @param id unique id for this queue to be registered with. if id == NULL then not
registered
    +     */
    +    public ThroughputQueue(String id) {
    +        this(-1, id);
    +    }
    +
    +    /**
    +     * Creates a bounded, registered {@code ThroughputQueue}
    +     *
    +     * @param maxSize maximum capacity of queue, if maxSize < 1 then unbounded
    +     * @param id      unique id for this queue to be registered with. if id == NULL then
not registered
    +     */
    +    public ThroughputQueue(int maxSize, String id) {
    +        if (maxSize < 1) {
    +            this.underlyingQueue = new LinkedBlockingQueue<>();
    +        } else {
    +            this.underlyingQueue = new LinkedBlockingQueue<>(maxSize);
    +        }
    +        this.elementsAdded = new AtomicLong(0);
    +        this.elementsRemoved = new AtomicLong(0);
    +        this.startTime = new AtomicLong(-1);
    +        this.active = false;
    +        this.maxQueuedTime = 0;
    +        this.maxQueueTimeLock = new ReentrantReadWriteLock();
    +        this.totalQueueTime = new AtomicLong(0);
    +        if (id != null) {
    +            try {
    +                ObjectName name = new ObjectName(String.format(NAME_TEMPLATE, id));
    +                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    +                mbs.registerMBean(this, name);
    +            } catch (MalformedObjectNameException | InstanceAlreadyExistsException |
MBeanRegistrationException | NotCompliantMBeanException e) {
    +                LOGGER.error("Failed to register MXBean : {}", e);
    +                throw new RuntimeException(e);
    +            }
    +        }
    +    }
    +
    +    @Override
    +    public boolean add(E e) {
    +        if (this.underlyingQueue.add(new ThroughputElement<E>(e))) {
    +            this.elementsAdded.incrementAndGet();
    +            synchronized (this) {
    +                if (!this.active) {
    +                    this.startTime.set(System.currentTimeMillis());
    +                    this.active = true;
    +                }
    +            }
    +            return true;
    +        }
    +        return false;
    +    }
    +
    +    @Override
    +    public boolean offer(E e) {
    +        if (this.underlyingQueue.offer(new ThroughputElement<E>(e))) {
    +            this.elementsAdded.incrementAndGet();
    +            synchronized (this) {
    +                if (!this.active) {
    +                    this.startTime.set(System.currentTimeMillis());
    +                    this.active = true;
    +                }
    +            }
    +            return true;
    +        }
    +        return false;
    +    }
    +
    +    @Override
    +    public void put(E e) throws InterruptedException {
    +        this.underlyingQueue.put(new ThroughputElement<E>(e));
    +        this.elementsAdded.incrementAndGet();
    +        synchronized (this) {
    +            if (!this.active) {
    +                this.startTime.set(System.currentTimeMillis());
    +                this.active = true;
    +            }
    +        }
    +    }
    +
    +    @Override
    +    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
{
    +        if (this.underlyingQueue.offer(new ThroughputElement<E>(e), timeout, unit))
{
    +            this.elementsAdded.incrementAndGet();
    +            synchronized (this) {
    +                if (!this.active) {
    +                    this.startTime.set(System.currentTimeMillis());
    +                    this.active = true;
    +                }
    +            }
    +            return true;
    +        }
    +        return false;
    +    }
    +
    +    @Override
    +    public E take() throws InterruptedException {
    +        ThroughputElement<E> e = this.underlyingQueue.take();
    +        this.elementsRemoved.incrementAndGet();
    --- End diff --
    
    This looks like the same logic as poll.  Could this be abstracted to a single method?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message