Mercurial > hg > release > thermostat-1.4
changeset 1546:32255ddb2c4c
Compiler project for prepared statement - part II
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-November/011554.html
reviewed-by: omajid
line wrap: on
line diff
--- a/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/ThreadCollector.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/ThreadCollector.java Tue Nov 18 16:03:17 2014 +0100 @@ -42,6 +42,7 @@ import com.redhat.thermostat.thread.model.SessionID; import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHeader; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import com.redhat.thermostat.thread.model.VmDeadLockData; @@ -56,8 +57,15 @@ boolean stopHarvester(); boolean isHarvesterCollecting(); - List<SessionID> getAvailableThreadSummarySessions(Range<Long> range); - SessionID getLastThreadSummarySession(); + /** + * Returns a list of sessions recorded during sampling. + */ + List<ThreadSession> getThreadSessions(Range<Long> range); + + /** + * Returns the last sampling session ID. + */ + SessionID getLastThreadSession(); ThreadSummary getLatestThreadSummary(SessionID session); List<ThreadSummary> getThreadSummary(SessionID session, Range<Long> range);
--- a/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java Tue Nov 18 16:03:17 2014 +0100 @@ -53,6 +53,7 @@ import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHarvestingStatus; import com.redhat.thermostat.thread.model.ThreadHeader; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import com.redhat.thermostat.thread.model.VmDeadLockData; @@ -134,17 +135,15 @@ } @Override - public List<SessionID> getAvailableThreadSummarySessions(Range<Long> range) { - return threadDao.getAvailableThreadSummarySessions(ref, range, Integer.MAX_VALUE); + public List<ThreadSession> getThreadSessions(Range<Long> range) { + return threadDao.getSessions(ref, range, Integer.MAX_VALUE); } @Override - public SessionID getLastThreadSummarySession() { - List<SessionID> sessions = - threadDao.getAvailableThreadSummarySessions(ref, - new Range<>(0l, Long.MAX_VALUE), - 1); - return sessions.isEmpty() ? null : sessions.get(0); + public SessionID getLastThreadSession() { + List<ThreadSession> sessions = + threadDao.getSessions(ref, new Range<>(0l, Long.MAX_VALUE), 1); + return sessions.isEmpty() ? null : new SessionID(sessions.get(0).getSession()); } @Override
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadCountController.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadCountController.java Tue Nov 18 16:03:17 2014 +0100 @@ -36,16 +36,16 @@ package com.redhat.thermostat.thread.client.controller.impl; +import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.common.model.Range; -import com.redhat.thermostat.thread.model.SessionID; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import com.redhat.thermostat.common.Timer; import com.redhat.thermostat.thread.client.common.chart.LivingDaemonThreadDifferenceChart; import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.client.common.view.ThreadCountView; +import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; +import java.util.List; +import java.util.concurrent.TimeUnit; class ThreadCountController extends CommonController { @@ -63,35 +63,55 @@ timer.setAction(new ThreadInformationDataCollector()); } - private class ThreadInformationDataCollector implements Runnable { - @Override - public void run() { + class ThreadInformationDataCollector implements Runnable { + + private void updateLastSession() { - SessionID lastSession = collector.getLastThreadSummarySession(); + ThreadCountView view = (ThreadCountView) ThreadCountController.this.view; + + // load the very latest thread summary + SessionID lastSession = collector.getLastThreadSession(); if (lastSession == null) { return; } - - ThreadCountView view = (ThreadCountView) ThreadCountController.this.view; - - // load the very latest thread summary ThreadSummary latestSummary = collector.getLatestThreadSummary(lastSession); if (latestSummary.getTimeStamp() != 0) { view.setLiveThreads(Long.toString(latestSummary.getCurrentLiveThreads())); view.setDaemonThreads(Long.toString(latestSummary.getCurrentDaemonThreads())); } + } + + private void updateChart() { + + ThreadCountView view = (ThreadCountView) ThreadCountController.this.view; long now = System.currentTimeMillis(); long lastHour = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1); - List<ThreadSummary> summaries = collector.getThreadSummary(lastSession, new Range<Long>(lastHour, now)); - if (summaries.size() != 0) { - for (ThreadSummary summary : summaries) { - model.addData(summary.getTimeStamp(), summary.getCurrentLiveThreads(), summary.getCurrentDaemonThreads()); + Range<Long> range = new Range<>(lastHour, now); + + boolean updateModel = false; + List<ThreadSession> sessions = collector.getThreadSessions(range); + for (ThreadSession session : sessions) { + List<ThreadSummary> summaries = collector.getThreadSummary(session.getSessionID(), range); + if (summaries.size() != 0) { + for (ThreadSummary summary : summaries) { + model.addData(summary.getTimeStamp(), summary.getCurrentLiveThreads(), summary.getCurrentDaemonThreads()); + } + updateModel = true; } + } + + if (updateModel) { view.updateLivingDaemonTimeline(model); } } + + @Override + public void run() { + updateLastSession(); + updateChart(); + } } }
--- a/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadCountControllerTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadCountControllerTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -45,6 +45,7 @@ import com.redhat.thermostat.thread.client.common.collector.ThreadCollector; import com.redhat.thermostat.thread.client.common.view.ThreadCountView; import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; import java.awt.Color; import java.util.ArrayList; @@ -60,6 +61,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -111,10 +113,17 @@ summaries.add(summary0); SessionID lastSession = mock(SessionID.class); - when(collector.getLastThreadSummarySession()).thenReturn(lastSession); + when(collector.getLastThreadSession()).thenReturn(lastSession); when(collector.getLatestThreadSummary(lastSession)).thenReturn(summary); - when(collector.getThreadSummary(any(SessionID.class), any(Range.class))).thenReturn(summaries); + + List<ThreadSession> sessions = new ArrayList<>(); + ThreadSession session0 = mock(ThreadSession.class); + SessionID id = new SessionID("0xcafe"); + when(session0.getSessionID()).thenReturn(id); + sessions.add(session0); + when(collector.getThreadSessions(any(Range.class))).thenReturn(sessions); + when(collector.getThreadSummary(eq(id), any(Range.class))).thenReturn(summaries); threadAction = captor.getValue(); threadAction.run();
--- a/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadCountView.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/client-swing/src/main/java/com/redhat/thermostat/thread/client/swing/impl/SwingThreadCountView.java Tue Nov 18 16:03:17 2014 +0100 @@ -46,6 +46,7 @@ import com.redhat.thermostat.client.swing.components.ChartPanel; import com.redhat.thermostat.thread.client.common.chart.LivingDaemonThreadDifferenceChart; import com.redhat.thermostat.thread.client.common.view.ThreadCountView; +import javax.swing.SwingWorker; public class SwingThreadCountView extends ThreadCountView implements SwingComponent { @@ -56,11 +57,26 @@ timelinePanel.addHierarchyListener(new ComponentVisibleListener() { @Override public void componentShown(Component component) { - SwingThreadCountView.this.notify(Action.VISIBLE); + SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { + @Override + protected Void doInBackground() throws Exception { + SwingThreadCountView.this.notify(Action.VISIBLE); + return null; + } + }; + worker.execute(); } + @Override public void componentHidden(Component component) { - SwingThreadCountView.this.notify(Action.HIDDEN); + SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { + @Override + protected Void doInBackground() throws Exception { + SwingThreadCountView.this.notify(Action.HIDDEN); + return null; + } + }; + worker.execute(); } }); }
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/ThreadDao.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/ThreadDao.java Tue Nov 18 16:03:17 2014 +0100 @@ -44,6 +44,7 @@ import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHarvestingStatus; import com.redhat.thermostat.thread.model.ThreadHeader; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import com.redhat.thermostat.thread.model.VmDeadLockData; @@ -133,7 +134,16 @@ void saveSummary(ThreadSummary summary); List<ThreadSummary> getSummary(VmRef ref, SessionID session, Range<Long> range, int limit); - List<SessionID> getAvailableThreadSummarySessions(VmRef ref, Range<Long> range, int limit); + + /** + * Returns a list of sessions registered by thread sampling. + */ + List<ThreadSession> getSessions(VmRef ref, Range<Long> range, int limit); + + /** + * Save the given session to the database. + */ + void saveSession(ThreadSession session); /** * Gets the total time interval for the entire data related to
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDAOCategoryRegistration.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDAOCategoryRegistration.java Tue Nov 18 16:03:17 2014 +0100 @@ -52,15 +52,17 @@ @Override public Set<String> getCategoryNames() { - Set<String> categories = new HashSet<>(5); + Set<String> categories = new HashSet<>(); + categories.add(ThreadDao.DEADLOCK_INFO.getName()); categories.add(ThreadDao.THREAD_HARVESTING_STATUS.getName()); categories.add(ThreadDao.THREAD_HEADER.getName()); categories.add(ThreadDao.THREAD_STATE.getName()); - categories.add(ThreadDaoCategories.THREAD_SUMMARY.getName()); categories.add(ThreadDao.THREAD_CONTENTION_SAMPLE.getName()); + + ThreadDaoCategories.register(categories); + return categories; } - }
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategories.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategories.java Tue Nov 18 16:03:17 2014 +0100 @@ -36,16 +36,25 @@ package com.redhat.thermostat.thread.dao.impl; +import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.storage.core.Category; import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; +import java.lang.reflect.Field; import java.util.Arrays; +import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; /** * */ public class ThreadDaoCategories { + private static final Logger logger = LoggingUtils.getLogger(ThreadDaoCategories.class); + public static final Category<ThreadSummary> THREAD_SUMMARY = new Category<>("vm-thread-summary", ThreadSummary.class, Arrays.<Key<?>>asList( @@ -56,4 +65,48 @@ ThreadDaoKeys.LIVE_THREADS_KEY, ThreadDaoKeys.DAEMON_THREADS_KEY), Arrays.<Key<?>>asList(Key.TIMESTAMP, ThreadDaoKeys.SESSION)); + + + public static final Category<ThreadSession> THREAD_SESSION = + new Category<>("vm-thread-session", ThreadSession.class, + Arrays.<Key<?>>asList( + Key.AGENT_ID, + Key.VM_ID, + ThreadDaoKeys.SESSION, + Key.TIMESTAMP), + Arrays.<Key<?>>asList(Key.TIMESTAMP, Key.VM_ID)); + + public static void register(Collection<String> collection) { + for (Field field : ThreadDaoCategories.class.getDeclaredFields()) { + field.setAccessible(true); + if (field.getType().isAssignableFrom(Category.class)) { + Category category = null; + try { + category = (Category) field.get(null); + collection.add(category.getName()); + + } catch (IllegalAccessException e) { + logger.log(Level.SEVERE, "Error while registering categories...", e); + throw new RuntimeException(e); + } + } + } + } + + public static void register(Storage storage) { + for (Field field : ThreadDaoCategories.class.getDeclaredFields()) { + field.setAccessible(true); + if (field.getType().isAssignableFrom(Category.class)) { + Category category = null; + try { + category = (Category) field.get(null); + storage.registerCategory(category); + + } catch (IllegalAccessException e) { + logger.log(Level.SEVERE, "Error while registering categories...", e); + throw new RuntimeException(e); + } + } + } + } }
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java Tue Nov 18 16:03:17 2014 +0100 @@ -51,10 +51,13 @@ import com.redhat.thermostat.thread.dao.ThreadDao; import com.redhat.thermostat.thread.dao.impl.descriptor.SummaryDescriptor; import com.redhat.thermostat.thread.dao.impl.descriptor.SummaryDescriptorBuilder; +import com.redhat.thermostat.thread.dao.impl.descriptor.ThreadSessionDescriptor; +import com.redhat.thermostat.thread.dao.impl.descriptor.ThreadSessionDescriptorBuilder; import com.redhat.thermostat.thread.model.SessionID; import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHarvestingStatus; import com.redhat.thermostat.thread.model.ThreadHeader; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import com.redhat.thermostat.thread.model.VmDeadLockData; @@ -69,6 +72,7 @@ private static final Logger logger = LoggingUtils.getLogger(ThreadDaoImpl.class); static final SummaryDescriptor SUMMARY = new SummaryDescriptorBuilder().build(); + static final ThreadSessionDescriptor SESSIONS = new ThreadSessionDescriptorBuilder().build(); // Queries @@ -194,7 +198,7 @@ public ThreadDaoImpl(Storage storage) { this.storage = storage; - storage.registerCategory(SUMMARY.getCategory()); + ThreadDaoCategories.register(storage); storage.registerCategory(THREAD_HARVESTING_STATUS); storage.registerCategory(THREAD_HEADER); @@ -476,22 +480,29 @@ } @Override - public List<SessionID> getAvailableThreadSummarySessions(VmRef ref, Range<Long> range, int limit) { - List<SessionID> result = new ArrayList<>(); + public List<ThreadSession> getSessions(VmRef ref, Range<Long> range, int limit) { + List<ThreadSession> result = new ArrayList<>(); - Cursor<ThreadSummary> cursor = null; + Cursor<ThreadSession> cursor = null; try { - cursor = SUMMARY.queryGet(ref, range, limit, storage); + cursor = SESSIONS.queryGet(ref, range, limit, storage); while (cursor.hasNext()) { - ThreadSummary summary = cursor.next(); - result.add(new SessionID(summary.getSession())); + ThreadSession summary = cursor.next(); + result.add(summary); } } catch (Exception ignore) { ignore.printStackTrace(); } return result; } + public void saveSession(ThreadSession session) { + try { + SESSIONS.statementAdd(session, storage); + + } catch (Exception ignore) { ignore.printStackTrace(); } + } + @Override public void saveHarvestingStatus(ThreadHarvestingStatus status) { StatementDescriptor<ThreadHarvestingStatus> desc = new StatementDescriptor<>(THREAD_HARVESTING_STATUS, DESC_ADD_THREAD_HARVESTING_STATUS);
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImplStatementDescriptorRegistration.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImplStatementDescriptorRegistration.java Tue Nov 18 16:03:17 2014 +0100 @@ -59,7 +59,9 @@ descs.add(ThreadDaoImpl.QUERY_LATEST_DEADLOCK_INFO); descs.add(ThreadDaoImpl.QUERY_LATEST_HARVESTING_STATUS); + // TODO: this needs to go in an helper class descs.addAll(ThreadDaoImpl.SUMMARY.describe()); + descs.addAll(ThreadDaoImpl.SESSIONS.describe()); descs.add(ThreadDaoImpl.DESC_ADD_THREAD_DEADLOCK_DATA); descs.add(ThreadDaoImpl.DESC_ADD_THREAD_HARVESTING_STATUS);
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptor.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptor.java Tue Nov 18 16:03:17 2014 +0100 @@ -62,14 +62,11 @@ protected String rangeDesc; protected String addDesc; - protected String sessionsDesc; - public Set<String> describe() { Set<String> description = new HashSet<>(); description.add(rangeDesc); description.add(addDesc); - description.add(sessionsDesc); return description; } @@ -106,40 +103,9 @@ } } - public Cursor<ThreadSummary> queryGet(VmRef ref, Range<Long> range, - int limit, Storage storage) - throws DescriptorParsingException, StatementExecutionException - { - // "QUERY vm-thread-summary WHERE 'vmId' = ?s , 'timeStamp' >= ?l - // AND 'timeStamp' <= ?l SORT 'timeStamp' DSC LIMIT ?i"; - - StatementDescriptor<ThreadSummary> desc = null; - try { - desc = new StatementDescriptor<>(getCategory(), sessionsDesc); - PreparedStatement<ThreadSummary> prepared = - storage.prepareStatement(desc); - - int i = 0; - prepared.setString(i++, ref.getVmId()); - - prepared.setLong(i++, range.getMin()); - prepared.setLong(i++, range.getMax()); - - prepared.setInt(i++, limit); - - return prepared.executeQuery(); - - } catch (Exception e) { - logger.log(Level.WARNING, - "exception executing statement: " + desc, e); - throw e; - } - } - public void statementAdd(ThreadSummary summary, Storage storage) throws DescriptorParsingException, StatementExecutionException { - // "ADD vm-thread-summary SET 'agentId' = ?s , 'vmId' = ?s , // 'session' = ?s , 'currentLiveThreads' = ?l , // 'currentDaemonThreads' = ?l , 'timeStamp' = ?l";
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorBuilder.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorBuilder.java Tue Nov 18 16:03:17 2014 +0100 @@ -56,17 +56,6 @@ SummaryDescriptor descriptor = new SummaryDescriptor(); descriptor.setCategory(category); - StringBuilder querySessions = new StringBuilder(); - querySessions.append("QUERY").append(" "); - querySessions.append(document).append(" "); - querySessions.append("WHERE").append(" '"); - querySessions.append(Key.VM_ID.getName()).append("' = ?s AND '"); - querySessions.append(Key.TIMESTAMP.getName()).append("' >= ?l AND '"); - querySessions.append(Key.TIMESTAMP.getName()).append("' <= ?l SORT '"); - querySessions.append(Key.TIMESTAMP.getName()).append("' DSC LIMIT ?i"); - - descriptor.sessionsDesc = querySessions.toString(); - StringBuilder summarySinceBuilder = new StringBuilder(); summarySinceBuilder.append("QUERY").append(" "); summarySinceBuilder.append(document).append(" ");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/ThreadSessionDescriptor.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,129 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl.descriptor; + +import com.redhat.thermostat.common.model.Range; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.storage.core.Cursor; +import com.redhat.thermostat.storage.core.DescriptorParsingException; +import com.redhat.thermostat.storage.core.PreparedStatement; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.core.StatementExecutionException; +import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.thread.model.ThreadSession; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + */ +public class ThreadSessionDescriptor extends Descriptor<ThreadSession> { + + private static final Logger logger = LoggingUtils.getLogger(ThreadSessionDescriptor.class); + + protected String querySessions; + protected String statementAdd; + + @Override + public Set<String> describe() { + Set<String> description = new HashSet<>(); + description.add(querySessions); + return description; + } + + public Cursor<ThreadSession> queryGet(VmRef ref, Range<Long> range, + int limit, Storage storage) + throws DescriptorParsingException, StatementExecutionException + { + // "QUERY vm-thread-session WHERE 'vmId' = ?s , 'timeStamp' >= ?l + // AND 'timeStamp' <= ?l SORT 'timeStamp' DSC LIMIT ?i"; + + StatementDescriptor<ThreadSession> desc = null; + try { + desc = new StatementDescriptor<>(getCategory(), querySessions); + PreparedStatement<ThreadSession> prepared = + storage.prepareStatement(desc); + + int i = 0; + prepared.setString(i++, ref.getVmId()); + + prepared.setLong(i++, range.getMin()); + prepared.setLong(i++, range.getMax()); + + prepared.setInt(i++, limit); + + return prepared.executeQuery(); + + } catch (Exception e) { + logger.log(Level.WARNING, + "exception executing statement: " + desc, e); + throw e; + } + } + + public void statementAdd(ThreadSession session, Storage storage) + throws DescriptorParsingException, StatementExecutionException + { + // "ADD vm-thread-session SET 'agentId' = ?s , 'vmId' = ?s , + // 'session' = ?s , 'timeStamp' = ?l" + + StatementDescriptor<ThreadSession> desc = + new StatementDescriptor<>(getCategory(), statementAdd); + + PreparedStatement<ThreadSession> prepared; + try { + + prepared = storage.prepareStatement(desc); + + int i = 0; + prepared.setString(i++, session.getAgentId()); + prepared.setString(i++, session.getVmId()); + prepared.setString(i++, session.getSession()); + + prepared.setLong(i++, session.getTimeStamp()); + + prepared.execute(); + + } catch (Exception e) { + logger.log(Level.WARNING, "exception executing statement: " + desc, e); + throw e; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/descriptor/ThreadSessionDescriptorBuilder.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,83 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl.descriptor; + +import com.redhat.thermostat.storage.core.Key; +import com.redhat.thermostat.thread.dao.impl.ThreadDaoCategories; +import com.redhat.thermostat.thread.dao.impl.ThreadDaoKeys; +import com.redhat.thermostat.thread.model.ThreadSession; + +/** + * + */ +public class ThreadSessionDescriptorBuilder extends DescriptorBuilder<ThreadSession> { + + public ThreadSessionDescriptorBuilder() { + super(ThreadDaoCategories.THREAD_SESSION); + } + + @Override + public ThreadSessionDescriptor build() { + + ThreadSessionDescriptor descriptor = new ThreadSessionDescriptor(); + descriptor.setCategory(category); + + StringBuilder querySessions = new StringBuilder(); + querySessions.append("QUERY").append(" "); + querySessions.append(document).append(" "); + querySessions.append("WHERE").append(" '"); + querySessions.append(Key.VM_ID.getName()).append("' = ?s AND '"); + querySessions.append(Key.TIMESTAMP.getName()).append("' >= ?l AND '"); + querySessions.append(Key.TIMESTAMP.getName()).append("' <= ?l SORT '"); + querySessions.append(Key.TIMESTAMP.getName()).append("' DSC LIMIT ?i"); + + descriptor.querySessions = querySessions.toString(); + + StringBuilder statementAdd = new StringBuilder(); + statementAdd.append("ADD").append(" "); + statementAdd.append(document).append(" "); + statementAdd.append("SET").append(" '"); + statementAdd.append(Key.AGENT_ID.getName()).append("' = ?s , '"); + statementAdd.append(Key.VM_ID.getName()).append("' = ?s , '"); + statementAdd.append(ThreadDaoKeys.SESSION.getName()).append("' = ?s , '"); + statementAdd.append(Key.TIMESTAMP.getName()).append("' = ?l"); + + descriptor.statementAdd = statementAdd.toString(); + + return descriptor; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSession.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,103 @@ +/* + * Copyright 2012-2014 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.thread.model; + +import com.redhat.thermostat.storage.core.Entity; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.BasePojo; +import com.redhat.thermostat.storage.model.TimeStampedPojo; + +/** + * + */ +@Entity +public class ThreadSession extends BasePojo implements TimeStampedPojo { + + private String vmId; + private long timestamp; + private String session; + private SessionID sessionID; + + public ThreadSession() { + this(null); + } + + public ThreadSession(String writerId) { + super(writerId); + } + + @Persist + public void setVmId(String vmId) { + this.vmId = vmId; + } + + @Persist + public String getVmId() { + return vmId; + } + + @Persist + public long getTimeStamp() { + return timestamp; + } + + @Persist + public void setTimeStamp(long timestamp) { + this.timestamp = timestamp; + } + + @Override + public String toString() { + return "[timestamp: " + timestamp + ", session: " + + ", vm: " + vmId + "]"; + } + + @Persist + public void setSession(String session) { + this.session = session; + sessionID = new SessionID(session); + } + + @Persist + public String getSession() { + return session; + } + + public SessionID getSessionID() { + return sessionID; + } +}
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSummary.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/model/ThreadSummary.java Tue Nov 18 16:03:17 2014 +0100 @@ -52,6 +52,7 @@ private long timestamp; private String session; + private SessionID sessionID; public ThreadSummary() { this(null); @@ -110,11 +111,16 @@ @Persist public void setSession(String session) { this.session = session; + sessionID = new SessionID(session); } @Persist public String getSession() { return session; } + + public SessionID getSessionID() { + return sessionID; + } }
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDAOCategoryRegistrationTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDAOCategoryRegistrationTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -39,13 +39,12 @@ import com.redhat.thermostat.storage.core.auth.CategoryRegistration; import com.redhat.thermostat.storage.internal.dao.DAOImplCategoryRegistration; import com.redhat.thermostat.thread.dao.ThreadDao; -import org.junit.Test; - import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.ServiceLoader; import java.util.Set; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -54,18 +53,28 @@ public class ThreadDAOCategoryRegistrationTest { + private static final int EXPECTED_CATEGORIES = 7; + @Test public void registersAllCategories() { ThreadDAOCategoryRegistration reg = new ThreadDAOCategoryRegistration(); Set<String> categories = reg.getCategoryNames(); - assertEquals(6, categories.size()); + assertEquals(EXPECTED_CATEGORIES, categories.size()); + assertFalse("null descriptor not allowed", categories.contains(null)); + assertTrue(categories.contains(ThreadDao.DEADLOCK_INFO.getName())); assertTrue(categories.contains(ThreadDao.THREAD_HEADER.getName())); assertTrue(categories.contains(ThreadDao.THREAD_STATE.getName())); - assertTrue(categories.contains(ThreadDaoCategories.THREAD_SUMMARY.getName())); assertTrue(categories.contains(ThreadDao.THREAD_HARVESTING_STATUS.getName())); assertTrue(categories.contains(ThreadDao.THREAD_CONTENTION_SAMPLE.getName())); + + Set<String> sourceCategories = new HashSet<>(); + ThreadDaoCategories.register(sourceCategories); + + for (String category : sourceCategories) { + assertTrue(categories.contains(category)); + } } /* @@ -92,7 +101,7 @@ // storage-core + this module assertEquals(2, registrations.size()); assertNotNull(threadCatReg); - assertEquals(6, threadCatReg.getCategoryNames().size()); + assertEquals(EXPECTED_CATEGORIES, threadCatReg.getCategoryNames().size()); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoCategoriesTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl; + +import com.redhat.thermostat.storage.core.Storage; +import java.util.HashSet; +import java.util.Set; +import junit.framework.TestCase; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ThreadDaoCategoriesTest extends TestCase { + + public void testRegister() throws Exception { + Set<String> set = new HashSet<>(); + ThreadDaoCategories.register(set); + + // no real need to check all, but adding more doesn't hurt + assertTrue(set.contains(ThreadDaoCategories.THREAD_SESSION.getName())); + assertTrue(set.contains(ThreadDaoCategories.THREAD_SUMMARY.getName())); + } + + public void testRegisterInStorage() throws Exception { + Storage storage = mock(Storage.class); + ThreadDaoCategories.register(storage); + + verify(storage).registerCategory(ThreadDaoCategories.THREAD_SESSION); + verify(storage).registerCategory(ThreadDaoCategories.THREAD_SUMMARY); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/DescriptorTester.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl.descriptor; + +import com.redhat.thermostat.storage.core.Category; +import com.redhat.thermostat.storage.core.StatementDescriptor; +import com.redhat.thermostat.storage.model.Pojo; +import com.redhat.thermostat.storage.testutils.StatementDescriptorTester; + +/** + * + */ +public class DescriptorTester { + + public static <C extends Pojo> void testStatement(Category<C> category, String statement) throws Exception { + + StatementDescriptorTester<C> tester = new StatementDescriptorTester<>(); + StatementDescriptor<C> desc = new StatementDescriptor<>(category, statement); + tester.testParseBasic(desc); + tester.testParseSemantic(desc); + } +}
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorBuilderTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorBuilderTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -36,10 +36,7 @@ package com.redhat.thermostat.thread.dao.impl.descriptor; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.testutils.StatementDescriptorTester; import com.redhat.thermostat.thread.dao.impl.ThreadDaoCategories; -import com.redhat.thermostat.thread.model.ThreadSummary; import junit.framework.TestCase; import org.junit.Test; @@ -47,14 +44,12 @@ @Test public void testBuild() { - SummaryDescriptor summary = new SummaryDescriptorBuilder().build(); assertNotNull(summary); } @Test public void testCategory() { - SummaryDescriptor summary = new SummaryDescriptorBuilder().build(); assertEquals(ThreadDaoCategories.THREAD_SUMMARY, summary.getCategory()); } @@ -62,29 +57,13 @@ @Test public void testAddDesc() throws Exception { SummaryDescriptor summary = new SummaryDescriptorBuilder().build(); - testStatement(summary.addDesc); + DescriptorTester.testStatement(summary.getCategory(), summary.addDesc); } @Test public void testRangeDesc() throws Exception { SummaryDescriptor summary = new SummaryDescriptorBuilder().build(); - testStatement(summary.rangeDesc); - } - - @Test - public void testSessionDesc() throws Exception { - - SummaryDescriptor summary = new SummaryDescriptorBuilder().build(); - testStatement(summary.sessionsDesc); - } - - private void testStatement(String statement) throws Exception { - - StatementDescriptorTester<ThreadSummary> tester = new StatementDescriptorTester<>(); - StatementDescriptor<ThreadSummary> desc = - new StatementDescriptor<>(ThreadDaoCategories.THREAD_SUMMARY, statement); - tester.testParseBasic(desc); - tester.testParseSemantic(desc); + DescriptorTester.testStatement(summary.getCategory(), summary.rangeDesc); } }
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/SummaryDescriptorTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -54,14 +54,6 @@ assertEquals(expected, descriptor.rangeDesc); } - public void testGetSessionsRange() throws Exception { - - String expected = - "QUERY vm-thread-summary WHERE 'vmId' = ?s AND 'timeStamp' >= ?l AND 'timeStamp' <= ?l SORT 'timeStamp' DSC LIMIT ?i"; - assertEquals(expected, descriptor.sessionsDesc); - - } - public void testAddSummary() throws Exception { String expected =
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/ThreadSessionDescriptorBuilderTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl.descriptor; + +import com.redhat.thermostat.thread.dao.impl.ThreadDaoCategories; +import junit.framework.TestCase; +import org.junit.Test; + +public class ThreadSessionDescriptorBuilderTest extends TestCase { + + @Test + public void testBuild() { + ThreadSessionDescriptor descriptor = new ThreadSessionDescriptorBuilder().build(); + assertNotNull(descriptor); + } + + @Test + public void testCategory() { + ThreadSessionDescriptor descriptor = new ThreadSessionDescriptorBuilder().build(); + assertEquals(ThreadDaoCategories.THREAD_SESSION, descriptor.getCategory()); + } + + @Test + public void testQuerySessions() throws Exception { + ThreadSessionDescriptor descriptor = new ThreadSessionDescriptorBuilder().build(); + DescriptorTester.testStatement(descriptor.getCategory(), descriptor.querySessions); + } + + @Test + public void testStatementAdd() throws Exception { + ThreadSessionDescriptor descriptor = new ThreadSessionDescriptorBuilder().build(); + DescriptorTester.testStatement(descriptor.getCategory(), descriptor.statementAdd); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/descriptor/ThreadSessionDescriptorTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,62 @@ +/* + * Copyright 2012-2014 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.thread.dao.impl.descriptor; + +import junit.framework.TestCase; +import org.junit.Before; + +public class ThreadSessionDescriptorTest extends TestCase { + + private ThreadSessionDescriptor descriptor; + + @Before + public void setUp() { + descriptor = new ThreadSessionDescriptorBuilder().build(); + } + + public void testQuerySessions() throws Exception { + String expected = + "QUERY vm-thread-session WHERE 'vmId' = ?s AND 'timeStamp' >= ?l AND 'timeStamp' <= ?l SORT 'timeStamp' DSC LIMIT ?i"; + assertEquals(expected, descriptor.querySessions); + } + + public void testStatementAdd() throws Exception { + String expected = + "ADD vm-thread-session SET 'agentId' = ?s , 'vmId' = ?s , 'session' = ?s , 'timeStamp' = ?l"; + assertEquals(expected, descriptor.statementAdd); + } +}
--- a/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/Harvester.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/Harvester.java Tue Nov 18 16:03:17 2014 +0100 @@ -42,7 +42,7 @@ import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.concurrent.ScheduledExecutorService; @@ -198,7 +198,7 @@ private class HarvesterAction implements Runnable { private boolean newSession; - private SessionID sessionID; + private ThreadSession session; private HarvesterAction(boolean newSession) { this.newSession = newSession; @@ -230,11 +230,13 @@ } catch (UnsupportedOperationException ignore) {} if (newSession) { - sessionID = new SessionID(); + session = harvesterHelper.createSession(); + harvesterHelper.saveSession(session); + newSession = false; } - harvesterHelper.collectAndSaveThreadData(sessionID, collectorBean); + harvesterHelper.collectAndSaveThreadData(session, collectorBean); } else { logger.log(Level.WARNING, "ThreadMXBean is null, is JMX available?");
--- a/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/HarvesterHelper.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/HarvesterHelper.java Tue Nov 18 16:03:17 2014 +0100 @@ -42,6 +42,7 @@ import com.redhat.thermostat.thread.model.ThreadContentionSample; import com.redhat.thermostat.thread.model.ThreadHeader; import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import java.lang.management.ThreadInfo; @@ -58,6 +59,7 @@ private ThreadHeaderHelper headerHelper; private ThreadStateHelper stateHelper; private ThreadContentionHelper contentionHelper; + private ThreadSessionHelper sessionHelper; HarvesterHelper(ThreadDao threadDao, Clock clock, String vmId, WriterID writerId) { @@ -65,14 +67,16 @@ new ThreadSummaryHelper(threadDao, writerId, vmId), new ThreadHeaderHelper(threadDao, writerId, vmId), new ThreadStateHelper(threadDao, writerId, vmId), - new ThreadContentionHelper(threadDao, writerId, vmId)); + new ThreadContentionHelper(threadDao, writerId, vmId), + new ThreadSessionHelper(threadDao, writerId, vmId, clock)); } HarvesterHelper(ThreadDao threadDao, Clock clock, String vmId, ThreadSummaryHelper summaryHelper, ThreadHeaderHelper headerHelper, ThreadStateHelper stateHelper, - ThreadContentionHelper contentionHelper) + ThreadContentionHelper contentionHelper, + ThreadSessionHelper sessionHelper) { this.vmId = vmId; this.clock = clock; @@ -82,16 +86,18 @@ this.stateHelper = stateHelper; this.contentionHelper = contentionHelper; + this.sessionHelper = sessionHelper; } - synchronized void collectAndSaveThreadData(SessionID session, + synchronized void collectAndSaveThreadData(ThreadSession session, ThreadMXBean collectorBean) { long timestamp = clock.getRealTimeMillis(); - ThreadSummary summary = summaryHelper.createThreadSummary(collectorBean, - timestamp, - session); + ThreadSummary summary = + summaryHelper.createThreadSummary(collectorBean, + timestamp, + session); summaryHelper.saveSummary(summary); // this two can't be null, but the check is there to allow for @@ -128,4 +134,12 @@ contentionHelper.saveContentionSample(contentionSample); } } + + public ThreadSession createSession() { + return sessionHelper.createSession(); + } + + public void saveSession(ThreadSession sessionID) { + sessionHelper.saveSession(sessionID); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/ThreadSessionHelper.java Tue Nov 18 16:03:17 2014 +0100 @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2014 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.thread.harvester; + +import com.redhat.thermostat.common.Clock; +import com.redhat.thermostat.storage.core.WriterID; +import com.redhat.thermostat.thread.dao.ThreadDao; +import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; + +/** + * + */ +class ThreadSessionHelper { + private final ThreadDao threadDao; + private final WriterID writerId; + private final String vmId; + private final Clock clock; + + public ThreadSessionHelper(ThreadDao threadDao, WriterID writerId, String vmId, Clock clock) { + + this.threadDao = threadDao; + this.writerId = writerId; + this.vmId = vmId; + this.clock = clock; + } + + ThreadSession createSession() { + ThreadSession session = new ThreadSession(); + + session.setSession(new SessionID().getId()); + session.setTimeStamp(clock.getRealTimeMillis()); + session.setVmId(vmId); + session.setAgentId(writerId.getWriterID()); + + return session; + } + + void saveSession(ThreadSession session) { + threadDao.saveSession(session); + } +}
--- a/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/ThreadSummaryHelper.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/ThreadSummaryHelper.java Tue Nov 18 16:03:17 2014 +0100 @@ -38,9 +38,8 @@ import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; - import java.lang.management.ThreadMXBean; /* @@ -57,7 +56,7 @@ this.threadDao = threadDao; } - ThreadSummary createThreadSummary(ThreadMXBean collectorBean, long timestamp, SessionID session) { + ThreadSummary createThreadSummary(ThreadMXBean collectorBean, long timestamp, ThreadSession session) { String wId = writerId.getWriterID(); @@ -68,7 +67,7 @@ summary.setTimeStamp(timestamp); summary.setVmId(vmId); - summary.setSession(session.getId()); + summary.setSession(session.getSession()); return summary; }
--- a/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/HarvesterHelperTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/HarvesterHelperTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -38,8 +38,8 @@ import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.SessionID; import com.redhat.thermostat.thread.model.ThreadHeader; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadState; import com.redhat.thermostat.thread.model.ThreadSummary; import java.lang.management.ThreadInfo; @@ -69,9 +69,10 @@ private ThreadHeaderHelper headerHelper; private ThreadStateHelper stateHelper; private ThreadContentionHelper contentionHelper; + private ThreadSessionHelper threadSessionHelper; private ThreadMXBean collectorBean; - private SessionID sessionID; + private ThreadSession session; @Before public void setUp() { summaryHelper = mock(ThreadSummaryHelper.class); @@ -88,7 +89,10 @@ vmId = "42"; - sessionID = new SessionID("0xcafe"); + session = new ThreadSession(); + session.setSession("0xcafe"); + + threadSessionHelper = mock(ThreadSessionHelper.class); } @Test @@ -97,20 +101,21 @@ ThreadSummary summary = mock(ThreadSummary.class); when(summaryHelper.createThreadSummary(collectorBean, DEFAULT_TIMESTAMP, - sessionID)). + session)). thenReturn(summary); HarvesterHelper harvester = new HarvesterHelper(threadDao, clock, vmId, summaryHelper, headerHelper, stateHelper, - contentionHelper); - harvester.collectAndSaveThreadData(sessionID, collectorBean); + contentionHelper, + threadSessionHelper); + harvester.collectAndSaveThreadData(session, collectorBean); verify(clock).getRealTimeMillis(); verify(summaryHelper).createThreadSummary(collectorBean, DEFAULT_TIMESTAMP, - sessionID); + session); verify(summaryHelper).saveSummary(summary); } @@ -124,8 +129,9 @@ summaryHelper, headerHelper, stateHelper, - contentionHelper); - harvester.collectAndSaveThreadData(sessionID, collectorBean); + contentionHelper, + threadSessionHelper); + harvester.collectAndSaveThreadData(session, collectorBean); verify(collectorBean).getAllThreadIds(); verify(collectorBean).getThreadInfo(ids, true, true); @@ -167,8 +173,9 @@ summaryHelper, headerHelper, stateHelper, - contentionHelper); - harvester.collectAndSaveThreadData(sessionID, collectorBean); + contentionHelper, + threadSessionHelper); + harvester.collectAndSaveThreadData(session, collectorBean); verify(headerHelper, times(2)).checkAndSaveThreadHeader(header1); verify(headerHelper).checkAndSaveThreadHeader(header2);
--- a/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/HarvesterTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/HarvesterTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -40,16 +40,15 @@ import com.redhat.thermostat.agent.utils.management.MXBeanConnectionPool; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.SessionID; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; - +import com.redhat.thermostat.thread.model.ThreadSession; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; @@ -241,7 +240,7 @@ harvesterRunnable.run(); - verify(helper).collectAndSaveThreadData(any(SessionID.class), eq(sunBean)); + verify(helper).collectAndSaveThreadData(any(ThreadSession.class), eq(sunBean)); } @Test @@ -277,7 +276,7 @@ harvesterRunnable.run(); - verify(helper).collectAndSaveThreadData(any(SessionID.class), eq(mxBean)); + verify(helper).collectAndSaveThreadData(any(ThreadSession.class), eq(mxBean)); } @Test
--- a/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/ThreadSummaryHelperTest.java Tue Nov 18 16:02:57 2014 +0100 +++ b/thread/harvester/src/test/java/com/redhat/thermostat/thread/harvester/ThreadSummaryHelperTest.java Tue Nov 18 16:03:17 2014 +0100 @@ -38,7 +38,7 @@ import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.thread.dao.ThreadDao; -import com.redhat.thermostat.thread.model.SessionID; +import com.redhat.thermostat.thread.model.ThreadSession; import com.redhat.thermostat.thread.model.ThreadSummary; import java.lang.management.ThreadMXBean; import org.junit.Before; @@ -64,7 +64,7 @@ private ThreadMXBean collectorBean; - private SessionID sessionID; + private ThreadSession session; @Before public void setUp() throws Exception { @@ -78,7 +78,8 @@ when(collectorBean.getThreadCount()).thenReturn(DEFAULT_THREAD_COUNT); when(collectorBean.getDaemonThreadCount()).thenReturn(DEFAULT_DAEMON_THREAD_COUNT); - sessionID = new SessionID("0xcafe"); + session = new ThreadSession(); + session.setSession("0xcafe"); } @Test @@ -88,7 +89,7 @@ long timestamp = -1l; - ThreadSummary summary = helper.createThreadSummary(collectorBean, timestamp, sessionID); + ThreadSummary summary = helper.createThreadSummary(collectorBean, timestamp, session); assertNotNull(summary); @@ -99,7 +100,7 @@ assertEquals(summary.getAgentId(), DEFAULT_W_ID); assertEquals(summary.getVmId(), vmId); - assertEquals(summary.getSession(), sessionID.getId()); + assertEquals(summary.getSession(), session.getSession()); assertEquals(summary.getCurrentLiveThreads(), DEFAULT_THREAD_COUNT); assertEquals(summary.getCurrentDaemonThreads(), DEFAULT_DAEMON_THREAD_COUNT);