Search in sources :

Example 26 with ChangeRecordInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo in project openmq by eclipse-ee4j.

the class ClusterSubscriptionInfo method getShareccInfo.

public ChangeRecordInfo getShareccInfo(int i) {
    if (pkt.getProp("shareccSeq" + i) == null) {
        return null;
    }
    ChangeRecordInfo cri = new ChangeRecordInfo();
    cri.setSeq((Long) pkt.getProp("shareccSeq" + i));
    cri.setUUID((String) pkt.getProp("shareccUUID" + i));
    cri.setResetUUID((String) pkt.getProp("shareccResetUUID" + i));
    cri.setType(pkt.getType());
    return cri;
}
Also used : ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 27 with ChangeRecordInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo in project openmq by eclipse-ee4j.

the class DBTool method doRestore.

/*
     * Restore the JDBC store from filebased backup files.
     */
private void doRestore() throws BrokerException {
    if (!Globals.getHAEnabled()) {
        throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
    }
    // instantiate the file store.
    Properties props = System.getProperties();
    String clusterID = Globals.getClusterID();
    String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
    logger.logToAll(Logger.INFO, "Restore persistent store for HA cluster " + clusterID + " from backup dir: " + backupDir);
    final FileStore fileStore = new FileStore(backupDir, false);
    // Brokers table.
    JDBCStore jdbcStore = null;
    try {
        // Re-create the jdbc store
        doRecreate();
        jdbcStore = (JDBCStore) StoreManager.getStore();
        /*
             * For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
             * retrieve those data from the top level directory (a.k.a cluster filestore).
             */
        // Properties table
        List haBrokers = null;
        Properties properties = fileStore.getAllProperties();
        Iterator propItr = properties.entrySet().iterator();
        while (propItr.hasNext()) {
            Map.Entry entry = (Map.Entry) propItr.next();
            String name = (String) entry.getKey();
            if (name.equals(STORE_PROPERTY_HABROKERS)) {
                // Retrieve all HABrokerInfo from a property
                haBrokers = (List) entry.getValue();
            } else {
                jdbcStore.updateProperty(name, entry.getValue(), false);
            }
        }
        propItr = null;
        properties = null;
        if (haBrokers == null || haBrokers.isEmpty()) {
            throw new BrokerException(br.getKString(BrokerResources.X_LOAD_ALL_BROKERINFO_FAILED));
        }
        // Configuration Change Record table
        List<ChangeRecordInfo> records = fileStore.getAllConfigRecords();
        for (int i = 0, len = records.size(); i < len; i++) {
            jdbcStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
        }
        records = null;
        // Consumer table
        Consumer[] consumerArray = fileStore.getAllInterests();
        for (int i = 0; i < consumerArray.length; i++) {
            jdbcStore.storeInterest(consumerArray[i], false);
        }
        consumerArray = null;
        // Broker & Session table.
        Iterator bkrItr = haBrokers.iterator();
        while (bkrItr.hasNext()) {
            HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
            jdbcStore.addBrokerInfo(bkrInfo, false);
        }
        /*
             * For each broker in the cluster, we will retrieve broker specific data, destinations, messages, transactions,
             * acknowledgements from their own filestore (a.k.a broker filestore).
             */
        bkrItr = haBrokers.iterator();
        while (bkrItr.hasNext()) {
            // Backup data for each broker
            HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
            String brokerID = bkrInfo.getId();
            long sessionID = bkrInfo.getSessionID();
            logger.logToAll(Logger.INFO, "Restore persistent data for broker " + brokerID);
            FileStore bkrFS = null;
            JMSBridgeStore jmsbridgeStore = null;
            try {
                String instanceRootDir = backupDir + File.separator + brokerID;
                bkrFS = new FileStore(instanceRootDir, false);
                // Destination table.
                Destination[] dstArray = bkrFS.getAllDestinations();
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    DestinationUID did = dstArray[i].getDestinationUID();
                    Destination dst = jdbcStore.getDestination(did);
                    if (dst == null) {
                        // Store the destination if not found
                        jdbcStore.storeDestination(dstArray[i], sessionID);
                    }
                }
                // Retrieve messages for each destination.
                for (int i = 0, len = dstArray.length; i < len; i++) {
                    for (Enumeration e = bkrFS.messageEnumeration(dstArray[i]); e.hasMoreElements(); ) {
                        DestinationUID did = dstArray[i].getDestinationUID();
                        Packet message = (Packet) e.nextElement();
                        SysMessageID mid = message.getSysMessageID();
                        // Get interest states for the message; Consumer State table
                        HashMap stateMap = bkrFS.getInterestStates(did, mid);
                        if (stateMap == null || stateMap.isEmpty()) {
                            jdbcStore.storeMessage(did, message, null, null, sessionID, false);
                        } else {
                            int size = stateMap.size();
                            ConsumerUID[] iids = new ConsumerUID[size];
                            int[] states = new int[size];
                            Iterator stateItr = stateMap.entrySet().iterator();
                            int j = 0;
                            while (stateItr.hasNext()) {
                                Map.Entry entry = (Map.Entry) stateItr.next();
                                iids[j] = (ConsumerUID) entry.getKey();
                                states[j] = ((Integer) entry.getValue()).intValue();
                                j++;
                            }
                            jdbcStore.storeMessage(did, message, iids, states, sessionID, false);
                        }
                    }
                }
                // Transaction table
                Collection txnList = bkrFS.getTransactions(brokerID);
                Iterator txnItr = txnList.iterator();
                while (txnItr.hasNext()) {
                    TransactionUID tid = (TransactionUID) txnItr.next();
                    TransactionInfo txnInfo = bkrFS.getTransactionInfo(tid);
                    TransactionAcknowledgement[] txnAck = bkrFS.getTransactionAcks(tid);
                    jdbcStore.storeTransaction(tid, txnInfo, sessionID);
                    for (int i = 0, len = txnAck.length; i < len; i++) {
                        jdbcStore.storeTransactionAck(tid, txnAck[i], false);
                    }
                }
                /**
                 ************************************************
                 * JMSBridge
                 *************************************************
                 */
                Properties bp = new Properties();
                bp.setProperty("instanceRootDir", instanceRootDir);
                bp.setProperty("reset", "false");
                bp.setProperty("logdomain", "imqdbmgr");
                jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                if (jmsbridgeStore != null) {
                    List bnames = jmsbridgeStore.getJMSBridges(null);
                    String bname = null;
                    Iterator itr = bnames.iterator();
                    while (itr.hasNext()) {
                        bname = (String) itr.next();
                        jdbcStore.addJMSBridge(bname, true, null);
                    }
                    jmsbridgeStore.closeJMSBridgeStore();
                    jmsbridgeStore = null;
                    bname = null;
                    itr = bnames.iterator();
                    while (itr.hasNext()) {
                        bname = (String) itr.next();
                        bp.setProperty("jmsbridge", bname);
                        if (jmsbridgeStore != null) {
                            jmsbridgeStore.closeJMSBridgeStore();
                            jmsbridgeStore = null;
                        }
                        logger.logToAll(logger.INFO, "Restore JMS bridge " + bname);
                        jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
                        List xids = jmsbridgeStore.getTMLogRecordKeysByName(bname, null);
                        logger.logToAll(logger.INFO, "\tRestore JMS bridge " + bname + " with " + xids.size() + " TM log records");
                        String xid = null;
                        byte[] lr = null;
                        Iterator itr1 = xids.iterator();
                        while (itr1.hasNext()) {
                            xid = (String) itr1.next();
                            lr = jmsbridgeStore.getTMLogRecord(xid, bname, null);
                            if (lr == null) {
                                logger.logToAll(Logger.INFO, "JMSBridge TM log record not found for " + xid);
                                continue;
                            }
                            jdbcStore.storeTMLogRecord(xid, lr, bname, true, null);
                        }
                    }
                }
            } finally {
                bkrFS.close();
                if (jmsbridgeStore != null) {
                    jmsbridgeStore.closeJMSBridgeStore();
                }
            }
        }
        logger.logToAll(Logger.INFO, "Restore persistent store complete.");
    } catch (Throwable ex) {
        ex.printStackTrace();
        throw new BrokerException(ex.getMessage());
    } finally {
        assert fileStore != null;
        try {
            fileStore.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StoreManager.releaseStore(true);
    }
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) JMSBridgeStore(com.sun.messaging.bridge.api.JMSBridgeStore) Packet(com.sun.messaging.jmq.io.Packet) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) FileStore(com.sun.messaging.jmq.jmsserver.persist.file.FileStore) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 28 with ChangeRecordInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo in project openmq by eclipse-ee4j.

the class DBTool method doRestoreSharecc.

/**
 * Restore the shared table for cluster config change record from a file
 */
private void doRestoreSharecc() throws BrokerException {
    Properties props = System.getProperties();
    String clusterID = Globals.getClusterID();
    String backupfile = (String) props.get(ShareConfigChangeStore.CLUSTER_SHARECC_PROP_PREFIX + ".backupfile");
    logger.logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE, clusterID, backupfile));
    ShareConfigChangeDBManager mgr = ShareConfigChangeDBManager.getDBManager();
    Connection conn = null;
    Exception myex = null;
    try {
        List<ChangeRecordInfo> records = ChangeRecord.prepareRestoreRecords(backupfile);
        ShareConfigRecordDAO dao = mgr.getDAOFactory().getShareConfigRecordDAO();
        Globals.getLogger().logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE_RECORDS, String.valueOf(records.size()), backupfile));
        conn = mgr.getConnection(true);
        ChangeRecordInfo resetcri = ChangeRecord.makeResetRecord(true);
        try {
            mgr.lockTables(conn, true, resetcri);
            Connection myconn = null;
            Exception myee = null;
            try {
                myconn = mgr.getConnection(false);
                Util.RetryStrategy retry = null;
                do {
                    try {
                        Iterator itr = records.iterator();
                        ChangeRecordInfo cri = null;
                        while (itr.hasNext()) {
                            cri = (ChangeRecordInfo) itr.next();
                            if (cri.getType() == ChangeRecordInfo.TYPE_RESET_PERSISTENCE) {
                                itr.remove();
                                continue;
                            }
                            cri.setResetUUID(resetcri.getUUID());
                            cri.setTimestamp(System.currentTimeMillis());
                            dao.insert(myconn, cri);
                        }
                        myconn.commit();
                        break;
                    } catch (Exception e) {
                        if (retry == null) {
                            retry = new Util.RetryStrategy(mgr);
                        }
                        retry.assertShouldRetry(e);
                    }
                } while (true);
            } catch (BrokerException e) {
                myee = e;
                throw e;
            } finally {
                Util.close(null, null, myconn, myee, mgr);
            }
        } finally {
            try {
                mgr.lockTables(conn, false);
            } catch (Exception e) {
                logger.log(Logger.WARNING, br.getKString(br.X_SHARECC_UNLOCK_TABLE, e.toString()), e);
            }
        }
        logger.logToAll(Logger.INFO, br.getKString(br.I_SHARECC_RESTORE_RECORDS_SUCCESS, Globals.getClusterID(), backupfile));
    } catch (Exception e) {
        myex = e;
        String emsg = br.getKString(br.E_SHARECC_RESTORE_RECORDS_FAIL, e.getMessage());
        Globals.getLogger().logToAll(Logger.ERROR, emsg, e);
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        }
        throw new BrokerException(emsg, e);
    } finally {
        Util.close(null, null, conn, myex, mgr);
    }
}
Also used : StringUtil(com.sun.messaging.jmq.util.StringUtil) ShareConfigRecordDAO(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigRecordDAO) ShareConfigChangeDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.sharecc.ShareConfigChangeDBManager) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 29 with ChangeRecordInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method getRecords.

/**
 * Get records since sequence seq (exclusive) or get all records
 *
 * @param conn database connection
 * @param seq sequence number, null if get all records
 * @param resetUUID last reset UUID this broker has processed
 * @return an array of ShareConfigRecord
 */
@Override
public List<ChangeRecordInfo> getRecords(Connection conn, Long seq, String resetUUID, boolean canReset) throws BrokerException {
    ArrayList<ChangeRecordInfo> records = new ArrayList<>();
    boolean myConn = false;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String selectSQL = null;
    Exception myex = null;
    try {
        // Get a connection
        CommDBManager mgr = getDBManager();
        if (conn == null) {
            conn = mgr.getConnection(false);
            myConn = true;
        }
        if (seq == null || resetUUID == null) {
            records = getAllRecords(conn, null);
        } else {
            selectSQL = selectSinceWithResetRecordSQL;
            pstmt = mgr.createPreparedStatement(conn, selectSQL);
            pstmt.setLong(1, seq.longValue());
            rs = pstmt.executeQuery();
            long seqv = -1;
            String uuidv = null;
            byte[] buf = null;
            int typv = 0;
            long tsv = -1;
            ChangeRecordInfo cri = null;
            boolean reseted = false, foundreset = false;
            String newResetUUID = null;
            while (rs.next()) {
                try {
                    seqv = rs.getLong(1);
                    uuidv = rs.getString(2);
                    buf = Util.readBytes(rs, 3);
                    typv = rs.getInt(4);
                    tsv = rs.getLong(5);
                    if (typv == ChangeRecordInfo.TYPE_RESET_PERSISTENCE) {
                        foundreset = true;
                        if (uuidv.equals(resetUUID)) {
                            continue;
                        }
                        newResetUUID = uuidv;
                        reseted = true;
                        break;
                    }
                    cri = new ChangeRecordInfo(Long.valueOf(seqv), uuidv, buf, typv, null, tsv);
                    cri.setResetUUID(resetUUID);
                    cri.setIsSelectAll(false);
                    records.add(cri);
                } catch (IOException e) {
                    IOException ex = getDBManager().wrapIOException("[" + selectAllSQL + "]", e);
                    logger.logStack(Logger.ERROR, BrokerResources.X_PARSE_CONFIGRECORD_FAILED, String.valueOf(seq), ex);
                }
            }
            if (!foundreset) {
                throw new BrokerException("Unexpected: shared database table " + getTableName() + " has no reset record", Status.PRECONDITION_FAILED);
            }
            if (reseted) {
                if (!canReset) {
                    throw new BrokerException(br.getKString(br.X_SHARECC_TABLE_RESET, "[" + resetUUID + ", " + newResetUUID + "]"), Status.PRECONDITION_FAILED);
                }
                logger.log(logger.INFO, br.getKString(br.I_SHARECC_USE_RESETED_TABLE, "[" + resetUUID + ", " + newResetUUID + "]"));
                records = getAllRecords(conn, null);
            }
        }
        if (myConn) {
            conn.commit();
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
        }
        Exception ex;
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else if (e instanceof SQLException) {
            ex = getDBManager().wrapSQLException("[" + selectSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(br.X_SHARECC_QUERY_RECORDS_FAIL, ex.getMessage()), ex);
    } finally {
        if (myConn) {
            closeSQLObjects(rs, pstmt, conn, myex);
        } else {
            closeSQLObjects(rs, pstmt, null, myex);
        }
    }
    return records;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) CommDBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.comm.CommDBManager) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 30 with ChangeRecordInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo in project openmq by eclipse-ee4j.

