changeset 8:3db016a4e023

beginnings of a storage layer
author Omair Majid <omajid@redhat.com>
date Tue, 29 Nov 2011 11:01:09 -0500
parents 93c6c1c31eb8
children 26a79795d026
files src/com/redhat/thermostat/agent/Agent.java src/com/redhat/thermostat/agent/Main.java src/com/redhat/thermostat/agent/MongoStorage.java src/com/redhat/thermostat/agent/Storage.java src/com/redhat/thermostat/agent/StorageConstants.java src/com/redhat/thermostat/agent/config/Configuration.java src/com/redhat/thermostat/common/Constants.java
diffstat 7 files changed, 135 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/agent/Agent.java	Mon Nov 28 17:57:18 2011 -0500
+++ b/src/com/redhat/thermostat/agent/Agent.java	Tue Nov 29 11:01:09 2011 -0500
@@ -4,11 +4,9 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.mongodb.DB;
 import com.redhat.thermostat.agent.config.Configuration;
 import com.redhat.thermostat.backend.Backend;
 import com.redhat.thermostat.backend.BackendRegistry;
-import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.LaunchException;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
@@ -17,24 +15,23 @@
  */
 public class Agent {
 
+    private static final Logger LOGGER = LoggingUtils.getLogger(Agent.class);
+
     private final UUID id;
     private final BackendRegistry backendRegistry;
     private final Configuration config;
 
-    private static final Logger LOGGER = LoggingUtils.getLogger(Agent.class);
+    private Storage storage;
 
-    private DB database;
-
-    public Agent(BackendRegistry backendRegistry, Configuration config, DB db) {
-        this(backendRegistry, UUID.randomUUID(), config, db);
+    public Agent(BackendRegistry backendRegistry, Configuration config, Storage storage) {
+        this(backendRegistry, UUID.randomUUID(), config, storage);
     }
 
-    public Agent(BackendRegistry registry, UUID agentId, Configuration config, DB db) {
+    public Agent(BackendRegistry registry, UUID agentId, Configuration config, Storage storage) {
         this.id = agentId;
         this.backendRegistry = registry;
         this.config = config;
-        this.database = db;
-        config.setCollection(database.getCollection(Constants.AGENT_CONFIG_COLLECTION_NAME));
+        this.storage = storage;
     }
 
     private void startBackends() throws LaunchException {
--- a/src/com/redhat/thermostat/agent/Main.java	Mon Nov 28 17:57:18 2011 -0500
+++ b/src/com/redhat/thermostat/agent/Main.java	Tue Nov 29 11:01:09 2011 -0500
@@ -7,9 +7,6 @@
 import java.util.logging.LogManager;
 import java.util.logging.Logger;
 
-import com.mongodb.DB;
-import com.mongodb.Mongo;
-import com.mongodb.MongoURI;
 import com.redhat.thermostat.agent.config.Configuration;
 import com.redhat.thermostat.backend.BackendLoadException;
 import com.redhat.thermostat.backend.BackendRegistry;
@@ -25,6 +22,8 @@
     }
 
     public static void main(String[] args) {
+        long startTimestamp = System.currentTimeMillis();
+
         try {
             LogManager.getLogManager().readConfiguration(
                     StringUtils.toInputStream(Constants.LOGGING_CONFIG));
@@ -46,7 +45,7 @@
 
         Configuration config = null;
         try {
-            config = new Configuration(args, props);
+            config = new Configuration(startTimestamp, args, props);
         } catch (LaunchException e1) {
             System.exit(Constants.EXIT_CONFIGURATION_ERROR);
         }
@@ -62,12 +61,9 @@
             System.exit(Constants.EXIT_BACKEND_LOAD_ERROR);
         }
 
-        Mongo mongo = null;
-        DB db = null;
+        Storage storage = new MongoStorage();
         try {
-            MongoURI mongoURI = new MongoURI(config.getDatabaseURIAsString());
-            mongo = new Mongo(mongoURI);
-            db = mongo.getDB(Constants.THERMOSTAT_DB);
+            storage.connect(config.getDatabaseURIAsString());
             logger.fine("connected");
         } catch (UnknownHostException uhe) {
             System.err.println("unknown host");
@@ -75,8 +71,9 @@
             System.exit(Constants.EXIT_UNABLE_TO_CONNECT_TO_DATABASE);
         }
 
-        Agent agent = new Agent(backendRegistry, config, db);
+        Agent agent = new Agent(backendRegistry, config, storage);
         config.setAgent(agent);
+        config.setStorage(storage);
         try {
             agent.start();
         } catch (LaunchException le) {
@@ -94,6 +91,6 @@
 
         agent.stop();
         logger.fine("agent unpublished");
-        
+
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/agent/MongoStorage.java	Tue Nov 29 11:01:09 2011 -0500
@@ -0,0 +1,66 @@
+package com.redhat.thermostat.agent;
+
+import java.net.UnknownHostException;
+import java.util.UUID;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBObject;
+import com.mongodb.Mongo;
+import com.mongodb.MongoURI;
+import com.mongodb.WriteConcern;
+import com.redhat.thermostat.agent.config.Configuration;
+
+public class MongoStorage implements Storage {
+
+    private Mongo mongo = null;
+    private DB db = null;
+
+    private UUID agentId = null;
+
+    @Override
+    public void connect(String uri) throws UnknownHostException {
+        connect(new MongoURI(uri));
+    }
+
+    public void connect(MongoURI uri) throws UnknownHostException {
+        mongo = new Mongo(uri);
+        db = mongo.getDB(StorageConstants.THERMOSTAT_DB);
+    }
+
+    @Override
+    public void setAgentId(UUID agentId) {
+        this.agentId = agentId;
+    }
+
+    @Override
+    public void addAgentInformation(Configuration config) {
+        DBCollection configCollection = db.getCollection(StorageConstants.COLLECTION_AGENT_CONFIG);
+        DBObject toInsert = config.toDBObject();
+        toInsert.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_ID, agentId.toString());
+        configCollection.insert(toInsert, WriteConcern.SAFE);
+    }
+
+    @Override
+    public void removeAgentInformation() {
+        DBCollection configCollection = db.getCollection(StorageConstants.COLLECTION_AGENT_CONFIG);
+        BasicDBObject toRemove = new BasicDBObject(StorageConstants.KEY_AGENT_CONFIG_AGENT_ID, agentId.toString());
+        configCollection.remove(toRemove, WriteConcern.NORMAL);
+    }
+
+    @Override
+    public String getBackendConfig(String backendName, String configurationKey) {
+        DBCollection configCollection = db.getCollection(StorageConstants.COLLECTION_AGENT_CONFIG);
+        BasicDBObject query = new BasicDBObject();
+        query.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_ID, agentId.toString());
+        query.put(StorageConstants.KEY_AGENT_CONFIG_BACKENDS + "." + backendName, new BasicDBObject("$exists", true));
+        DBObject config = configCollection.findOne(query);
+        Object value = config.get(configurationKey);
+        if (value instanceof String) {
+            return (String) value;
+        }
+        return null;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/agent/Storage.java	Tue Nov 29 11:01:09 2011 -0500
@@ -0,0 +1,22 @@
+package com.redhat.thermostat.agent;
+
+import java.net.UnknownHostException;
+import java.util.UUID;
+
+import com.redhat.thermostat.agent.config.Configuration;
+
+public interface Storage {
+    public void connect(String uri) throws UnknownHostException;
+
+    public void setAgentId(UUID id);
+
+    public void addAgentInformation(Configuration config);
+
+    public void removeAgentInformation();
+
+    /**
+     * @return {@code null} if the value is invalid or missing
+     */
+    public String getBackendConfig(String backendName, String configurationKey);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/redhat/thermostat/agent/StorageConstants.java	Tue Nov 29 11:01:09 2011 -0500
@@ -0,0 +1,13 @@
+package com.redhat.thermostat.agent;
+
+public class StorageConstants {
+    public static final String THERMOSTAT_DB = "thermostat";
+
+    public static final String COLLECTION_AGENT_CONFIG = "agent-config";
+
+    public static final String KEY_AGENT_CONFIG_AGENT_ID = "agent-id";
+    public static final String KEY_AGENT_CONFIG_BACKENDS = "backends";
+
+    public static final String KEY_AGENT_CONFIG_AGENT_START_TIME = "start-time";
+
+}
--- a/src/com/redhat/thermostat/agent/config/Configuration.java	Mon Nov 28 17:57:18 2011 -0500
+++ b/src/com/redhat/thermostat/agent/config/Configuration.java	Tue Nov 29 11:01:09 2011 -0500
@@ -12,23 +12,26 @@
 import java.util.logging.Level;
 
 import com.mongodb.BasicDBObject;
-import com.mongodb.DBCollection;
 import com.mongodb.DBObject;
-import com.mongodb.WriteConcern;
 import com.redhat.thermostat.agent.Agent;
 import com.redhat.thermostat.agent.Defaults;
+import com.redhat.thermostat.agent.Storage;
+import com.redhat.thermostat.agent.StorageConstants;
 import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.LaunchException;
 
 public final class Configuration {
 
     /* FIXME
-     * 
+     *
      * This class needs some love.  It mixes up startup configuration with runtime configuration,
      * while each is handled in very different ways.  It probably should be split into separate
      * classes, but it makes very little sense to do that before we have a Storage abstraction
      * hiding implementation details (ie Mongo API stuff).
      */
+
+    private final long startTimestamp;
+
     private Properties props;
 
     private Level logLevel;
@@ -41,9 +44,9 @@
     private String hostname;
 
     private Agent agent;
-    private DBCollection dbCollection = null;
+    private Storage storage = null;
 
-    public Configuration(String[] args, Properties props) throws LaunchException {
+    public Configuration(long startTime, String[] args, Properties props) throws LaunchException {
         this.props = props;
 
         initFromDefaults();
@@ -62,6 +65,7 @@
                 e.printStackTrace();
             }
         }
+        startTimestamp = startTime;
     }
 
     private void initFromDefaults() {
@@ -93,9 +97,8 @@
         }
     }
 
-    // TODO hide Mongo stuff behind Storage facade
-    public void setCollection(DBCollection collection) {
-        dbCollection = collection;
+    public void setStorage(Storage storage) {
+        this.storage = storage;
     }
 
     public Level getLogLevel() {
@@ -110,12 +113,12 @@
         return hostname;
     }
 
-    // TODO all of this should be assembled somewhere behind the Storage facade, once it exists.
+    // TODO move this into Storage as well
     public DBObject toDBObject() {
         BasicDBObject result = new BasicDBObject();
         // TODO explicit exception if agent not yet set.
-        result.put(Constants.AGENT_ID, agent.getId().toString());
-        result.put(Constants.AGENT_CONFIG_KEY_HOST, hostname);
+        result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_ID, agent.getId().toString());
+        result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, startTimestamp);
         // TODO create nested backend config parts
         return result;
     }
@@ -125,15 +128,13 @@
     }
 
     public void publish() {
-        // TODO Hide Mongo stuff behind Storage facade.
-        dbCollection.insert(toDBObject(), WriteConcern.SAFE);
+        storage.addAgentInformation(this);
         // TODO Start configuration-change-detection thread.
     }
 
     public void unpublish() {
         // TODO Stop configuration-change-detection thread.
-        // TODO hide Mongo stuff behind storage facade.
-        dbCollection.remove(new BasicDBObject(Constants.AGENT_ID, agent.getId().toString()), WriteConcern.NORMAL);
+        storage.removeAgentInformation();
     }
 
     public List<String> getStartupBackendClassNames() {
@@ -164,16 +165,13 @@
     }
 
     /**
-     * 
+     *
      * @param backendName
      * @param configurationKey
      * @return
      */
     public String getBackendConfigValue(String backendName, String configurationKey) {
-        // TODO hide Mongo stuff behind Storage facade.
-        DBObject config = dbCollection.findOne(new BasicDBObject(Constants.AGENT_ID, agent.getId().toString()));
-        // TODO get the appropriate value from this agent's configuration.
-        return null;
+        return storage.getBackendConfig(backendName, configurationKey);
     }
 
     /**
--- a/src/com/redhat/thermostat/common/Constants.java	Mon Nov 28 17:57:18 2011 -0500
+++ b/src/com/redhat/thermostat/common/Constants.java	Tue Nov 29 11:01:09 2011 -0500
@@ -17,23 +17,15 @@
     public static final int EXIT_BACKEND_LOAD_ERROR = 5;
     public static final int EXIT_BACKEND_START_ERROR = 6;
 
-    public static final String THERMOSTAT_DB = "thermostat";
-
-    public static final String AGENT_CONFIG_COLLECTION_NAME = "agent-configs";
-    public static final String AGENT_ID = "agent-id";
-
     public static final int SAMPLING_INTERVAL_UNKNOWN = -1;
 
-    public static final String AGENT_CONFIG_KEY_HOST = "host";
-    public static final String AGENT_CONFIG_KEY_BACKENDS = "backends";
-    public static final String AGENT_CONFIG_KEY_BACKEND_ACTIVE = "active";
-
     public static final String AGENT_ARGUMENT_LOCAL = "--local";
     public static final String AGENT_ARGUMENT_LOGLEVEL = "--loglevel";
 
     public static final String AGENT_PROPERTY_MONGOS_PORT = "mongos_port";
     public static final String AGENT_PROPERTY_MONGOD_PORT = "mongod_port";
     public static final String AGENT_PROPERTY_BACKENDS = "backends";
+    public static final String AGENT_PROPERTY_BACKEND_ACTIVE = "active";
 
     public static final String AGENT_LOCAL_HOSTNAME = "localhost";