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

org.graylog2.cluster.ClusterConfigServiceImpl Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.cluster;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.eventbus.EventBus;
import com.mongodb.DBCollection;
import com.mongodb.WriteConcern;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoConnection;
import org.graylog2.events.ClusterEventBus;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.system.NodeId;
import org.graylog2.shared.plugins.ChainingClassLoader;
import org.graylog2.shared.utilities.AutoValueUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.mongojack.DBCursor;
import org.mongojack.DBQuery;
import org.mongojack.DBSort;
import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.Set;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;

public class ClusterConfigServiceImpl implements ClusterConfigService {
    private static final Logger LOG = LoggerFactory.getLogger(ClusterConfigServiceImpl.class);

    @VisibleForTesting
    static final String COLLECTION_NAME = "cluster_config";

    private final JacksonDBCollection dbCollection;
    private final NodeId nodeId;
    private final ObjectMapper objectMapper;
    private final ChainingClassLoader chainingClassLoader;
    private final EventBus clusterEventBus;

    @Inject
    public ClusterConfigServiceImpl(final MongoJackObjectMapperProvider mapperProvider,
                                    final MongoConnection mongoConnection,
                                    final NodeId nodeId,
                                    final ObjectMapper objectMapper,
                                    final ChainingClassLoader chainingClassLoader,
                                    final ClusterEventBus clusterEventBus) {
        this(JacksonDBCollection.wrap(prepareCollection(mongoConnection), ClusterConfig.class, String.class, mapperProvider.get()),
                nodeId, objectMapper, chainingClassLoader, clusterEventBus);
    }

    ClusterConfigServiceImpl(final JacksonDBCollection dbCollection,
                             final NodeId nodeId,
                             final ObjectMapper objectMapper,
                             final ChainingClassLoader chainingClassLoader,
                             final EventBus clusterEventBus) {
        this.nodeId = checkNotNull(nodeId);
        this.dbCollection = checkNotNull(dbCollection);
        this.objectMapper = checkNotNull(objectMapper);
        this.chainingClassLoader = chainingClassLoader;
        this.clusterEventBus = checkNotNull(clusterEventBus);
    }

    @VisibleForTesting
    static DBCollection prepareCollection(final MongoConnection mongoConnection) {
        DBCollection coll = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
        coll.createIndex(DBSort.asc("type"), "unique_type", true);
        coll.setWriteConcern(WriteConcern.JOURNALED);

        return coll;
    }

    private  T extractPayload(Object payload, Class type) {
        try {
            return objectMapper.convertValue(payload, type);
        } catch (IllegalArgumentException e) {
            LOG.debug("Error while deserializing payload", e);
            return null;
        }
    }

    @Override
    public  T get(Class type) {
        ClusterConfig config = dbCollection.findOne(DBQuery.is("type", type.getCanonicalName()));

        if (config == null) {
            LOG.debug("Couldn't find cluster config of type {}", type.getCanonicalName());
            return null;
        }

        T result = extractPayload(config.payload(), type);
        if (result == null) {
            LOG.error("Couldn't extract payload from cluster config (type: {})", type.getCanonicalName());
        }

        return result;
    }

    @Override
    public  T getOrDefault(Class type, T defaultValue) {
        return firstNonNull(get(type), defaultValue);
    }

    @Override
    public  void write(T payload) {
        if (payload == null) {
            LOG.debug("Payload was null. Skipping.");
            return;
        }

        String canonicalClassName = AutoValueUtils.getCanonicalName(payload.getClass());
        ClusterConfig clusterConfig = ClusterConfig.create(canonicalClassName, payload, nodeId.toString());

        dbCollection.update(DBQuery.is("type", canonicalClassName), clusterConfig, true, false, WriteConcern.JOURNALED);

        ClusterConfigChangedEvent event = ClusterConfigChangedEvent.create(
                DateTime.now(DateTimeZone.UTC), nodeId.toString(), canonicalClassName);
        clusterEventBus.post(event);
    }

    @Override
    public  int remove(Class type) {
        final String canonicalName = type.getCanonicalName();
        final WriteResult result = dbCollection.remove(DBQuery.is("type", canonicalName));
        return result.getN();
    }

    @Override
    public Set> list() {
        final DBCursor clusterConfigs = dbCollection.find();
        final ImmutableSet.Builder> classes = ImmutableSet.builder();

        for (ClusterConfig clusterConfig : clusterConfigs) {
            final String type = clusterConfig.type();
            try {
                final Class cls = chainingClassLoader.loadClass(type);
                classes.add(cls);
            } catch (ClassNotFoundException e) {
                LOG.debug("Couldn't find configuration class \"{}\"", type, e);
            }
        }

        return classes.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy