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

org.graylog2.indexer.indices.ShardsInfo Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.indexer.indices;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.EnumUtils;

import java.util.Locale;
import java.util.function.Function;

public record ShardsInfo(String index, int shard, ShardType shardType, State state, long docs, String store, String ip, String node ) {
    public static boolean isStartedPrimaryShard(ShardsInfo shardsInfo) {
        return shardsInfo.shardType() == ShardsInfo.ShardType.PRIMARY && shardsInfo.state() == ShardsInfo.State.STARTED;
    }

    public static ShardsInfo create(JsonNode jsonNode) {

        String index = jsonNode.get("index").asText();
        int shard = jsonNode.get("shard").asInt();
        String ip = getValueOrDefault(jsonNode, "ip", JsonNode::asText, null);

        String store = getValueOrDefault(jsonNode, "store", JsonNode::asText, null);
        String node = getValueOrDefault(jsonNode, "node", JsonNode::asText, null);
        long docs = getValueOrDefault(jsonNode, "docs", JsonNode::asLong, 0L);

        State state = EnumUtils.getEnumIgnoreCase(State.class, jsonNode.get("state").asText(), State.UNKNOWN);
        ShardType shardType = ShardType.fromString(jsonNode.get("prirep").asText());

        return new ShardsInfo(index, shard, shardType, state, docs, store, ip, node);
    }

    private static  T getValueOrDefault(JsonNode jsonNode, String nodeName, Function valueConverter, T defaultValue) {
        return jsonNode.hasNonNull(nodeName) ? valueConverter.apply(jsonNode) :  defaultValue;
    }

    public enum State {
        INITIALIZING,
        RELOCATING,
        UNASSIGNED,
        STARTED,
        UNKNOWN
    }

    public enum ShardType {
        PRIMARY,
        REPLICA,
        UNKNOWN;
        public static ShardType fromString(String value) {
            return switch (value.toLowerCase(Locale.ENGLISH)) {
                case "r" -> REPLICA;
                case "p" -> PRIMARY;
                default -> UNKNOWN;
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy