From cf386901f8df9a104b85b3b503daf731130f02ef Mon Sep 17 00:00:00 2001 From: Tglman Date: Sun, 12 Jan 2025 21:21:02 +0000 Subject: [PATCH] refactor:removed legacy pool implementations, with relative test cases --- .../core/db/OPartitionedDatabasePool.java | 499 ------------------ .../db/OPartitionedDatabasePoolFactory.java | 195 ------- .../db/document/ODatabaseDocumentPool.java | 61 --- .../document/ODatabaseDocumentTxPooled.java | 170 ------ .../core/db/OPartitionedDatabasePoolTest.java | 128 ----- .../ODatabaseDocumentPoolOpenCloseTest.java | 88 --- .../paginated/StorageBackupMTStateTest.java | 6 +- .../SimulateOperationsAgainstServer.java | 13 +- .../orient/server/HookInstallServerTest.java | 5 +- .../auto/CRUDDocumentPhysicalTest.java | 19 - .../auto/DatabaseThreadFactoryTest.java | 78 --- .../test/database/auto/DbClosedTest.java | 43 -- .../test/database/auto/DbCreationTest.java | 82 +-- .../test/database/auto/ObjectDBBaseTest.java | 18 + .../test/database/auto/ObjectTreeTest.java | 2 +- .../orient/test/database/auto/PoolTest.java | 91 ---- .../test/database/auto/SQLDeleteTest.java | 17 +- .../auto/embedded-test-db-from-scratch.xml | 2 - .../auto/remote-test-db-from-scratch.xml | 2 - .../speed/LocalMTCreateDocumentSpeedTest.java | 39 +- .../speed/OrientDbWriteLotsOfDataTest.java | 6 +- 21 files changed, 65 insertions(+), 1499 deletions(-) delete mode 100755 core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePool.java delete mode 100644 core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolFactory.java delete mode 100644 core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPool.java delete mode 100755 core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentTxPooled.java delete mode 100644 core/src/test/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolTest.java delete mode 100755 core/src/test/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPoolOpenCloseTest.java delete mode 100755 tests/src/test/java/com/orientechnologies/orient/test/database/auto/DatabaseThreadFactoryTest.java delete mode 100644 tests/src/test/java/com/orientechnologies/orient/test/database/auto/PoolTest.java diff --git a/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePool.java b/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePool.java deleted file mode 100755 index 16c14849973..00000000000 --- a/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePool.java +++ /dev/null @@ -1,499 +0,0 @@ -/* - * - * * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * For more information: http://orientdb.com - * - */ -package com.orientechnologies.orient.core.db; - -import com.orientechnologies.common.concur.lock.OInterruptedException; -import com.orientechnologies.common.exception.OException; -import com.orientechnologies.common.log.OLogManager; -import com.orientechnologies.common.log.OLogger; -import com.orientechnologies.orient.core.OOrientListenerAbstract; -import com.orientechnologies.orient.core.Orient; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; -import com.orientechnologies.orient.core.exception.ODatabaseException; -import com.orientechnologies.orient.core.exception.OStorageExistsException; -import com.orientechnologies.orient.core.metadata.security.OToken; -import com.orientechnologies.orient.core.storage.OStorage; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.Semaphore; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Database pool which has good multicore scalability characteristics because of creation of several - * partitions for each logical thread group which drastically decrease thread contention when we - * acquire new connection to database. - * - *

To acquire connection from the pool call {@link #acquire()} method but to release connection - * you just need to call {@link - * com.orientechnologies.orient.core.db.document.ODatabaseDocument#close()} method. - * - *

In case of remote storage database pool will keep connections to the remote storage till you - * close pool. So in case of remote storage you should close pool at the end of it's usage, it also - * may be closed on application shutdown but you should not rely on this behaviour. - * - *

This pool has one noticeable difference from other pools. If you perform several subsequent - * acquire calls in the same thread the same instance of database will be returned, but - * amount of calls to close method should match to amount of acquire calls to release database back - * in the pool. It will allow you to use such feature as transaction propagation when you perform - * call of one service from another one. - * - *

Given pool has two parameters now, amount of maximum connections for single partition and - * total amount of connections which may be hold by pool. When you start to use pool it will - * automatically split by several partitions, each partition is independent from other which gives - * us very good multicore scalability. Amount of partitions will be close to amount of cores but it - * is not mandatory and depends how much application is loaded. Amount of connections which may be - * hold by single partition is defined by user but we suggest to use default parameters if your - * application load is not extremely high. - * - *

If total amount of connections which allowed to be hold by this pool is reached thread will - * wait till free connection will be available. If total amount of connection is set to value 0 or - * less it means that there is no connection limit. - * - * @author Andrey Lomakin (a.lomakin-at-orientdb.com) - * @since 06/11/14 - */ -@Deprecated -public class OPartitionedDatabasePool extends OOrientListenerAbstract { - private static final OLogger logger = - OLogManager.instance().logger(OPartitionedDatabasePool.class); - - private static final int HASH_INCREMENT = 0x61c88647; - private static final int MIN_POOL_SIZE = 2; - private static final AtomicInteger nextHashCode = new AtomicInteger(); - protected final Map properties = new HashMap(); - private final String url; - private final String userName; - private final String password; - private final int maxPartitonSize; - private final AtomicBoolean poolBusy = new AtomicBoolean(); - private int maxPartitions = Runtime.getRuntime().availableProcessors(); - private final Semaphore connectionsCounter; - private volatile ThreadLocal poolData = new ThreadPoolData(); - private volatile PoolPartition[] partitions; - private volatile boolean closed = false; - private boolean autoCreate = false; - - @Deprecated - public OPartitionedDatabasePool(String url, String userName, String password) { - this(url, userName, password, Runtime.getRuntime().availableProcessors(), -1); - } - - @Deprecated - public OPartitionedDatabasePool( - String url, String userName, String password, int maxPartitionSize, int maxPoolSize) { - this.url = url; - this.userName = userName; - this.password = password; - if (maxPoolSize > 0) { - connectionsCounter = new Semaphore(maxPoolSize); - this.maxPartitions = 1; - this.maxPartitonSize = maxPoolSize; - } else { - this.maxPartitonSize = maxPartitionSize; - connectionsCounter = null; - } - - final PoolPartition[] pts = new PoolPartition[maxPartitions]; - - for (int i = 0; i < pts.length; i++) { - final PoolPartition partition = new PoolPartition(); - pts[i] = partition; - - initQueue(url, partition); - } - - partitions = pts; - - Orient.instance().registerWeakOrientStartupListener(this); - Orient.instance().registerWeakOrientShutdownListener(this); - } - - private static int nextHashCode() { - return nextHashCode.getAndAdd(HASH_INCREMENT); - } - - public String getUrl() { - return url; - } - - public String getUserName() { - return userName; - } - - public int getMaxPartitonSize() { - return maxPartitonSize; - } - - public int getAvailableConnections() { - checkForClose(); - - int result = 0; - - for (PoolPartition partition : partitions) { - if (partition != null) { - result += partition.currentSize.get() - partition.acquiredConnections.get(); - } - } - - if (result < 0) return 0; - - return result; - } - - public int getCreatedInstances() { - checkForClose(); - - int result = 0; - - for (PoolPartition partition : partitions) { - if (partition != null) { - result += partition.currentSize.get(); - } - } - - if (result < 0) return 0; - - return result; - } - - public ODatabaseDocumentTx acquire() { - checkForClose(); - - final PoolData data = poolData.get(); - if (data.acquireCount > 0) { - data.acquireCount++; - - assert data.acquiredDatabase != null; - - final ODatabaseDocumentTx db = data.acquiredDatabase; - - db.activateOnCurrentThread(); - - properties.entrySet().forEach(p -> db.setProperty(p.getKey(), p.getValue())); - return db; - } - - try { - if (connectionsCounter != null) connectionsCounter.acquire(); - } catch (InterruptedException ie) { - throw OException.wrapException( - new OInterruptedException("Acquiring of new connection was interrupted"), ie); - } - - boolean acquired = false; - try { - while (true) { - final PoolPartition[] pts = partitions; - - final int index = (pts.length - 1) & data.hashCode; - - PoolPartition partition = pts[index]; - if (partition == null) { - if (!poolBusy.get() && poolBusy.compareAndSet(false, true)) { - if (pts == partitions) { - partition = pts[index]; - - if (partition == null) { - partition = new PoolPartition(); - initQueue(url, partition); - pts[index] = partition; - } - } - - poolBusy.set(false); - } - - continue; - } else { - final DatabaseDocumentTxPooled db = partition.queue.poll(); - if (db == null) { - if (pts.length < maxPartitions) { - if (!poolBusy.get() && poolBusy.compareAndSet(false, true)) { - if (pts == partitions) { - final PoolPartition[] newPartitions = new PoolPartition[partitions.length << 1]; - System.arraycopy(partitions, 0, newPartitions, 0, partitions.length); - - partitions = newPartitions; - } - - poolBusy.set(false); - } - - continue; - } else { - if (partition.currentSize.get() >= maxPartitonSize) - throw new IllegalStateException( - "You have reached maximum pool size for given partition"); - - final DatabaseDocumentTxPooled newDb = new DatabaseDocumentTxPooled(url); - properties.entrySet().forEach(p -> newDb.setProperty(p.getKey(), p.getValue())); - openDatabase(newDb); - newDb.partition = partition; - - data.acquireCount = 1; - data.acquiredDatabase = newDb; - - partition.acquiredConnections.incrementAndGet(); - partition.currentSize.incrementAndGet(); - - acquired = true; - return newDb; - } - } else { - properties.entrySet().forEach(p -> db.setProperty(p.getKey(), p.getValue())); - - openDatabase(db); - db.partition = partition; - partition.acquiredConnections.incrementAndGet(); - - data.acquireCount = 1; - data.acquiredDatabase = db; - - acquired = true; - - return db; - } - } - } - } finally { - if (!acquired && connectionsCounter != null) connectionsCounter.release(); - } - } - - public boolean isAutoCreate() { - return autoCreate; - } - - public OPartitionedDatabasePool setAutoCreate(final boolean autoCreate) { - this.autoCreate = autoCreate; - return this; - } - - public boolean isClosed() { - return closed; - } - - protected void openDatabase(final DatabaseDocumentTxPooled db) { - if (autoCreate) { - if (!db.getURL().startsWith("remote:") && !db.exists()) { - try { - db.create(); - } catch (OStorageExistsException ex) { - logger.debug("Can not create storage %s because it already exists.", ex, db.getStorage()); - db.internalOpen(); - } - } else { - db.internalOpen(); - } - } else { - db.internalOpen(); - } - } - - @Override - public void onShutdown() { - close(); - } - - @Override - public void onStartup() { - if (poolData == null) poolData = new ThreadPoolData(); - } - - public void close() { - if (closed) return; - - closed = true; - - for (PoolPartition partition : partitions) { - if (partition == null) continue; - - final Queue queue = partition.queue; - - while (!queue.isEmpty()) { - DatabaseDocumentTxPooled db = queue.poll(); - if (!db.isClosed()) { - db.activateOnCurrentThread(); - db.close(); - } - } - } - - partitions = null; - poolData = null; - } - - private void initQueue(String url, PoolPartition partition) { - ConcurrentLinkedQueue queue = partition.queue; - - for (int n = 0; n < MIN_POOL_SIZE; n++) { - final DatabaseDocumentTxPooled db = new DatabaseDocumentTxPooled(url); - properties.entrySet().forEach(p -> db.setProperty(p.getKey(), p.getValue())); - queue.add(db); - } - - partition.currentSize.addAndGet(MIN_POOL_SIZE); - } - - private void checkForClose() { - if (closed) throw new IllegalStateException("Pool is closed"); - } - - /** - * Sets a property value - * - * @param iName Property name - * @param iValue new value to set - * @return The previous value if any, otherwise null - */ - public Object setProperty(final String iName, final Object iValue) { - if (iValue != null) { - return properties.put(iName.toLowerCase(Locale.ENGLISH), iValue); - } else { - return properties.remove(iName.toLowerCase(Locale.ENGLISH)); - } - } - - /** - * Gets the property value. - * - * @param iName Property name - * @return The previous value if any, otherwise null - */ - public Object getProperty(final String iName) { - return properties.get(iName.toLowerCase(Locale.ENGLISH)); - } - - private static final class PoolData { - private final int hashCode; - private int acquireCount; - private DatabaseDocumentTxPooled acquiredDatabase; - - private PoolData() { - hashCode = nextHashCode(); - } - } - - private static final class PoolPartition { - private final AtomicInteger currentSize = new AtomicInteger(); - private final AtomicInteger acquiredConnections = new AtomicInteger(); - private final ConcurrentLinkedQueue queue = - new ConcurrentLinkedQueue(); - } - - private static class ThreadPoolData extends ThreadLocal { - @Override - protected PoolData initialValue() { - return new PoolData(); - } - } - - private final class DatabaseDocumentTxPooled extends ODatabaseDocumentTx { - private PoolPartition partition; - - private DatabaseDocumentTxPooled(String iURL) { - super(iURL, false); - } - - @Override - public DB open(OToken iToken) { - throw new ODatabaseException("Impossible to open a database managed by a pool "); - } - - @Override - public DB open(String iUserName, String iUserPassword) { - throw new ODatabaseException("Impossible to open a database managed by a pool "); - } - - /** - * @return true if database is obtained from the pool and false - * otherwise. - */ - @Override - public boolean isPooled() { - return true; - } - - protected void internalOpen() { - if (internal == null) { - super.open(userName, password); - } else { - internal.activateOnCurrentThread(); - internal.setStatus(STATUS.OPEN); - } - if (getMetadata().getSchema().countClasses() == 0) getMetadata().reload(); - } - - @Override - public void close() { - if (poolData != null) { - final PoolData data = poolData.get(); - if (data.acquireCount == 0) return; - - data.acquireCount--; - - if (data.acquireCount > 0) return; - - PoolPartition p = partition; - partition = null; - - final OStorage storage = getStorage(); - if (storage == null) return; - - // if connection is lost and storage is closed as result we should not put closed connection - // back to the pool - if (!storage.isClosed()) { - activateOnCurrentThread(); - super.internal.internalClose(true); - - data.acquiredDatabase = null; - - p.queue.offer(this); - } else { - // close database instance but be ready that it will throw exception because of storage is - // closed - try { - super.close(); - } catch (Exception e) { - logger.error( - "Error during closing of database % when storage %s was already closed", - e, getUrl(), storage.getName()); - } - - data.acquiredDatabase = null; - - // we create new connection instead of old one - final DatabaseDocumentTxPooled db = new DatabaseDocumentTxPooled(url); - p.queue.offer(db); - } - - if (connectionsCounter != null) connectionsCounter.release(); - - p.acquiredConnections.decrementAndGet(); - } else { - super.close(); - } - } - } -} diff --git a/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolFactory.java b/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolFactory.java deleted file mode 100644 index bc22c48fec9..00000000000 --- a/core/src/main/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolFactory.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * - * * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * For more information: http://orientdb.com - * - */ -package com.orientechnologies.orient.core.db; - -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; -import com.googlecode.concurrentlinkedhashmap.EvictionListener; -import com.orientechnologies.common.log.OLogManager; -import com.orientechnologies.common.log.OLogger; -import com.orientechnologies.orient.core.OOrientListenerAbstract; -import com.orientechnologies.orient.core.Orient; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; - -/** - * Factory for {@link OPartitionedDatabasePool} pool, which also works as LRU cache with good - * mutlicore architecture support. - * - *

In case of remote storage database pool will keep connections to the remote storage till you - * close pool. So in case of remote storage you should close pool factory at the end of it's usage, - * it also may be closed on application shutdown but you should not rely on this behaviour. - * - * @author Andrey Lomakin (a.lomakin-at-orientdb.com) - * @since 06/11/14 - */ -public class OPartitionedDatabasePoolFactory extends OOrientListenerAbstract { - private static final OLogger logger = - OLogManager.instance().logger(OPartitionedDatabasePoolFactory.class); - private volatile int maxPoolSize = 64; - private boolean closed = false; - - private final ConcurrentLinkedHashMap poolStore; - - private final EvictionListener evictionListener = - new EvictionListener() { - @Override - public void onEviction( - final PoolIdentity poolIdentity, - final OPartitionedDatabasePool partitionedDatabasePool) { - partitionedDatabasePool.close(); - } - }; - - public OPartitionedDatabasePoolFactory() { - this(100); - } - - public OPartitionedDatabasePoolFactory(final int capacity) { - poolStore = - new ConcurrentLinkedHashMap.Builder() - .maximumWeightedCapacity(capacity) - .listener(evictionListener) - .build(); - - Orient.instance().registerWeakOrientStartupListener(this); - Orient.instance().registerWeakOrientShutdownListener(this); - } - - public int getMaxPoolSize() { - return maxPoolSize; - } - - public void setMaxPoolSize(int maxPoolSize) { - checkForClose(); - - this.maxPoolSize = maxPoolSize; - } - - public void reset() { - while (!poolStore.isEmpty()) { - final Iterator poolIterator = poolStore.values().iterator(); - - while (poolIterator.hasNext()) { - final OPartitionedDatabasePool pool = poolIterator.next(); - - try { - pool.close(); - } catch (Exception e) { - logger.error("Error during pool close", e); - } - - poolIterator.remove(); - } - } - - for (OPartitionedDatabasePool pool : poolStore.values()) pool.close(); - - poolStore.clear(); - } - - public OPartitionedDatabasePool get( - final String url, final String userName, final String userPassword) { - checkForClose(); - - final PoolIdentity poolIdentity = new PoolIdentity(url, userName, userPassword); - - OPartitionedDatabasePool pool = poolStore.get(poolIdentity); - if (pool != null && !pool.isClosed()) return pool; - - if (pool != null) poolStore.remove(poolIdentity, pool); - - while (true) { - pool = new OPartitionedDatabasePool(url, userName, userPassword, 8, maxPoolSize); - - final OPartitionedDatabasePool oldPool = poolStore.putIfAbsent(poolIdentity, pool); - - if (oldPool != null) { - if (!oldPool.isClosed()) { - return oldPool; - } else { - poolStore.remove(poolIdentity, oldPool); - } - } else { - return pool; - } - } - } - - public Collection getPools() { - checkForClose(); - - return Collections.unmodifiableCollection(poolStore.values()); - } - - public void close() { - if (closed) return; - - closed = true; - reset(); - } - - private void checkForClose() { - if (closed) throw new IllegalStateException("Pool factory is closed"); - } - - public boolean isClosed() { - return closed; - } - - @Override - public void onShutdown() { - close(); - } - - private static final class PoolIdentity { - private final String url; - private final String userName; - private final String userPassword; - - private PoolIdentity(String url, String userName, String userPassword) { - this.url = url; - this.userName = userName; - this.userPassword = userPassword; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - final PoolIdentity that = (PoolIdentity) o; - - if (!url.equals(that.url)) return false; - if (!userName.equals(that.userName)) return false; - if (!userPassword.equals(that.userPassword)) return false; - - return true; - } - - @Override - public int hashCode() { - int result = url.hashCode(); - result = 31 * result + userName.hashCode(); - result = 31 * result + userPassword.hashCode(); - return result; - } - } -} diff --git a/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPool.java b/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPool.java deleted file mode 100644 index fb14468ae36..00000000000 --- a/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPool.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * - * * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * For more information: http://orientdb.com - * - */ -package com.orientechnologies.orient.core.db.document; - -import com.orientechnologies.orient.core.db.ODatabasePoolBase; - -/** - * @deprecated use {@link com.orientechnologies.orient.core.db.OPartitionedDatabasePool} or {@link - * com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory} instead. - */ -@Deprecated -public class ODatabaseDocumentPool extends ODatabasePoolBase { - - private static ODatabaseDocumentPool globalInstance = new ODatabaseDocumentPool(); - - public ODatabaseDocumentPool() { - super(); - } - - public ODatabaseDocumentPool( - final String iURL, final String iUserName, final String iUserPassword) { - super(iURL, iUserName, iUserPassword); - } - - public static ODatabaseDocumentPool global() { - globalInstance.setup(); - return globalInstance; - } - - public static ODatabaseDocumentPool global(final int iPoolMin, final int iPoolMax) { - globalInstance.setup(iPoolMin, iPoolMax); - return globalInstance; - } - - @Override - protected ODatabaseDocumentTx createResource( - Object owner, String iDatabaseName, Object... iAdditionalArgs) { - return new ODatabaseDocumentTxPooled( - (ODatabaseDocumentPool) owner, - iDatabaseName, - (String) iAdditionalArgs[0], - (String) iAdditionalArgs[1]); - } -} diff --git a/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentTxPooled.java b/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentTxPooled.java deleted file mode 100755 index c22b3448b9b..00000000000 --- a/core/src/main/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentTxPooled.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * - * * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * * - * * For more information: http://orientdb.com - * - */ -package com.orientechnologies.orient.core.db.document; - -import com.orientechnologies.common.log.OLogManager; -import com.orientechnologies.common.log.OLogger; -import com.orientechnologies.orient.core.db.ODatabase; -import com.orientechnologies.orient.core.db.ODatabasePoolBase; -import com.orientechnologies.orient.core.db.ODatabasePooled; -import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal; -import com.orientechnologies.orient.core.exception.ODatabaseException; -import com.orientechnologies.orient.core.metadata.security.OToken; - -/** - * Pooled wrapper to the ODatabaseDocumentTx class. Allows to being reused across calls. The close() - * method does not close the database for real but release it to the owner pool. The database born - * as opened and will leave open until the pool is closed. - * - * @author Luca Garulli (l.garulli--(at)--orientdb.com) - * @see ODatabasePoolBase - */ -@SuppressWarnings("unchecked") -public class ODatabaseDocumentTxPooled extends ODatabaseDocumentTx implements ODatabasePooled { - private static final OLogger logger = - OLogManager.instance().logger(ODatabaseDocumentTxPooled.class); - - private ODatabaseDocumentPool ownerPool; - private String userName; - - public ODatabaseDocumentTxPooled( - final ODatabaseDocumentPool iOwnerPool, - final String iURL, - final String iUserName, - final String iUserPassword) { - super(iURL); - ownerPool = iOwnerPool; - userName = iUserName; - - super.open(iUserName, iUserPassword); - } - - public void reuse(final Object iOwner, final Object[] iAdditionalArgs) { - ownerPool = (ODatabaseDocumentPool) iOwner; - getLocalCache().invalidate(); - // getMetadata().reload(); - ODatabaseRecordThreadLocal.instance().set(this); - - try { - callOnOpenListeners(); - } catch (Exception e) { - logger.error("Error on reusing database '%s' in pool", e, getName()); - } - } - - @Override - public ODatabaseDocumentTxPooled open(final String iUserName, final String iUserPassword) { - throw new UnsupportedOperationException( - "Database instance was retrieved from a pool. You cannot open the database in this way. Use" - + " directly a ODatabaseDocumentTx instance if you want to manually open the" - + " connection"); - } - - @Override - public ODatabaseDocumentTxPooled open(final OToken iToken) { - throw new UnsupportedOperationException( - "Database instance was retrieved from a pool. You cannot open the database in this way. Use" - + " directly a ODatabaseDocumentTx instance if you want to manually open the" - + " connection"); - } - - @Override - public ODatabaseDocumentTxPooled create() { - throw new UnsupportedOperationException( - "Database instance was retrieved from a pool. You cannot open the database in this way. Use" - + " directly a ODatabaseDocumentTx instance if you want to manually open the" - + " connection"); - } - - @Override - public DB create(String incrementalBackupPath) { - throw new UnsupportedOperationException( - "Database instance was retrieved from a pool. You cannot open the database in this way. Use" - + " directly a ODatabaseDocumentTx instance if you want to manually open the" - + " connection"); - } - - public boolean isUnderlyingOpen() { - return !super.isClosed(); - } - - @Override - public boolean isClosed() { - return ownerPool == null || super.isClosed(); - } - - /** - * @return true if database is obtained from the pool and false - * otherwise. - */ - @Override - public boolean isPooled() { - return true; - } - - /** Avoid to close it but rather release itself to the owner pool. */ - @Override - public void close() { - if (isClosed()) return; - - checkOpenness(); - - if (ownerPool != null && ownerPool.getConnectionsInCurrentThread(getURL(), userName) > 1) { - ownerPool.release(this); - return; - } - - try { - commit(true); - } catch (Exception e) { - logger.error("Error on releasing database '%s' in pool", e, getName()); - } - - try { - callOnCloseListeners(); - } catch (Exception e) { - logger.error("Error on releasing database '%s' in pool", e, getName()); - } - - getLocalCache().clear(); - - if (ownerPool != null) { - final ODatabaseDocumentPool localCopy = ownerPool; - ownerPool = null; - localCopy.release(this); - } - - ODatabaseRecordThreadLocal.instance().remove(); - } - - public void forceClose() { - super.close(); - } - - // @Override - protected void checkOpenness() { - if (ownerPool == null) - throw new ODatabaseException( - "Database instance has been released to the pool. Get another database instance from the" - + " pool with the right username and password"); - - // super.checkOpenness(); - } -} diff --git a/core/src/test/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolTest.java b/core/src/test/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolTest.java deleted file mode 100644 index a6cdfecda1d..00000000000 --- a/core/src/test/java/com/orientechnologies/orient/core/db/OPartitionedDatabasePoolTest.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.orientechnologies.orient.core.db; - -import static com.orientechnologies.orient.core.config.OGlobalConfiguration.STORAGE_ENCRYPTION_KEY; -import static com.orientechnologies.orient.core.config.OGlobalConfiguration.STORAGE_ENCRYPTION_METHOD; -import static org.assertj.core.api.Assertions.assertThat; - -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; -import com.orientechnologies.orient.core.sql.executor.OResultSet; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import org.junit.After; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; - -/** Created by frank on 29/06/2016. */ -public class OPartitionedDatabasePoolTest { - - @Rule public TestName name = new TestName(); - - private ODatabaseDocument db; - private OPartitionedDatabasePool pool; - - @Before - public void setUp() throws Exception { - db = new ODatabaseDocumentTx("memory:" + name.getMethodName()).create(); - pool = new OPartitionedDatabasePool(db.getURL(), "admin", "admin"); - } - - @After - public void tearDown() throws Exception { - - db.activateOnCurrentThread(); - db.drop(); - } - - @Test - public void shouldAutoCreateDatabase() throws Exception { - - ODatabaseDocument db = pool.acquire(); - - assertThat(db.exists()).isTrue(); - assertThat(db.isClosed()).isFalse(); - db.close(); - - assertThat(db.isClosed()).isTrue(); - - pool.close(); - } - - @Test(expected = IllegalStateException.class) - public void shouldThrowIllegalStateWhenAcquireAfterClose() throws Exception { - - pool.close(); - - pool.acquire(); - } - - @Test - public void shouldReturnSameDatabaseOnSameThread() throws Exception { - - ODatabaseDocument db1 = pool.acquire(); - ODatabaseDocument db2 = pool.acquire(); - - assertThat(db1).isSameAs(db2); - - db1.close(); - - // same instances!!! - assertThat(db1.isClosed()).isFalse(); - assertThat(db2.isClosed()).isFalse(); - - db2.close(); - assertThat(db2.isClosed()).isTrue(); - - pool.close(); - } - - @Test - public void testMultiThread() { - - Assume.assumeTrue(Runtime.getRuntime().availableProcessors() > 2); - // do a query and assert on other thread - Runnable acquirer = - () -> { - ODatabaseDocument db = pool.acquire(); - - try { - assertThat(db.isActiveOnCurrentThread()).isTrue(); - - OResultSet res = db.query("SELECT * FROM OUser"); - - assertThat(res.stream()).hasSize(3); - - } finally { - - db.close(); - } - }; - - // spawn 20 threads - List> futures = - IntStream.range(0, 19) - .boxed() - .map(i -> CompletableFuture.runAsync(acquirer)) - .collect(Collectors.toList()); - - futures.forEach(cf -> cf.join()); - } - - @Test - public void shouldUseEncryption() throws Exception { - - pool.setProperty(STORAGE_ENCRYPTION_METHOD.getKey(), "aes"); - pool.setProperty(STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA=="); - - ODatabaseDocument dbFromPool = pool.acquire(); - - assertThat(dbFromPool.getProperty(STORAGE_ENCRYPTION_METHOD.getKey())).isEqualTo("aes"); - assertThat(dbFromPool.getProperty(STORAGE_ENCRYPTION_KEY.getKey())) - .isEqualTo("T1JJRU5UREJfSVNfQ09PTA=="); - } -} diff --git a/core/src/test/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPoolOpenCloseTest.java b/core/src/test/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPoolOpenCloseTest.java deleted file mode 100755 index 69521e6af44..00000000000 --- a/core/src/test/java/com/orientechnologies/orient/core/db/document/ODatabaseDocumentPoolOpenCloseTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.orientechnologies.orient.core.db.document; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; -import com.orientechnologies.orient.core.exception.ODatabaseException; -import com.orientechnologies.orient.core.metadata.schema.OClass; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class ODatabaseDocumentPoolOpenCloseTest { - private ODatabaseDocument dbo; - - @Before - public void setUp() throws Exception { - String url = "memory:" + ODatabaseDocumentPoolOpenCloseTest.class.getSimpleName(); - dbo = new ODatabaseDocumentTx(url).create(); - } - - @After - public void tearDown() throws Exception { - dbo.activateOnCurrentThread(); - dbo.drop(); - } - - @Test - public void openCloseClearThreadLocal() { - OPartitionedDatabasePool pool = new OPartitionedDatabasePool(dbo.getURL(), "admin", "admin"); - try { - ODatabaseDocument db = pool.acquire(); - db.close(); - assertNull(ODatabaseRecordThreadLocal.instance().getIfDefined()); - } finally { - pool.close(); - } - } - - @Test(expected = ODatabaseException.class) - public void failureOpenPoolDatabase() { - OPartitionedDatabasePool pool = new OPartitionedDatabasePool(dbo.getURL(), "admin", "admin"); - try { - ODatabaseDocument db = pool.acquire(); - db.open("admin", "admin"); - } finally { - pool.close(); - } - } - - @Test - public void checkSchemaRefresh() throws ExecutionException, InterruptedException { - final OPartitionedDatabasePool pool = - new OPartitionedDatabasePool(dbo.getURL(), "admin", "admin"); - try { - ODatabaseDocument db = pool.acquire(); - ExecutorService exec = Executors.newSingleThreadExecutor(); - Future f = - exec.submit( - new Callable() { - - @Override - public Object call() throws Exception { - ODatabaseDocument db1 = pool.acquire(); - db1.getMetadata().getSchema().createClass("Test"); - db1.close(); - return null; - } - }); - f.get(); - - exec.shutdown(); - - db.activateOnCurrentThread(); - OClass clazz = db.getMetadata().getSchema().getClass("Test"); - assertNotNull(clazz); - db.close(); - } finally { - pool.close(); - } - } -} diff --git a/core/src/test/java/com/orientechnologies/orient/core/storage/impl/local/paginated/StorageBackupMTStateTest.java b/core/src/test/java/com/orientechnologies/orient/core/storage/impl/local/paginated/StorageBackupMTStateTest.java index 90b5e2477a4..b11fabfa052 100755 --- a/core/src/test/java/com/orientechnologies/orient/core/storage/impl/local/paginated/StorageBackupMTStateTest.java +++ b/core/src/test/java/com/orientechnologies/orient/core/storage/impl/local/paginated/StorageBackupMTStateTest.java @@ -6,7 +6,7 @@ import com.orientechnologies.orient.core.command.OCommandOutputListener; import com.orientechnologies.orient.core.config.OGlobalConfiguration; import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; +import com.orientechnologies.orient.core.db.ODatabasePool; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.db.record.ridbag.ORidBag; @@ -59,7 +59,7 @@ public class StorageBackupMTStateTest { private File backupDir; private volatile boolean stop = false; - private volatile OPartitionedDatabasePool pool; + private volatile ODatabasePool pool; @Test @Ignore @@ -97,7 +97,7 @@ public void testRun() throws Exception { databaseDocumentTx.close(); - pool = new OPartitionedDatabasePool(dbURL, "admin", "admin"); + pool = new ODatabasePool(dbURL, "admin", "admin"); System.out.println("Start data modification"); final ExecutorService executor = Executors.newFixedThreadPool(5); diff --git a/distributed/src/test/java/com/orientechnologies/orient/server/distributed/SimulateOperationsAgainstServer.java b/distributed/src/test/java/com/orientechnologies/orient/server/distributed/SimulateOperationsAgainstServer.java index f77d138d1b9..23e4ec8231b 100644 --- a/distributed/src/test/java/com/orientechnologies/orient/server/distributed/SimulateOperationsAgainstServer.java +++ b/distributed/src/test/java/com/orientechnologies/orient/server/distributed/SimulateOperationsAgainstServer.java @@ -16,12 +16,15 @@ package com.orientechnologies.orient.server.distributed; import com.orientechnologies.orient.core.config.OGlobalConfiguration; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory; +import com.orientechnologies.orient.core.db.OrientDB; +import com.orientechnologies.orient.core.db.OrientDBConfig; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.exception.OConcurrentModificationException; import com.orientechnologies.orient.core.exception.ORecordNotFoundException; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.executor.OResult; +import com.orientechnologies.orient.core.util.OURLConnection; +import com.orientechnologies.orient.core.util.OURLHelper; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutorService; @@ -41,7 +44,7 @@ public class SimulateOperationsAgainstServer { protected String userName = "admin"; protected String userPassword = "admin"; - private final OPartitionedDatabasePoolFactory poolFactory = new OPartitionedDatabasePoolFactory(); + private OrientDB ctx; public static void main(String[] args) { new SimulateOperationsAgainstServer().randomExecute(); @@ -257,6 +260,10 @@ protected void log( } protected ODatabaseDocument getDatabase(final String dbUrl) { - return poolFactory.get(dbUrl, userName, userPassword).acquire(); + OURLConnection data = OURLHelper.parse(dbUrl); + if (ctx == null) { + ctx = new OrientDB(data.getType() + ":" + data.getPath(), OrientDBConfig.defaultConfig()); + } + return ctx.open(data.getDbName(), userName, userPassword); } } diff --git a/server/src/test/java/com/orientechnologies/orient/server/HookInstallServerTest.java b/server/src/test/java/com/orientechnologies/orient/server/HookInstallServerTest.java index 12ec79cec4a..1b0a39a6ea3 100755 --- a/server/src/test/java/com/orientechnologies/orient/server/HookInstallServerTest.java +++ b/server/src/test/java/com/orientechnologies/orient/server/HookInstallServerTest.java @@ -2,7 +2,7 @@ import com.orientechnologies.common.io.OFileUtils; import com.orientechnologies.orient.core.Orient; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; +import com.orientechnologies.orient.core.db.ODatabasePool; import com.orientechnologies.orient.core.db.OrientDB; import com.orientechnologies.orient.core.db.OrientDBConfig; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; @@ -100,8 +100,7 @@ public void after() throws IOException { public void test() { final int initValue = count; - OPartitionedDatabasePool pool = - new OPartitionedDatabasePool("remote:localhost/test", "admin", "adminpwd"); + ODatabasePool pool = new ODatabasePool("remote:localhost/test", "admin", "adminpwd"); for (int i = 0; i < 10; i++) { ODatabaseDocument some = pool.acquire(); try { diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/CRUDDocumentPhysicalTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/CRUDDocumentPhysicalTest.java index c2394ad2d6d..1a5933515cf 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/CRUDDocumentPhysicalTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/CRUDDocumentPhysicalTest.java @@ -18,10 +18,8 @@ import com.orientechnologies.orient.core.db.ODatabase.OPERATION_MODE; import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal; import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentAbstract; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.db.record.OIdentifiable; import com.orientechnologies.orient.core.exception.ORecordNotFoundException; import com.orientechnologies.orient.core.id.ORID; @@ -72,23 +70,6 @@ public CRUDDocumentPhysicalTest(@Optional String url) { super(url); } - @Test - public void testPool() { - OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin"); - @SuppressWarnings("deprecation") - final ODatabaseDocument[] dbs = new ODatabaseDocumentTx[pool.getMaxPartitonSize()]; - - for (int i = 0; i < 10; ++i) { - for (int db = 0; db < dbs.length; ++db) - //noinspection resource - dbs[db] = pool.acquire(); - //noinspection deprecation - for (ODatabaseDocument oDatabaseDocumentTx : dbs) oDatabaseDocumentTx.close(); - } - - pool.close(); - } - @Test public void cleanAll() { record = database.newInstance(); diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DatabaseThreadFactoryTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DatabaseThreadFactoryTest.java deleted file mode 100755 index 8c5506ba3a5..00000000000 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DatabaseThreadFactoryTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.orientechnologies.orient.test.database.auto; - -import com.orientechnologies.orient.core.Orient; -import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal; -import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal; -import com.orientechnologies.orient.core.db.ODatabaseThreadLocalFactory; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.exception.ODatabaseException; -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Optional; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -/** @author Luca Molino (molino.luca--at--gmail.com) */ -@Test -public class DatabaseThreadFactoryTest extends DocumentDBBaseTest { - private final OPartitionedDatabasePoolFactory poolFactory = new OPartitionedDatabasePoolFactory(); - - @Parameters(value = "url") - public DatabaseThreadFactoryTest(@Optional String url) { - super(url); - } - - @BeforeMethod - @Override - public void beforeMethod() throws Exception {} - - @BeforeClass - public void init() { - try { - ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().get(); - db.close(); - ODatabaseRecordThreadLocal.instance().remove(); - } catch (ODatabaseException ode) { - } - } - - @Test(expectedExceptions = {ODatabaseException.class}) - public void testNoFactory() { - ODatabaseRecordThreadLocal.instance().get(); - Assert.fail("Database Should not be set in Current Thread"); - } - - @Test(dependsOnMethods = "testNoFactory") - public void testFactory() { - Orient.instance() - .registerThreadDatabaseFactory( - new ODatabaseThreadLocalFactory() { - - @Override - public ODatabaseDocumentInternal getThreadDatabase() { - return poolFactory.get(url, "admin", "admin").acquire(); - } - }); - ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().get(); - Assert.assertNotNull(db); - db.close(); - } -} diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbClosedTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbClosedTest.java index 80c1b497580..2415c9b2a0b 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbClosedTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbClosedTest.java @@ -17,18 +17,12 @@ import com.orientechnologies.orient.core.config.OGlobalConfiguration; import com.orientechnologies.orient.core.db.ODatabase; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.test.database.base.SetupTest; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.Test; @Test(groups = "db") public class DbClosedTest extends DocumentDBBaseTest { - private OPartitionedDatabasePool pool; @Parameters(value = {"url"}) public DbClosedTest(@Optional String url) { @@ -37,43 +31,6 @@ public DbClosedTest(@Optional String url) { setDropDb(true); } - @BeforeClass - public void before() { - pool = new OPartitionedDatabasePool(url, "admin", "admin"); - } - - @AfterClass - public void after() { - pool.close(); - } - - public void testDoubleDb() { - ODatabaseDocument db = pool.acquire(); - - // now I am getting another db instance - ODatabaseDocument dbAnother = pool.acquire(); - dbAnother.close(); - - db.activateOnCurrentThread(); - db.close(); - } - - public void testDoubleDbWindowsPath() { - ODatabaseDocument db = pool.acquire(); - - // now I am getting another db instance - ODatabaseDocument dbAnother = pool.acquire(); - dbAnother.close(); - - db.activateOnCurrentThread(); - db.close(); - } - - @Test(dependsOnMethods = {"testDoubleDb", "testDoubleDbWindowsPath"}) - public void testStorageClosed() { - if (SetupTest.instance().isReuseDatabase()) return; - } - @Test public void testRemoteConns() { if (!url.startsWith("remote:")) return; diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbCreationTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbCreationTest.java index 05eb4473b9e..0c68bcd90ef 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbCreationTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/DbCreationTest.java @@ -18,9 +18,7 @@ import com.orientechnologies.orient.client.db.ODatabaseHelper; import com.orientechnologies.orient.core.Orient; import com.orientechnologies.orient.core.db.ODatabase; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.exception.ODatabaseException; import com.orientechnologies.orient.core.exception.OStorageException; @@ -40,8 +38,6 @@ @Test(groups = "db") public class DbCreationTest extends ObjectDBBaseTest { - private OPartitionedDatabasePool pool; - @Parameters(value = "url") public DbCreationTest(@Optional String url) { super(url); @@ -52,15 +48,11 @@ public DbCreationTest(@Optional String url) { @BeforeClass @Override - public void beforeClass() throws Exception { - pool = new OPartitionedDatabasePool(url, "admin", "admin"); - } + public void beforeClass() throws Exception {} @AfterClass @Override - public void afterClass() throws Exception { - pool.close(); - } + public void afterClass() throws Exception {} @BeforeMethod @Override @@ -71,9 +63,7 @@ public void beforeMethod() throws Exception {} public void afterMethod() throws Exception {} @AfterMethod - public void tearDown() { - if (url.contains("remote:")) ODatabaseDocumentPool.global().close(); - } + public void tearDown() {} @Test public void testDbCreationDefault() throws IOException { @@ -155,72 +145,6 @@ public void testSubFolderDbCreate() throws IOException { ODatabaseHelper.dropDatabase(db, getStorageType()); } - @Test(dependsOnMethods = {"testChangeLocale"}) - public void testSubFolderDbCreateConnPool() throws IOException { - int pos = url.lastIndexOf("/"); - - final String u; - if (pos > -1) u = url.substring(0, pos) + "/sub/subTest"; - else { - pos = url.lastIndexOf(":"); - u = url.substring(0, pos + 1) + "sub/subTest"; - } - - ODatabaseDocument db = new ODatabaseDocumentTx(u); - - ODatabaseHelper.dropDatabase(db, getStorageType()); - ODatabaseHelper.createDatabase(db, u, getStorageType()); - - db = ODatabaseDocumentPool.global().acquire(u, "admin", "admin"); - if (u.startsWith("remote:")) db.close(); - - ODatabaseHelper.dropDatabase(db, getStorageType()); - } - - @Test(dependsOnMethods = "testSubFolderDbCreateConnPool") - public void testCreateAndConnectionPool() throws IOException { - ODatabaseDocument db = new ODatabaseDocumentTx(url); - - db.activateOnCurrentThread(); - ODatabaseHelper.dropDatabase(db, getStorageType()); - - ODatabaseHelper.createDatabase(db, url, getStorageType()); - - if (pool != null) { - pool.close(); - } - pool = new OPartitionedDatabasePool(url, "admin", "admin"); - - // Get connection from pool - db = pool.acquire(); - db.close(); - - // Destroy db in the back of the pool - db = new ODatabaseDocumentTx(url); - ODatabaseHelper.dropDatabase(db, getStorageType()); - - // Re-create it so that the db exists for the pool - db = new ODatabaseDocumentTx(url); - ODatabaseHelper.createDatabase(db, url, getStorageType()); - } - - @Test(dependsOnMethods = {"testCreateAndConnectionPool"}) - public void testOpenCloseConnectionPool() throws IOException { - ODatabaseDocument db = new ODatabaseDocumentTx(url); - if (!ODatabaseHelper.existsDatabase(db, null)) { - ODatabaseHelper.createDatabase(db, url, getStorageType()); - db.close(); - } - if (pool != null) { - pool.close(); - } - pool = new OPartitionedDatabasePool(url, "admin", "admin"); - - for (int i = 0; i < 500; i++) { - pool.acquire().close(); - } - } - @Test(dependsOnMethods = {"testChangeLocale"}) public void testSubFolderMultipleDbCreateSameName() throws IOException { int pos = url.lastIndexOf("/"); diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectDBBaseTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectDBBaseTest.java index e8b9f13f6b1..e2c83fdc965 100644 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectDBBaseTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectDBBaseTest.java @@ -44,6 +44,24 @@ protected ODatabaseSession rawSession(String user, String password) { return session; } + protected ODatabaseSession rawSession(String suffix, String user, String password) { + ODatabaseDocumentTx session = new ODatabaseDocumentTx(this.url + suffix); + session.open(user, password); + return session; + } + + protected ODatabaseObject session(String suffix, String user, String password) { + OObjectDatabaseTx session = new OObjectDatabaseTx(this.url + suffix); + session.open(user, password); + return session; + } + + protected void dropAndCreateDatabase(String suffix) throws IOException { + ODatabaseObject database = new OObjectDatabaseTx(url + suffix); + ODatabaseHelper.dropDatabase(database, getStorageType()); + ODatabaseHelper.createDatabase(database, url + suffix, getStorageType()); + } + protected void reopendb(String user, String password) { if (!database.isClosed() && !database.isActiveOnCurrentThread()) { database = new OObjectDatabaseTx(this.url); diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectTreeTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectTreeTest.java index 15816f552cc..022b0dfe411 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectTreeTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/ObjectTreeTest.java @@ -902,7 +902,7 @@ public void testCascadeDeleteMap() { database.delete(test); } - @Test(dependsOnMethods = "testPool") + @Test() public void testCustomTypes() { OObjectSerializerContext serializerContext = new OObjectSerializerContext(); serializerContext.bind( diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/PoolTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/PoolTest.java deleted file mode 100644 index a7e4643c9ee..00000000000 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/PoolTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.orientechnologies.orient.test.database.auto; - -import com.orientechnologies.orient.core.config.OGlobalConfiguration; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import org.testng.Assert; -import org.testng.annotations.Optional; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -/** - * @author Andrey Lomakin (a.lomakin-at-orientdb.com) - * @since 3/31/14 - */ -@Test -public class PoolTest extends DocumentDBBaseTest { - private int counter = 0; - private final Object lock = new Object(); - - private final CountDownLatch latch = new CountDownLatch(1); - - @Parameters(value = "url") - public PoolTest(@Optional String url) { - super(url); - } - - public void testPoolSize() throws Exception { - ExecutorService executorService = Executors.newCachedThreadPool(); - - final int maxSize = OGlobalConfiguration.DB_POOL_MAX.getValueAsInteger(); - final Random random = new Random(); - - final List> futures = new ArrayList>(); - - for (int i = 0; i < maxSize / 2; i++) - futures.add(executorService.submit(new Acquirer(maxSize, random))); - - latch.countDown(); - - for (Future future : futures) future.get(); - } - - private final class Acquirer implements Callable { - private final int maxSize; - private final Random random; - - private Acquirer(int maxSize, Random random) { - this.maxSize = maxSize; - this.random = random; - } - - @Override - public Void call() throws Exception { - final int delay = random.nextInt(500) + 200; - - ODatabaseDocument databaseDocumentTx; - synchronized (lock) { - databaseDocumentTx = ODatabaseDocumentPool.global().acquire(url, "admin", "admin"); - counter++; - } - - try { - latch.await(); - Thread.sleep(delay); - - Assert.assertEquals(ODatabaseDocumentPool.global().getMaxSize(), maxSize); - - synchronized (lock) { - Assert.assertEquals( - ODatabaseDocumentPool.global().getAvailableConnections(url, "admin"), - maxSize - counter); - } - } finally { - synchronized (lock) { - databaseDocumentTx.close(); - counter--; - } - } - - return null; - } - } -} diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLDeleteTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLDeleteTest.java index 2461fc211d2..40f283c8150 100644 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLDeleteTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLDeleteTest.java @@ -15,8 +15,6 @@ */ package com.orientechnologies.orient.test.database.auto; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; -import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.sql.executor.OResultSet; import org.testng.Assert; import org.testng.annotations.Optional; @@ -51,25 +49,24 @@ public void deleteWithWhereOperator() { } @Test - public void deleteInPool() { - OPartitionedDatabasePool pool = new OPartitionedDatabasePool(url, "admin", "admin"); - ODatabaseDocument db = pool.acquire(); + public void delete() { - final Long total = db.countClass("Profile"); + final Long total = database.countClass("Profile"); OResultSet resultset = - db.query("select from Profile where sex = 'male' and salary > 120 and salary <= 133"); + database.query("select from Profile where sex = 'male' and salary > 120 and salary <= 133"); long queryCount = resultset.stream().count(); OResultSet records = - db.command("delete from Profile where sex = 'male' and salary > 120 and salary <= 133"); + database.command( + "delete from Profile where sex = 'male' and salary > 120 and salary <= 133"); long count = records.next().getProperty("count"); Assert.assertEquals(count, queryCount); - Assert.assertEquals(db.countClass("Profile"), total - count); + Assert.assertEquals(database.countClass("Profile"), total - count); - db.close(); + database.close(); } } diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/embedded-test-db-from-scratch.xml b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/embedded-test-db-from-scratch.xml index 213848f9f69..327601b9c5c 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/embedded-test-db-from-scratch.xml +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/embedded-test-db-from-scratch.xml @@ -156,11 +156,9 @@ - - diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/remote-test-db-from-scratch.xml b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/remote-test-db-from-scratch.xml index 220ab8bee4b..0f5ed6c0beb 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/remote-test-db-from-scratch.xml +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/remote-test-db-from-scratch.xml @@ -149,11 +149,9 @@ - - diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/speed/LocalMTCreateDocumentSpeedTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/speed/LocalMTCreateDocumentSpeedTest.java index c8e9cc3cee5..13fc0a42068 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/speed/LocalMTCreateDocumentSpeedTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/speed/LocalMTCreateDocumentSpeedTest.java @@ -1,14 +1,13 @@ package com.orientechnologies.orient.test.database.speed; -import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePoolFactory; +import com.orientechnologies.orient.core.db.OrientDB; +import com.orientechnologies.orient.core.db.OrientDBConfig; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; -import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.core.id.ORecordId; import com.orientechnologies.orient.core.metadata.security.OSecurity; import com.orientechnologies.orient.core.record.impl.ODocument; -import com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializerBinary; +import com.orientechnologies.orient.core.util.OURLConnection; +import com.orientechnologies.orient.core.util.OURLHelper; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -29,7 +28,8 @@ @Test public class LocalMTCreateDocumentSpeedTest { private static final Random random = new Random(); - private ODatabaseDocumentInternal database; + private OURLConnection data; + private ODatabaseDocument database; private Date date = new Date(); private CountDownLatch latch = new CountDownLatch(1); private List futures; @@ -38,19 +38,20 @@ public class LocalMTCreateDocumentSpeedTest { private final List users = new ArrayList(); - private final OPartitionedDatabasePoolFactory poolFactory = new OPartitionedDatabasePoolFactory(); + private OrientDB ctx; @BeforeClass public void init() { - database = new ODatabaseDocumentTx(System.getProperty("url")); - database.setSerializer(new ORecordSerializerBinary()); - - if (database.exists()) { - database.open("admin", "admin"); - database.drop(); + data = OURLHelper.parse(System.getProperty("url")); + ctx = new OrientDB(data.getType() + ":" + data.getPath(), OrientDBConfig.defaultConfig()); + if (ctx.exists(data.getDbName())) { + ctx.drop(data.getDbName()); } + ctx.execute( + "create database ? " + data.getDbType() + " users(admin identfied by 'admin' role admin)", + data.getDbName()); + database = ctx.open(data.getDbName(), "admin", "admin"); - database.create(); database.getMetadata().getSchema().createClass("Account"); final OSecurity security = database.getMetadata().getSecurity(); @@ -104,7 +105,7 @@ public void cycle() throws Exception { @AfterClass public void deinit() { - if (database != null) database.drop(); + ctx.drop(data.getDbName()); } private final class Saver implements Callable { @@ -122,9 +123,7 @@ public Long call() throws Exception { final String user = users.get(random.nextInt(users.size())); - final OPartitionedDatabasePool pool = - poolFactory.get(System.getProperty("url"), user, user); - final ODatabaseDocument database = pool.acquire(); + final ODatabaseDocument database = ctx.open(data.getDbName(), user, user); ODocument record = new ODocument("Account"); record.field("id", 1); @@ -159,9 +158,7 @@ public Reader(long docCount, int clusterId) { public Long call() throws Exception { latch.await(); - final OPartitionedDatabasePool pool = - poolFactory.get(System.getProperty("url"), "admin", "admin"); - final ODatabaseDocument database = pool.acquire(); + final ODatabaseDocument database = ctx.open(data.getDbName(), "admin", "admin"); long counter = 0; long start = System.nanoTime(); diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/speed/OrientDbWriteLotsOfDataTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/speed/OrientDbWriteLotsOfDataTest.java index c289b75175b..1b7d499efaa 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/speed/OrientDbWriteLotsOfDataTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/speed/OrientDbWriteLotsOfDataTest.java @@ -15,7 +15,7 @@ */ package com.orientechnologies.orient.test.database.speed; -import com.orientechnologies.orient.core.db.OPartitionedDatabasePool; +import com.orientechnologies.orient.core.db.ODatabasePool; import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OSchema; @@ -46,7 +46,7 @@ public static void main(String[] args) throws InterruptedException { public void testThreaded(int numthreads, TXTYPE txtype) throws InterruptedException { // create document pool - OPartitionedDatabasePool pool = new OPartitionedDatabasePool(DBURI, DBUSR, DBPWD); + ODatabasePool pool = new ODatabasePool(DBURI, DBUSR, DBPWD); // create the schema for the test class if it doesn't exist ODatabaseDocument db = pool.acquire(); @@ -102,7 +102,7 @@ class RunTest extends Thread { private double statTotalSecs; private TXTYPE txtype; - public RunTest(OPartitionedDatabasePool pool, TXTYPE txtype) { + public RunTest(ODatabasePool pool, TXTYPE txtype) { this.txtype = txtype; this.db = pool.acquire(); }