jetbrains.exodus.ExodusException Java Examples

The following examples show how to use jetbrains.exodus.ExodusException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EnvironmentTestsBase.java    From xodus with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown() throws Exception {
    try {
        if (env != null) {
            env.close();
            env = null;
        }
    } catch (final ExodusException e) {
        archiveDB(getClass().getName() + '.' + System.currentTimeMillis() + ".tar.gz");
        throw e;
    } finally {
        invalidateSharedCaches();
        deleteRW();
        if (processor != null) {
            processor.finish();
        }
    }
}
 
Example #2
Source File: PublishPayloadXodusLocalPersistence.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {

    try {
        final AtomicLong prevMax = new AtomicLong(0);
        for (final Bucket bucket : buckets) {
            bucket.getEnvironment().executeInReadonlyTransaction(txn -> {
                try (final Cursor cursor = bucket.getStore().openCursor(txn)) {
                    while (cursor.getNext()) {
                        final KeyPair keypair = serializer.deserializeKey(byteIterableToBytes(cursor.getKey()));
                        if (keypair.getId() > prevMax.get()) {
                            prevMax.set(keypair.getId());
                        }
                    }
                }
            });
        }
        maxId = prevMax.get();

    } catch (final ExodusException e) {
        log.error("An error occurred while preparing the Publish Payload persistence.");
        log.debug("Original Exception:", e);
        throw new UnrecoverableException(false);
    }
}
 
Example #3
Source File: ComparableSetBinding.java    From xodus with Apache License 2.0 6 votes vote down vote up
@Override
public void writeObject(@NotNull final LightOutputStream output, @NotNull final Comparable object) {
    final ComparableSet set = (ComparableSet) object;
    final Class itemClass = set.getItemClass();
    if (itemClass == null) {
        throw new ExodusException("Attempt to write empty ComparableSet");
    }
    final ComparableValueType type = ComparableValueType.getPredefinedType(itemClass);
    output.writeByte(type.getTypeId());
    final ComparableBinding itemBinding = type.getBinding();
    //noinspection Convert2Lambda
    set.forEach(new ComparableSet.Consumer() {
        @Override
        public void accept(@NotNull final Comparable item, final int index) {
            itemBinding.writeObject(output, item);
        }
    });
}
 
Example #4
Source File: LogLockingTests.java    From xodus with Apache License 2.0 6 votes vote down vote up
@Test
public void testDirectoryAlreadyLocked() throws IOException {
    initLog(1);
    File xdLockFile = new File(getLogDirectory(), "xd.lck");
    Assert.assertTrue(xdLockFile.exists());
    Log prevLog = log;
    boolean alreadyLockedEx = false;
    try {
        log = null;
        initLog(1);
    } catch (ExodusException ex) {
        alreadyLockedEx = true;
    }
    Assert.assertTrue(alreadyLockedEx);
    prevLog.close();
    closeLog();
}
 
Example #5
Source File: ProjectDatabase.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
public static void reset() {
  if (projectDatabase != null) {
    if (nonNull(projectDatabase.entityStore)) {
      try {
        EnvironmentImpl environment =
            (EnvironmentImpl) projectDatabase.entityStore.getEnvironment();
        environment.flushAndSync();
        projectDatabase.entityStore.close();
      } catch (ExodusException e) {
        // wait transaction
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e1) {
          log.catching(e1);
        }
        projectDatabase.entityStore.close();
      }
      projectDatabase.entityStore = null;
    }
    if (nonNull(projectDatabase.environment)) {
      projectDatabase.environment.close();
      projectDatabase.environment = null;
    }
    projectDatabase.open();
  }
}
 
Example #6
Source File: ProjectDatabase.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private synchronized void close() {

    if (nonNull(this.entityStore)) {
      try {
        EnvironmentImpl environment = (EnvironmentImpl) this.entityStore.getEnvironment();
        environment.flushAndSync();
        this.entityStore.close();
      } catch (ExodusException e) {
        // wait transaction
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e1) {
          log.catching(e1);
        }
        this.entityStore.close();
      }
      this.entityStore = null;
    }

    if (nonNull(this.environment)) {
      this.environment.close();
      this.environment = null;
    }
  }
 
Example #7
Source File: EnvironmentCloser.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
/**
 * Retries if the ExodusException is retryable or rethrows the Exception
 *
 * @param e the exception
 */
private boolean retry(final ExodusException e) {
    if ("Finish all transactions before closing database environment".equals(e.getMessage())) {
        //This exception means we still have pending transactions
        tryNo += 1;
        log.debug("Could not close {}, transactions still aren't finished yet. Retrying again in {}ms (Retry {} of {})", name, retryInterval, tryNo, maxTries);
        try {
            Thread.sleep(retryInterval);
            //Let's call ourselves recursively!
            return close();
        } catch (final InterruptedException interruptedException) {
            log.debug("Interrupted Exception when trying to close {}", name, interruptedException);
            Thread.currentThread().interrupt();
            return false;
        }
    } else {
        //Not the exception we wanted, rethrow
        throw e;
    }
}
 
Example #8
Source File: LogLockingTests.java    From xodus with Apache License 2.0 6 votes vote down vote up
@Test
public void testDirectoryReleaseLock() throws IOException {
    initLog(1);
    File xdLockFile = new File(getLogDirectory(), "xd.lck");
    Assert.assertTrue(xdLockFile.exists());
    closeLog();
    xdLockFile = new File(getLogDirectory(), "xd.lck");
    BufferedReader bufferedReader = new BufferedReader(new FileReader(xdLockFile));
    System.out.println(bufferedReader.readLine());
    bufferedReader.close();
    Assert.assertTrue(xdLockFile.exists());
    boolean alreadyLockedEx = false;
    try {
        initLog(1);
    } catch (ExodusException ex) {
        alreadyLockedEx = true;
    }
    Assert.assertFalse(alreadyLockedEx);
    closeLog();
}
 
Example #9
Source File: PersistentStoreTransaction.java    From xodus with Apache License 2.0 6 votes vote down vote up
@Override
@NotNull
public PersistentEntity newEntity(@NotNull final String entityType) {
    try {
        final int entityTypeId = store.getEntityTypeId(this, entityType, true);
        final long entityLocalId = store.getEntitiesSequence(this, entityTypeId).increment();
        store.getEntitiesTable(this, entityTypeId).putRight(
            txn, LongBinding.longToCompressedEntry(entityLocalId), ZERO_VERSION_ENTRY);
        final PersistentEntityId id = new PersistentEntityId(entityTypeId, entityLocalId);
        // update iterables' cache
        new EntityAddedHandleCheckerImpl(this, id, mutableCache(), mutatedInTxn).updateCache();
        return new PersistentEntity(store, id);
    } catch (Exception e) {
        throw ExodusException.toEntityStoreException(e);
    }
}
 
Example #10
Source File: EnvironmentLockTest.java    From xodus with Apache License 2.0 6 votes vote down vote up
private void openConcurrentEnvironment() {
    processor.start();
    new Job(processor) {
        @Override
        protected void execute() {
            final File dir = getEnvDirectory();
            try {
                FileDataReader reader = new FileDataReader(dir);
                env = newEnvironmentInstance(LogConfig.create(reader, new FileDataWriter(reader, LOCK_ID)), new EnvironmentConfig().setLogLockTimeout(5000));
                wasOpened[0] = true;
            } catch (ExodusException e) {
                Assert.assertTrue(e.getMessage().contains(LOCK_ID));
                wasOpened[0] = false;
            }
        }
    };
}
 
Example #11
Source File: EntityBlobTests.java    From xodus with Apache License 2.0 6 votes vote down vote up
@TestFor(issues = "XD-531")
public void testEntityStoreClear() {
    final PersistentEntityStoreImpl store = getEntityStore();
    store.getConfig().setMaxInPlaceBlobSize(0);
    store.executeInTransaction(txn -> txn.newEntity("User").setBlobString("bio", "I was born"));
    store.executeInReadonlyTransaction(txn -> {
        final InputStream content = store.getBlobVault().getContent(0, ((PersistentStoreTransaction) txn).getEnvironmentTransaction());
        assertNotNull(content);
        try {
            content.close();
        } catch (IOException e) {
            throw ExodusException.toExodusException(e);
        }
    });
    store.clear();
    store.executeInReadonlyTransaction(txn -> assertNull(store.getBlobVault().getContent(0, ((PersistentStoreTransaction) txn).getEnvironmentTransaction())));
}
 
Example #12
Source File: EntityTests.java    From xodus with Apache License 2.0 6 votes vote down vote up
public void testAsciiUTFDecodingBenchmark() {
    final String s = "This is sample ASCII string of not that great size, but large enough to use in the benchmark";
    TestUtil.time("Constructing string from data input", () -> {
        try {
            final LightByteArrayOutputStream out = new LightByteArrayOutputStream();
            DataOutputStream output = new DataOutputStream(out);
            output.writeUTF(s);
            final InputStream stream = new ByteArraySizedInputStream(out.toByteArray(), 0, out.size());
            stream.mark(Integer.MAX_VALUE);
            for (int i = 0; i < 10000000; i++) {
                stream.reset();
                assertEquals(s, new DataInputStream(stream).readUTF());
            }
        } catch (IOException e) {
            throw ExodusException.toEntityStoreException(e);
        }
    });
    TestUtil.time("Constructing strings from bytes", () -> {
        final byte bytes[] = s.getBytes();
        for (int i = 0; i < 10000000; i++) {
            assertEquals(s, UTFUtil.fromAsciiByteArray(bytes, 0, bytes.length));
        }
    });
}
 
Example #13
Source File: BTreeBase.java    From xodus with Apache License 2.0 6 votes vote down vote up
@NotNull
protected LeafNode loadLeaf(final long address) {
    final RandomAccessLoggable loggable = getLoggable(address);
    final byte type = loggable.getType();
    switch (type) {
        case LEAF:
        case DUP_LEAF:
            return new LeafNode(loggable);
        case LEAF_DUP_BOTTOM_ROOT:
        case LEAF_DUP_INTERNAL_ROOT:
            if (allowsDuplicates) {
                return new LeafNodeDup(this, loggable);
            } else {
                throw new ExodusException("Try to load leaf with duplicates, but tree is not configured to support duplicates.");
            }
        default:
            DataCorruptionException.raise("Unexpected loggable type: " + type, log, address);
            // dummy unreachable statement
            throw new RuntimeException();
    }
}
 
Example #14
Source File: EnvironmentCloser.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
/**
 * Closes the environment. Retries until the maximum number of retries was hit.
 * <p>
 * All exceptions are rethrown.
 *
 * @return true if the close was successful, false otherwise
 */
public boolean close() {
    try {
        if (!environment.isOpen()) {
            log.warn("Tried to close store {} although it is already closed", name);
            return false;
        }
        if (tryNo < maxTries) {
            environment.close();
            return true;
        } else {
            log.error("Could not close store {} after {} tries.", name, tryNo);
            return false;
        }
    } catch (final ExodusException e) {
        return retry(e);
    }
}
 
Example #15
Source File: LogTests.java    From xodus with Apache License 2.0 6 votes vote down vote up
@Test
public void testClearInvalidLog() throws IOException {
    initLog(2);
    getLog().beginWrite();
    for (int i = 0; i < 2048; ++i) {
        getLog().write(DUMMY_LOGGABLE);
    }
    getLog().endWrite();
    closeLog();
    try {
        initLog(1);
    } catch (ExodusException e) {
        return;
    }
    Assert.assertTrue("Expected exception wasn't thrown", true);
}
 
Example #16
Source File: LogUtil.java    From xodus with Apache License 2.0 6 votes vote down vote up
public static long getAddress(final String logFilename) {
    final int length = logFilename.length();
    if (length != LOG_FILE_NAME_WITH_EXT_LENGTH || !logFilename.endsWith(LOG_FILE_EXTENSION)) {
        throw new ExodusException("Invalid log file name: " + logFilename);
    }
    long address = 0;
    for (int i = 0; i < LOG_FILE_NAME_LENGTH; ++i) {
        final char c = logFilename.charAt(i);
        final Integer integer = ALPHA_INDEXES.get(c);
        if (integer == null) {
            throw new ExodusException("Invalid log file name: " + logFilename);
        }
        address = (address << 5) + integer;
    }
    return address * LOG_BLOCK_ALIGNMENT;
}
 
Example #17
Source File: LogUtil.java    From xodus with Apache License 2.0 6 votes vote down vote up
public static String getLogFilename(long address) {
    if (address < 0) {
        throw new ExodusException("Starting address of a log file is negative: " + address);
    }
    if (address % LOG_BLOCK_ALIGNMENT != 0) {
        throw new ExodusException("Starting address of a log file is badly aligned: " + address);
    }
    address /= LOG_BLOCK_ALIGNMENT;
    char[] name = new char[LOG_FILE_NAME_WITH_EXT_LENGTH];
    for (int i = 1; i <= LOG_FILE_NAME_LENGTH; i++) {
        name[LOG_FILE_NAME_LENGTH - i] = LOG_FILE_NAME_ALPHABET[(int) (address & 0x1f)];
        address >>= 5;
    }
    System.arraycopy(LOG_FILE_EXTENSION_CHARS, 0, name, LOG_FILE_NAME_LENGTH, LOG_FILE_EXTENSION_LENGTH);
    return new String(name);
}
 
Example #18
Source File: BufferedDataWriter.java    From xodus with Apache License 2.0 6 votes vote down vote up
void write(byte[] b, int len) throws ExodusException {
    int off = 0;
    final int count = this.count + len;
    MutablePage currentPage = this.currentPage;
    while (len > 0) {
        int bytesToWrite = pageSize - currentPage.writtenCount;
        if (bytesToWrite == 0) {
            currentPage = allocNewPage();
            bytesToWrite = pageSize;
        }
        if (bytesToWrite > len) {
            bytesToWrite = len;
        }
        System.arraycopy(b, off, currentPage.bytes, currentPage.writtenCount, bytesToWrite);
        currentPage.writtenCount += bytesToWrite;
        len -= bytesToWrite;
        off += bytesToWrite;
    }
    this.count = count;
}
 
Example #19
Source File: ReadWriteTransaction.java    From xodus with Apache License 2.0 6 votes vote down vote up
@NotNull
ITreeMutable getMutableTree(@NotNull final StoreImpl store) {
    checkIsFinished();
    if (getEnvironment().getEnvironmentConfig().getEnvTxnSingleThreadWrites()) {
        final Thread creatingThread = getCreatingThread();
        if (!creatingThread.equals(Thread.currentThread())) {
            throw new ExodusException("Can't create mutable tree in a thread different from the one which transaction was created in");
        }
    }
    final int structureId = store.getStructureId();
    ITreeMutable result = mutableTrees.get(structureId);
    if (result == null) {
        result = getTree(store).getMutableCopy();
        mutableTrees.put(structureId, result);
    }
    return result;
}
 
Example #20
Source File: EnvironmentImpl.java    From xodus with Apache License 2.0 6 votes vote down vote up
private static void applyEnvironmentSettings(@NotNull final String location,
                                             @NotNull final EnvironmentConfig ec) {
    final File propsFile = new File(location, ENVIRONMENT_PROPERTIES_FILE);
    if (propsFile.exists() && propsFile.isFile()) {
        try {
            try (InputStream propsStream = new FileInputStream(propsFile)) {
                final Properties envProps = new Properties();
                envProps.load(propsStream);
                for (final Map.Entry<Object, Object> entry : envProps.entrySet()) {
                    ec.setSetting(entry.getKey().toString(), entry.getValue());
                }
            }
        } catch (IOException e) {
            throw ExodusException.toExodusException(e);
        }
    }
}
 
Example #21
Source File: Users.java    From xodus with Apache License 2.0 6 votes vote down vote up
private static void registerNewUser(final Environment env, final Store users, final Store emails, final String username, final String email) {
    env.executeInTransaction(txn -> {
        final ArrayByteIterable usernameEntry = stringToEntry(username);
        final ArrayByteIterable emailEntry = stringToEntry(email);
        final boolean exists;
        try (Cursor usersCursor = users.openCursor(txn)) {
            exists = usersCursor.getSearchBoth(usernameEntry, emailEntry);
            if (!exists) {
                users.put(txn, usernameEntry, emailEntry);
            }
        }
        if (!exists) {
            try (Cursor emailsCursor = emails.openCursor(txn)) {
                if (emailsCursor.getSearchBoth(emailEntry, usernameEntry)) {
                    throw new ExodusException("It can't be: users & emails are inconsistent!");
                }
                emails.put(txn, emailEntry, usernameEntry);
            }
        }
        System.out.println((exists ? "User is already registered: " : "New user registered: ") + username + " " + email);
    });
}
 
Example #22
Source File: ReentrantTransactionDispatcher.java    From xodus with Apache License 2.0 6 votes vote down vote up
/**
 * Release transaction that was acquired in a thread with specified permits.
 */
void releaseTransaction(@NotNull final Thread thread, final int permits) {
    try (CriticalSection ignored = criticalSection.enter()) {
        int currentThreadPermits = getThreadPermits(thread);
        if (permits > currentThreadPermits) {
            throw new ExodusException("Can't release more permits than it was acquired");
        }
        acquiredPermits -= permits;
        currentThreadPermits -= permits;
        if (currentThreadPermits == 0) {
            threadPermits.remove(thread);
        } else {
            threadPermits.put(thread, currentThreadPermits);
        }
        notifyNextWaiters();
    }
}
 
