/*
 * Copyright © 2015 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * 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 co.cask.cdap.data.stream.service;

import co.cask.cdap.data.stream.StreamCoordinatorClient;
import co.cask.cdap.data.stream.StreamPropertyListener;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AbstractIdleService;
import org.apache.twill.common.Cancellable;

import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Basic implementation of a {@link StreamWriterSizeCollector}.
 */
public class BasicStreamWriterSizeCollector extends AbstractIdleService implements StreamWriterSizeCollector {

  private final StreamCoordinatorClient streamCoordinatorClient;
  private final ConcurrentMap<String, AtomicLong> streamSizes;
  private final List<Cancellable> truncationSubscriptions;

  public BasicStreamWriterSizeCollector(StreamCoordinatorClient streamCoordinatorClient) {
    this.streamCoordinatorClient = streamCoordinatorClient;
    this.streamSizes = Maps.newConcurrentMap();
    this.truncationSubscriptions = Lists.newArrayList();
  }

  @Override
  protected void startUp() throws Exception {
    // No-op
  }

  @Override
  protected void shutDown() throws Exception {
    for (Cancellable subscription : truncationSubscriptions) {
      subscription.cancel();
    }
  }

  @Override
  public long getTotalCollected(String streamName) {
    AtomicLong collected = streamSizes.get(streamName);
    return collected != null ? collected.get() : 0;
  }

  @Override
  public synchronized void received(String streamName, long dataSize) {
    AtomicLong value = streamSizes.get(streamName);
    if (value == null) {
      value = streamSizes.putIfAbsent(streamName, new AtomicLong(dataSize));
      if (value != null) {
        // This is the first time that we've seen this stream, we subscribe to generation changes
        truncationSubscriptions.add(streamCoordinatorClient.addListener(streamName, new StreamPropertyListener() {
          @Override
          public void generationChanged(String streamName, int generation) {
            // Handle stream truncation by resetting the size aggregated so far
            streamSizes.replace(streamName, new AtomicLong(0));
          }
        }));
      }
    }
    if (value != null) {
      value.addAndGet(dataSize);
    }
  }
}

/*
 * Copyright © 2015 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * 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 co.cask.cdap.data.stream.service;

import co.cask.cdap.data.stream.StreamCoordinatorClient;
import co.cask.cdap.data.stream.StreamPropertyListener;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Inject;
import org.apache.twill.common.Cancellable;

import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Basic implementation of a {@link StreamWriterSizeCollector}.
 */
public class BasicStreamWriterSizeCollector extends AbstractIdleService implements StreamWriterSizeCollector {

  private final StreamCoordinatorClient streamCoordinatorClient;
  private final ConcurrentMap<String, AtomicLong> streamSizes;
  private final List<Cancellable> truncationSubscriptions;

  @Inject
  public BasicStreamWriterSizeCollector(StreamCoordinatorClient streamCoordinatorClient) {
    this.streamCoordinatorClient = streamCoordinatorClient;
    this.streamSizes = Maps.newConcurrentMap();
    this.truncationSubscriptions = Lists.newArrayList();
  }

  @Override
  protected void startUp() throws Exception {
    // No-op
  }

  @Override
  protected void shutDown() throws Exception {
    for (Cancellable subscription : truncationSubscriptions) {
      subscription.cancel();
    }
  }

  @Override
  public long getTotalCollected(String streamName) {
    AtomicLong collected = streamSizes.get(streamName);
    return collected != null ? collected.get() : 0;
  }

  @Override
  public synchronized void received(String streamName, long dataSize) {
    AtomicLong value = streamSizes.get(streamName);
    if (value == null) {
      value = streamSizes.putIfAbsent(streamName, new AtomicLong(dataSize));
      if (value != null) {
        // This is the first time that we've seen this stream, we subscribe to generation changes
        truncationSubscriptions.add(streamCoordinatorClient.addListener(streamName, new StreamPropertyListener() {
          @Override
          public void generationChanged(String streamName, int generation) {
            // Handle stream truncation by resetting the size aggregated so far
            streamSizes.replace(streamName, new AtomicLong(0));
          }
        }));
      }
    }
    if (value != null) {
      value.addAndGet(dataSize);
    }
  }
}

Fix guice inject annotation missing on constructor