public FeedbackChannelImpl(AgentContext agentContext, String name) throws IOException { m_agentContext = agentContext; m_name = name; m_storeManager = new FeedbackStoreManager(agentContext, name); }
@Override public void write(int type, Map<String, String> properties) throws IOException { m_storeManager.write(type, properties); }
public void stop() throws IOException { m_storeManager.close(); }
/** * Return the feedbackstore for the specified storeId. If there are multiple files for this storeId the last one is * returned * * @param the * storeId * @return the feedbackstore for that storeID */ private FeedbackStore getLastStore(long storeID) throws IOException { FeedbackStore currentStore = getCurrentStore(); if (currentStore != null && currentStore.getId() == storeID) { return currentStore; } int lastFileNo = getLastLogfileNumber(storeID); return createStore(storeID, lastFileNo); }
/** * Give the highest eventId that is is present is the specified store * * @param the * storeId * @return the highest event present in the store, >= 0, or <tt>-1</tt> if this manager is already closed. * @throws IOException * in case of I/O problems accessing the store(s). */ public long getHighestEventID(long storeID) throws IOException { if (m_closed.get()) { return -1L; } FeedbackStore store = getLastStore(storeID); try { return store.getLastEventID(); } finally { closeIfNeeded(store); } }
@Test public void testReadWriteLogEvents() throws Exception { FeedbackStoreManager feedbackStoreManager = new FeedbackStoreManager(m_agentContext, "test"); long storeID = getStoreID(feedbackStoreManager); assertEquals(feedbackStoreManager.getHighestEventID(storeID), 0); feedbackStoreManager.write(1, new HashMap<String, String>()); assertEquals(feedbackStoreManager.getHighestEventID(storeID), 1); assertEquals(feedbackStoreManager.getEvents(storeID, 1, 1).size(), 1); }
FeedbackStore currentStore = getCurrentStore(); if (isMaximumStoreSizeReached(currentStore)) { int newFileNo = getLastLogfileNumber(storeID) + 1; currentStore = setStore(createStore(storeID, newFileNo)); cleanupOldStoreFiles(); Event result = new Event(getTargetID(), storeID, nextEventId, System.currentTimeMillis(), type, properties); handleException(currentStore, ex);
@Test public void testExceptionHandling() throws Exception { FeedbackStoreManager feedbackStoreManager = new FeedbackStoreManager(m_agentContext, "test"); feedbackStoreManager.forceCreateNewStore(); SortedSet<Long> allFeedbackStoreIDs = feedbackStoreManager.getAllFeedbackStoreIDs(); assertEquals(allFeedbackStoreIDs.size(), 2); }
try { if (m_storeFileIdx.isEmpty()) { store = newFeedbackStore(); Integer fileNo = m_storeFileIdx.get(lastStoreId).last(); store = createStore(lastStoreId, fileNo); setStore(store); handleException(store, ex);
private void synchronizeStore(long storeID, InputStream queryInput, Writer sendWriter) throws IOException { long highestLocal = m_storeManager.getHighestEventID(storeID); if (highestLocal <= 0) { List<Event> events = m_storeManager.getEvents(storeID, lowest, highestLocal > highest ? highest : highestLocal); if (events == null) {
FeedbackStore[] stores = getAllStores(storeID); try { List<Record> records = new ArrayList<>(); handleException(store, ex); closeIfNeeded(stores);
private long getStoreID(FeedbackStoreManager feedbackStoreManager) throws Exception { SortedSet<Long> allFeedbackStoreIDs = feedbackStoreManager.getAllFeedbackStoreIDs(); assertEquals(allFeedbackStoreIDs.size(), 1); return allFeedbackStoreIDs.first(); }
/** * Return all feedbackstores for a single storeId. * * @param storeId * the storeId * @return a list of all feedbackstores for this storeId */ private FeedbackStore[] getAllStores(long storeId) throws IOException { List<FeedbackStore> stores = new ArrayList<>(); SortedSet<Integer> storeFileNos = m_storeFileIdx.get(storeId); FeedbackStore currentStore = getCurrentStore(); if (currentStore.getId() == storeId) { // The last one is the current store... storeFileNos = storeFileNos.headSet(storeFileNos.last()); } for (Integer fileNo : storeFileNos) { stores.add(createStore(storeId, fileNo)); } if (currentStore.getId() == storeId) { stores.add(currentStore); } return stores.toArray(new FeedbackStore[stores.size()]); }
@Test public void testReadFromOldStore() throws Exception { FeedbackStoreManager feedbackStoreManager = new FeedbackStoreManager(m_agentContext, "test"); long storeID = getStoreID(feedbackStoreManager); assertEquals(feedbackStoreManager.getHighestEventID(storeID), 0); feedbackStoreManager.write(1, new HashMap<String, String>()); assertEquals(feedbackStoreManager.getHighestEventID(storeID), 1); assertEquals(feedbackStoreManager.getEvents(storeID, 0, 1).size(), 1); feedbackStoreManager.forceCreateNewStore(); assertEquals(feedbackStoreManager.getEvents(storeID, 0, 1).size(), 1); }
SortedSet<Long> storeIDs = m_storeManager.getAllFeedbackStoreIDs(); for (Long storeID : storeIDs) { URL queryURL = new URL(serverURL, m_name + "/" + COMMAND_QUERY + "?" + PARAMETER_TARGETID + "=" + identification + "&" + PARAMETER_LOGID + "=" + storeID);
@Test public void testLogfileRotation() throws Exception { int maxSize = 100 * 1024; FeedbackStoreManager feedbackStoreManager = new FeedbackStoreManager(m_agentContext, "test", maxSize, maxSize / 5); long storeID = getStoreID(feedbackStoreManager); int recordCount = 1000; assertEquals(feedbackStoreManager.getHighestEventID(storeID), 0); // absolutely exceed the set filesize for this store for (int i = 0; i < recordCount; i++) { HashMap<String, String> eventProps = new HashMap<>(); eventProps.put("key", "value" + i); feedbackStoreManager.write(i, eventProps); } File[] logFiles = getLogFiles(); assertTrue(logFiles.length > 1); // take the last 1000 events... List<Event> events = feedbackStoreManager.getEvents(storeID, 1, 1000); assertEquals(events.size(), 1000); long logFileSize = 0; for (File file : logFiles) { logFileSize += file.length(); } assertTrue(logFileSize < maxSize); }
@Test public void testEmptyRepository() throws Exception { FeedbackStoreManager feedbackStoreManager = new FeedbackStoreManager(m_agentContext, "test"); assertNotNull(getStoreID(feedbackStoreManager)); }