Example #23
Source File: BTree.java    From xodus with Apache License 2.0 6 votes vote down vote up
public BTree(@NotNull final Log log,
             @NotNull final BTreeBalancePolicy policy,
             final long rootAddress,
             final boolean allowsDuplicates,
             final int structureId) {
    super(log, policy, allowsDuplicates, structureId);
    if (rootAddress == Loggable.NULL_ADDRESS) {
        throw new IllegalArgumentException("Can't instantiate not empty tree with null root address.");
    }
    rootLoggable = getLoggable(rootAddress);
    final byte type = rootLoggable.getType();
    // load size, but check if it exists
    if (type != BOTTOM_ROOT && type != INTERNAL_ROOT) {
        throw new ExodusException("Unexpected root page type: " + type);
    }
    final ByteIterableWithAddress data = rootLoggable.getData();
    final ByteIteratorWithAddress it = data.iterator();
    size = CompressedUnsignedLongByteIterable.getLong(it);
    root = loadRootPage(data.clone((int) (it.getAddress() - data.getDataAddress())));
}
 
Example #24
Source File: EnvironmentImpl.java    From xodus with Apache License 2.0 5 votes vote down vote up
@Override
public void truncateStore(@NotNull final String storeName, @NotNull final Transaction txn) {
    final ReadWriteTransaction t = throwIfReadonly(txn, "Can't truncate a store in read-only transaction");
    StoreImpl store = openStore(storeName, StoreConfig.USE_EXISTING, t, false);
    if (store == null) {
        throw new ExodusException("Attempt to truncate unknown store '" + storeName + '\'');
    }
    t.storeRemoved(store);
    final TreeMetaInfo metaInfoCloned = store.getMetaInfo().clone(allocateStructureId());
    store = new StoreImpl(this, storeName, metaInfoCloned);
    t.storeCreated(store);
}
 
Example #25
Source File: ContextualEnvironmentImpl.java    From xodus with Apache License 2.0 5 votes vote down vote up
void finishTransactionUnsafe(@NotNull final TransactionBase txn) {
    final Thread thread = txn.getCreatingThread();
    final Deque<TransactionBase> stack = threadTxns.get(thread);
    if (stack == null) {
        throw new ExodusException("Transaction was already finished");
    }
    if (txn != stack.peek()) {
        throw new ExodusException("Can't finish transaction: nested transaction is not finished");
    }
    stack.pop();
    if (stack.isEmpty()) {
        threadTxns.remove(thread);
    }
    super.finishTransaction(txn);
}
 
Example #26
Source File: ContextualEnvironmentImpl.java    From xodus with Apache License 2.0 5 votes vote down vote up
@Override
protected void finishTransaction(@NotNull final TransactionBase txn) {
    final Thread thread = txn.getCreatingThread();
    if (!Thread.currentThread().equals(thread)) {
        throw new ExodusException("Can't finish transaction in a thread different from the one which it was created in");
    }
    finishTransactionUnsafe(txn);
}
 
Example #27
Source File: ContextualEnvironmentImpl.java    From xodus with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T computeInExclusiveTransaction(@NotNull TransactionalComputable<T> computable) {
    final Transaction current = getCurrentTransaction();
    if (current == null) {
        return super.computeInExclusiveTransaction(computable);
    }
    if (!current.isExclusive()) {
        throw new ExodusException("Current transaction should be exclusive");
    }
    return computable.compute(current);
}
 
Example #28
Source File: ContextualEnvironmentImpl.java    From xodus with Apache License 2.0 5 votes vote down vote up
@Override
public void executeInExclusiveTransaction(@NotNull TransactionalExecutable executable) {
    final Transaction current = getCurrentTransaction();
    if (current == null) {
        super.executeInExclusiveTransaction(executable);
    } else {
        if (!current.isExclusive()) {
            throw new ExodusException("Current transaction should be exclusive");
        }
        executable.execute(current);
    }
}
 
Example #29
Source File: ReentrantTransactionDispatcher.java    From xodus with Apache License 2.0 5 votes vote down vote up
/**
 * Downgrade transaction (making it holding only 1 permit) that was acquired in a thread with specified permits.
 */
void downgradeTransaction(@NotNull final Thread thread, final int permits) {
    if (permits > 1) {
        try (CriticalSection ignored = criticalSection.enter()) {
            int currentThreadPermits = getThreadPermits(thread);
            if (permits > currentThreadPermits) {
                throw new ExodusException("Can't release more permits than it was acquired");
            }
            acquiredPermits -= (permits - 1);
            currentThreadPermits -= (permits - 1);
            threadPermits.put(thread, currentThreadPermits);
            notifyNextWaiters();
        }
    }
}
 
Example #30
Source File: BTreeMutable.java    From xodus with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(@NotNull INode ln) {
    final ByteIterable value = ln.getValue();
    if (value == null) {
        throw new ExodusException("Value can't be null");
    }
    return add(ln.getKey(), value);
}