From bce219cdadbad724b1c6b2704695d8adf11eb65d Mon Sep 17 00:00:00 2001 From: Fiachra Corcoran Date: Tue, 7 Aug 2018 22:19:13 +0100 Subject: DB utils update for db initialization Issue-ID: DMAAP-566 Change-Id: I3dad4059a3b192f2c54598985893b1334809d1f0 Signed-off-by: Fiachra Corcoran --- .../datarouter/authz/impl/ProvDataProvider.java | 4 +- .../datarouter/provisioning/StatisticsServlet.java | 4 +- .../datarouter/provisioning/SynchronizerTask.java | 2 +- .../datarouter/provisioning/beans/LOGJSONable.java | 2 +- .../dmaap/datarouter/provisioning/utils/DB.java | 597 +++------------------ .../provisioning/utils/LogfileLoader.java | 71 +-- 6 files changed, 69 insertions(+), 611 deletions(-) (limited to 'datarouter-prov/src/main/java') diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvDataProvider.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvDataProvider.java index 580fe99d..0357d174 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvDataProvider.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/authz/impl/ProvDataProvider.java @@ -52,14 +52,14 @@ public interface ProvDataProvider { /** Get the identity of the owner of a feed by group id - Rally : US708115 * - * @param feedid, user the ID of the feed whose owner is being looked up. + * @param feedId, user the ID of the feed whose owner is being looked up. * @return the feed owner's identity by group. */ public String getGroupByFeedGroupId(String owner, String feedId); /** Get the identity of the owner of a sub by group id Rally : US708115 * - * @param subid, user the ID of the feed whose owner is being looked up. + * @param subId, user the ID of the feed whose owner is being looked up. * @return the feed owner's identity by group. */ public String getGroupBySubGroupId(String owner, String subId); diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java index ff0218fd..b7030504 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java @@ -356,8 +356,8 @@ public class StatisticsServlet extends BaseServlet { /** * queryGeneretor - Generating sql query - * @exception SQL Query parse exception. - * @param Map as key value pare of all user input fields + * @exception ParseException + * @param map as key value pare of all user input fields */ public String queryGeneretor(Map map) throws ParseException{ diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java index 1e5751a3..2e0fc21c 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/SynchronizerTask.java @@ -83,7 +83,7 @@ import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities; *
    *
  1. Checking DNS once per minute to see which POD the DNS CNAME points to. The CNAME will point to * the active (master) POD.
  2. - *
  3. On non-master (standby) PODs, fetches provisioning data and logs in order to keep MySQL in sync.
  4. + *
  5. On non-master (standby) PODs, fetches provisioning data and logs in order to keep MariaDB in sync.
  6. *
  7. Providing information to other parts of the system as to the current role (ACTIVE, STANDBY, UNKNOWN) * of this POD.
  8. *
diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java index 20dde64b..53897a73 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/LOGJSONable.java @@ -27,7 +27,7 @@ package org.onap.dmaap.datarouter.provisioning.beans; import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; /** - * An object that can be represented as a {@link JSONObject}. + * An object that can be represented as a {@link LOGJSONObject}. * @author Robert Eby * @version $Id: JSONable.java,v 1.1 2013/04/26 21:00:26 eby Exp $ */ diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java index 01605718..98cc824b 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/DB.java @@ -24,32 +24,11 @@ package org.onap.dmaap.datarouter.provisioning.utils; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.LineNumberReader; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.NoSuchElementException; -import java.util.Properties; -import java.util.Queue; -import java.util.Set; - import org.apache.log4j.Logger; -import org.onap.dmaap.datarouter.provisioning.beans.DeliveryRecord; -import org.onap.dmaap.datarouter.provisioning.beans.ExpiryRecord; -import org.onap.dmaap.datarouter.provisioning.beans.Loadable; -import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord; + +import java.io.*; +import java.sql.*; +import java.util.*; /** * Load the DB JDBC driver, and manage a simple pool of connections to the DB. @@ -59,15 +38,14 @@ import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord; */ public class DB { /** The name of the properties file (in CLASSPATH) */ - public static final String CONFIG_FILE = "provserver.properties"; + private static final String CONFIG_FILE = "provserver.properties"; - private static String DB_DRIVER = "com.mysql.jdbc.Driver"; - private static String DB_URL = "jdbc:mysql://127.0.0.1:3306/datarouter"; - private static String DB_LOGIN = "datarouter"; - private static String DB_PASSWORD = "datarouter"; + private static String DB_URL; + private static String DB_LOGIN; + private static String DB_PASSWORD; private static Properties props; private static Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); - private static Queue queue = new LinkedList(); + private static final Queue queue = new LinkedList<>(); public static String HTTPS_PORT; public static String HTTP_PORT; @@ -80,29 +58,23 @@ public class DB { public DB() { if (props == null) { props = new Properties(); - InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE); - try { + try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) { props.load(inStream); - DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver"); - DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url"); - DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login"); + String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver"); + DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url"); + DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login"); DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password"); HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port"); HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port"); Class.forName(DB_DRIVER); } catch (IOException e) { - intlogger.fatal("PROV9003 Opening properties: "+e.getMessage()); + intlogger.fatal("PROV9003 Opening properties: " + e.getMessage()); e.printStackTrace(); System.exit(1); } catch (ClassNotFoundException e) { - intlogger.fatal("PROV9004 cannot find the DB driver: "+e); + intlogger.fatal("PROV9004 cannot find the DB driver: " + e); e.printStackTrace(); System.exit(1); - } finally { - try { - inStream.close(); - } catch (IOException e) { - } } } } @@ -120,41 +92,40 @@ public class DB { */ @SuppressWarnings("resource") public Connection getConnection() throws SQLException { - Connection c = null; - while (c == null) { + Connection connection = null; + while (connection == null) { synchronized (queue) { try { - c = queue.remove(); - } catch (NoSuchElementException e) { + connection = queue.remove(); + } catch (NoSuchElementException nseEx) { int n = 0; do { // Try up to 3 times to get a connection try { - c = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD); - } catch (SQLException e1) { + connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD); + } catch (SQLException sqlEx) { if (++n >= 3) - throw e1; + throw sqlEx; } - } while (c == null); + } while (connection == null); } } - if (c != null && !c.isValid(1)) { - c.close(); - c = null; + if (connection != null && !connection.isValid(1)) { + connection.close(); + connection = null; } } - return c; + return connection; } /** * Returns a JDBC connection to the pool. - * @param c the Connection to return - * @throws SQLException + * @param connection the Connection to return */ - public void release(Connection c) { - if (c != null) { + public void release(Connection connection) { + if (connection != null) { synchronized (queue) { - if (!queue.contains(c)) - queue.add(c); + if (!queue.contains(connection)) + queue.add(connection); } } } @@ -165,499 +136,51 @@ public class DB { * @return true if all retrofits worked, false otherwise */ public boolean runRetroFits() { - return retroFit1() - && retroFit2() - && retroFit3() - && retroFit4() - && retroFit5() - && retroFit6() - && retroFit7() - && retroFit8() - && retroFit9() //New retroFit call to add CREATED_DATE column Rally:US674199 - 1610 - && retroFit10() //New retroFit call to add BUSINESS_DESCRIPTION column Rally:US708102 - 1610 - && retroFit11() //New retroFit call for groups feature Rally:US708115 - 1610 - ; + return retroFit1(); } + /** - * Retrofit 1 - Make sure the expected tables are in MySQL and are initialized. - * Uses mysql_init_0000 and mysql_init_0001 to setup the DB. + * Retrofit 1 - Make sure the expected tables are in DB and are initialized. + * Uses sql_init_01.sql to setup the DB. * @return true if the retrofit worked, false otherwise */ private boolean retroFit1() { - final String[] expected_tables = { - "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS", "SUBSCRIPTIONS" + final String[] expectedTables = { + "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS", + "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES", + "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS" }; - Connection c = null; + Connection connection = null; try { - c = getConnection(); - Set tables = getTableSet(c); + connection = getConnection(); + Set actualTables = getTableSet(connection); boolean initialize = false; - for (String s : expected_tables) { - initialize |= !tables.contains(s); + for (String table : expectedTables) { + initialize |= !actualTables.contains(table); } if (initialize) { intlogger.info("PROV9001: First time startup; The database is being initialized."); - runInitScript(c, 0); // script 0 creates the provisioning tables - runInitScript(c, 1); // script 1 initializes PARAMETERS - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - /** - * Retrofit 2 - if the LOG_RECORDS table is missing, add it. - * Uses mysql_init_0002 to create this table. - * @return true if the retrofit worked, false otherwise - */ - private boolean retroFit2() { - Connection c = null; - try { - // If LOG_RECORDS table is missing, add it - c = getConnection(); - Set tables = getTableSet(c); - if (!tables.contains("LOG_RECORDS")) { - intlogger.info("PROV9002: Creating LOG_RECORDS table."); - runInitScript(c, 2); // script 2 creates the LOG_RECORDS table - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - /** - * Retrofit 3 - if the FEEDS_UNIQUEID table (from release 1.0.*) exists, drop it. - * If SUBSCRIPTIONS.SUBID still has the auto_increment attribute, remove it. - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit3() { - Connection c = null; - try { - // if SUBSCRIPTIONS.SUBID still has auto_increment, remove it - boolean doremove = false; - c = getConnection(); - DatabaseMetaData md = c.getMetaData(); - ResultSet rs = md.getColumns("datarouter", "", "SUBSCRIPTIONS", "SUBID"); - if (rs != null) { - while (rs.next()) { - doremove = rs.getString("IS_AUTOINCREMENT").equals("YES"); - } - rs.close(); - rs = null; - } - if (doremove) { - intlogger.info("PROV9002: Modifying SUBSCRIPTIONS SUBID column to remove auto increment."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE SUBSCRIPTIONS MODIFY COLUMN SUBID INT UNSIGNED NOT NULL"); - s.close(); - } - - // Remove the FEEDS_UNIQUEID table, if it exists - Set tables = getTableSet(c); - if (tables.contains("FEEDS_UNIQUEID")) { - intlogger.info("PROV9002: Dropping FEEDS_UNIQUEID table."); - Statement s = c.createStatement(); - s.execute("DROP TABLE FEEDS_UNIQUEID"); - s.close(); - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - private long nextid = 0; // used for initial creation of LOG_RECORDS table. - /** - * Retrofit 4 - if old log tables exist (from release 1.0.*), copy them to LOG_RECORDS, then drop them. - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit4() { - Connection c = null; - try { - c = getConnection(); - Set tables = getTableSet(c); - if (tables.contains("PUBLISH_RECORDS")) { - intlogger.info("PROV9002: Copying PUBLISH_RECORDS to LOG_RECORDS table."); - copyLogTable("PUBLISH_RECORDS", PublishRecord.class); - intlogger.info("PROV9002: Dropping PUBLISH_RECORDS table."); - Statement s = c.createStatement(); - s.execute("DROP TABLE PUBLISH_RECORDS"); - s.close(); - } - if (tables.contains("DELIVERY_RECORDS")) { - intlogger.info("PROV9002: Copying DELIVERY_RECORDS to LOG_RECORDS table."); - copyLogTable("DELIVERY_RECORDS", DeliveryRecord.class); - intlogger.info("PROV9002: Dropping DELIVERY_RECORDS table."); - Statement s = c.createStatement(); - s.execute("DROP TABLE DELIVERY_RECORDS"); - s.close(); - } - if (tables.contains("EXPIRY_RECORDS")) { - intlogger.info("PROV9002: Copying EXPIRY_RECORDS to LOG_RECORDS table."); - copyLogTable("EXPIRY_RECORDS", ExpiryRecord.class); - intlogger.info("PROV9002: Dropping EXPIRY_RECORDS table."); - Statement s = c.createStatement(); - s.execute("DROP TABLE EXPIRY_RECORDS"); - s.close(); - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - /** - * Retrofit 5 - Create the new routing tables required for Release 2. - * Adds a new "SUSPENDED" column to FEEDS and SUBSCRIPTIONS. - * Modifies the LOG_RECORDS table to handle new R2 records. - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit5() { - final String[] expected_tables = { - "INGRESS_ROUTES", "EGRESS_ROUTES", "NETWORK_ROUTES", "NODESETS", "NODES" - }; - Connection c = null; - try { - // If expected tables are not present, then add new routing tables - c = getConnection(); - Set tables = getTableSet(c); - boolean initialize = false; - for (String s : expected_tables) { - initialize |= !tables.contains(s); - } - if (initialize) { - intlogger.info("PROV9002: Adding routing tables for Release 2.0."); - runInitScript(c, 3); // script 3 creates the routing tables - } - - // Add SUSPENDED column to FEEDS/SUBSCRIPTIONS - DatabaseMetaData md = c.getMetaData(); - for (String tbl : new String[] {"FEEDS", "SUBSCRIPTIONS" }) { - boolean add_col = true; - ResultSet rs = md.getColumns("datarouter", "", tbl, "SUSPENDED"); - if (rs != null) { - add_col = !rs.next(); - rs.close(); - rs = null; - } - if (add_col) { - intlogger.info("PROV9002: Adding SUSPENDED column to "+tbl+" table."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE "+tbl+" ADD COLUMN SUSPENDED BOOLEAN DEFAULT FALSE"); - s.close(); - } - } - - // Modify LOG_RECORDS for R2 - intlogger.info("PROV9002: Modifying LOG_RECORDS table."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE LOG_RECORDS MODIFY COLUMN TYPE ENUM('pub', 'del', 'exp', 'pbf', 'dlx') NOT NULL"); - s.close(); - s = c.createStatement(); - s.execute("ALTER TABLE LOG_RECORDS MODIFY COLUMN REASON ENUM('notRetryable', 'retriesExhausted', 'diskFull', 'other')"); - s.close(); - boolean add_col = true; - ResultSet rs = md.getColumns("datarouter", "", "LOG_RECORDS", "CONTENT_LENGTH_2"); - if (rs != null) { - add_col = !rs.next(); - rs.close(); - rs = null; + runInitScript(connection, 1); } - if (add_col) { - intlogger.info("PROV9002: Fixing two columns in LOG_RECORDS table (this may take some time)."); - s = c.createStatement(); - s.execute("ALTER TABLE LOG_RECORDS MODIFY COLUMN CONTENT_LENGTH BIGINT NOT NULL, ADD COLUMN CONTENT_LENGTH_2 BIGINT AFTER RECORD_ID"); - s.close(); - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - /** - * Retrofit 6 - Adjust LOG_RECORDS.USER to be 50 chars (MR #74). - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit6() { - Connection c = null; - try { - c = getConnection(); - // Modify LOG_RECORDS for R2 - intlogger.info("PROV9002: Modifying LOG_RECORDS.USER length."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE LOG_RECORDS MODIFY COLUMN USER VARCHAR(50)"); - s.close(); - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - /** - * Retrofit 7 - Adjust LOG_RECORDS.FEED_FILEID and LOG_RECORDS.DELIVERY_FILEID to be 256 chars. - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit7() { - Connection c = null; - try { - c = getConnection(); - // Modify LOG_RECORDS for long (>128) FILEIDs - intlogger.info("PROV9002: Modifying LOG_RECORDS.USER length."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE LOG_RECORDS MODIFY COLUMN FEED_FILEID VARCHAR(256), MODIFY COLUMN DELIVERY_FILEID VARCHAR(256)"); - s.close(); } catch (SQLException e) { intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); return false; } finally { - if (c != null) - release(c); + if (connection != null) + release(connection); } return true; } - /** - * Retrofit 8 - Adjust FEEDS.NAME to be 255 chars (MR #74). - * @return true if the retrofit worked, false otherwise - */ - @SuppressWarnings("resource") - private boolean retroFit8() { - Connection c = null; - try { - c = getConnection(); - intlogger.info("PROV9002: Modifying FEEDS.NAME length."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE FEEDS MODIFY COLUMN NAME VARCHAR(255)"); - s.close(); - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - - /** - * Retrofit 9 - Add column FEEDS.CREATED_DATE and SUBSCRIPTIONS.CREATED_DATE, 1610 release user story US674199. - * @return true if the retrofit worked, false otherwise - */ - - @SuppressWarnings("resource") - private boolean retroFit9() { - Connection c = null; - try { - c = getConnection(); - // Add CREATED_DATE column to FEEDS/SUBSCRIPTIONS tables - DatabaseMetaData md = c.getMetaData(); - for (String tbl : new String[] {"FEEDS", "SUBSCRIPTIONS" }) { - boolean add_col = true; - ResultSet rs = md.getColumns("datarouter", "", tbl, "CREATED_DATE"); - if (rs != null) { - add_col = !rs.next(); - rs.close(); - rs = null; - } - if (add_col) { - intlogger.info("PROV9002: Adding CREATED_DATE column to "+tbl+" table."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE "+tbl+" ADD COLUMN CREATED_DATE timestamp DEFAULT CURRENT_TIMESTAMP"); - s.close(); - } - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - - /** - * Retrofit 10 -Adding business BUSINESS_DESCRIPTION to FEEDS table (Rally - * US708102). - * - * @return true if the retrofit worked, false otherwise - */ - - @SuppressWarnings("resource") - private boolean retroFit10() { - Connection c = null; - boolean addColumn = true; - - try { - - c = getConnection(); - // Add BUSINESS_DESCRIPTION column to FEEDS table - DatabaseMetaData md = c.getMetaData(); - boolean add_col = true; - ResultSet rs = md.getColumns("datarouter", "", "FEEDS", "BUSINESS_DESCRIPTION"); - if (rs != null) { - add_col = !rs.next(); - rs.close(); - rs = null; - } - if(add_col) { - intlogger - .info("PROV9002: Adding BUSINESS_DESCRIPTION column to FEEDS table."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE FEEDS ADD COLUMN BUSINESS_DESCRIPTION varchar(1000) DEFAULT NULL AFTER DESCRIPTION, MODIFY COLUMN DESCRIPTION VARCHAR(1000)"); - s.close(); - } - } - catch (SQLException e) { - intlogger - .fatal("PROV9000: The database credentials are not working: " - + e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - - - /*New retroFit method is added for groups feature Rally:US708115 - 1610 - * @retroFit11() - * @parmas: none - * @return - boolean if table and fields are created (Group table, group id in FEEDS, SUBSCRIPTION TABLES) - */ - @SuppressWarnings("resource") - private boolean retroFit11() { - final String[] expected_tables = { - "GROUPS" - }; - Connection c = null; - - try { - // If expected tables are not present, then add new routing tables - c = getConnection(); - Set tables = getTableSet(c); - boolean initialize = false; - for (String s : expected_tables) { - initialize |= !tables.contains(s); - } - if (initialize) { - intlogger.info("PROV9002: Adding GROUPS table for Release 1610."); - runInitScript(c, 4); // script 4 creates the routing tables - } - - // Add GROUPID column to FEEDS/SUBSCRIPTIONS - DatabaseMetaData md = c.getMetaData(); - for (String tbl : new String[] {"FEEDS", "SUBSCRIPTIONS" }) { - boolean add_col = true; - ResultSet rs = md.getColumns("datarouter", "", tbl, "GROUPID"); - if (rs != null) { - add_col = !rs.next(); - rs.close(); - rs = null; - } - if (add_col) { - intlogger.info("PROV9002: Adding GROUPID column to "+tbl+" table."); - Statement s = c.createStatement(); - s.execute("ALTER TABLE "+tbl+" ADD COLUMN GROUPID INT(10) UNSIGNED NOT NULL DEFAULT 0 AFTER FEEDID"); - s.close(); - } - } - } catch (SQLException e) { - intlogger.fatal("PROV9000: The database credentials are not working: "+e.getMessage()); - return false; - } finally { - if (c != null) - release(c); - } - return true; - } - - - /** - * Copy the log table table_name to LOG_RECORDS; - * @param table_name the name of the old (1.0.*) table to copy - * @param table_class the class used to instantiate a record from the table - * @throws SQLException if there is a problem getting a MySQL connection - */ - @SuppressWarnings("resource") - private void copyLogTable(String table_name, Class table_class) throws SQLException { - long start = System.currentTimeMillis(); - int n = 0; - Connection c1 = getConnection(); - Connection c2 = getConnection(); - - try { - Constructor cnst = table_class.getConstructor(ResultSet.class); - PreparedStatement ps = c2.prepareStatement(LogfileLoader.INSERT_SQL); - Statement stmt = c1.createStatement(); - ResultSet rs = stmt.executeQuery("select * from "+table_name); - while (rs.next()) { - Loadable rec = cnst.newInstance(rs); - rec.load(ps); - ps.setLong(18, ++nextid); - ps.executeUpdate(); - if ((++n % 10000) == 0) - intlogger.debug(" "+n+" records done."); - } - stmt.close(); - ps.close(); - } catch (SQLException e) { - e.printStackTrace(); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (InstantiationException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - - release(c1); - release(c2); - long x = (System.currentTimeMillis() - start); - intlogger.debug(" "+n+" records done in "+x+" ms."); - } /** * Get a set of all table names in the DB. - * @param c a DB connection + * @param connection a DB connection * @return the set of table names */ - private Set getTableSet(Connection c) { + private Set getTableSet(Connection connection) { Set tables = new HashSet(); try { - DatabaseMetaData md = c.getMetaData(); + DatabaseMetaData md = connection.getMetaData(); ResultSet rs = md.getTables("datarouter", "", "", null); if (rs != null) { while (rs.next()) { @@ -672,19 +195,19 @@ public class DB { /** * Initialize the tables by running the initialization scripts located in the directory specified * by the property org.onap.dmaap.datarouter.provserver.dbscripts. Scripts have names of - * the form mysql_init_NNNN. - * @param c a DB connection - * @param n the number of the mysql_init_NNNN script to run + * the form sql_init_NN.sql + * @param connection a DB connection + * @param scriptId the number of the sql_init_NN.sql script to run */ - private void runInitScript(Connection c, int n) { - String scriptdir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts"); + private void runInitScript(Connection connection, int scriptId) { + String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts"); StringBuilder sb = new StringBuilder(); try { - String scriptfile = String.format("%s/mysql_init_%04d", scriptdir, n); - if (!(new File(scriptfile)).exists()) + String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId); + if (!(new File(scriptFile)).exists()) return; - LineNumberReader in = new LineNumberReader(new FileReader(scriptfile)); + LineNumberReader in = new LineNumberReader(new FileReader(scriptFile)); String line; while ((line = in.readLine()) != null) { if (!line.startsWith("--")) { @@ -694,7 +217,7 @@ public class DB { // Execute one DDL statement String sql = sb.toString(); sb.setLength(0); - Statement s = c.createStatement(); + Statement s = connection.createStatement(); s.execute(sql); s.close(); } diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java index 830e21c9..45b87883 100644 --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LogfileLoader.java @@ -59,7 +59,7 @@ import org.onap.dmaap.datarouter.provisioning.beans.PublishRecord; /** * This class provides methods that run in a separate thread, in order to process logfiles uploaded into the spooldir. - * These logfiles are loaded into the MySQL LOG_RECORDS table. In a running provisioning server, there should only be + * These logfiles are loaded into the MariaDB LOG_RECORDS table. In a running provisioning server, there should only be * two places where records can be loaded into this table; here, and in the method DB.retroFit4() which may be run at * startup to load the old (1.0) style log tables into LOG_RECORDS; *

