[jboss-svn-commits] JBL Code SVN: r35962 - in labs/jbossrules/trunk: drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Nov 12 12:38:05 EST 2010


Author: salaboy21
Date: 2010-11-12 12:38:04 -0500 (Fri, 12 Nov 2010)
New Revision: 35962

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/command/impl/ContextImplWithEviction.java
   labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/EvictionJob.java
   labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/test/java/org/drools/grid/TempEvictionTest.java
Modified:
   labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/GridNodeServer.java
   labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/io/impl/NodeData.java
Log:
JBRULES-2790: Drools Grid Impl 2 EvictionJob for take care of _TEMP_ variables in remote context
	- EvictionJob working in TempEvictionTest. I need to find out a way to hide the Job scheduling from the user.

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/command/impl/ContextImplWithEviction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/command/impl/ContextImplWithEviction.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/command/impl/ContextImplWithEviction.java	2010-11-12 17:38:04 UTC (rev 35962)
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2010 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.command.impl;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.drools.command.Context;
+import org.drools.command.ContextManager;
+
+/**
+ *
+ * @author salaboy
+ */
+public class ContextImplWithEviction extends ContextImpl{
+
+    private  Logger log = Logger.getLogger( ContextImplWithEviction.class.getName() );
+    
+    private Map<String, Long> evictionMap = new ConcurrentHashMap<String, Long>();
+    
+    private boolean           evictionEnabled = true;
+    //I need a way to set up this parameters 
+    
+    //time to look up for evicted entries
+    private long              evictionWakeUpTime = 5 * 60 * 1000 ; // 5 minutes
+    //Time that the entry is valid
+    private long              entryEvictionTime = 60 * 60 * 1000;  // 1 hour
+    
+    //private LinkedBlockingQueue<ChangeSet> queue;
+    
+    public ContextImplWithEviction(String name, ContextManager manager, Context delegate) {
+        super(name, manager, delegate);
+    }
+
+    public ContextImplWithEviction(String name, ContextManager manager) {
+        super(name, manager);
+        
+    }
+    
+    @Override
+    public Object get(String identifier){
+        Object result = super.get(identifier);
+        if(evictionEnabled && identifier != null && result != null){
+            long currentTimeStamp = System.currentTimeMillis();
+            evictionMap.put(identifier, currentTimeStamp);
+            log.log(Level.FINE,"Updating key=" +identifier +"@"+super.getName()+":"+currentTimeStamp);
+        }
+        return result;
+    }
+    
+    @Override
+    public void set(String name,
+                    Object object) {
+        if(evictionEnabled){
+            long currentTimeStamp = System.currentTimeMillis();
+            evictionMap.put(name, currentTimeStamp );
+            log.log(Level.FINE,"Setting key=" +name +"@"+super.getName()+":"+currentTimeStamp);
+        }
+        super.set(name, object);
+    }
+    
+    public long getEvictionWakeUpTime() {
+        return evictionWakeUpTime;
+    }
+
+    public Map<String, Long> getEvictionMap() {
+        return evictionMap;
+    }
+
+    public boolean isEvictionEnabled() {
+        return evictionEnabled;
+    }
+
+    public void setEvictionEnabled(boolean evictionEnabled) {
+        this.evictionEnabled = evictionEnabled;
+    }
+
+    public long getEntryEvictionTime() {
+        return entryEvictionTime;
+    }
+
+    public void setEntryEvictionTime(long entryEvictionTime) {
+        this.entryEvictionTime = entryEvictionTime;
+    }
+
+    public void setEvictionWakeUpTime(long evictionWakeUpTime) {
+        this.evictionWakeUpTime = evictionWakeUpTime;
+    }
+    
+
+}

Added: labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/EvictionJob.java
===================================================================
--- labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/EvictionJob.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/EvictionJob.java	2010-11-12 17:38:04 UTC (rev 35962)
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2010 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+
+package org.drools.grid.impl;
+
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import org.drools.command.impl.ContextImplWithEviction;
+import org.drools.time.Job;
+import org.drools.time.JobContext;
+
+/**
+ *
+ * @author salaboy
+ */
+public class EvictionJob implements Job {
+    private static Logger log = Logger.getLogger( EvictionJob.class.getName() );
+    private ContextImplWithEviction contextImpl;
+
+    public EvictionJob(ContextImplWithEviction contextImpl) {
+        this.contextImpl = contextImpl;
+    }
+    
+    
+    
+    public void execute(JobContext ctx) {
+           Map<String, Long> evictionMap = contextImpl.getEvictionMap();
+            for(String key : evictionMap.keySet()){
+                Long lastTimeAccessed = evictionMap.get(key);
+                long validTime = (lastTimeAccessed + contextImpl.getEntryEvictionTime());
+                
+                long evicted = validTime - System.currentTimeMillis();
+                if( evicted < 0 ){
+                    log.log(Level.FINE, "Removing _TEMP_ key ="+key+" Based on evictions policies / Evicted Time="+evicted);
+                    evictionMap.remove(key);
+                    contextImpl.remove(key);
+                    
+                }
+            }    
+    }
+
+}

