/*
 * Copyright (c) 2014 Spotify AB.
 *
 * 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 com.spotify.helios.servicescommon.statistics;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import org.apache.curator.framework.state.ConnectionState;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ZooKeeperMetricsImpl implements ZooKeeperMetrics {

  private static final String TYPE = "zookeeper";

  private final String prefix;
  private final Meter transientErrorMeter;
  private final Meter connectionStateChanged;
  private final Map<ConnectionState, Meter> connectionStates = new HashMap<>();
  private final MetricRegistry registry;

  public ZooKeeperMetricsImpl(final String group, final MetricRegistry registry) {
    this.prefix = MetricRegistry.name(group, TYPE) + ".";
    this.registry = registry;
    this.transientErrorMeter = registry.meter(prefix + "transient_error_meter");
    this.connectionStateChanged = registry.meter(prefix + "connection_state_changed");
  }

  @Override
  public void zookeeperTransientError() {
    transientErrorMeter.mark();
  }

  @Override
  public void updateTimer(final String name, final long duration, final TimeUnit timeUnit) {
    registry.timer(prefix + name).update(duration, timeUnit);
  }

  @Override
  public void connectionStateChanged(final ConnectionState newState) {
    connectionStateChanged.mark();

    final Meter meter = connectionStates.computeIfAbsent(newState,
        state -> registry.meter(prefix + "connection_state_" + state.name()));
    if (meter != null) {
      meter.mark();
    }
  }
}

@@ -793,56 +793,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A
 impo
@@ -1068,88 +1068,8 @@
 ed;%0A
-  private final Map%3CConnectionState, Meter%3E connectionStates = new HashMap%3C%3E();%0A
   pr
@@ -1444,16 +1444,221 @@
 anged%22);
+%0A%0A    // create all of the meter instances immediately so that we report 0 values after a restart%0A    for (final ConnectionState state : ConnectionState.values()) %7B%0A      connectionStateMeter(state);%0A    %7D
 %0A  %7D%0A%0A  
@@ -2040,92 +2040,134 @@
 ();%0A
-%0A
     
-final Meter meter = connectionStates.computeIfAbsent(newState,%0A        state -%3E
+connectionStateMeter(newState).mark();%0A  %7D%0A%0A  private Meter connectionStateMeter(final ConnectionState state) %7B%0A    return
  reg
@@ -2226,61 +2226,9 @@
 e())
-);%0A    if (meter != null) %7B%0A      meter.mark();%0A    %7D
+;
 %0A  %7D

create connectionState meters immediately