001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.stress;
018:
019: import org.objectweb.speedo.pobjects.collection.AllCollection;
020: import org.objectweb.util.monolog.api.BasicLevel;
021:
022: import javax.jdo.JDOFatalException;
023: import javax.jdo.PersistenceManager;
024:
025: /**
026: *
027: * @author M. Guillemin
028: */
029: public abstract class AllCollectionHelper extends StressHelper {
030:
031: /**
032: * is the lists of object identifier prepared before the transaction
033: * execution.
034: * if (oids == null) {
035: * db is not initialised
036: * } else if (oids != null && oids.length==0) {
037: * db initialised and keepOid = false
038: * } else {
039: * db initialised and keepOid == true
040: * }
041: */
042: protected static Object[] oids = null;
043: protected String DBSIZE = getLoggerName() + ".dbsize";
044: protected String NO_DB_INIT = getLoggerName() + ".nodbinit";
045:
046: public AllCollectionHelper(String s) {
047: super (s);
048: }
049:
050: protected String[] getClassNamesToInit() {
051: return new String[] { AllCollection.class.getName() };
052: }
053:
054: protected boolean linkObject() {
055: return true;
056: }
057:
058: protected boolean keepOid() {
059: return true;
060: }
061:
062: /**
063: * IMPORTANT: dot not removed data on support in order to avoid next
064: * creations
065: */
066: public void setUp() throws Exception {
067: logger.log(BasicLevel.DEBUG, "setUp.");
068: cleanup();
069: initDataStructure(false);
070: debug = logger.isLoggable(BasicLevel.DEBUG);
071: }
072:
073: /**
074: * Creates the persistent object if it is not already done.
075: * @param task the task to prepare
076: * @param _ctx the context of the test.
077: */
078: protected void prepareTask(Task task, Object _ctx) {
079: super .prepareTask(task, _ctx);
080: ACCtx ctx = (ACCtx) _ctx;
081: if (oids == null) {
082: synchronized (TestCollectionConcurrency.class) {
083: if (oids == null && !Boolean.getBoolean(NO_DB_INIT)) {
084: //Initialisation the database
085: logger.log(BasicLevel.INFO, "\tPreparing test...");
086: new PrepareTestAllCollection(this ).prepare(ctx,
087: keepOid());
088: if (keepOid()) {
089: //keep oids in the static variable
090: oids = ctx.oids;
091: } else {
092: //db initialized without oids
093: oids = new Object[0];
094: }
095: logger.log(BasicLevel.INFO, "\tTest Prepared.");
096: }
097: }
098: }
099: ctx.oids = oids;
100: }
101:
102: /**
103: * The context to use for the AllCollection object
104: */
105: public class ACCtx {
106: /**
107: * The identifier of the created object (see keepOid method)
108: */
109: public Object oids[];
110: /**
111: * The number of persistent object
112: */
113: public int dbSize;
114: /**
115: * indicates if the oid must be kept during the preparation of the task
116: */
117: public boolean keepOidOnPrepare;
118:
119: public ACCtx(int dbSize) {
120: this .dbSize = dbSize;
121: oids = new Object[dbSize];
122: }
123:
124: public void initOnPrepare(int nbTx, boolean fetchOid) {
125: this .keepOidOnPrepare = fetchOid;
126: }
127:
128: public String toString() {
129: return "dbSize = " + dbSize;
130: }
131: }
132: }
133:
134: class PrepareTestAllCollection extends StressHelper {
135:
136: private final static int NB_CREATION = 100;
137: private final static int NB_THREAD = 4;
138:
139: public PrepareTestAllCollection(AllCollectionHelper helper) {
140: super (helper.getName());
141: }
142:
143: protected String[] getClassNamesToInit() {
144: return new String[] { AllCollection.class.getName() };
145: }
146:
147: protected String getLoggerName() {
148: return STRESS_LOG_NAME + ".AllCollectionHelper";
149: }
150:
151: protected String getLogPrefix() {
152: return super .getLogPrefix() + "\t";
153: }
154:
155: protected void perform(StressHelper.Task task, int threadId,
156: int txId, Object ctx, PerformResult res) {
157: AllCollectionHelper.ACCtx pctx = (AllCollectionHelper.ACCtx) ctx;
158: int plus = pctx.dbSize % NB_CREATION;
159: int nbTx = task.txToExecute.length;
160: PersistenceManager pm = getPM(task, threadId, txId);
161: try {
162: res.beginTest();
163: beginTx(pm, task, threadId, txId);
164: AllCollection ac = null;
165:
166: if (plus > 0) {
167: //The first transaction creates the additional object
168: for (int oid = 0; txId == 0 && oid < plus; oid++) {
169: long oids = (oid * 6);
170: //long[] ls = new long[]{oids, oids + 1, oids + 2, oids + 3, oids + 4, oids + 5};
171: long[] ls = new long[] { oids, oids + 1 };
172: ac = new AllCollection("stressCollection" + oid);
173: ac.setLongs(ls);
174: ac.setRefs(new Object[] { ac });
175: logger.log(BasicLevel.DEBUG, "make persistent ac="
176: + ac.getId());
177: pm.makePersistent(ac);
178: if (pctx.keepOidOnPrepare) {
179: pctx.oids[oid] = pm.getObjectId(ac);
180: }
181: }
182: }
183: // The other transactions create 'nbCreation' objects
184: for (int no = 0; no < NB_CREATION; no++) {
185: int oid = (txId * NB_CREATION) + no + plus;
186: if (oid < pctx.dbSize) {
187: long oids = (oid * 6);
188: //long[] ls = new long[]{oids, oids + 1, oids + 2, oids + 3, oids + 4, oids + 5};
189: long[] ls = new long[] { oids, oids + 1 };
190: ac = new AllCollection("stressCollection" + oid);
191: ac.setLongs(ls);
192: ac.setRefs(new Object[] { ac });
193: logger.log(BasicLevel.DEBUG, "make persistent ac="
194: + ac.getId());
195: pm.makePersistent(ac);
196: if (pctx.keepOidOnPrepare) {
197: pctx.oids[oid] = pm.getObjectId(ac);
198: }
199: }
200: }
201:
202: commitTx(pm, task, threadId, txId);
203: res.endTest();
204: } catch (JDOFatalException e) {
205: rollbackOnException(pm, e, res, task, threadId, txId);
206: } catch (Throwable e) {
207: stopOnError(pm, e, res, task, threadId, txId);
208: } finally {
209: closePM(pm, threadId, txId, task, res);
210: }
211:
212: }
213:
214: public void prepare(AllCollectionHelper.ACCtx ctx, boolean fetchOid) {
215: int plus = ctx.dbSize % NB_CREATION;
216: int nbTx = (ctx.dbSize / NB_CREATION) + (plus > 0 ? 1 : 0);
217: ctx.initOnPrepare(nbTx, fetchOid);
218: perform(NB_THREAD, nbTx, Integer.getInteger(TIMEOUT, 200000)
219: .intValue(), ctx);
220: }
221: }
|