Modified: labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/GridNodeServer.java
===================================================================
--- labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/GridNodeServer.java	2010-11-12 17:11:00 UTC (rev 35961)
+++ labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/impl/GridNodeServer.java	2010-11-12 17:38:04 UTC (rev 35962)
@@ -45,7 +45,7 @@
         this.gnode = gnode;
         this.data = data;
     }
-
+    
     public void messageReceived(Conversation conversation,
                                 Message msg) {
         final CommandImpl cmd = (CommandImpl) msg.getBody();
@@ -90,4 +90,7 @@
                      CommandImpl cmd);
     }
 
+    public NodeData getData() {
+        return data;
+    }  
 }

Modified: labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/io/impl/NodeData.java
===================================================================
--- labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/io/impl/NodeData.java	2010-11-12 17:11:00 UTC (rev 35961)
+++ labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/main/java/org/drools/grid/io/impl/NodeData.java	2010-11-12 17:38:04 UTC (rev 35962)
@@ -5,6 +5,7 @@
 import org.drools.command.Context;
 import org.drools.command.ContextManager;
 import org.drools.command.impl.ContextImpl;
+import org.drools.command.impl.ContextImplWithEviction;
 import org.drools.grid.ContextManagerImpl;
 
 public class NodeData {
@@ -30,9 +31,10 @@
                        this );
         // Setup TEMP context, this will hold all short lived instanceId and instances
         // TODO: TEMP context should have a time/utilisation eviction queue added 
-        this.temp = new ContextImpl( TEMP,
+        this.temp = new ContextImplWithEviction( TEMP,
                                      this.contextManager,
-                                     this.root );
+                                     this.root); 
+        
         ((ContextManagerImpl) this.contextManager).addContext( this.temp );
     }
 