the class ShareConfigRecordDAOImpl method insertAll.

@Override
public void insertAll(List<ChangeRecordInfo> recs, String oldTableName) throws BrokerException {
    Connection conn = null;
    Exception myex = null;
    try {
        conn = getDBManager().getConnection(false);
        ChangeRecordInfo rec = null;
        Iterator<ChangeRecordInfo> itr = recs.iterator();
        while (itr.hasNext()) {
            rec = itr.next();
            insert(conn, rec);
        }
        conn.commit();
    } catch (Exception e) {
        myex = e;
        String emsg = br.getKString(br.X_SHARECC_MIGRATE_DATA, oldTableName, e.getMessage());
        try {
            if (conn != null) {
                conn.rollback();
            }
        } catch (SQLException rbe) {
            logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED + "[" + emsg + "]:" + rbe, rbe);
        }
        Exception ex = e;
        if (e instanceof SQLException) {
            ex = getDBManager().wrapSQLException("[" + emsg + "]", (SQLException) e);
        }
        throw new BrokerException(emsg, ex);
    } finally {
        closeSQLObjects(null, null, conn, myex);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo) IOException(java.io.IOException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException)

Aggregations

ChangeRecordInfo (com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)35 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)11 GPacket (com.sun.messaging.jmq.io.GPacket)9 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)4 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)3 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)3 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)3 JMSBridgeStore (com.sun.messaging.bridge.api.JMSBridgeStore)2 Packet (com.sun.messaging.jmq.io.Packet)2 ClusterManager (com.sun.messaging.jmq.jmsserver.cluster.api.ClusterManager)2 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)2 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)2 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)2 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)2 ChangeRecord (com.sun.messaging.jmq.jmsserver.multibroker.ChangeRecord)2 ClusterDestInfo (com.sun.messaging.jmq.jmsserver.multibroker.raptor.ClusterDestInfo)2 ClusterFirstInfoInfo (com.sun.messaging.jmq.jmsserver.multibroker.raptor.ClusterFirstInfoInfo)2 ClusterSubscriptionInfo (com.sun.messaging.jmq.jmsserver.multibroker.raptor.ClusterSubscriptionInfo)2 HABrokerInfo (com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)2