Search in sources :

Example 26 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class DatabasePinger method pingDb.

public void pingDb(EnvironmentFacade facade) {
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Beginning ping transaction");
        }
        final Database db = facade.openDatabase(PING_DATABASE_NAME, DATABASE_CONFIG);
        DatabaseEntry key = new DatabaseEntry();
        IntegerBinding.intToEntry(ID, key);
        DatabaseEntry value = new DatabaseEntry();
        LongBinding.longToEntry(System.currentTimeMillis(), value);
        Transaction txn = null;
        try {
            txn = facade.beginTransaction(_pingTransactionConfig);
            db.put(txn, key, value);
            txn.commit();
            txn = null;
        } finally {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Ping transaction completed");
            }
            if (txn != null) {
                txn.abort();
            }
        }
    } catch (RuntimeException de) {
        RuntimeException handledException = facade.handleDatabaseException("DatabaseException from DatabasePinger ", de);
        LOGGER.debug("Non fatal exception on invoking DatabasePinger. Ignoring...", handledException);
    }
}
Also used : Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry)

Example 27 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class UpgradeFrom4To5 method upgradeQueues.

private Set<String> upgradeQueues(final Environment environment, final UpgradeInteractionHandler handler, List<AMQShortString> potentialDurableSubs, Transaction transaction) {
    LOGGER.info("Queues");
    final Set<String> existingQueues = new HashSet<String>();
    if (environment.getDatabaseNames().contains(OLD_QUEUE_DB_NAME)) {
        final QueueRecordBinding binding = new QueueRecordBinding(potentialDurableSubs);
        CursorOperation databaseOperation = new CursorOperation() {

            @Override
            public void processEntry(final Database sourceDatabase, final Database targetDatabase, final Transaction transaction, final DatabaseEntry key, final DatabaseEntry value) {
                QueueRecord record = binding.entryToObject(value);
                DatabaseEntry newValue = new DatabaseEntry();
                binding.objectToEntry(record, newValue);
                targetDatabase.put(transaction, key, newValue);
                existingQueues.add(record.getNameShortString().toString());
                sourceDatabase.delete(transaction, key);
            }
        };
        new DatabaseTemplate(environment, OLD_QUEUE_DB_NAME, NEW_QUEUE_DB_NAME, transaction).run(databaseOperation);
        environment.removeDatabase(transaction, OLD_QUEUE_DB_NAME);
        LOGGER.info(databaseOperation.getRowCount() + " Queue entries");
    }
    return existingQueues;
}
Also used : Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) DatabaseEntry(com.sleepycat.je.DatabaseEntry) HashSet(java.util.HashSet)

Example 28 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class UpgradeFrom4To5 method upgradeDelivery.

