All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arpnetworking.clusteraggregator.models.StatusResponse Maven / Gradle / Ivy

Go to download

(Re)Aggregates host level statistics across clusters and writes both host and cluster statistics to various destinations.

There is a newer version: 1.13.7
Show newest version
/*
 * Copyright 2014 Groupon.com
 *
 * 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.arpnetworking.clusteraggregator.models;

import akka.actor.Address;
import akka.cluster.Member;
import com.arpnetworking.clusteraggregator.ClusterStatusCache;
import com.arpnetworking.commons.builder.OvalBuilder;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Iterables;
import org.joda.time.Period;
import scala.collection.JavaConversions;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Response model for the status http endpoint.
 *
 * @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
 */
public final class StatusResponse {
    public String getClusterLeader() {
        return _clusterLeader != null ? _clusterLeader.toString() : null;
    }

    public String getLocalAddress() {
        return _localAddress.toString();
    }

    @JsonProperty("isLeader")
    public boolean isLeader() {
        return _localAddress.equals(_clusterLeader);
    }

    @JsonSerialize(contentUsing = MemberSerializer.class)
    public Iterable getMembers() {
        return Iterables.unmodifiableIterable(_members);
    }

    public Map getLocalMetrics() {
        return Collections.unmodifiableMap(_localMetrics);
    }

    public Optional> getAllocations() {
        return _allocations.map(Collections::unmodifiableList);
    }

    @JsonUnwrapped
    public VersionInfo getVersionInfo() {
        return VersionInfo.getInstance();
    }

    private StatusResponse(final Builder builder) {
        if (builder._clusterState == null) {
            _clusterLeader = null;
            _members = Collections.emptyList();
        } else {
            _clusterLeader = builder._clusterState.getClusterState().getLeader();
            _members = builder._clusterState.getClusterState().getMembers();
        }

        _localAddress = builder._localAddress;
        _localMetrics = builder._localMetrics;
        _allocations = flatten(
                Optional.ofNullable(builder._clusterState)
                        .map(ClusterStatusCache.StatusResponse::getAllocations));
    }

    private  Optional flatten(final Optional> value) {
        if (value.isPresent()) {
            return value.get();
        }
        return Optional.empty();
    }

    private final Address _localAddress;
    private final Address _clusterLeader;
    private final Iterable _members;
    private final Map _localMetrics;
    private final Optional> _allocations;

    /**
     * Builder for a {@link StatusResponse}.
     */
    public static class Builder extends OvalBuilder {
        /**
         * Public constructor.
         */
        public Builder() {
            super(StatusResponse::new);
        }

        /**
         * Sets the cluster state. Optional.
         *
         * @param value The cluster state.
         * @return This builder.
         */
        public Builder setClusterState(final ClusterStatusCache.StatusResponse value) {
            _clusterState = value;
            return this;
        }

        /**
         * Sets the local address of this cluster node. Required. Cannot be null.
         *
         * @param value The address of the local node.
         * @return This builder.
         */
        public Builder setLocalAddress(final Address value) {
            _localAddress = value;
            return this;
        }

        /**
         * Sets the local metrics for this cluster node.
         *
         * @param value The local metrics.
         * @return This builder.
         */
        public Builder setLocalMetrics(final Map value) {
            _localMetrics = value;
            return this;
        }

        private ClusterStatusCache.StatusResponse _clusterState;
        private Address _localAddress;
        private Map _localMetrics;
    }

    private static final class MemberSerializer extends JsonSerializer {
        @Override
        public void serialize(
                final Member value,
                final JsonGenerator gen,
                final SerializerProvider serializers)
                throws IOException {
            gen.writeStartObject();
            gen.writeStringField("address", value.address().toString());
            gen.writeObjectField("roles", JavaConversions.setAsJavaSet(value.roles()));
            gen.writeNumberField("upNumber", value.upNumber());
            gen.writeStringField("status", value.status().toString());
            gen.writeNumberField("uniqueAddress", value.uniqueAddress().longUid());
            gen.writeEndObject();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy