Mercurial > hg > thermostat-ng > agent
view agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java @ 2767:30a68cb57107
Fix agent to properly activate backends asynchronously
Reviewed-by: jerboaa, jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023942.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/023978.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024496.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024955.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025248.html
author | Christopher Koehler <chkoehle@redhat.com> |
---|---|
date | Wed, 04 Oct 2017 15:08:14 -0400 |
parents | 597f67d95197 |
children | 6ea7021a74d6 |
line wrap: on
line source
/* * Copyright 2012-2017 Red Hat, Inc. * * This file is part of Thermostat. * * Thermostat 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 2, or (at your * option) any later version. * * Thermostat 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 Thermostat; see the file COPYING. If not see * <http://www.gnu.org/licenses/>. * * Linking this code with other modules is making a combined work * based on this code. Thus, the terms and conditions of the GNU * General Public License cover the whole combination. * * As a special exception, the copyright holders of this code give * you permission to link this code with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also * meet, for each linked independent module, the terms and conditions * of the license of that module. An independent module is a module * which is not derived from or based on this code. If you modify * this code, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish * to do so, delete this exception statement from your version. */ package com.redhat.thermostat.agent; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; import com.redhat.thermostat.agent.config.AgentStartupConfiguration; import com.redhat.thermostat.agent.dao.AgentInfoDAO; import com.redhat.thermostat.agent.dao.BackendInfoDAO; import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.backend.BackendActivator; import com.redhat.thermostat.backend.BackendRegistry; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.LaunchException; import com.redhat.thermostat.common.ThermostatExtensionRegistry; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.storage.model.AgentInformation; import com.redhat.thermostat.storage.model.BackendInformation; /** * Represents the Agent running on a host. */ public class Agent { private static final Logger logger = LoggingUtils.getLogger(Agent.class); private static final int BACKEND_ACTIVATION_TIMEOUT_SECONDS = 65; private static final int BACKEND_DEACTIVATION_TIMEOUT_SECONDS = 10; private static final int BACKEND_ACTIVATOR_THREAD_COUNT = 2; private final BackendRegistry backendRegistry; private final AgentStartupConfiguration config; private final Map<Backend, BackendInformation> backendInfos; private final AgentInfoDAO agentDao; private final BackendInfoDAO backendDao; private final WriterID writerID; private final BackendActivator backendActivator; private final Object backendActivationLock = new Object(); private AgentInformation agentInfo; private boolean started = false; private ActionListener<ThermostatExtensionRegistry.Action> backendRegistryListener = new ActionListener<ThermostatExtensionRegistry.Action>() { @Override public void actionPerformed(ActionEvent<ThermostatExtensionRegistry.Action> actionEvent) { final Backend backend = (Backend) actionEvent.getPayload(); switch (actionEvent.getActionId()) { case SERVICE_ADDED: { if (!backendInfos.containsKey(backend)) { backendActivator.activateBackend(backend, new Runnable() { @Override public void run() { synchronized (backendActivationLock) { BackendInformation info = AgentHelper.createBackendInformation(backend, getId()); backendDao.addBackendInformation(info); backendInfos.put(backend, info); } } }); } break; } case SERVICE_REMOVED: { final BackendInformation info = backendInfos.get(backend); if (info != null) { backendActivator.deactivateBackend(backend, new Runnable() { @Override public void run() { synchronized (backendActivationLock) { backendDao.removeBackendInformation(info); backendInfos.remove(backend); } } }); } break; } default: { logger.log(Level.WARNING, "received unknown event from BackendRegistry: " + actionEvent.getActionId()); break; } } } }; public Agent(BackendRegistry registry, AgentStartupConfiguration config, AgentInfoDAO agentInfoDao, BackendInfoDAO backendInfoDao, WriterID writerId) { this(registry, config, agentInfoDao, backendInfoDao, writerId, new BackendActivator(BACKEND_ACTIVATOR_THREAD_COUNT, BACKEND_ACTIVATION_TIMEOUT_SECONDS)); } Agent(BackendRegistry registry, AgentStartupConfiguration config, AgentInfoDAO agentInfoDao, BackendInfoDAO backendInfoDao, WriterID writerId, BackendActivator backendActivator) { this.backendRegistry = registry; this.config = config; this.agentDao = agentInfoDao; this.backendDao = backendInfoDao; this.writerID = writerId; this.backendActivator = backendActivator; this.backendInfos = new ConcurrentHashMap<>(); backendRegistry.addActionListener(backendRegistryListener); } public synchronized void start() throws LaunchException { if (!started) { agentInfo = createAgentInformation(); agentInfo.setAgentId(getId()); agentDao.addAgentInformation(agentInfo); backendRegistry.start(); started = true; } else { logger.warning("Attempt to start agent when already started."); } } Map<Backend, BackendInformation> getBackendInfos() { return backendInfos; } private AgentInformation createAgentInformation() { String writerId = getId(); AgentInformation agentInfo = new AgentInformation(writerId); agentInfo.setStartTime(config.getStartTime()); agentInfo.setAlive(true); return agentInfo; } public synchronized void stop() { if (started) { backendRegistry.stop(); backendActivator.waitForAllBackendsToDeactivate(BACKEND_DEACTIVATION_TIMEOUT_SECONDS); backendActivator.shutdown(); if (config.purge()) { removeAllAgentRelatedInformation(); } else { updateAgentStatusToStopped(); } started = false; } else { logger.warning("Attempt to stop agent which is not active"); } } private void removeAllAgentRelatedInformation() { System.out.println("purging database"); logger.info("purging database"); agentDao.removeAgentInformation(agentInfo); //storage.purge(agentInfo.getAgentId()); TODO Need purge functionality } private void updateAgentStatusToStopped() { agentInfo.setStopTime(System.currentTimeMillis()); agentInfo.setAlive(false); agentDao.updateAgentInformation(agentInfo); } public String getId() { return writerID.getWriterID(); } }