This method maintains an {@link RLEBitSet} which can be used to easily see what records are presently in the @@ -148,7 +148,7 @@ public class LogfileLoader extends Thread { } /** * Run continuously to look for new logfiles in the spool directory and import them into the DB. - * The spool is checked once per second. If free space on the MySQL filesystem falls below + * The spool is checked once per second. If free space on the MariaDB filesystem falls below * REQUIRED_FREE_PCT (normally 20%) then the oldest logfile entries are removed and the LOG_RECORDS * table is compacted until free space rises above the threshold. */ @@ -361,72 +361,7 @@ public class LogfileLoader extends Thread { db.release(conn); } } -// OLD CODE - commented here for historical purposes -// -// private boolean pruneRecordsOldAlgorithm() { -// // Determine space available -- available space must be at least 20% under /opt/app/mysql -// int pct = getFreePercentage(); -// boolean did1 = false; -// while (pct < REQUIRED_FREE_PCT) { -// logger.info("PROV8008: Free space is " + pct + "% - removing old log entries"); -// boolean didit = removeOldestEntries(); -// pct = didit ? getFreePercentage() : 100; // don't loop endlessly -// did1 |= didit; -// } -// return did1; -// } -// private int getFreePercentage() { -// FileSystem fs = (Paths.get("/opt/app/mysql")).getFileSystem(); -// long total = 0; -// long avail = 0; -// try { -// for (FileStore store : fs.getFileStores()) { -// total += store.getTotalSpace(); -// avail += store.getUsableSpace(); -// } -// } catch (IOException e) { -// } -// try { fs.close(); } catch (Exception e) { } -// return (int)((avail * 100) / total); -// } -// private boolean removeOldestEntries() { -// // Remove the last days worth of entries -// Connection conn = null; -// try { -// conn = db.getConnection(); -// Statement stmt = conn.createStatement(); -// ResultSet rs = stmt.executeQuery("select min(event_time) as MIN from LOG_RECORDS"); -// if (rs != null) { -// if (rs.next()) { -// // Compute the end of the first day of logs -// long first = rs.getLong("MIN"); -// Calendar cal = new GregorianCalendar(); -// cal.setTime(new Date(first)); -// cal.add(Calendar.DAY_OF_YEAR, 1); -// cal.set(Calendar.HOUR_OF_DAY, 0); -// cal.set(Calendar.MINUTE, 0); -// cal.set(Calendar.SECOND, 0); -// cal.set(Calendar.MILLISECOND, 0); -// if (!stmt.execute("delete from LOG_RECORDS where event_time < " + cal.getTimeInMillis())) { -// int count = stmt.getUpdateCount(); -// logger.info("PROV0009: Removed "+count+" old log entries."); -// stmt.execute("OPTIMIZE TABLE LOG_RECORDS"); -// } -// rs.close(); -// stmt.close(); -// return true; -// } -// rs.close(); -// } -// stmt.close(); -// } catch (SQLException e) { -// System.err.println(e); -// e.printStackTrace(); -// } finally { -// db.release(conn); -// } -// return false; -// } + @SuppressWarnings("resource") private int[] process(File f) { int ok = 0, total = 0; -- cgit 1.2.3-korg