private Set<Long> upgradeDelivery(final Environment environment, final Set<String> existingQueues, final UpgradeInteractionHandler handler, Transaction transaction) {
    final Set<Long> messagesToDiscard = new HashSet<Long>();
    final Set<String> queuesToDiscard = new HashSet<String>();
    final QueueEntryKeyBinding queueEntryKeyBinding = new QueueEntryKeyBinding();
    LOGGER.info("Delivery Records");
    CursorOperation databaseOperation = new CursorOperation() {

        @Override
        public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
            QueueEntryKey entryKey = queueEntryKeyBinding.entryToObject(key);
            Long messageId = entryKey.getMessageId();
            final String queueName = entryKey.getQueueName().toString();
            if (!existingQueues.contains(queueName)) {
                if (queuesToDiscard.contains(queueName)) {
                    messagesToDiscard.add(messageId);
                } else {
                    String lineSeparator = System.getProperty("line.separator");
                    String question = MessageFormat.format("Found persistent messages for non-durable queue ''{1}''. " + " Do you with to create this queue and move all the messages into it?" + lineSeparator + "NOTE: Answering No will result in these messages being discarded!", queueName);
                    UpgradeInteractionResponse response = handler.requireResponse(question, UpgradeInteractionResponse.YES, UpgradeInteractionResponse.YES, UpgradeInteractionResponse.NO, UpgradeInteractionResponse.ABORT);
                    if (response == UpgradeInteractionResponse.YES) {
                        createQueue(environment, transaction, queueName);
                        existingQueues.add(queueName);
                    } else if (response == UpgradeInteractionResponse.NO) {
                        queuesToDiscard.add(queueName);
                        messagesToDiscard.add(messageId);
                    } else {
                        throw new StoreException("Unable is aborted!");
                    }
                }
            }
            if (!messagesToDiscard.contains(messageId)) {
                DatabaseEntry newKey = new DatabaseEntry();
                queueEntryKeyBinding.objectToEntry(entryKey, newKey);
                targetDatabase.put(transaction, newKey, value);
            }
        }
    };
    new DatabaseTemplate(environment, OLD_DELIVERY_DB, NEW_DELIVERY_DB, transaction).run(databaseOperation);
    if (!messagesToDiscard.isEmpty()) {
        databaseOperation = new CursorOperation() {

            @Override
            public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
                QueueEntryKey entryKey = queueEntryKeyBinding.entryToObject(key);
                Long messageId = entryKey.getMessageId();
                if (messagesToDiscard.contains(messageId)) {
                    messagesToDiscard.remove(messageId);
                }
            }
        };
        new DatabaseTemplate(environment, NEW_DELIVERY_DB, transaction).run(databaseOperation);
    }
    LOGGER.info(databaseOperation.getRowCount() + " Delivery Records entries ");
    environment.removeDatabase(transaction, OLD_DELIVERY_DB);
    return messagesToDiscard;
}
Also used : AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) DatabaseEntry(com.sleepycat.je.DatabaseEntry) StoreException(org.apache.qpid.server.store.StoreException) Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) HashSet(java.util.HashSet)

Example 29 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class UpgradeFrom4To5 method createQueue.

protected void createQueue(final Environment environment, Transaction transaction, final String queueName) {
    final QueueRecordBinding binding = new QueueRecordBinding(null);
    final BindingTuple bindingTuple = new BindingTuple();
    DatabaseRunnable queueCreateOperation = new DatabaseRunnable() {

        @Override
        public void run(Database newQueueDatabase, Database newBindingsDatabase, Transaction transaction) {
            AMQShortString queueNameAMQ = AMQShortString.createAMQShortString(queueName);
            QueueRecord record = new QueueRecord(queueNameAMQ, null, false, null);
            DatabaseEntry key = new DatabaseEntry();
            TupleOutput output = new TupleOutput();
            AMQShortStringEncoding.writeShortString(record.getNameShortString(), output);
            TupleBase.outputToEntry(output, key);
            DatabaseEntry newValue = new DatabaseEntry();
            binding.objectToEntry(record, newValue);
            newQueueDatabase.put(transaction, key, newValue);
            FieldTable emptyArguments = FieldTableFactory.createFieldTable(Collections.emptyMap());
            addBindingToDatabase(bindingTuple, newBindingsDatabase, transaction, queueNameAMQ, AMQShortString.valueOf(ExchangeDefaults.DIRECT_EXCHANGE_NAME), queueNameAMQ, emptyArguments);
            // TODO QPID-3490 we should not persist a default exchange binding
            addBindingToDatabase(bindingTuple, newBindingsDatabase, transaction, queueNameAMQ, AMQShortString.valueOf(ExchangeDefaults.DEFAULT_EXCHANGE_NAME), queueNameAMQ, emptyArguments);
        }
    };
    new DatabaseTemplate(environment, NEW_QUEUE_DB_NAME, NEW_BINDINGS_DB_NAME, transaction).run(queueCreateOperation);
}
Also used : AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) Transaction(com.sleepycat.je.Transaction) FieldTable(org.apache.qpid.server.protocol.v0_8.FieldTable) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) TupleOutput(com.sleepycat.bind.tuple.TupleOutput)

Example 30 with Database

use of com.sleepycat.je.Database in project qpid-broker-j by apache.

the class UpgradeFrom4To5 method upgradeQueueBindings.

private void upgradeQueueBindings(Environment environment, UpgradeInteractionHandler handler, final List<AMQShortString> potentialDurableSubs, Transaction transaction) {
    if (environment.getDatabaseNames().contains(OLD_BINDINGS_DB_NAME)) {
        LOGGER.info("Queue Bindings");
        final BindingTuple bindingTuple = new BindingTuple();
        CursorOperation databaseOperation = new CursorOperation() {

            @Override
            public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
                // All the information required in binding entries is actually in the *key* not value.
                BindingRecord oldBindingRecord = bindingTuple.entryToObject(key);
                AMQShortString queueName = oldBindingRecord.getQueueName();
                AMQShortString exchangeName = oldBindingRecord.getExchangeName();
                AMQShortString routingKey = oldBindingRecord.getRoutingKey();
                FieldTable arguments = oldBindingRecord.getArguments();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(String.format("Processing binding for queue %s, exchange %s, routingKey %s arguments %s", queueName, exchangeName, routingKey, arguments));
                }
                // only topic exchange should have a JMS selector key in binding
                if (potentialDurableSubs.contains(queueName) && exchangeName.equals(AMQShortString.valueOf(ExchangeDefaults.TOPIC_EXCHANGE_NAME))) {
                    Map<String, Object> argumentsMap = new HashMap<>();
                    if (arguments != null) {
                        argumentsMap.putAll(FieldTable.convertToMap(arguments));
                    }
                    String selectorFilterKey = AMQPFilterTypes.JMS_SELECTOR.getValue();
                    if (!argumentsMap.containsKey(selectorFilterKey)) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.info("adding the empty string (i.e. 'no selector') value for " + queueName + " and exchange " + exchangeName);
                        }
                        argumentsMap.put(selectorFilterKey, "");
                    }
                    arguments = FieldTable.convertToFieldTable(argumentsMap);
                }
                addBindingToDatabase(bindingTuple, targetDatabase, transaction, queueName, exchangeName, routingKey, arguments);
            }
        };
        new DatabaseTemplate(environment, OLD_BINDINGS_DB_NAME, NEW_BINDINGS_DB_NAME, transaction).run(databaseOperation);
        environment.removeDatabase(transaction, OLD_BINDINGS_DB_NAME);
        LOGGER.info(databaseOperation.getRowCount() + " Queue Binding entries");
    }
}
Also used : AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) FieldTable(org.apache.qpid.server.protocol.v0_8.FieldTable) HashMap(java.util.HashMap) DatabaseEntry(com.sleepycat.je.DatabaseEntry) AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) Transaction(com.sleepycat.je.Transaction) Database(com.sleepycat.je.Database) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Aggregations

Database (com.sleepycat.je.Database)73 DatabaseEntry (com.sleepycat.je.DatabaseEntry)46 Transaction (com.sleepycat.je.Transaction)35 DatabaseConfig (com.sleepycat.je.DatabaseConfig)27 UUID (java.util.UUID)12 AMQShortString (org.apache.qpid.server.protocol.v0_8.AMQShortString)11 Environment (com.sleepycat.je.Environment)10 OperationStatus (com.sleepycat.je.OperationStatus)8 Cursor (com.sleepycat.je.Cursor)7 HashSet (java.util.HashSet)7 StoreException (org.apache.qpid.server.store.StoreException)7 HashMap (java.util.HashMap)6 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)5 File (java.io.File)5 ArrayList (java.util.ArrayList)5 FieldTable (org.apache.qpid.server.protocol.v0_8.FieldTable)5 NewPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.NewPreparedTransaction)5 OldPreparedTransaction (org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom5To6.OldPreparedTransaction)5 Test (org.junit.Test)5 TupleOutput (com.sleepycat.bind.tuple.TupleOutput)4