Added: labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/test/java/org/drools/grid/TempEvictionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/test/java/org/drools/grid/TempEvictionTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-grid/drools-grid-impl/src/test/java/org/drools/grid/TempEvictionTest.java	2010-11-12 17:38:04 UTC (rev 35962)
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2010 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+package org.drools.grid;
+
+import java.io.Serializable;
+import java.util.Date;
+import org.drools.grid.impl.GridNodeServer;
+import org.drools.grid.io.MessageReceiverHandler;
+import org.drools.grid.impl.GridNodeImpl;
+import java.util.Map;
+import java.util.HashMap;
+import org.drools.command.impl.ContextImplWithEviction;
+import org.drools.grid.impl.EvictionJob;
+import org.drools.time.Trigger;
+import org.drools.time.impl.JDKTimerService;
+import org.junit.Assert;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author salaboy
+ */
+public class TempEvictionTest {
+
+    private Map<String, GridServiceDescription> coreServicesMap;
+
+    public TempEvictionTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    @Before
+    public void setUp() {
+
+
+        coreServicesMap = new HashMap<String, GridServiceDescription>();
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+
+    @Test
+    public void simpleEvictionTest() throws InterruptedException {
+        MessageReceiverHandler handler = new GridNodeImpl("myNode").getMessageReceiverHandler();
+        ContextImplWithEviction contextTemp = (ContextImplWithEviction) ((GridNodeServer) handler).getData().getTemp();
+        ((ContextImplWithEviction) contextTemp).setEntryEvictionTime(2000); // 2 seconds 
+        ((ContextImplWithEviction) contextTemp).setEvictionWakeUpTime(1000); // 1 seconds
+        JDKTimerService timer = new JDKTimerService(1);
+
+        contextTemp.set("myvalue", "value");
+
+
+        Long evictionWakeUpTime = contextTemp.getEvictionWakeUpTime();
+
+
+        timer.scheduleJob(new EvictionJob(contextTemp), null, new MockTrigger(new Date(), evictionWakeUpTime));
+        //Set the timestamp for the first time
+        contextTemp.set("myvalue", "value");
+
+        Thread.sleep(1000);
+        
+        //Update the timestamp
+        String value = (String) contextTemp.get("myvalue");
+
+        //Wait for eviction
+        Thread.sleep(4000);
+
+        Assert.assertNull((String) contextTemp.get("myvalue"));
+
+
+    }
+
+    public static class MockTrigger
+            implements
+            Trigger,
+            Serializable {
+
+        private Date date;
+        private Date nextTime;
+        private long delay;
+
+        public MockTrigger() {
+        }
+
+        public MockTrigger(Date date, long delay) {
+            this.date = date;
+            this.nextTime = date;
+            this.delay = delay;
+
+        }
+
+        public Date hasNextFireTime() {
+            return this.nextTime;
+        }
+
+        public Date nextFireTime() {
+            nextTime = new Date(this.date.getTime() + delay);
+            this.date = nextTime;
+            return this.nextTime;
+
+        }
+    }
+//    @Test
+//    public void evictionTemp() throws InterruptedException {
+//        Grid grid1 = new GridImpl(new HashMap<String, Object>());
+//        configureGrid1(grid1,
+//                8000,
+//                null);
+//
+//        Grid grid2 = new GridImpl(new HashMap<String, Object>());
+//        configureGrid1(grid2,
+//                -1,
+//                grid1.get(WhitePages.class));
+//
+//        GridNode n1 = grid1.createGridNode("n1");
+//        grid1.get(SocketService.class).addService("n1", 8000, n1);
+//
+//        GridServiceDescription<GridNode> n1Gsd = grid2.get(WhitePages.class).lookup("n1");
+//        GridConnection<GridNode> conn = grid2.get(ConnectionFactoryService.class).createConnection(n1Gsd);
+//        GridNode remoteN1 = conn.connect();
+//
+//        KnowledgeBuilder kbuilder = remoteN1.get(KnowledgeBuilderFactoryService.class).newKnowledgeBuilder();
+//
+//        Assert.assertNotNull(kbuilder);
+//
+//        String rule = "package test\n"
+//                + "rule \"test\""
+//                + "  when"
+//                + "  then"
+//                + "      System.out.println(\"Rule Fired!\");"
+//                + " end";
+//
+//        kbuilder.add(new ByteArrayResource(rule.getBytes()),
+//                ResourceType.DRL);
+//
+//        KnowledgeBuilderErrors errors = kbuilder.getErrors();
+//        if (errors != null && errors.size() > 0) {
+//            for (KnowledgeBuilderError error : errors) {
+//                System.out.println("Error: " + error.getMessage());
+//
+//            }
+//            fail("KnowledgeBase did not build");
+//        }
+//
+//        KnowledgeBase kbase = remoteN1.get(KnowledgeBaseFactoryService.class).newKnowledgeBase();
+//
+//        Assert.assertNotNull(kbase);
+//
+//        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+//
+//        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
+//
+//        Assert.assertNotNull(session);
+//
+//       
+//        
+//        Thread.sleep(10000);
+//        
+//        StatefulKnowledgeSession session2 = kbase.newStatefulKnowledgeSession();
+//        Assert.assertNotNull(session2);
+//        
+//        Thread.sleep(10000);
+//        session2.insert(new NodeTests.MyObject("something"));
+//        int i = session2.fireAllRules();
+//        Assert.assertEquals(1, i);
+//        
+//        Thread.sleep(30000);
+//        
+//         remoteN1.dispose();
+//        grid1.get(SocketService.class).close();
+//
+//    }
+//
+//    private void configureGrid1(Grid grid,
+//            int port,
+//            WhitePages wp) {
+//
+//        //Local Grid Configuration, for our client
+//        GridPeerConfiguration conf = new GridPeerConfiguration();
+//
+//        //Configuring the Core Services White Pages
+//        GridPeerServiceConfiguration coreSeviceWPConf = new CoreServicesLookupConfiguration(coreServicesMap);
+//        conf.addConfiguration(coreSeviceWPConf);
+//
+//        //Configuring the Core Services Scheduler
+//        GridPeerServiceConfiguration coreSeviceSchedulerConf = new CoreServicesSchedulerConfiguration();
+//        conf.addConfiguration(coreSeviceSchedulerConf);
+//
+//        //Configuring the WhitePages 
+//        WhitePagesLocalConfiguration wplConf = new WhitePagesLocalConfiguration();
+//        wplConf.setWhitePages(wp);
+//        conf.addConfiguration(wplConf);
+//
+////        //Create a Local Scheduler
+////        SchedulerLocalConfiguration schlConf = new SchedulerLocalConfiguration( "myLocalSched" );
+////        conf.addConfiguration( schlConf );
+//
+//        if (port >= 0) {
+//            //Configuring the SocketService
+//            MultiplexSocketServiceCongifuration socketConf = new MultiplexSocketServiceCongifuration(new MultiplexSocketServerImpl("127.0.0.1",
+//                    new MinaAcceptorFactoryService(),
+//                    SystemEventListenerFactory.getSystemEventListener(),
+//                    grid));
+//            socketConf.addService(WhitePages.class.getName(), wplConf.getWhitePages(), port);
+////            socketConf.addService( SchedulerService.class.getName(), schlConf.getSchedulerService(), port );
+//
+//            conf.addConfiguration(socketConf);
+//        }
+//        conf.configure(grid);
+//
+//    }
+}



More information about the jboss-svn-commits mailing list