<commit_before>/*
 * Copyright 2014 <NAME> <<EMAIL>>.
 *
 * 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 org.elasticlib.node.components;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.elasticlib.common.hash.Guid;
import org.elasticlib.common.model.AgentInfo;
import org.elasticlib.common.model.ReplicationDef;
import static org.elasticlib.node.manager.storage.DatabaseEntries.entry;
import org.elasticlib.node.manager.storage.StorageManager;
import org.elasticlib.node.repository.Agent;
import org.elasticlib.node.repository.Repository;

/**
 * Manages replication agents.
 */
public class ReplicationAgentsPool {

    private static final String REPLICATION_CUR_SEQS = "replicationCurSeqs";

    private final StorageManager storageManager;
    private final RepositoriesProvider repositoriesProvider;
    private final Map<Guid, Agent> agents = new HashMap<>();
    private Database curSeqsDb;

    /**
     * Constructor.
     *
     * @param storageManager Persistent storage provider.
     * @param repositoriesProvider Repositories provider.
     */
    public ReplicationAgentsPool(StorageManager storageManager, RepositoriesProvider repositoriesProvider) {
        this.storageManager = storageManager;
        this.repositoriesProvider = repositoriesProvider;
    }

    /**
     * Initializes the pool.
     */
    public void start() {
        curSeqsDb = storageManager.openDeferredWriteDatabase(REPLICATION_CUR_SEQS);
    }

    /**
     * Stops all agents.
     */
    public void stop() {
        agents.values().forEach(Agent::stop);
        agents.clear();
    }

    /**
     * Creates and (if possible) starts a new agent.
     *
     * @param def Definition of the related replication.
     */
    public void createAgent(ReplicationDef def) {
        // Ensures agent won't see a stale value.
        curSeqsDb.delete(null, entry(def.getGuid()));
        tryStartAgent(def);
    }

    /**
     * Starts a new agent. Does nothing if agent is already started.
     *
     * @param def Definition of the related replication.
     */
    public void startAgent(ReplicationDef def) {
        if (agents.containsKey(def.getGuid())) {
            return;
        }
        startAgent(def.getGuid(),
                   repositoriesProvider.getRepository(def.getSource()),
                   repositoriesProvider.getRepository(def.getDestination()));
    }

    /**
     * Starts a new agent if its source and destination repositories are available. Does nothing if agent is already
     * started.
     *
     * @param def Definition of the related replication.
     */
    public void tryStartAgent(ReplicationDef def) {
        if (agents.containsKey(def.getGuid())) {
            return;
        }
        Optional<Repository> source = repositoriesProvider.tryGetRepository(def.getSource());
        Optional<Repository> destination = repositoriesProvider.tryGetRepository(def.getDestination());
        if (!source.isPresent() || !destination.isPresent()) {
            return;
        }
        startAgent(def.getGuid(), source.get(), destination.get());
    }

    private void startAgent(Guid guid, Repository source, Repository destination) {
        DatabaseEntry curSeqKey = entry(guid);
        Agent agent = new ReplicationAgent(source, destination, curSeqsDb, curSeqKey);

        agents.put(guid, agent);
        agent.start();
    }

    /**
     * Stops an agent. Does nothing if agent is already stopped.
     *
     * @param guid GUID of the related replication.
     */
    public void stopAgent(Guid guid) {
        if (!agents.containsKey(guid)) {
            return;
        }
        agents.remove(guid).stop();
    }

