From: <bsc...@us...> - 2007-11-13 15:16:56
|
Revision: 1939 http://unicore.svn.sourceforge.net/unicore/?rev=1939&view=rev Author: bschuller Date: 2007-11-13 07:16:54 -0800 (Tue, 13 Nov 2007) Log Message: ----------- add mysql based persistence Modified Paths: -------------- wsrflite/wsrfcore/trunk/pom.xml wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/DefaultHome.java wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/WSRFInstanceImpl.java wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/SimpleLoadTest.java Added Paths: ----------- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/MySQLPersist.java wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/MySQLPersistTest.java Modified: wsrflite/wsrfcore/trunk/pom.xml =================================================================== --- wsrflite/wsrfcore/trunk/pom.xml 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/pom.xml 2007-11-13 15:16:54 UTC (rev 1939) @@ -31,8 +31,8 @@ </contributor> </contributors> <scm> - <connection>scm:svn:https://unicore.svn.sourceforge.net/svnroot/unicore/wsrflite/wsrfcore/tags/version_1_8_4</connection> - <developerConnection>scm:svn:https://unicore.svn.sourceforge.net/svnroot/unicore/wsrflite/wsrfcore/tags/version_1_8_4</developerConnection> + <connection>scm:svn:https://unicore.svn.sourceforge.net/svnroot/unicore/wsrflite/wsrfcore/trunk</connection> + <developerConnection>scm:svn:https://unicore.svn.sourceforge.net/svnroot/unicore/wsrflite/wsrfcore/trunk</developerConnection> </scm> <build> <plugins> @@ -149,6 +149,12 @@ <version>1.8.0.5</version> </dependency> <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>5.1.5</version> + <optional>true</optional> + </dependency> + <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.2</version> Modified: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/DefaultHome.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/DefaultHome.java 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/DefaultHome.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -250,6 +250,7 @@ public void destroyWSRFServiceInstance(String resourceId) throws Exception{ serviceInstances.remove(resourceId); + terminationTimes.remove(resourceId); } public long getNumberOfInstances(){ Modified: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/WSRFInstanceImpl.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/WSRFInstanceImpl.java 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/impl/WSRFInstanceImpl.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -146,10 +146,10 @@ public void setTerminationTime(Calendar newTT){ try{ + terminationTime=newTT; home.setTerminationTime(uniqueID,newTT); - terminationTime=newTT; }catch(Exception e){ - logger.log(Level.WARNING,"error setting tt",e); + logger.warning("Error setting tt (null home?)"); } } Modified: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/EhCache.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -25,6 +25,8 @@ public static String configName="de.fzj.unicore.wsrflite.persistence.cacheconfig"; + public static String DEACTIVATE_CACHE="de.fzj.unicore.wsrflite.persistence.cache.deactivate"; + /** * default config for ehcache</br> * This is an in-memory cache only, @@ -47,6 +49,19 @@ * @return */ public static EhCache createCache(String id) { + if(Kernel.getKernel().getProperty(DEACTIVATE_CACHE)!=null)return new EhCache(id){ + @Override + public void put(WSRFInstance inst) { + } + @Override + public WSRFInstance read(String uniqueID) { + return null; + } + @Override + public void remove(String id) { + } + }; + return new EhCache(id); } Added: wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/MySQLPersist.java =================================================================== --- wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/MySQLPersist.java (rev 0) +++ wsrflite/wsrfcore/trunk/src/main/java/de/fzj/unicore/wsrflite/persistence/MySQLPersist.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -0,0 +1,288 @@ +package de.fzj.unicore.wsrflite.persistence; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.logging.Level; +import java.util.logging.Logger; + +import de.fzj.unicore.wsrflite.Kernel; + + +/** + * use MySQL as persistence backend + * + * + * @author daivandy + */ +public class MySQLPersist extends AbstractStore { + + protected static Logger logger=Logger.getLogger(MySQLPersist.class.getName()); + + protected Connection conn; + + private PreparedStatement psInsert; + private PreparedStatement psUpdate; + private PreparedStatement psDelete; + private PreparedStatement psRead; + + public static final String JDBC_DRIVER="mysql.jdbcDriver"; + public static final String DATABASE="mysql.dbName"; + public static final String HOST="mysql.host"; + public static final String USERNAME="mysql.user"; + public static final String PASSWORD="mysql.pass"; + + + private String sqlInsert, sqlUpdate, sqlDelete, sqlRead, sqlHost, dbName, sqlUser, sqlPass; + + protected String jdbcDriver; + + protected String shutdownStatement="SHUTDOWN"; + + public static final String clearDBOnStartup="de.fzj.unicore.wsrflite.mysqldb.clearAllOnStartup"; + + + protected String createConnString(String serviceName){ + this.serviceName=serviceName; + + String conn = "jdbc:mysql://"+sqlHost+"/"+dbName; + logger.info("Connecting to: "+conn); + return conn; + } + + protected String getUserName(){ + return ""; + } + + protected String getPassword(){ + return ""; + } + + protected void createTablesAndPreparedStatements(){ + try { + if("true".equalsIgnoreCase(System.getProperty(clearDBOnStartup))){ + //clear things on startup + try{ + Statement s=conn.createStatement(); + s.execute("DROP TABLE "+serviceName); + s.close(); + }catch(SQLException e){ + } + } + + String sql= "CREATE TABLE "+serviceName+"(id VARCHAR(255) PRIMARY KEY, data BLOB, transId INT(255)) TYPE=INNODB"; + Statement s=conn.createStatement(); + s.executeUpdate(sql); + s.close(); + } catch (SQLException e) { + //ignore + } + try { + sqlUpdate = "UPDATE "+serviceName+" SET data=? WHERE id=?"; + sqlInsert = "INSERT INTO "+serviceName+"(id,data) VALUES (?,?)"; + sqlDelete = "DELETE FROM "+serviceName+" WHERE id=?"; + sqlRead = "SELECT data FROM "+serviceName+" WHERE id=?"; + + psUpdate=conn.prepareStatement(sqlUpdate); + psInsert=conn.prepareStatement(sqlInsert); + psDelete=conn.prepareStatement(sqlDelete); + psRead=conn.prepareStatement(sqlRead); + + } catch (SQLException e) { + logger.log(Level.SEVERE,"Could not create statement(s).",e); + } + + } + + + @Override + public void init(String serviceName) { + super.init(serviceName); + + //create connection + try { + // needs to be replaced by System.setProperty() + jdbcDriver = Kernel.getKernel().getProperty(JDBC_DRIVER, "com.mysql.jdbc.Driver"); + dbName = Kernel.getKernel().getProperty(DATABASE, "wsrflite"); + sqlHost = Kernel.getKernel().getProperty(HOST); + sqlUser =Kernel.getKernel().getProperty(USERNAME); + sqlPass = Kernel.getKernel().getProperty(PASSWORD); + + Class.forName(jdbcDriver); + + conn=DriverManager.getConnection(createConnString(serviceName), sqlUser, sqlPass); + + conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + + //create tables if not existing + createTablesAndPreparedStatements(); + getUniqueIDs(); + } + catch (Exception e) { + logger.log(Level.SEVERE,"Error initialising database for <"+serviceName+">",e); + throw new RuntimeException(e); + } + } + + + /** + * shutdown the db engine + */ + public void shutdown() { + try{ + logger.info("Shutting down persistence for <"+serviceName+">"); + Statement s=conn.createStatement(); + s.execute(shutdownStatement); + }catch(Exception e){ + + } + finally{ + try{ conn.close(); }catch(Exception e){} + } + } + + + /** + * query the db --- not part of the Store contract, but may be used from admin interfaces + * + * @param sql + * @return ResultSet + */ + protected ResultSet query(String sql){ + try { + Statement s=conn.createStatement(); + ResultSet r=s.executeQuery(sql); + //s.close(); //TODO + return r; + } catch (SQLException e) { + logger.log(Level.SEVERE,"Error executing "+sql,e); + return null; + } + } + + /** + * execute a blob of SQL + * @param sql + * @return + */ + protected boolean execute(String sql){ + try { + Statement s=conn.createStatement(); + s.executeUpdate(sql); + s.close(); + return true; + } catch (SQLException e) { + logger.log(Level.SEVERE,"Error executing "+sql,e); + return false; + } + } + + protected void _persist(WSRFInstanceBean bean)throws SQLException, IOException{ + + String uniqueId=bean.uniqueID; + + if(uniqueIDs.contains(uniqueId)) {//update + + + /* MySQL specific solution with ByteStream/BLOB */ + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out; + + out = new ObjectOutputStream(baos); + out.writeObject(bean); + out.close(); + + byte[] beanAsByteObject = baos.toByteArray(); + + psUpdate.setObject(1,beanAsByteObject); + psUpdate.setString(2,uniqueId); + + psUpdate.execute(); + + if(logger.isLoggable(Level.FINEST)){ + logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" updated."); + } + } + else{ //insert + + // MySQL specific solution with ByteStream/BLOB + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out; + + out = new ObjectOutputStream(baos); + out.writeObject(bean); + out.close(); + + byte[] beanAsByteObject = baos.toByteArray(); + + psInsert.setObject(2,beanAsByteObject); + psInsert.setString(1,uniqueId); + + psInsert.execute(); + + uniqueIDs.add(uniqueId); + if(logger.isLoggable(Level.FINEST)){ + logger.finest("["+serviceName+"] "+psInsert.getUpdateCount()+" inserted."); + } + } + } + + @Override + protected void _getUniqueIDs() { + try { + ResultSet r=query("SELECT id from "+serviceName); + + while(r.next()){ + uniqueIDs.add(r.getString(1)); + } + } catch (SQLException e) { + logger.log(Level.WARNING,"Error getting id list",e); + } + + } + + protected WSRFInstanceBean _read(String uniqueID) { + try{ + //logger.info("Read "+uniqueID); + + /* MySQL specific solution with ByteStream/BLOB */ + ByteArrayInputStream bis; + ObjectInputStream ois; + + + psRead.setString(1,uniqueID); + ResultSet r=psRead.executeQuery(); + if(r.next()){ + byte[] bytes = r.getBytes(1); + bis = new ByteArrayInputStream(bytes); + ois = new ObjectInputStream(bis); + WSRFInstanceBean bean=(WSRFInstanceBean) ois.readObject(); + return bean; + } + else logger.log(Level.INFO, "Not found: "+uniqueID); + }catch(Exception e){ + logger.log(Level.SEVERE,"Error",e); + } + return null; + } + + protected void _remove(String uniqueID){ + try{ + psDelete.setString(1,uniqueID); + psDelete.execute(); + uniqueIDs.remove(uniqueID); + logger.finest("Removed "+uniqueID); + }catch(Exception e){ + logger.log(Level.WARNING,"Error",e); + } + } + +} Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/LoadTestPersistence.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -79,7 +79,7 @@ assertNotNull(w1); assertTrue(w1.getUniqueID().equals(keys.get(i))); Calendar c1=(Calendar)w1.getResourcePropertyObject(WSResourceImpl.RPterminationTimeQName); - assertEquals(c1,c); + assertEquals(c,c1); } long time=System.currentTimeMillis()-start; int i=p.getUniqueIDs().length; Added: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/MySQLPersistTest.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/MySQLPersistTest.java (rev 0) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/MySQLPersistTest.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -0,0 +1,111 @@ +package de.fzj.unicore.wsrflite.functional; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +import junit.framework.TestCase; +import de.fzj.unicore.wsrflite.Home; +import de.fzj.unicore.wsrflite.Kernel; +import de.fzj.unicore.wsrflite.persistence.IPersistenceProperties; +import de.fzj.unicore.wsrflite.persistence.MySQLPersist; +import de.fzj.unicore.wsrflite.persistence.PersistenceManager; +import de.fzj.unicore.wsrflite.persistence.PersistenceSettings; +import de.fzj.unicore.wsrflite.xfire.XFireKernel; +import de.fzj.unicore.wsrflite.xmlbeans.impl.MockWSResourceImpl; +import de.fzj.unicore.wsrflite.xmlbeans.impl.WSResourceHomeImpl; +import de.fzj.unicore.wsrflite.xmlbeans.impl.WSResourceImpl; + +public class MySQLPersistTest extends TestCase { + + protected void setUp(){ + Logger.getLogger("de").setLevel(Level.SEVERE); + Logger.getLogger("org").setLevel(Level.SEVERE); + Logger.getLogger("com").setLevel(Level.SEVERE); + Kernel.getKernel().setProperty(IPersistenceProperties.WSRF_PERSIST_CLASSNAME,MySQLPersist.class.getName()); + Kernel.getKernel().setProperty(XFireKernel.PERSISTENT+"."+"wsrf", "true"); + System.setProperty(MySQLPersist.clearDBOnStartup,"true"); + + // setting up the mysql configuration + Kernel.getKernel().setProperty(MySQLPersist.JDBC_DRIVER, "com.mysql.jdbc.Driver"); + Kernel.getKernel().setProperty(MySQLPersist.HOST, "localhost"); + Kernel.getKernel().setProperty(MySQLPersist.DATABASE, "wsrflite"); + Kernel.getKernel().setProperty(MySQLPersist.USERNAME, "wsrflite"); + Kernel.getKernel().setProperty(MySQLPersist.PASSWORD, "test123"); +} + + public void test1()throws Exception{ + int n=10; + ArrayList<String> keys=new ArrayList<String>(); + PersistenceManager.init(); + PersistenceSettings ps=PersistenceSettings.get(MockWSResourceImpl.class); + MySQLPersist p=(MySQLPersist)PersistenceManager.getPersist("wsrf", ps); + Calendar c=new GregorianCalendar(2071,3,30); + Home home=new WSResourceHomeImpl(); + for(int i=0;i<n;i++){ + WSResourceImpl w1=new MockWSResourceImpl(); + w1.setHome(home); + HashMap<String,Object> map=new HashMap<String,Object>(); + map.put(WSResourceImpl.INIT_INITIAL_TERMINATION_TIME,c); + w1.initialise("wsrf",map); + p.persist(w1); + keys.add(w1.getUniqueID()); + } + + for(int i=0;i<n;i++){ + WSResourceImpl w1=(WSResourceImpl)p.read(keys.get(i)); + assertNotNull(w1); + assertTrue(w1.getUniqueID().equals(keys.get(i))); + Calendar c1=(Calendar)w1.getResourcePropertyObject(WSResourceImpl.RPterminationTimeQName); + assertEquals(c,c1); + } + assertTrue(p.getUniqueIDs().length==n); + + PersistenceSettings ps2=PersistenceManager.getPersistenceSettings(MockWSResourceImpl.class); + assertNotNull(ps2); + + p.shutdown(); + } + + public void testLoadOnce()throws Exception{ + int n=10; + ArrayList<String> keys=new ArrayList<String>(); + PersistenceManager.init(); + MySQLPersist p=new MySQLPersist(); + System.setProperty(MySQLPersist.clearDBOnStartup,"true"); + p.init("testy"); + //use non-default settings (load-once semantics) + PersistenceSettings ps=new PersistenceSettings(true,true,PersistenceSettings.findPersistentFields(MockWSResourceImpl.class)); + p.setPersistenceSettings(ps); + Calendar c=new GregorianCalendar(2071,3,30); + Home home=new WSResourceHomeImpl(); + for(int i=0;i<n;i++){ + WSResourceImpl w1=new MockWSResourceImpl(); + w1.setHome(home); + HashMap<String,Object> map=new HashMap<String,Object>(); + map.put(WSResourceImpl.INIT_INITIAL_TERMINATION_TIME,c); + w1.initialise("wsrf",map); + p.persist(w1); + keys.add(w1.getUniqueID()); + } + + + for(int i=0;i<n;i++){ + WSResourceImpl w1=(WSResourceImpl)p.read(keys.get(i)); + assertNotNull(w1); + assertTrue(w1.getUniqueID().equals(keys.get(i))); + Calendar c1=(Calendar)w1.getResourcePropertyObject(WSResourceImpl.RPterminationTimeQName); + assertEquals(c1,c); + WSResourceImpl w2=(WSResourceImpl)p.read(keys.get(i)); + assertNotNull(w2); + assertEquals(w1,w2); + } + assertTrue(p.getUniqueIDs().length==n); + p.shutdown(); + } + + +} Modified: wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/SimpleLoadTest.java =================================================================== --- wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/SimpleLoadTest.java 2007-11-13 11:43:37 UTC (rev 1938) +++ wsrflite/wsrfcore/trunk/src/test/java/de/fzj/unicore/wsrflite/functional/SimpleLoadTest.java 2007-11-13 15:16:54 UTC (rev 1939) @@ -29,7 +29,7 @@ private int fault=0; private List<String>uids=new ArrayList<String>(); - private static int num_instances=1000; + private static int num_instances=10; @Override protected void setUp() throws Exception { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |