diff options
Diffstat (limited to 'mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java')
-rw-r--r-- | mdbc-server/src/main/java/org/onap/music/mdbc/StateManager.java | 60 |
1 files changed, 34 insertions, 26 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..fb39637 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 @@ -36,6 +36,7 @@ import org.onap.music.mdbc.mixins.MusicInterface.OwnershipReturn; import org.onap.music.mdbc.ownership.OwnershipAndCheckpoint; import org.onap.music.mdbc.tables.MriReference; import org.onap.music.mdbc.tables.MusicTxDigestDaemon; +import org.onap.music.mdbc.tables.MusicTxDigestId; import org.onap.music.mdbc.tables.TxCommitProgress; import java.io.IOException; @@ -92,7 +93,7 @@ public class StateManager { /** a set of ranges that should be periodically updated with latest information, if null all tables should be warmed up */ private Set<Range> rangesToWarmup; /** map of transactions that have already been applied/updated in this sites SQL db */ - private Map<Range, Pair<MriReference, Integer>> alreadyApplied; + private Map<Range, Pair<MriReference, MusicTxDigestId>> alreadyApplied; private OwnershipAndCheckpoint ownAndCheck; private Thread txDaemon ; @@ -114,6 +115,7 @@ public class StateManager { //\fixme this might not be used, delete? try { info.load(this.getClass().getClassLoader().getResourceAsStream("music.properties")); + info.load(this.getClass().getClassLoader().getResourceAsStream("key.properties")); info.putAll(MDBCUtils.getMdbcProperties()); } catch (IOException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); @@ -122,13 +124,17 @@ public class StateManager { cassandraUrl = info.getProperty(Configuration.KEY_CASSANDRA_URL, Configuration.CASSANDRA_URL_DEFAULT); musicmixin = info.getProperty(Configuration.KEY_MUSIC_MIXIN_NAME, Configuration.MUSIC_MIXIN_DEFAULT); + String writeLocksOnly = info.getProperty(Configuration.KEY_WRITE_LOCKS_ONLY); + MDBCUtils.writeLocksOnly = (writeLocksOnly==null) ? Configuration.WRITE_LOCK_ONLY_DEFAULT : Boolean.parseBoolean(writeLocksOnly); + initMusic(); - initSqlDatabase(); - initTxDaemonThread(); + Map<Range, Pair<MriReference, MusicTxDigestId>> alreadyApplied = initSqlDatabase(); + String t = info.getProperty(Configuration.KEY_OWNERSHIP_TIMEOUT); long timeout = (t == null) ? Configuration.DEFAULT_OWNERSHIP_TIMEOUT : Integer.parseInt(t); - alreadyApplied = new ConcurrentHashMap<>(); ownAndCheck = new OwnershipAndCheckpoint(alreadyApplied, timeout); + + initTxDaemonThread(); } protected String cleanSqlUrl(String url){ @@ -160,7 +166,12 @@ public class StateManager { this.mdbcConnections = new HashMap<>(); } - protected void initSqlDatabase() throws MDBCServiceException { + /** + * Do everything necessary to initialize the sql database + * @return the current checkpoint location of this database, if restarting + * @throws MDBCServiceException + */ + protected Map<Range, Pair<MriReference, MusicTxDigestId>> initSqlDatabase() throws MDBCServiceException { if(!this.sqlDBUrl.toLowerCase().startsWith("jdbc:postgresql")) { try { Connection sqlConnection = DriverManager.getConnection(this.sqlDBUrl, this.info); @@ -178,16 +189,21 @@ public class StateManager { } } - // Verify the tables in MUSIC match the tables in the database - // and create triggers on any tables that need them + Map<Range, Pair<MriReference, MusicTxDigestId>> alreadyAppliedToDb = null; try { MdbcConnection mdbcConn = (MdbcConnection) openConnection("init"); mdbcConn.initDatabase(); + alreadyAppliedToDb = mdbcConn.getDBInterface().getCheckpointLocations(); closeConnection("init"); } catch (QueryException e) { - logger.error("Error syncrhonizing tables"); + logger.error("Error initializing sql database tables"); logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.QUERYERROR, ErrorTypes.QUERYERROR, ErrorSeverity.CRITICAL); } + + if (alreadyAppliedToDb==null) { + alreadyAppliedToDb = new ConcurrentHashMap<>(); + } + return alreadyAppliedToDb; } /** @@ -309,11 +325,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 +348,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 +422,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); } } |