diff options
3 files changed, 91 insertions, 58 deletions
diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java b/mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java index 66c8fa9..8d42370 100644 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java @@ -309,11 +309,9 @@ public class StateManager { } mdbcConnections.remove(connectionId); } - if(connectionRanges.containsKey(connectionId)){ - //We relinquish all locks obtained by a given - //relinquish(connectionRanges.get(connectionId)); - connectionRanges.remove(connectionId); - } + + connectionRanges.remove(connectionId); + } /** @@ -334,18 +332,12 @@ public class StateManager { ErrorTypes.QUERYERROR); sqlConnection = null; } - //check if a range was already created for this connection - //TODO: later we could try to match it to some more sticky client id - DatabasePartition ranges; - if(connectionRanges.containsKey(id)){ - ranges=connectionRanges.get(id); - } - else{ - //TODO: we don't need to create a partition for each connection - ranges=new DatabasePartition(musicInterface.generateUniqueKey()); - connectionRanges.put(id,ranges); - } - //Create MDBC connection + + //TODO: later we could try to match it to some more sticky client id + DatabasePartition ranges=new DatabasePartition(musicInterface.generateUniqueKey()); + connectionRanges.put(id,ranges); + + //Create MDBC connection try { newConnection = new MdbcConnection(id,this.sqlDBUrl+"/"+this.sqlDBName, sqlConnection, info, this.musicInterface, transactionInfo,ranges, this); @@ -414,7 +406,7 @@ public class StateManager { * Close all connections for this server, relinquishing any locks/partitions owned by this server */ public void releaseAllPartitions() { - for(String connection: this.connectionRanges.keySet()) { + for(String connection: this.mdbcConnections.keySet()) { closeConnection(connection); } } diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java b/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java index ec91ceb..8cab635 100755 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/mixins/MySQLMixin.java @@ -37,7 +37,6 @@ import java.util.TreeSet; import java.util.UUID; import org.apache.commons.lang3.tuple.Pair; import org.json.JSONObject; -import org.json.JSONTokener; import org.onap.music.exceptions.MDBCServiceException; import org.onap.music.logging.EELFLoggerDelegate; import org.onap.music.mdbc.Configuration; @@ -45,7 +44,6 @@ import org.onap.music.mdbc.MDBCUtils; import org.onap.music.mdbc.Range; import org.onap.music.mdbc.TableInfo; import org.onap.music.mdbc.query.SQLOperation; -import org.onap.music.mdbc.query.SQLOperationType; import org.onap.music.mdbc.tables.Operation; import org.onap.music.mdbc.tables.StagingTable; import net.sf.jsqlparser.JSQLParserException; @@ -814,15 +812,31 @@ public class MySQLMixin implements DBInterface { private ArrayList<String> getMusicKey(String tbl, String cmd, String sql) { ArrayList<String> musicKeys = new ArrayList<String>(); /* - * if (cmd.equalsIgnoreCase("insert")) { //create key, return key musicKeys.add(msm.generatePrimaryKey()); } - * else if (cmd.equalsIgnoreCase("update") || cmd.equalsIgnoreCase("delete")) { try { - * net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sql); String where; if (stmt instanceof - * Update) { where = ((Update) stmt).getWhere().toString(); } else if (stmt instanceof Delete) { where = - * ((Delete) stmt).getWhere().toString(); } else { System.err.println("Unknown type: " +stmt.getClass()); where - * = ""; } ResultSet rs = executeSQLRead("SELECT * FROM " + tbl + " WHERE " + where); musicKeys = - * msm.getMusicKeysWhere(tbl, Utils.parseResults(getTableInfo(tbl), rs)); } catch (JSQLParserException e) { - * - * e.printStackTrace(); } catch (SQLException e) { //Not a valid sql query e.printStackTrace(); } } + if (cmd.equalsIgnoreCase("insert")) { + //create key, return key + musicKeys.add(msm.generatePrimaryKey()); + } else if (cmd.equalsIgnoreCase("update") || cmd.equalsIgnoreCase("delete")) { + try { + net.sf.jsqlparser.statement.Statement stmt = CCJSqlParserUtil.parse(sql); + String where; + if (stmt instanceof Update) { + where = ((Update) stmt).getWhere().toString(); + } else if (stmt instanceof Delete) { + where = ((Delete) stmt).getWhere().toString(); + } else { + System.err.println("Unknown type: " +stmt.getClass()); + where = ""; + } + ResultSet rs = executeSQLRead("SELECT * FROM " + tbl + " WHERE " + where); + musicKeys = msm.getMusicKeysWhere(tbl, Utils.parseResults(getTableInfo(tbl), rs)); + } catch (JSQLParserException e) { + + e.printStackTrace(); + } catch (SQLException e) { + //Not a valid sql query + e.printStackTrace(); + } + } */ return musicKeys; } @@ -939,17 +953,7 @@ public class MySQLMixin implements DBInterface { ArrayList<String> cols = new ArrayList<String>(); ArrayList<Object> vals = new ArrayList<Object>(); - Iterator<String> colIterator = jsonOp.keys(); - while (colIterator.hasNext()) { - String col = colIterator.next(); - // FIXME: should not explicitly refer to cassandramixin - if (col.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { - // reserved name - continue; - } - cols.add(col); - vals.add(jsonOp.get(col)); - } + constructColValues(jsonOp, cols, vals); // build and replay the queries StringBuilder sql = constructSQL(op, cols, vals); @@ -965,7 +969,11 @@ public class MySQLMixin implements DBInterface { logger.warn("Error Replaying operation: " + sql.toString() + "; Replacing insert/replace/viceversa and replaying "); - buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); + try { + buildAndExecuteSQLInverse(jdbcStmt, op, cols, vals); + } catch (Exception e) { + logger.warn(" Error replaying inverse operation; " + sql + "Ignore the exception"); + } } } catch (SQLException sqlE) { // This applies for replaying transactions involving Eventually Consistent tables @@ -977,6 +985,20 @@ public class MySQLMixin implements DBInterface { } } + public void constructColValues(JSONObject jsonOp, ArrayList<String> cols, + ArrayList<Object> vals) { + Iterator<String> colIterator = jsonOp.keys(); + while(colIterator.hasNext()) { + String col = colIterator.next(); + //FIXME: should not explicitly refer to cassandramixin + if (col.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { + //reserved name + continue; + } + cols.add(col); + vals.add(jsonOp.get(col)); + } + } protected void buildAndExecuteSQLInverse(Statement jdbcStmt, Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws SQLException, MDBCServiceException { @@ -999,7 +1021,7 @@ public class MySQLMixin implements DBInterface { * @throws MDBCServiceException */ - protected StringBuilder constructSQLInverse(Operation op, ArrayList<String> cols, ArrayList<Object> vals) + public StringBuilder constructSQLInverse(Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws MDBCServiceException { StringBuilder sqlInverse = null; switch (op.getOperationType()) { @@ -1015,7 +1037,7 @@ public class MySQLMixin implements DBInterface { return sqlInverse; } - protected StringBuilder constructSQL(Operation op, ArrayList<String> cols, ArrayList<Object> vals) + public StringBuilder constructSQL(Operation op, ArrayList<String> cols, ArrayList<Object> vals) throws MDBCServiceException { StringBuilder sql = null; switch (op.getOperationType()) { @@ -1059,7 +1081,7 @@ public class MySQLMixin implements DBInterface { sql.append(") VALUES ("); sep = ""; for (Object val : vals) { - sql.append(sep + "\"" + val + "\""); + sql.append(sep + (val!=JSONObject.NULL?"\"" + val +"\"":"null")); sep = ", "; } sql.append(");"); @@ -1074,7 +1096,7 @@ public class MySQLMixin implements DBInterface { sql.append(r + " SET "); sep = ""; for (int i = 0; i < cols.size(); i++) { - sql.append(sep + cols.get(i) + "=\"" + vals.get(i) + "\""); + sql.append(sep + cols.get(i) + (vals.get(i)!=JSONObject.NULL?"=\"" + vals.get(i) +"\"":"=null")); sep = ", "; } sql.append(" WHERE "); @@ -1095,7 +1117,7 @@ public class MySQLMixin implements DBInterface { String and = ""; for (String key : primaryKeys.keySet()) { // We cannot use the default primary key for the sql table and operations - if (!key.equals(mi.getMusicDefaultPrimaryKeyName())) { + if(!key.equals(MusicMixin.MDBC_PRIMARYKEY_NAME)) { Object val = primaryKeys.get(key); keyCondStmt.append(and + key + "=\"" + val + "\""); and = " AND "; @@ -1162,4 +1184,4 @@ public class MySQLMixin implements DBInterface { } } -} +}
\ No newline at end of file diff --git a/mdbc-server/src/main/java/org/onap/music/mdbc/tools/TxDigestDecompression.java b/mdbc-server/src/main/java/org/onap/music/mdbc/tools/TxDigestDecompression.java index 0b422fa..6b7b7be 100644 --- a/mdbc-server/src/main/java/org/onap/music/mdbc/tools/TxDigestDecompression.java +++ b/mdbc-server/src/main/java/org/onap/music/mdbc/tools/TxDigestDecompression.java @@ -21,13 +21,15 @@ package org.onap.music.mdbc.tools; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.UUID; import org.onap.music.exceptions.MDBCServiceException; import org.onap.music.logging.EELFLoggerDelegate; -import org.onap.music.mdbc.StateManager; +import org.onap.music.mdbc.mixins.MusicInterface; import org.onap.music.mdbc.mixins.MusicMixin; +import org.onap.music.mdbc.mixins.MySQLMixin; import org.onap.music.mdbc.tables.MusicRangeInformationRow; import org.onap.music.mdbc.tables.MusicTxDigestId; import org.onap.music.mdbc.tables.Operation; @@ -42,6 +44,12 @@ import org.onap.music.mdbc.tables.StagingTable; public class TxDigestDecompression { public static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(TxDigestDecompression.class); MusicMixin mi; + MySQLMixin ms; + + public TxDigestDecompression(MusicInterface _mi) { + mi = (MusicMixin) _mi; + ms = new MySQLMixin(); + } public TxDigestDecompression() { Properties prop = new Properties(); @@ -52,6 +60,7 @@ public class TxDigestDecompression { } try { mi = new MusicMixin(null, "mdbcservername", prop); + ms = new MySQLMixin(); } catch (MDBCServiceException e) { e.printStackTrace(); return; @@ -64,16 +73,7 @@ public class TxDigestDecompression { List<MusicRangeInformationRow> rows = mi.getAllMriRows(); for (MusicRangeInformationRow row: rows) { UUID mriId = row.getPartitionIndex(); - for (MusicTxDigestId id: row.getRedoLog()) { - StagingTable st = mi.getTxDigest(id); - System.out.print(id.transactionId + ": ["); - String sep = ""; - for (Operation op: st.getOperationList()) { - System.out.print(sep + op.getOperationType() + "-" + op.getTable() + "->" + op.getVal()); - sep =", "; - } - System.out.println("]"); - } + extractedRedoLog(row); } } catch (MDBCServiceException e) { e.printStackTrace(); @@ -81,6 +81,25 @@ public class TxDigestDecompression { } System.exit(0); } + + public void extractedRedoLog(MusicRangeInformationRow row) throws MDBCServiceException { + for (MusicTxDigestId id: row.getRedoLog()) { + StagingTable st = mi.getTxDigest(id); + System.out.print(id.transactionId + ": ["); + String sep = ", "; + for (Operation op: st.getOperationList()) { + + ArrayList<String> cols = new ArrayList<String>(); + ArrayList<Object> vals = new ArrayList<Object>(); + ms.constructColValues(op.getVal(), cols, vals); + StringBuilder sql = ms.constructSQL(op, cols, vals); + + System.out.print(sql + sep); + + } + System.out.println("]"); + } + } public static void main(String[] args) { TxDigestDecompression txDecompress = new TxDigestDecompression(); |