changeset 225:ddc853ede286

Filter out non running database from client view reviewed-by: vanaltj, rkennke review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000789.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 13 Apr 2012 15:22:34 +0200
parents 56aadbbb9156
children 085b44ad8ccb
files client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAO.java common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAOImpl.java common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/src/main/java/com/redhat/thermostat/common/storage/Storage.java common/src/test/java/com/redhat/thermostat/common/dao/HostInfoDAOTest.java
diffstat 8 files changed, 173 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Fri Apr 13 15:22:34 2012 +0200
@@ -80,7 +80,7 @@
 
         @Override
         public Collection<HostRef> getHosts() {
-            return hostsDAO.getHosts();
+            return hostsDAO.getAliveHosts();
         }
 
         @Override
--- a/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowControllerImplTest.java	Fri Apr 13 15:22:34 2012 +0200
@@ -65,6 +65,7 @@
 import com.redhat.thermostat.common.dao.HostRef;
 import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.storage.Chunk;
 
 public class MainWindowControllerImplTest {
 
@@ -157,8 +158,8 @@
         expectedHosts.add(new HostRef("123", "fluffhost1"));
         expectedHosts.add(new HostRef("456", "fluffhost2"));
 
-        when(mockHostsDAO.getHosts()).thenReturn(expectedHosts);
-
+        when(mockHostsDAO.getAliveHosts()).thenReturn(expectedHosts);
+        
         controller.doUpdateTreeAsync();
 
         ArgumentCaptor<HostsVMsLoader> arg = ArgumentCaptor.forClass(HostsVMsLoader.class);
--- a/common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAO.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAO.java	Fri Apr 13 15:22:34 2012 +0200
@@ -60,4 +60,5 @@
     void putHostInfo(HostInfo info);
 
     Collection<HostRef> getHosts();
+    Collection<HostRef> getAliveHosts();
 }
--- a/common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAOImpl.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/HostInfoDAOImpl.java	Fri Apr 13 15:22:34 2012 +0200
@@ -40,6 +40,7 @@
 import java.util.Collection;
 
 import com.redhat.thermostat.common.model.HostInfo;
+import com.redhat.thermostat.common.storage.AgentInformation;
 import com.redhat.thermostat.common.storage.Chunk;
 import com.redhat.thermostat.common.storage.Cursor;
 import com.redhat.thermostat.common.storage.Key;
@@ -66,11 +67,16 @@
     public void putHostInfo(HostInfo info) {
         storage.putChunk(converter.toChunk(info));
     }
-
+    
     @Override
     public Collection<HostRef> getHosts() {
+        return getHosts(new Chunk(hostInfoCategory, false));
+    }
+    
+    private Collection<HostRef> getHosts(Chunk filter) {
         Collection<HostRef> hosts = new ArrayList<HostRef>();
-        Cursor hostsCursor = storage.findAllFromCategory(hostInfoCategory);
+        
+        Cursor hostsCursor = storage.findAll(filter);
         while(hostsCursor.hasNext()) {
             Chunk hostChunk = hostsCursor.next();
             String agentId = hostChunk.get(Key.AGENT_ID);
@@ -79,7 +85,26 @@
         }
         return hosts;
     }
