001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.storagemanager.logging;
012:
013: import com.versant.core.storagemanager.StorageManagerFactory;
014: import com.versant.core.storagemanager.StorageManager;
015: import com.versant.core.metric.HasMetrics;
016: import com.versant.core.metadata.ModelMetaData;
017: import com.versant.core.server.DataStoreInfo;
018: import com.versant.core.server.CompiledQueryCache;
019: import com.versant.core.metric.BaseMetric;
020: import com.versant.core.metric.Metric;
021: import com.versant.core.metric.HasMetrics;
022: import com.versant.core.logging.LogEventStore;
023:
024: import java.util.List;
025: import java.util.Set;
026:
027: /**
028: * Decorates another SMF to create SMs that log events and maintain
029: * performance counters.
030: */
031: public final class LoggingStorageManagerFactory implements
032: StorageManagerFactory, HasMetrics {
033:
034: private final StorageManagerFactory smf;
035: private final LogEventStore pes;
036:
037: public int txCount;
038: public int txCommitCount;
039: public int txCommitErrorCount;
040: public int txRollbackCount;
041: public int txFlushCount;
042: public int txFlushErrorCount;
043: public int fetchCount;
044: public int fetchErrorCount;
045: public int queryCount;
046: public int queryErrorCount;
047:
048: private static final String CAT_MEM = "Memory";
049:
050: private final BaseMetric metricMemoryFree = new BaseMetric(
051: "MemFreeKB", "Mem Free KB", CAT_MEM,
052: "Available heap memory in KB", 0, Metric.CALC_AVERAGE);
053: private final BaseMetric metricMemoryTotal = new BaseMetric(
054: "MemTotalKB", "Mem Total KB", CAT_MEM,
055: "Total heap memory in KB", 0, Metric.CALC_AVERAGE);
056:
057: private static final String CAT_TX = "Transactions";
058:
059: private final BaseMetric metricTx = new BaseMetric("Tx", "Tx",
060: CAT_TX, "Transactions started", 3,
061: Metric.CALC_DELTA_PER_SECOND);
062: private final BaseMetric metricTxCommit = new BaseMetric(
063: "TxCommit", "Tx Commit", CAT_TX, "Transactions committed",
064: 3, Metric.CALC_DELTA_PER_SECOND);
065: private final BaseMetric metricTxCommitError = new BaseMetric(
066: "TxCommitError", "Tx Commit Error", CAT_TX,
067: "Transactions that failed with an error during commit", 3,
068: Metric.CALC_DELTA_PER_SECOND);
069: private final BaseMetric metricTxRollback = new BaseMetric(
070: "TxRollback", "Tx Rollback", CAT_TX,
071: "Transactions rolled back", 3, Metric.CALC_DELTA);
072: private final BaseMetric metricTxFlush = new BaseMetric("TxFlush",
073: "Tx Flush", CAT_TX, "Flushes prior to transaction commit",
074: 3, Metric.CALC_DELTA_PER_SECOND);
075: private final BaseMetric metricTxFlushError = new BaseMetric(
076: "TxFlushError", "Tx Flush Error", CAT_TX,
077: "Flushes that failed with an error", 3,
078: Metric.CALC_DELTA_PER_SECOND);
079:
080: private static final String CAT_FETCH = "Fetches";
081:
082: private final BaseMetric metricFetch = new BaseMetric("Fetch",
083: "Fetch", CAT_FETCH, "Number of calls to fetch data", 3,
084: Metric.CALC_DELTA_PER_SECOND);
085: private final BaseMetric metricFetchError = new BaseMetric(
086: "FetchError", "Fetch Error", CAT_FETCH,
087: "Number of calls to fetch data that failed with an error",
088: 3, Metric.CALC_DELTA_PER_SECOND);
089: private final BaseMetric metricQuery = new BaseMetric("Query",
090: "Query", CAT_FETCH, "Number of queries executed", 3,
091: Metric.CALC_DELTA_PER_SECOND);
092: private final BaseMetric metricQueryError = new BaseMetric(
093: "QueryError", "Query Error", CAT_FETCH,
094: "Number of queries executed that failed with an error", 3,
095: Metric.CALC_DELTA_PER_SECOND);
096:
097: public LoggingStorageManagerFactory(StorageManagerFactory smf,
098: LogEventStore pes) {
099: this .smf = smf;
100: this .pes = pes;
101: }
102:
103: public LogEventStore getLogEventStore() {
104: return pes;
105: }
106:
107: public void init(boolean full, ClassLoader loader) {
108: smf.init(full, loader);
109: }
110:
111: public void destroy() {
112: smf.destroy();
113: }
114:
115: public StorageManager getStorageManager() {
116: StorageManager sm = smf.getStorageManager();
117: return new LoggingStorageManager(this , sm);
118: }
119:
120: public void returnStorageManager(StorageManager sm) {
121: smf.returnStorageManager(sm);
122: }
123:
124: public ModelMetaData getModelMetaData() {
125: return smf.getModelMetaData();
126: }
127:
128: public Object getDatastoreConnection() {
129: return smf.getDatastoreConnection();
130: }
131:
132: public void closeIdleDatastoreConnections() {
133: smf.closeIdleDatastoreConnections();
134: }
135:
136: public DataStoreInfo getDataStoreInfo() {
137: return smf.getDataStoreInfo();
138: }
139:
140: public StorageManagerFactory getInnerStorageManagerFactory() {
141: return smf;
142: }
143:
144: public void addMetrics(List list) {
145: list.add(metricMemoryFree);
146: list.add(metricMemoryTotal);
147: list.add(metricTx);
148: list.add(metricTxCommit);
149: list.add(metricTxCommitError);
150: list.add(metricTxRollback);
151: list.add(metricTxFlush);
152: list.add(metricTxFlushError);
153: list.add(metricFetch);
154: list.add(metricFetchError);
155: list.add(metricQuery);
156: list.add(metricQueryError);
157: if (smf instanceof HasMetrics) {
158: ((HasMetrics) smf).addMetrics(list);
159: }
160: }
161:
162: public void sampleMetrics(int[][] buf, int pos) {
163: Runtime r = Runtime.getRuntime();
164: buf[metricMemoryFree.getIndex()][pos] = (int) (r.freeMemory() >> 10);
165: buf[metricMemoryTotal.getIndex()][pos] = (int) (r.totalMemory() >> 10);
166: buf[metricTx.getIndex()][pos] = txCount;
167: buf[metricTxCommit.getIndex()][pos] = txCommitCount;
168: buf[metricTxCommitError.getIndex()][pos] = txCommitErrorCount;
169: buf[metricTxRollback.getIndex()][pos] = txRollbackCount;
170: buf[metricTxFlush.getIndex()][pos] = txFlushCount;
171: buf[metricTxFlushError.getIndex()][pos] = txFlushErrorCount;
172: buf[metricFetch.getIndex()][pos] = fetchCount;
173: buf[metricFetchError.getIndex()][pos] = fetchErrorCount;
174: buf[metricQuery.getIndex()][pos] = queryCount;
175: buf[metricQueryError.getIndex()][pos] = queryErrorCount;
176: if (smf instanceof HasMetrics) {
177: ((HasMetrics) smf).sampleMetrics(buf, pos);
178: }
179: }
180:
181: public void supportedOptions(Set options) {
182: smf.supportedOptions(options);
183: }
184:
185: public CompiledQueryCache getCompiledQueryCache() {
186: return smf.getCompiledQueryCache();
187: }
188:
189: }
|