Search in sources :

Example 51 with Database

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

the class Upgrader method upgradeIfNecessary.

public void upgradeIfNecessary() {
    boolean isEmpty = _environment.getDatabaseNames().isEmpty();
    DatabaseConfig dbConfig = new DatabaseConfig();
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);
    Database versionDb = null;
    try {
        versionDb = _environment.openDatabase(null, VERSION_DB_NAME, dbConfig);
        if (versionDb.count() == 0L) {
            int sourceVersion = isEmpty ? BDBConfigurationStore.VERSION : identifyOldStoreVersion();
            DatabaseEntry key = new DatabaseEntry();
            IntegerBinding.intToEntry(sourceVersion, key);
            DatabaseEntry value = new DatabaseEntry();
            LongBinding.longToEntry(System.currentTimeMillis(), value);
            versionDb.put(null, key, value);
        }
        int version = getSourceVersion(versionDb);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Source message store version is " + version);
        }
        if (version > BDBConfigurationStore.VERSION) {
            throw new StoreException("Database version " + version + " is higher than the most recent known version: " + BDBConfigurationStore.VERSION);
        }
        performUpgradeFromVersion(version, versionDb);
    } finally {
        if (versionDb != null) {
            versionDb.close();
        }
    }
}
Also used : Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) DatabaseConfig(com.sleepycat.je.DatabaseConfig) StoreException(org.apache.qpid.server.store.StoreException)

Example 52 with Database

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

the class ReplicatedEnvironmentFacadeTest method testNodeRolledback.

@Test
public void testNodeRolledback() throws Exception {
    DatabaseConfig createConfig = createDatabaseConfig();
    TestStateChangeListener masterListener = new TestStateChangeListener();
    ReplicatedEnvironmentFacade node1 = addNode(TEST_NODE_NAME, TEST_NODE_HOST_PORT, true, masterListener, new NoopReplicationGroupListener());
    assertTrue("Environment was not created", masterListener.awaitForStateChange(State.MASTER, _timeout, TimeUnit.SECONDS));
    String replicaNodeHostPort = "localhost:" + _portHelper.getNextAvailable();
    String replicaName = TEST_NODE_NAME + 1;
    ReplicatedEnvironmentFacade node2 = createReplica(replicaName, replicaNodeHostPort, new NoopReplicationGroupListener());
    Database db = node1.openDatabase("mydb", createConfig);
    // Put a record (that will be replicated)
    putRecord(node1, db, 1, "value1", false);
    node2.close();
    // Put a record (that will be only on node1 as node2 is now offline)
    putRecord(node1, db, 2, "value2", false);
    db.close();
    // Stop node1
    node1.close();
    LOGGER.debug("RESTARTING " + replicaName);
    // Restart the node2, making it primary so it becomes master
    TestStateChangeListener node2StateChangeListener = new TestStateChangeListener();
    node2 = addNode(replicaName, replicaNodeHostPort, true, node2StateChangeListener, new NoopReplicationGroupListener());
    boolean awaitForStateChange = node2StateChangeListener.awaitForStateChange(State.MASTER, _timeout, TimeUnit.SECONDS);
    assertTrue(replicaName + " did not go into desired state; current actual state is " + node2StateChangeListener.getCurrentActualState(), awaitForStateChange);
    db = node2.openDatabase("mydb", DatabaseConfig.DEFAULT);
    // Do a transaction on node2. The two environments will have diverged
    putRecord(node2, db, 3, "diverged", false);
    LOGGER.debug("RESTARTING " + TEST_NODE_NAME);
    // Now restart node1 and ensure that it realises it needs to rollback before it can rejoin.
    TestStateChangeListener node1StateChangeListener = new TestStateChangeListener();
    final CountDownLatch _replicaRolledback = new CountDownLatch(1);
    node1 = addNode(node1StateChangeListener, new NoopReplicationGroupListener() {

        @Override
        public void onNodeRolledback() {
            LOGGER.debug("onNodeRolledback in " + TEST_NODE_NAME);
            _replicaRolledback.countDown();
        }
    });
    assertTrue("Node 1 did not go into desired state", node1StateChangeListener.awaitForStateChange(State.REPLICA, _timeout, TimeUnit.SECONDS));
    assertTrue("Node 1 did not experience rollback within timeout", _replicaRolledback.await(_timeout, TimeUnit.SECONDS));
    // Finally do one more transaction through the master
    putRecord(node2, db, 4, "value4", false);
    db.close();
    LOGGER.debug("CLOSING");
    node1.close();
    node2.close();
}
Also used : ReplicatedEnvironmentFacade(org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade) Database(com.sleepycat.je.Database) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) DatabaseConfig(com.sleepycat.je.DatabaseConfig) Test(org.junit.Test)

