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

io.cereebro.snitch.detect.redis.RedisRelationshipDetector Maven / Gradle / Ivy

There is a newer version: 1.2.4
Show newest version
/*
 * Copyright © 2017 the original authors (http://cereebro.io)
 *
 * 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 io.cereebro.snitch.detect.redis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import io.cereebro.core.Component;
import io.cereebro.core.ComponentType;
import io.cereebro.core.Dependency;
import io.cereebro.core.Relationship;
import io.cereebro.core.RelationshipDetector;

/**
 * Redis RelationshipDetector. Aside from a Redis sentinel master, nothing
 * carries a name in Redis, therefore for a basic connection we can only detect
 * a relationship to a typed Redis component with a default name.
 * 
 * @author michaeltecourt
 *
 */
public class RedisRelationshipDetector implements RelationshipDetector {

    static final String DEFAULT_NAME = "default";

    private final RedisProperties redisProperties;
    private final List connectionFactories;

    /**
     * Redis RelationshipDetector.
     * 
     * @param redisProperties
     *            Redis configuration properties (nullable).
     * @param redisConnectionFactories
     *            Available RedisConnectionFactory instances (nullable).
     */
    public RedisRelationshipDetector(RedisProperties redisProperties,
            List redisConnectionFactories) {
        this.redisProperties = redisProperties;
        this.connectionFactories = new ArrayList<>();
        if (!CollectionUtils.isEmpty(redisConnectionFactories)) {
            this.connectionFactories.addAll(redisConnectionFactories);
        }
    }

    @Override
    public Set detect() {
        if (CollectionUtils.isEmpty(connectionFactories)) {
            return new HashSet<>();
        }
        String sentinelMaster = getRedisSentinelMasterName();
        String name = StringUtils.hasText(sentinelMaster) ? sentinelMaster : DEFAULT_NAME;
        return Dependency.on(Component.of(name, ComponentType.REDIS)).asRelationshipSet();
    }

    /**
     * Returns the name of the Redis Sentinel master, extracted from
     * configuration properties.
     * 
     * @return Nullable Redis Sentinel master name.
     */
    private String getRedisSentinelMasterName() {
        if (redisProperties != null && redisProperties.getSentinel() != null) {
            return redisProperties.getSentinel().getMaster();
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy