r406 - in testTopiaPostgresError/trunk/src/main: java/org/nuiton/test/topia resources
Author: fdesbois Date: 2010-04-21 11:27:50 +0200 (Wed, 21 Apr 2010) New Revision: 406 Log: Third version : - no more ConcurrentException (token + lock) - use Timer and change superclass of Thread to TimerTask to use periodically execution (each 3 hours) - The execution takes approximatively 1 hour, so 2 hours of inactivity before restarting the thread Modified: testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/AbstractThread.java testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/ReadThread.java testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/TestManager.java testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/WriteThread.java testTopiaPostgresError/trunk/src/main/resources/log4j.properties Modified: testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/AbstractThread.java =================================================================== --- testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/AbstractThread.java 2010-04-18 10:14:54 UTC (rev 405) +++ testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/AbstractThread.java 2010-04-21 09:27:50 UTC (rev 406) @@ -1,6 +1,8 @@ package org.nuiton.test.topia; +import java.util.TimerTask; +import java.util.logging.Level; import org.apache.commons.lang.time.DurationFormatUtils; import org.nuiton.topia.TopiaException; import org.slf4j.Logger; @@ -17,7 +19,7 @@ * Mise a jour: $Date$ * par : $Author$ */ -public abstract class AbstractThread implements Runnable { +public abstract class AbstractThread extends TimerTask { private static final Logger logger = LoggerFactory.getLogger(AbstractThread.class); @@ -25,51 +27,92 @@ protected TestManager manager = TestManager.getInstance(); - protected volatile boolean stop; - protected long startTime; - - protected long startMilliseconds; protected String name; - public AbstractThread(long startMilliseconds, String name) { - this.startMilliseconds = startMilliseconds; + protected int currentExecution; + + public int nbExecutions; + + protected static long globalStart = System.currentTimeMillis(); + + protected static final Object lock = new Object(); + protected static Boolean token = false; + + public AbstractThread(int nbExecutions, String name) { this.name = name; + this.nbExecutions = nbExecutions * 1000; } - public synchronized void stop() { - stop = true; - } - @Override public void run() { + startTime = System.currentTimeMillis(); + if (logger.isInfoEnabled()) { - logger.info("Start " + name + " in " + startMilliseconds + "ms"); + logger.info("Start " + name + " for " + nbExecutions + + " executions after " + + DurationFormatUtils.formatDurationHMS( + startTime - globalStart)); } - startTime = System.currentTimeMillis(); - try { - Thread.sleep(startMilliseconds); - } catch (InterruptedException eee) { - if (logger.isErrorEnabled()) { - logger.error("Thread interrupted during sleep", eee); - } - } + for (currentExecution = 0; currentExecution < nbExecutions; + currentExecution ++) { + if (!manager.isStopped()) { + try { + synchronized (lock) { + // Do not stop waiting until token is free + while (token) { + if (logger.isDebugEnabled()) { + logger.debug(name + " waiting..."); + } + lock.wait(); + if (logger.isDebugEnabled()) { + logger.debug(name + " no longer waiting"); + } + } + // Take the token to lock other thread + if (logger.isDebugEnabled()) { + logger.debug(name + " take the token"); + } + token = true; + } + if (logger.isDebugEnabled()) { + logger.debug(name + " execute..."); + } + // Execute + execute(); + synchronized (lock) { + if (logger.isDebugEnabled()) { + logger.debug(name + " release the token"); + } + // Release the token and notify other that the token + // is free + token = false; + lock.notifyAll(); + } + // Sleep to permit the unlocked Thread to have time to take + // the token + if (logger.isDebugEnabled()) { + logger.debug(name + " go to sleep a while"); + } + Thread.sleep(200); - while(!stop) { - try { - execute(); - } catch (TopiaException eee) { - long stopTime = System.currentTimeMillis(); - if (logger.isErrorEnabled()) { - logger.error("ERROR after " + - DurationFormatUtils.formatDurationHMS( - stopTime - startTime), eee); + } catch (InterruptedException eee) { + if (logger.isErrorEnabled()) { + logger.error("Interrupted during sleep or wait", eee); + } + } catch (TopiaException eee) { + long stopTime = System.currentTimeMillis(); + if (logger.isErrorEnabled()) { + logger.error("ERROR after " + + DurationFormatUtils.formatDurationHMS( + stopTime - startTime), eee); + } + // Will stop the application + manager.stopTest(); } - // Will stop the application - manager.stopTest(); } } @@ -78,5 +121,6 @@ } } - protected abstract void execute() throws TopiaException; + protected abstract void execute() + throws TopiaException, InterruptedException; } Modified: testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/ReadThread.java =================================================================== --- testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/ReadThread.java 2010-04-18 10:14:54 UTC (rev 405) +++ testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/ReadThread.java 2010-04-21 09:27:50 UTC (rev 406) @@ -26,11 +26,13 @@ protected int tic; public ReadThread() { - super(1000, ReadThread.class.getName()); + // 15 executions during approximatively 1 hour + super(15, ReadThread.class.getName()); } @Override - protected synchronized void execute() throws TopiaException { + protected synchronized void execute() + throws TopiaException, InterruptedException { TopiaContext transaction = null; try { transaction = manager.getRootContext().beginTransaction(); @@ -44,22 +46,16 @@ logger.debug("Reading " + versions.size() + " entries"); } - Thread.sleep(500); - tic++; - if ((tic % 100 == 0) && logger.isInfoEnabled()) { + if ((tic % 300 == 0) && logger.isInfoEnabled()) { long stopTime = System.currentTimeMillis(); long time = stopTime - startTime; logger.info(versions.size() + " values read after " + - DurationFormatUtils.formatDurationHMS(time)); + DurationFormatUtils.formatDurationHMS(time) + + " (exec " + currentExecution + ")"); } - - } catch (InterruptedException eee) { - if (logger.isErrorEnabled()) { - logger.error("Thread interrupted during sleep", eee); - } } finally { if (transaction != null) { transaction.closeContext(); Modified: testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/TestManager.java =================================================================== --- testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/TestManager.java 2010-04-18 10:14:54 UTC (rev 405) +++ testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/TestManager.java 2010-04-21 09:27:50 UTC (rev 406) @@ -3,15 +3,12 @@ import java.util.Locale; import java.util.Properties; -import java.util.logging.Level; +import java.util.Timer; import org.nuiton.i18n.I18n; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaNotFoundException; -import org.nuiton.topia.migration.MigrationServiceDAOHelper; -import org.nuiton.topia.migration.TMSVersion; -import org.nuiton.topia.migration.TMSVersionDAO; import org.nuiton.topia.migration.TMSVersionImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,8 +29,6 @@ private static final Logger logger = LoggerFactory.getLogger(TestManager.class); -// protected ApplicationConfig configuration; - protected static TestManager instance; protected Properties configuration; @@ -42,6 +37,16 @@ protected ReadThread readThread; + protected Timer readTimer; + + protected Timer writeTimer; + + private static final long THREE_HOURS_PERIOD = 10800000; + + protected volatile boolean stop; + + public static final Object waitForStop = new Object(); + public static TestManager getInstance() { if (instance == null) { instance = new TestManager(); @@ -68,73 +73,44 @@ TMSVersionImpl.class.getName()); I18n.init(Locale.FRANCE); - - // Create first entry -// TopiaContext transaction = null; -// try { -// if (logger.isInfoEnabled()) { -// logger.info("Open first transaction to create main entry"); -// } -// transaction = TopiaContextFactory.getContext(configuration). -// beginTransaction(); -// TMSVersionDAO dao = -// MigrationServiceDAOHelper.getTMSVersionDAO(transaction); -// -// TMSVersion entry = dao.create(TMSVersion.VERSION, "1"); -// -// mainTopiaId = entry.getTopiaId(); -// if (logger.isInfoEnabled()) { -// logger.info("Save the entry with id = " + mainTopiaId); -// } -// transaction.commitTransaction(); -// -// } catch (TopiaException eee) { -// if (logger.isErrorEnabled()) { -// logger.error("Error when create the first entry : ", eee); -// } -// } finally { -// if (transaction != null) { -// try { -// transaction.closeContext(); -// } catch (TopiaException eee) { -// if (logger.isErrorEnabled()) { -// logger.error("Error on closeContext : ", eee); -// } -// } -// } -// } } public TopiaContext getRootContext() throws TopiaNotFoundException { return TopiaContextFactory.getContext(configuration); } + public boolean isStopped() { + return stop; + } + public void startTest() { - if (writeThread == null) { - writeThread = new WriteThread(); - new Thread(writeThread).start(); + // Execution of thread during 1 hour + 2 hours of inactivity + if (readTimer == null) { + readTimer = new Timer(); + readTimer.schedule(new ReadThread(), 1000, THREE_HOURS_PERIOD); } - if (readThread == null) { - readThread = new ReadThread(); - new Thread(readThread).start(); + if (writeTimer == null) { + writeTimer = new Timer(); + writeTimer.schedule(new WriteThread(), 0, THREE_HOURS_PERIOD); } } public void stopTest() { - if (writeThread != null) { - writeThread.stop(); - writeThread = null; - } - if (readThread != null) { - readThread.stop(); - readThread = null; - } - try { - getRootContext().closeContext(); - } catch (TopiaException eee) { - if (logger.isErrorEnabled()) { - logger.error("Error on close root context : ", eee); + if (!stop) { + stop = true; + if (readTimer != null) { + readTimer.cancel(); } + if (writeTimer != null) { + writeTimer.cancel(); + } + try { + getRootContext().closeContext(); + } catch (TopiaException eee) { + if (logger.isErrorEnabled()) { + logger.error("Error on close root context : ", eee); + } + } } } Modified: testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/WriteThread.java =================================================================== --- testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/WriteThread.java 2010-04-18 10:14:54 UTC (rev 405) +++ testTopiaPostgresError/trunk/src/main/java/org/nuiton/test/topia/WriteThread.java 2010-04-21 09:27:50 UTC (rev 406) @@ -29,7 +29,8 @@ protected int tic; public WriteThread() { - super(0, WriteThread.class.getName()); + // 15 executions during approximatively 1 hour + super(15, WriteThread.class.getName()); } /** @@ -39,9 +40,11 @@ * of saved). * * @throws TopiaException + * @throws InterruptedException */ @Override - protected synchronized void execute() throws TopiaException { + protected synchronized void execute() + throws TopiaException, InterruptedException { TopiaContext transaction = null; try { transaction = manager.getRootContext().beginTransaction(); @@ -53,8 +56,6 @@ // Create an alea between 0 and 1000 int alea = (int)(random.nextFloat() * 1000); - boolean actionDone = false; - // DELETE if alea is divided by 100 if (alea % 100 == 0) { // Alea number over number of existing entries @@ -75,7 +76,6 @@ dao.delete(exist); increment++; transaction.commitTransaction(); - actionDone = true; } // UPDATE if alea is divided by 20 } else if (alea % 20 == 0) { @@ -98,7 +98,6 @@ exist.setVersion(String.valueOf(increment)); increment++; transaction.commitTransaction(); - actionDone = true; } // CREATE if alea is divided by 10 } else if (alea % 10 == 0) { @@ -113,24 +112,18 @@ increment ++; transaction.commitTransaction(); - actionDone = true; } - Thread.sleep(100); - tic++; - if ((tic % 600 == 0) && logger.isInfoEnabled()) { + if ((tic % 300 == 0) && logger.isInfoEnabled()) { long stopTime = System.currentTimeMillis(); long time = stopTime - startTime; logger.info(increment + " create/update or delete actions" + - " after " + DurationFormatUtils.formatDurationHMS(time)); + " after " + DurationFormatUtils.formatDurationHMS(time) + + " (exec " + currentExecution + ")"); } - } catch (InterruptedException eee) { - if (logger.isErrorEnabled()) { - logger.error("Thread interrupted during sleep", eee); - } } finally { if (transaction != null) { transaction.closeContext(); Modified: testTopiaPostgresError/trunk/src/main/resources/log4j.properties =================================================================== --- testTopiaPostgresError/trunk/src/main/resources/log4j.properties 2010-04-18 10:14:54 UTC (rev 405) +++ testTopiaPostgresError/trunk/src/main/resources/log4j.properties 2010-04-21 09:27:50 UTC (rev 406) @@ -13,7 +13,4 @@ log4j.appender.file.Threshold=DEBUG log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %5p [%t] (%c:%L) %M - %m%n - -log4j.logger.org.nuiton.test.topia=DEBUG -log4j.logger.org.nuiton.topia=INFO \ No newline at end of file +log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %5p [%t] (%c:%L) %M - %m%n \ No newline at end of file
participants (1)
-
fdesbois@users.nuiton.org