Example 53 with Database

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

the class UpgradeFrom4to5Test method loadBindings.

private List<BindingRecord> loadBindings() {
    final BindingTuple bindingTuple = new BindingTuple();
    final List<BindingRecord> queueBindings = new ArrayList<BindingRecord>();
    CursorOperation databaseOperation = new CursorOperation() {

        @Override
        public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
            BindingRecord bindingRecord = bindingTuple.entryToObject(key);
            AMQShortString queueName = bindingRecord.getQueueName();
            AMQShortString exchangeName = bindingRecord.getExchangeName();
            AMQShortString routingKey = bindingRecord.getRoutingKey();
            FieldTable arguments = bindingRecord.getArguments();
            queueBindings.add(new BindingRecord(exchangeName, queueName, routingKey, arguments));
        }
    };
    new DatabaseTemplate(_environment, BINDING_DB_NAME, null).run(databaseOperation);
    return queueBindings;
}
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) BindingTuple(org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom4To5.BindingTuple) ArrayList(java.util.ArrayList) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) BindingRecord(org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom4To5.BindingRecord)

Example 54 with Database

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

the class UpgradeFrom4to5Test method assertMetadataForQueue.

private void assertMetadataForQueue(final String queueName, final int expectedQueueSize, final Set<Long> messageIdsForQueue) {
    final AtomicInteger metadataCounter = new AtomicInteger();
    CursorOperation databaseOperation = new CursorOperation() {

        @Override
        public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
            Long messageId = LongBinding.entryToLong(key);
            boolean messageIsForTheRightQueue = messageIdsForQueue.contains(messageId);
            if (messageIsForTheRightQueue) {
                metadataCounter.incrementAndGet();
            }
        }
    };
    new DatabaseTemplate(_environment, MESSAGE_META_DATA_DB_NAME, null).run(databaseOperation);
    assertEquals("Unxpected number of entries in metadata db for queue " + queueName, expectedQueueSize, metadataCounter.get());
}
Also used : Transaction(com.sleepycat.je.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry)

Example 55 with Database

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

the class UpgradeFrom4to5Test method assertDeliveriesForQueue.

private Set<Long> assertDeliveriesForQueue(final String queueName, final int expectedQueueSize) {
    final QueueEntryKeyBinding queueEntryKeyBinding = new QueueEntryKeyBinding();
    final AtomicInteger deliveryCounter = new AtomicInteger();
    final Set<Long> messagesForQueue = new HashSet<Long>();
    CursorOperation deliveryDatabaseOperation = new CursorOperation() {

        @Override
        public void processEntry(Database sourceDatabase, Database targetDatabase, Transaction transaction, DatabaseEntry key, DatabaseEntry value) {
            QueueEntryKey entryKey = queueEntryKeyBinding.entryToObject(key);
            String thisQueueName = entryKey.getQueueName().toString();
            if (thisQueueName.equals(queueName)) {
                deliveryCounter.incrementAndGet();
                messagesForQueue.add(entryKey.getMessageId());
            }
        }
    };
    new DatabaseTemplate(_environment, DELIVERY_DB_NAME, null).run(deliveryDatabaseOperation);
    assertEquals("Unxpected number of entries in delivery db for queue " + queueName, expectedQueueSize, deliveryCounter.get());
    return messagesForQueue;
}
Also used : QueueEntryKey(org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom4To5.QueueEntryKey) Transaction(com.sleepycat.je.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Database(com.sleepycat.je.Database) DatabaseEntry(com.sleepycat.je.DatabaseEntry) AMQShortString(org.apache.qpid.server.protocol.v0_8.AMQShortString) QueueEntryKeyBinding(org.apache.qpid.server.store.berkeleydb.upgrade.UpgradeFrom4To5.QueueEntryKeyBinding) HashSet(java.util.HashSet)

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