Author: degenaro
Date: Tue May 14 18:15:36 2019
New Revision: 1859242

URL: http://svn.apache.org/viewvc?rev=1859242&view=rev
Log:
UIMA-6033 quiesced column not created when extending from older DUCC DB restore.

Added:
    
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java
   (with props)
Modified:
    uima/uima-ducc/trunk/src/main/admin/db_util.py
    uima/uima-ducc/trunk/src/main/admin/start_ducc
    
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/RmStatePersistence.java

Modified: uima/uima-ducc/trunk/src/main/admin/db_util.py
URL: 
http://svn.apache.org/viewvc/uima/uima-ducc/trunk/src/main/admin/db_util.py?rev=1859242&r1=1859241&r2=1859242&view=diff
==============================================================================
--- uima/uima-ducc/trunk/src/main/admin/db_util.py (original)
+++ uima/uima-ducc/trunk/src/main/admin/db_util.py Tue May 14 18:15:36 2019
@@ -205,3 +205,12 @@ def configure_database(DUCC_HOME, DUCC_H
     if( db_autostart ):
         stop_database(pidfile)
     return ret
+
+def update_database(DUCC_HOME, jvm):    
+    CMD = [jvm, '-DDUCC_HOME=' + DUCC_HOME, 
'org.apache.uima.ducc.database.DbUpdate']
+    CMD = ' '.join(CMD)
+    if ( execute(CMD) != 0 ):
+        print 'Database schema update failure.'
+    #else:
+    #    print 'Database schema update success.'
+    

Modified: uima/uima-ducc/trunk/src/main/admin/start_ducc
URL: 
http://svn.apache.org/viewvc/uima/uima-ducc/trunk/src/main/admin/start_ducc?rev=1859242&r1=1859241&r2=1859242&view=diff
==============================================================================
--- uima/uima-ducc/trunk/src/main/admin/start_ducc (original)
+++ uima/uima-ducc/trunk/src/main/admin/start_ducc Tue May 14 18:15:36 2019
@@ -34,6 +34,8 @@ from ducc        import Ducc
 from ducc_util import ThreadPool
 from ducc_base import find_ducc_home
 
+import db_util as dbu
+
 class StartDucc(DuccUtil):
 
     def __init__(self):
@@ -369,6 +371,7 @@ class StartDucc(DuccUtil):
                     node = self.get_db_host()
                     com = 'database'
                     self.db_acct_start(node,com)
+                    dbu.update_database(self.DUCC_HOME, self.jvm)
                 except Exception (e):
                     # print e
                     print sys.exc_info()[0], "Can't start the database."

Added: 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java
URL: 
http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java?rev=1859242&view=auto
==============================================================================
--- 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java
 (added)
+++ 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java
 Tue May 14 18:15:36 2019
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+*/
+
+package org.apache.uima.ducc.database;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.uima.ducc.common.utils.DuccLogger;
+
+import com.datastax.driver.core.AuthProvider;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.PlainTextAuthProvider;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.SimpleStatement;
+import com.datastax.driver.core.exceptions.InvalidQueryException;
+
+/**
+ * The purpose of this class is to update the DB with additions for newer 
versions of DUCC.
+ * 
+ * For example, when upgrading from DUCC 2.2.2 to 3.0.0 the new column 
"quiesced" needs to
+ * be added to the rmnodes table.  The methodology is to call the 
RmStatePersistence class to
+ * get an alter statement, and execute it against the "old" database in order 
to modernize it.
+ * If installing 3.0.0, the column is already there.  Adding the column more 
than once is harmless.
+ * 
+ * This code was originally designed to be called by start_ducc whenever the 
database is started.
+ */
+public class DbUpdate
+{
+       private DuccLogger logger = null;
+       
+       public static final String DUCC_KEYSPACE = "ducc";
+       public static final String DUCC_DATABASE_USER = "ducc";
+       
+       private String ducc_home = System.getProperty("DUCC_HOME");
+       private String ducc_properties = 
Paths.get(ducc_home,"resources","ducc.properties").toString();
+       private String ducc_private_properties = 
Paths.get(ducc_home,"resources.private","ducc.private.properties").toString();
+       
+       public DbUpdate() {
+               if (ducc_home == null) {
+            throw new IllegalStateException("DUCC_HOME must be set as a system 
property: -DDUCC_HOME=whatever");
+        }
+       }
+       
+       public Properties getDuccProperties(String filePath) {
+               Properties properties = null;
+               try {
+                       File file = new File(filePath);
+                       FileInputStream fis = new FileInputStream(file);
+                       properties = new Properties();
+                       properties.load(fis);
+                       fis.close();
+               }
+               catch (FileNotFoundException e) {
+                       throw new IllegalStateException(e);
+               } 
+               catch (IOException e) {
+                       throw new IllegalStateException(e);
+               }
+               return properties;
+       }
+       
+       private String getDbUser(Properties properties) {
+               String retVal = null;
+               String key = "ducc.database.user";
+               retVal = properties.getProperty(key);
+               if(retVal == null) {
+                       retVal = DUCC_DATABASE_USER;
+               }
+               return retVal;
+       }
+       
+       private String getDbHost(Properties properties) {
+               String retVal = null;
+               String key = "ducc.database.host";
+               retVal = properties.getProperty(key);
+               return retVal;
+       }
+       
+       private String getDbHostList(Properties properties) {
+               String retVal = null;
+               String key = "ducc.database.host.list";
+               retVal = properties.getProperty(key);
+               if(retVal == null) {
+                       retVal = getDbHost(properties);
+               }
+               return retVal;
+       }
+       
+       private String getDbPw(Properties properties) {
+               String retVal = null;
+               String key = "db_password";
+               retVal = properties.getProperty(key);
+               return retVal;
+       }
+       
+       private void updateDb(Session session) {
+               updateDb(session, RmStatePersistence.getAlterList());
+       }
+       
+       private void updateDb(Session session, List<SimpleStatement> list) {
+               String location = "updateDb";
+               for ( SimpleStatement s : list ) {
+            doLog(location, "EXECUTE STATEMENT:", s.toString());
+            try {
+               session.execute(s);
+            }
+            catch(InvalidQueryException e) {
+               String cause = e.getMessage();
+               if(cause.contains("conflicts with an existing column")) {
+                       //OK
+               }
+               else {
+                       throw e;
+               }
+            }
+        }
+       }
+
+       private void doLog(String methodName, Object ... msg) {      
+               if ( logger == null ) {
+                       StringBuffer buf = new StringBuffer(methodName);
+                       for ( Object o : msg ) {
+                               buf.append(" ");
+                               if ( o == null ) {
+                                       buf.append("<null>");
+                               } 
+                               else {
+                                       buf.append(o.toString());
+                               } 
+                       }            
+                       System.out.println(buf);
+               } 
+               else {
+                       logger.info(methodName, null, msg);
+                       return;
+               }
+       }
+       
+       public void update() {
+               Properties propsPublic = getDuccProperties(ducc_properties);
+               Properties propsPrivate = 
getDuccProperties(ducc_private_properties);
+               String ducc_db_user = getDbUser(propsPublic);
+               String ducc_db_host_list = getDbHostList(propsPublic);
+               String[] ducc_db_host_array = 
(String[])(Arrays.asList(ducc_db_host_list.split("\\s+")).toArray());
+               String ducc_db_pw = getDbPw(propsPrivate);
+               //System.out.println(ducc_db_host_list);
+               //System.out.println(ducc_db_user);
+               //System.out.println(ducc_db_pw);
+               AuthProvider auth = new PlainTextAuthProvider(ducc_db_user, 
ducc_db_pw);
+               Cluster cluster = Cluster.builder()
+                .withAuthProvider(auth)
+                .addContactPoints(ducc_db_host_array)
+                .build();
+               Session session = cluster.connect();
+               session.execute("USE " + DUCC_KEYSPACE);
+               updateDb(session);
+               session.close();
+               cluster.close();
+       }
+       
+    public static void main(String[] args)
+    {
+        int rc = 0;
+       try {
+               DbUpdate dbAlter = new DbUpdate();
+               dbAlter.update();
+        } 
+        catch ( Throwable e ) {
+            System.out.println("Errors altering database");
+            e.printStackTrace();
+            rc = 1;
+        } 
+        System.exit(rc);
+    }
+
+}

Propchange: 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/DbUpdate.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/RmStatePersistence.java
URL: 
http://svn.apache.org/viewvc/uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/RmStatePersistence.java?rev=1859242&r1=1859241&r2=1859242&view=diff
==============================================================================
--- 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/RmStatePersistence.java
 (original)
+++ 
uima/uima-ducc/trunk/uima-ducc-database/src/main/java/org/apache/uima/ducc/database/RmStatePersistence.java
 Tue May 14 18:15:36 2019
@@ -139,6 +139,18 @@ public class RmStatePersistence
         } 
     }
 
+    /*
+     * Alter is used to update table in older DUCCs.
+     * The "quiesced" column was added in DUCC 3.0.0.
+     */
+    public static List<SimpleStatement> getAlterList() {
+       List<SimpleStatement> list = new ArrayList<SimpleStatement>();
+       StringBuffer sb;
+       sb = new StringBuffer("ALTER TABLE " + RM_NODE_TABLE + " ADD 
"+"quiesced"+" "+"boolean");
+       list.add(new SimpleStatement(sb.toString()));
+       return list;
+    }
+    
     static List<SimpleStatement> mkSchema()
        throws Exception
     {


Reply via email to