-
+    
+    @Override
+    public Collection<HostRef> getAliveHosts() {
+        
+        Collection<HostRef> hosts = new ArrayList<HostRef>();
+        
+        Chunk agents = new Chunk(AgentInformation.AGENT_INFO_CATEGORY, false);
+        agents.put(AgentInformation.AGENT_ALIVE_KEY, true);
+        Cursor agentCursor = storage.findAll(agents);
+        while(agentCursor.hasNext()) {
+            Chunk chunk = agentCursor.next();
+            
+            Chunk filter = new Chunk(hostInfoCategory, false);
+            filter.put(Key.AGENT_ID, chunk.get(Key.AGENT_ID));
+            
+            hosts.addAll(getHosts(filter));
+        }
+        
+        return hosts;
+    }
     @Override
     public long getCount() {
         return storage.getCount(hostInfoCategory);
--- a/common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Fri Apr 13 15:22:34 2012 +0200
@@ -42,13 +42,18 @@
 
 public class AgentInformation {
 
+    public static final Category AGENT_INFO_CATEGORY =
+            new Category(StorageConstants.CATEGORY_AGENT_CONFIG, Key.AGENT_ID);
+
+    public static final Key<Boolean> AGENT_ALIVE_KEY = new Key<>("alive", false);
+    
     private long startTime;
     private long stopTime;
 
     private boolean alive;
     
     private List<BackendInformation> backends = new ArrayList<BackendInformation>();
-
+    
     public long getStartTime() {
         return startTime;
     }
--- a/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Fri Apr 13 15:22:34 2012 +0200
@@ -291,7 +291,8 @@
         Category cat = query.getCategory();
         DBCollection coll = getCachedCollection(cat.getName());
         ChunkConverter converter = new ChunkConverter();
-        DBCursor dbCursor = coll.find(converter.chunkToDBObject(query));
+        DBObject obj = converter.chunkToDBObject(query);
+        DBCursor dbCursor = coll.find(obj);
         return new MongoCursor(dbCursor, query.getCategory());
     }
 
@@ -310,7 +311,7 @@
         DBObject dbResult = coll.findOne(converter.chunkToDBObject(query));
         return dbResult == null ? null : converter.dbObjectToChunk(dbResult, cat);
     }
-
+    
     @Override
     public Cursor findAllFromCategory(Category category) {
         DBCollection coll = getCachedCollection(category.getName());
--- a/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Fri Apr 13 15:22:34 2012 +0200
@@ -69,7 +69,7 @@
     public abstract Chunk find(Chunk query);
 
     public abstract Cursor findAllFromCategory(Category category);
-
+    
     public abstract long getCount(Category category);
 
     // TODO these will move to appropriate DAO
--- a/common/src/test/java/com/redhat/thermostat/common/dao/HostInfoDAOTest.java	Thu Apr 12 19:27:05 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/HostInfoDAOTest.java	Fri Apr 13 15:22:34 2012 +0200
@@ -44,13 +44,16 @@
 
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
+import org.mockito.internal.verification.Times;
 
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.times;
 
 import com.redhat.thermostat.common.model.HostInfo;
+import com.redhat.thermostat.common.storage.AgentInformation;
 import com.redhat.thermostat.common.storage.Category;
 import com.redhat.thermostat.common.storage.Chunk;
 import com.redhat.thermostat.common.storage.Cursor;
@@ -128,7 +131,8 @@
 
         Storage storage = mock(Storage.class);
         when(storage.findAllFromCategory(HostInfoDAO.hostInfoCategory)).thenReturn(cursor);
-
+        when(storage.findAll(any(Chunk.class))).thenReturn(cursor);
+        
         return storage;
     }
 
@@ -164,7 +168,8 @@
 
         Storage storage = mock(Storage.class);
         when(storage.findAllFromCategory(HostInfoDAO.hostInfoCategory)).thenReturn(cursor);
-
+        when(storage.findAll(any(Chunk.class))).thenReturn(cursor);
+        
         return storage;
     }
 
@@ -196,4 +201,127 @@
         Long count = dao.getCount();
         assertEquals((Long) 5L, count);
     }
