User: starksm 
  Date: 01/10/30 17:51:10

  Modified:    src/main/org/jboss/mq/server MessageCache.java
  Log:
  Use the ServiceMBeanSupport.log to for logging and change the
  debug msgs to trace to avoid the excessive logging to server.log
  
  Revision  Changes    Path
  1.3       +173 -133  jbossmq/src/main/org/jboss/mq/server/MessageCache.java
  
  Index: MessageCache.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/server/MessageCache.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MessageCache.java 2001/10/28 04:07:36     1.2
  +++ MessageCache.java 2001/10/31 01:51:10     1.3
  @@ -23,342 +23,380 @@
    * later.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Hiram Chirino</a>
  - * @version    $Revision: 1.2 $
  + * @version    $Revision: 1.3 $
    */
  -public class MessageCache extends ServiceMBeanSupport implements MessageCacheMBean, 
MBeanRegistration, Runnable {
  -
  -   org.apache.log4j.Category cat = 
org.apache.log4j.Category.getInstance(MessageCache.class);
  -
  +public class MessageCache extends ServiceMBeanSupport implements MessageCacheMBean, 
MBeanRegistration, Runnable
  +{
      // The cached messages are orded in a LRU linked list
      private LinkedList lruCache = new LinkedList();
  -
  -   // Provides a Unique ID to MessageHanles 
  +   
  +   // Provides a Unique ID to MessageHanles
      private long messageCounter = 0;
      int cacheHits = 0;
      int cacheMisses = 0;
  -
  +   
      private File dataFile;
      private String dataDirectory;
      private Thread referenceSoftner;
  -
  +   
      private long highMemoryMark = 1024L * 1000 * 16;
      private long maxMemoryMark = 1024L * 1000 * 32;
      public static final long ONE_MEGABYTE = 1024L * 1000;
  -
  +   
      int softRefCacheSize = 0;
      int totalCacheSize = 0;
  -
  +   
      // Used to get notified when message are being deleted by GC
      ReferenceQueue referenceQueue = new ReferenceQueue();
  -
  +   
      /**
       * Adds a message to the cache
       */
  -   public MessageReference add(SpyMessage message) throws javax.jms.JMSException {
  -      cat.debug("add lock aquire");
  -      synchronized (this) {
  +   public MessageReference add(SpyMessage message) throws javax.jms.JMSException
  +   {
  +      log.trace("add lock aquire");
  +      synchronized (this)
  +      {
            MessageReference mh = new MessageReference(this, new 
Long(messageCounter++), message);
            lruCache.addLast(mh);
            totalCacheSize++;
            validateSoftReferenceDepth();
  -
  -         cat.debug("add lock release");
  +         
  +         log.trace("add lock release");
            return mh;
         }
      }
  -
  +   
      /**
       * removes a message from the cache
       */
  -   public void remove(MessageReference mr) throws JMSException {
  -      cat.debug("remove lock aquire");
  -      synchronized (this) {
  +   public void remove(MessageReference mr) throws JMSException
  +   {
  +      log.trace("remove lock aquire");
  +      synchronized (this)
  +      {
            mr.clear();
            lruCache.remove(mr);
            totalCacheSize--;
  -         cat.debug("remove lock release");
  +         log.trace("remove lock release");
         }
      }
  -
  +   
      /**
       * The strategy is that we keep the most recently used messages as
  -    * Hard references.  Then we make the older ones soft references.  Making 
  +    * Hard references.  Then we make the older ones soft references.  Making
       * something a soft reference stores it to disk so we need to avoid making
       * soft references if we can avoid it.  But once it is made a soft reference does
       * not mean that it is removed from memory.  Depending on how agressive the JVM's
       * GC is, it may stay around long enough for it to be used by a client doing a 
read,
       * saving us read from the file system.  If memory gets tight the GC will remove
  -    * the soft references.  What we want to do is make sure there are at least some 
  +    * the soft references.  What we want to do is make sure there are at least some
       * soft references available so that the GC can reclaim memory.
       * @see Runnable#run()
       */
  -   public void run() {
  -      try {
  -         while (true) {
  -            cat.debug("Waiting for a reference to get GCed.");
  +   public void run()
  +   {
  +      try
  +      {
  +         while (true)
  +         {
  +            log.trace("Waiting for a reference to get GCed.");
               // Get the next soft reference that was canned by the GC
  -
  +            
               Reference r = referenceQueue.remove(1000);
  -            if (r != null) {
  +            if (r != null)
  +            {
                  softRefCacheSize--;
                  // the GC will free a set of messages together, so we poll them
                  // all before we validate the soft reference depth.
  -               while ((r = referenceQueue.poll()) != null) {
  +               while ((r = referenceQueue.poll()) != null)
  +               {
                     softRefCacheSize--;
                  }
               }
  -            cat.debug("Validating soft reference count.");
  +            log.trace("Validating soft reference count.");
               validateSoftReferenceDepth();
            }
  -      } catch (JMSException e) {
  -         cat.error("Message Cache Thread Stopped: ", e);
  -      } catch (InterruptedException e) {
  +      } catch (JMSException e)
  +      {
  +         log.error("Message Cache Thread Stopped: ", e);
  +      } catch (InterruptedException e)
  +      {
            // Signal to exit the thread.
         }
  -      cat.debug("Thread exiting.");
  +      log.debug("Thread exiting.");
      }
  -
  -   /** 
  -    * This method is in charge of determining if it time to convert some 
  +   
  +   /**
  +    * This method is in charge of determining if it time to convert some
       * hard references over to soft references.
       */
  -   public void validateSoftReferenceDepth() throws JMSException {
  -      cat.debug("run lock aquire");
  -      synchronized (this) {
  -
  +   public void validateSoftReferenceDepth() throws JMSException
  +   {
  +      log.trace("run lock aquire");
  +      synchronized (this)
  +      {
  +         
            // howmany to change over to soft refs
            int chnageCount = 0;
  -
  +         
            long currentMem = Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory();
  -         if (currentMem > highMemoryMark) {
  -            // we need to get more aggresive... how much?? lets get 
  +         if (currentMem > highMemoryMark)
  +         {
  +            // we need to get more aggresive... how much?? lets get
               // a mesurment from 0 to 1
               float severity = ((float) (currentMem - highMemoryMark)) / 
(maxMemoryMark - highMemoryMark);
               severity = Math.min(severity, 1.0F);
  -            cat.info("Memory usage serverity=" + severity);
  +            log.info("Memory usage serverity=" + severity);
               int totoalMessageInMem = getHardRefCacheSize() + getSoftRefCacheSize();
               int howManyShouldBeSoft = (int) ((totoalMessageInMem) * severity);
               chnageCount = howManyShouldBeSoft - getSoftRefCacheSize();
            }
  -
  -         // Ignore change counts of 1 since this will happen too often even 
  +         
  +         // Ignore change counts of 1 since this will happen too often even
            // if the serverity is low since it will round up.
  -         if (chnageCount > 1) {
  -            cat.debug("Converting " + chnageCount + " hard ref to to soft refs");
  +         if (chnageCount > 1)
  +         {
  +            log.debug("Converting " + chnageCount + " hard ref to to soft refs");
               Iterator iter = lruCache.iterator();
  -            for (int i = 0; i < chnageCount && iter.hasNext(); i++) {
  +            for (int i = 0; i < chnageCount && iter.hasNext(); i++)
  +            {
                  MessageReference mr = (MessageReference) iter.next();
                  mr.makeSoft();
                  iter.remove();
               }
            }
  -         cat.debug("run lock release");
  +         log.trace("run lock release");
         }
  -
  +      
      }
  -
  +   
      /**
       * This gets called when a MessageReference is de-referenced.
       * We will pop it to the top of the RLU
       */
  -   synchronized public void messageReferenceUsedEvent(MessageReference mh, boolean 
wasHard) {
  -      cat.debug("messageReferenceUsedEvent lock aquire");
  -      synchronized (this) {
  +   synchronized public void messageReferenceUsedEvent(MessageReference mh, boolean 
wasHard)
  +   {
  +      log.trace("messageReferenceUsedEvent lock aquire");
  +      synchronized (this)
  +      {
            if (wasHard)
               lruCache.remove(mh);
            lruCache.addLast(mh);
  -         cat.debug("messageReferenceUsedEvent lock released");
  +         log.trace("messageReferenceUsedEvent lock released");
         }
      }
  -
  +   
      
//////////////////////////////////////////////////////////////////////////////////
  -   // Perisitence methods used by the MessageReference. 
  +   // Perisitence methods used by the MessageReference.
      // TODO: delegate this work to a PM.
      
//////////////////////////////////////////////////////////////////////////////////
  -   SpyMessage loadFromStorage(MessageReference mh) throws IOException, 
ClassNotFoundException {
  +   SpyMessage loadFromStorage(MessageReference mh) throws IOException, 
ClassNotFoundException
  +   {
         File f = new File(dataFile, "Message-" + mh.referenceId);
         ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(new 
FileInputStream(f)));
         Object rc = is.readObject();
         is.close();
         return (SpyMessage) rc;
      }
  -
  -   void saveToStorage(MessageReference mh, SpyMessage message) throws IOException {
  +   
  +   void saveToStorage(MessageReference mh, SpyMessage message) throws IOException
  +   {
         File f = new File(dataFile, "Message-" + mh.referenceId);
         ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(new 
FileOutputStream(f)));
         os.writeObject(message);
         os.close();
      }
  -
  -   void removeFromStorage(MessageReference mh) throws IOException {
  +   
  +   void removeFromStorage(MessageReference mh) throws IOException
  +   {
         File f = new File(dataFile, "Message-" + mh.referenceId);
         f.delete();
      }
  -
  +   
      
//////////////////////////////////////////////////////////////////////////////////
      //
      // The following section deals the the JMX interface to manage the Cache
      //
      
//////////////////////////////////////////////////////////////////////////////////
  -
  +   
      /**
       * This gets called after the MBean has been initialized.
       */
  -   synchronized public void initService() throws Exception {
  -
  +   synchronized public void initService() throws Exception
  +   {
  +      
         File jbossHome = new File(System.getProperty("jboss.system.home"));
         dataFile = new File(jbossHome, dataDirectory);
  -      cat.debug("Data directory set to: " + dataFile.getCanonicalPath());
  -
  +      log.debug("Data directory set to: " + dataFile.getCanonicalPath());
  +      
         dataFile.mkdirs();
         if (!dataFile.isDirectory())
            throw new Exception("The configured data directory is not valid: " + 
dataDirectory);
  -
  +      
         // Clean out the directory of any previous files.
         File files[] = dataFile.listFiles();
  -      cat.debug("Removing " + files.length + " file(s) from: " + 
dataFile.getCanonicalPath());
  -      for (int i = 0; i < files.length; i++) {
  +      log.debug("Removing " + files.length + " file(s) from: " + 
dataFile.getCanonicalPath());
  +      for (int i = 0; i < files.length; i++)
  +      {
            files[i].delete();
         }
  -
  -      JMSServer server = (JMSServer) getServer().invoke(new 
ObjectName(JBossMQServiceMBean.OBJECT_NAME), "getJMSServer", new Object[] {}, new 
String[] {});
  +      
  +      JMSServer server = (JMSServer) getServer().invoke(new 
ObjectName(JBossMQServiceMBean.OBJECT_NAME),
  +         "getJMSServer", new Object[]{}, new String[]{});
         server.setMessageCache(this);
      }
  -
  +   
      /**
       * This gets called to start the cache service.
       */
  -   synchronized public void startService() throws Exception {
  -
  +   synchronized public void startService() throws Exception
  +   {
  +      
         if (getState() == ServiceMBeanSupport.STARTED)
            throw new Exception("Cannot be initialized from the current state");
  -
  +      
         referenceSoftner = new Thread(this, "JBossMQ Cache Reference Softner");
         referenceSoftner.setDaemon(true);
         referenceSoftner.start();
  -
  +      
      }
  -
  +   
      /**
       * This gets called to stop the cache service.
       */
  -   synchronized public void stopService() {
  +   synchronized public void stopService()
  +   {
         if (!(getState() == ServiceMBeanSupport.STARTED))
            return;
  -
  +      
         referenceSoftner.interrupt();
         referenceSoftner = null;
      }
  -
  +   
      /**
       * Gets the dataDirectory
       * @return Returns a String
       */
  -   public String getDataDirectory() {
  +   public String getDataDirectory()
  +   {
         return dataDirectory;
      }
      /**
       * Sets the dataDirectory
       * @param dataDirectory The dataDirectory to set
       */
  -   public void setDataDirectory(String dataDirectory) {
  +   public void setDataDirectory(String dataDirectory)
  +   {
         this.dataDirectory = dataDirectory;
      }
  -
  +   
      /**
       * Gets the hardRefCacheSize
       * @return Returns a int
       */
  -   public int getHardRefCacheSize() {
  +   public int getHardRefCacheSize()
  +   {
         return lruCache.size();
      }
  -
  +   
      /**
       * Gets the softRefCacheSize
       * @return Returns a int
       */
  -   public int getSoftRefCacheSize() {
  +   public int getSoftRefCacheSize()
  +   {
         return softRefCacheSize;
      }
  -
  +   
      /**
       * Gets the totalCacheSize
       * @return Returns a int
       */
  -   public int getTotalCacheSize() {
  +   public int getTotalCacheSize()
  +   {
         return totalCacheSize;
      }
  -
  +   
      /**
       * Gets the cacheMisses
       * @return Returns a int
       */
  -   public int getCacheMisses() {
  +   public int getCacheMisses()
  +   {
         return cacheMisses;
      }
  -
  +   
      /**
       * Gets the cacheHits
       * @return Returns a int
       */
  -   public int getCacheHits() {
  +   public int getCacheHits()
  +   {
         return cacheHits;
      }
  -
  +   
      /**
       * Gets the highMemoryMark
       * @return Returns a long
       */
  -   public long getHighMemoryMark() {
  +   public long getHighMemoryMark()
  +   {
         return highMemoryMark / ONE_MEGABYTE;
      }
      /**
       * Sets the highMemoryMark
       * @param highMemoryMark The highMemoryMark to set
       */
  -   public void setHighMemoryMark(long highMemoryMark) {
  +   public void setHighMemoryMark(long highMemoryMark)
  +   {
         this.highMemoryMark = highMemoryMark * ONE_MEGABYTE;
      }
  -
  +   
      /**
       * Gets the maxMemoryMark
       * @return Returns a long
       */
  -   public long getMaxMemoryMark() {
  +   public long getMaxMemoryMark()
  +   {
         return maxMemoryMark / ONE_MEGABYTE;
      }
  -
  +   
      /**
       * Gets the CurrentMemoryUsage
       * @return Returns a long
       */
  -   public long getCurrentMemoryUsage() {
  +   public long getCurrentMemoryUsage()
  +   {
         return (Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory()) / ONE_MEGABYTE;
      }
  -
  +   
      /**
       * Sets the maxMemoryMark
       * @param maxMemoryMark The maxMemoryMark to set
       */
  -   public void setMaxMemoryMark(long maxMemoryMark) {
  +   public void setMaxMemoryMark(long maxMemoryMark)
  +   {
         this.maxMemoryMark = maxMemoryMark * ONE_MEGABYTE;
      }
  -
  +   
      /**
       * @see ServiceMBeanSupport#getName()
       */
  -   public String getName() {
  +   public String getName()
  +   {
         return "MessageCache";
      }
      
      /**
  -    * This test creates 5000 x 100K messages and places them 
  +    * This test creates 5000 x 100K messages and places them
       * in the MessageCache.  With out a cache this would be
  -    * 500 Megs of memory needed.  The cache will allow us to 
  +    * 500 Megs of memory needed.  The cache will allow us to
       * stay withing 64 Megs of RAM.
       */
  -   public void testBigLoad() throws Exception {
  -
  +   public void testBigLoad() throws Exception
  +   {
  +      
         MessageCache cache = new MessageCache();
         File tempDir = new File("Temp-" + System.currentTimeMillis());
         tempDir.mkdirs();
  @@ -367,47 +405,49 @@
         cache.setMaxMemoryMark(60);
         cache.init();
         cache.start();
  -
  +      
         LinkedList ll = new LinkedList();
  -
  +      
         int TEST_SIZE = 5000;
         // Create a whole bunch of messages.
         java.util.Random rand = new java.util.Random(System.currentTimeMillis());
  -      cat.info("Adding the messages");
  -      for (int i = 0; i < TEST_SIZE; i++) {
  -         //cat.info("Used 
Mem="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
  +      log.info("Adding the messages");
  +      for (int i = 0; i < TEST_SIZE; i++)
  +      {
  +         //log.info("Used 
Mem="+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
            org.jboss.mq.SpyBytesMessage bm = new org.jboss.mq.SpyBytesMessage();
            bm.writeBytes(new byte[1024 * 100]); // 100K messages
            MessageReference mr = cache.add(bm);
            ll.add(mr);
  -
  +         
            // Randomly pickout messages out of the cache..
            int pick = rand.nextInt(i + 1);
            mr = (MessageReference) ll.get(pick);
            mr.getMessage();
         }
  -
  -      cat.info("Used Mem=" + (Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory()));
  +      
  +      log.info("Used Mem=" + (Runtime.getRuntime().totalMemory() - 
Runtime.getRuntime().freeMemory()));
         //assertTrue("getTotalCacheSize check", cache.getTotalCacheSize() == 
TEST_SIZE);
  -      cat.info("Messages with Hard Refs=" + cache.getHardRefCacheSize());
  -      cat.info("Messages with Soft Refs=" + cache.getSoftRefCacheSize());
  -
  -      cat.info("Removing the messages");
  +      log.info("Messages with Hard Refs=" + cache.getHardRefCacheSize());
  +      log.info("Messages with Soft Refs=" + cache.getSoftRefCacheSize());
  +      
  +      log.info("Removing the messages");
         Iterator iter = ll.iterator();
  -      for (int i = 0; i < TEST_SIZE; i++) {
  +      for (int i = 0; i < TEST_SIZE; i++)
  +      {
            MessageReference mr = (MessageReference) iter.next();
            iter.remove();
            cache.remove(mr);
         }
  -
  -      cat.info("Stopping");
  +      
  +      log.info("Stopping");
         //assertTrue("getTotalCacheSize check", cache.getTotalCacheSize() == 0);
         cache.stop();
         //assertTrue("Data directory clean up check", tempDir.listFiles().length == 
0);
         tempDir.delete();
  -
  -      cat.info("Cache Hits=" + cache.getCacheHits());
  -      cat.info("Cache Misses=" + cache.getCacheMisses());
  +      
  +      log.info("Cache Hits=" + cache.getCacheHits());
  +      log.info("Cache Misses=" + cache.getCacheMisses());
      }
  -
  +   
   }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to