    /**
     * Stops and deletes an agent.
     *
     * @param guid GUID of the related replication.
     */
    public void deleteAgent(Guid guid) {
        stopAgent(guid);

        // Can't use a transaction on a deffered write database :(
        curSeqsDb.delete(null, entry(guid));<commit_msg>Enable a stopped replication agent to be restarted<commit_after>/*
 * Copyright 2014 <NAME> <<EMAIL>>.
 *
 * 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 org.elasticlib.node.components;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.elasticlib.common.hash.Guid;
import org.elasticlib.common.model.AgentInfo;
import org.elasticlib.common.model.AgentState;
import org.elasticlib.common.model.ReplicationDef;
import static org.elasticlib.node.manager.storage.DatabaseEntries.entry;
import org.elasticlib.node.manager.storage.StorageManager;
import org.elasticlib.node.repository.Agent;
import org.elasticlib.node.repository.Repository;

/**
 * Manages replication agents.
 */
public class ReplicationAgentsPool {

    private static final String REPLICATION_CUR_SEQS = "replicationCurSeqs";

    private final StorageManager storageManager;
    private final RepositoriesProvider repositoriesProvider;
    private final Map<Guid, Agent> agents = new HashMap<>();
    private Database curSeqsDb;

    /**
     * Constructor.
     *
     * @param storageManager Persistent storage provider.
     * @param repositoriesProvider Repositories provider.
     */
    public ReplicationAgentsPool(StorageManager storageManager, RepositoriesProvider repositoriesProvider) {
        this.storageManager = storageManager;
        this.repositoriesProvider = repositoriesProvider;
    }

    /**
     * Initializes the pool.
     */
    public void start() {
        curSeqsDb = storageManager.openDeferredWriteDatabase(REPLICATION_CUR_SEQS);
    }

    /**
     * Stops all agents.
     */
    public void stop() {
        agents.values().forEach(Agent::stop);
        agents.clear();
    }

    /**
     * Creates and (if possible) starts a new agent.
     *
     * @param def Definition of the related replication.
     */
    public void createAgent(ReplicationDef def) {
        // Ensures agent won't see a stale value.
        curSeqsDb.delete(null, entry(def.getGuid()));
        tryStartAgent(def);
    }

    /**
     * Starts a new agent. Does nothing if agent is already started.
     *
     * @param def Definition of the related replication.
     */
    public void startAgent(ReplicationDef def) {
        if (isStarted(def.getGuid())) {
            return;
        }
        startAgent(def.getGuid(),
                   repositoriesProvider.getRepository(def.getSource()),
                   repositoriesProvider.getRepository(def.getDestination()));
    }

    /**
     * Starts a new agent if its source and destination repositories are available. Does nothing if agent is already
     * started.
     *
     * @param def Definition of the related replication.
     */
    public void tryStartAgent(ReplicationDef def) {
        if (isStarted(def.getGuid())) {
            return;
        }
        Optional<Repository> source = repositoriesProvider.tryGetRepository(def.getSource());
        Optional<Repository> destination = repositoriesProvider.tryGetRepository(def.getDestination());
        if (!source.isPresent() || !destination.isPresent()) {
            return;
        }
        startAgent(def.getGuid(), source.get(), destination.get());
    }

    private boolean isStarted(Guid guid) {
        if (!agents.containsKey(guid)) {
            return false;
        }
        AgentState state = agents.get(guid).info().getState();
        return state != AgentState.STOPPED && state != AgentState.ERROR;
    }

    private void startAgent(Guid guid, Repository source, Repository destination) {
        DatabaseEntry curSeqKey = entry(guid);
        Agent agent = new ReplicationAgent(source, destination, curSeqsDb, curSeqKey);

        Agent previous = agents.put(guid, agent);
        if (previous != null) {
            previous.stop();
        }
        agent.start();
    }

    /**
     * Stops an agent. Does nothing if agent is already stopped.
     *
     * @param guid GUID of the related replication.
     */
    public void stopAgent(Guid guid) {
        if (!agents.containsKey(guid)) {
            return;
        }
        agents.remove(guid).stop();
    }

    /**
     * Stops and deletes an agent.
     *
     * @param guid GUID of the related replication.
     */
    public void deleteAgent(Guid guid) {
        stopAgent(guid);

        // Can't use a transaction on a deffered write database :(
        curSeqsDb.delete(null, entry(guid));
{'fraction_non_alphanumeric': 0.09744658676393955, 'fraction_numerical': 0.0016675351745700886, 'mean_word_length': 2.79889152810768, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4827977', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2493, 'n_words': 734}