+    
+    @Test
+    public void getAliveHostSingle() {
+        Storage storage = setupStorageForSingleAliveHost();
+
+        HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage);
+        Collection<HostRef> hosts = hostsDAO.getAliveHosts();
+
+        // cursor 3 from the above storage should not be used
+        assertEquals(1, hosts.size());
+        assertTrue(hosts.contains(new HostRef("123", "fluffhost1")));
+        verify(storage, times(2)).findAll(any(Chunk.class));
+    }
+    
+    private Storage setupStorageForSingleAliveHost() {
+        
+        // agents
+        
+        Chunk agentConfig1 = new Chunk(AgentInformation.AGENT_INFO_CATEGORY, false);
+        agentConfig1.put(Key.AGENT_ID, "123");
+        agentConfig1.put(AgentInformation.AGENT_ALIVE_KEY, true);
+        
+        Cursor cursor1 = mock(Cursor.class);
+        when(cursor1.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor1.next()).thenReturn(agentConfig1);
+        
+        // hosts
+        
+        Chunk hostConfig1 = new Chunk(HostInfoDAO.hostInfoCategory, false);
+        hostConfig1.put(HostInfoDAO.hostNameKey, "fluffhost1");
+        hostConfig1.put(Key.AGENT_ID, "123");
+        
+        Chunk hostConfig2 = new Chunk(HostInfoDAO.hostInfoCategory, false);
+        hostConfig2.put(HostInfoDAO.hostNameKey, "fluffhost2");
+        hostConfig2.put(Key.AGENT_ID, "456");
+        
+        Cursor cursor2 = mock(Cursor.class);
+        when(cursor2.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor2.next()).thenReturn(hostConfig1);
+
+        Cursor cursor3 = mock(Cursor.class);
+        when(cursor3.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor3.next()).thenReturn(hostConfig2);
+        
+        // storage
+        
+        Storage storage = mock(Storage.class);
+        when(storage.findAll(any(Chunk.class))).thenReturn(cursor1).thenReturn(cursor2).thenReturn(cursor3);
+        
+        return storage;
+    }
+    
+    @Test
+    public void getAliveHost3() {
+        Storage storage = setupStorageForSingleAliveHost3();
+
+        HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage);
+        Collection<HostRef> hosts = hostsDAO.getAliveHosts();
+
+        // cursor 3 from the above storage should not be used
+        assertEquals(3, hosts.size());
+        assertTrue(hosts.contains(new HostRef("123", "fluffhost1")));
+        assertTrue(hosts.contains(new HostRef("456", "fluffhost2")));
+        assertTrue(hosts.contains(new HostRef("678", "fluffhost3")));
+        verify(storage, times(4)).findAll(any(Chunk.class));
+    }
+    
+    private Storage setupStorageForSingleAliveHost3() {
+        
+        // agents
+        
+        Chunk agentConfig1 = new Chunk(AgentInformation.AGENT_INFO_CATEGORY, false);
+        agentConfig1.put(Key.AGENT_ID, "123");
+        agentConfig1.put(AgentInformation.AGENT_ALIVE_KEY, true);
+        
+        Chunk agentConfig2 = new Chunk(AgentInformation.AGENT_INFO_CATEGORY, false);
+        agentConfig2.put(Key.AGENT_ID, "456");
+        agentConfig2.put(AgentInformation.AGENT_ALIVE_KEY, true);
+        
+        Chunk agentConfig3 = new Chunk(AgentInformation.AGENT_INFO_CATEGORY, false);
+        agentConfig3.put(Key.AGENT_ID, "678");
+        agentConfig3.put(AgentInformation.AGENT_ALIVE_KEY, true);
+        
+        Cursor cursor1 = mock(Cursor.class);
+        when(cursor1.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
+        when(cursor1.next()).thenReturn(agentConfig1).thenReturn(agentConfig2).thenReturn(agentConfig3);
+        
+        // hosts
+        
+        Chunk hostConfig1 = new Chunk(HostInfoDAO.hostInfoCategory, false);
+        hostConfig1.put(HostInfoDAO.hostNameKey, "fluffhost1");
+        hostConfig1.put(Key.AGENT_ID, "123");
+        
+        Chunk hostConfig2 = new Chunk(HostInfoDAO.hostInfoCategory, false);
+        hostConfig2.put(HostInfoDAO.hostNameKey, "fluffhost2");
+        hostConfig2.put(Key.AGENT_ID, "456");
+        
+        Chunk hostConfig3 = new Chunk(HostInfoDAO.hostInfoCategory, false);
+        hostConfig3.put(HostInfoDAO.hostNameKey, "fluffhost3");
+        hostConfig3.put(Key.AGENT_ID, "678");
+        
+        Cursor cursor2 = mock(Cursor.class);
+        when(cursor2.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor2.next()).thenReturn(hostConfig1);
+
+        Cursor cursor3 = mock(Cursor.class);
+        when(cursor3.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor3.next()).thenReturn(hostConfig2);
+        
+        Cursor cursor4 = mock(Cursor.class);
+        when(cursor4.hasNext()).thenReturn(true).thenReturn(false);
+        when(cursor4.next()).thenReturn(hostConfig3);
+        
+        // storage
+        
+        Storage storage = mock(Storage.class);
+        when(storage.findAll(any(Chunk.class))).thenReturn(cursor1).
+                                                thenReturn(cursor2).
+                                                thenReturn(cursor3).
+                                                thenReturn(cursor4);
+        
+        return storage;
+    }
 }