Skip to content

Commit

Permalink
refactor: Use of ScalableRWLock and minor fix in the storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Feb 8, 2024
1 parent dcf6210 commit a191162
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ public boolean exists() {
"Cannot check the existence of a database in a remote server. Please use the console or the OServerAdmin class.");
}

public void close(final boolean iForce, boolean onDelete) {
public void close(final boolean iForce) {
if (status == STATUS.CLOSED) return;

final OStorageRemoteSession session = getCurrentSession();
Expand Down Expand Up @@ -697,7 +697,7 @@ public void shutdown() {
if (status == STATUS.CLOSED) return;

status = STATUS.CLOSING;
close(true, false);
close(true);
} finally {
stateLock.writeLock().unlock();
}
Expand Down Expand Up @@ -2466,7 +2466,7 @@ public STATUS getStatus() {
}

public void close() {
close(false, false);
close(false);
}

public boolean dropCluster(final String iClusterName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* * For more information: http://orientdb.com
*
*/

package com.orientechnologies.orient.core.db;

import static com.orientechnologies.orient.core.config.OGlobalConfiguration.FILE_DELETE_DELAY;
Expand Down Expand Up @@ -61,7 +62,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -84,13 +84,15 @@ public class OrientDBEmbedded implements OrientDBInternal {

/** Keeps track of next possible storage id. */
private static final AtomicInteger nextStorageId = new AtomicInteger();

/** Storage IDs current assigned to the storage. */
private static final Set<Integer> currentStorageIds =
Collections.newSetFromMap(new ConcurrentHashMap<>());

protected final Map<String, OAbstractPaginatedStorage> storages = new HashMap<>();
protected final Map<String, OSharedContext> sharedContexts = new HashMap<>();
protected final Set<ODatabasePoolInternal> pools = new HashSet<>();
protected final Map<String, OAbstractPaginatedStorage> storages = new ConcurrentHashMap<>();
protected final Map<String, OSharedContext> sharedContexts = new ConcurrentHashMap<>();
protected final Set<ODatabasePoolInternal> pools =
Collections.newSetFromMap(new ConcurrentHashMap<>());
protected final OrientDBConfig configurations;
protected final String basePath;
protected final OEngine memory;
Expand Down Expand Up @@ -279,7 +281,7 @@ private synchronized void checkAndCloseStorages(long delay) {
Set<String> toClose = new HashSet<>();
for (OAbstractPaginatedStorage storage : storages.values()) {
if (storage.getType().equalsIgnoreCase(ODatabaseType.PLOCAL.name())
&& storage.getSessionCount() == 0) {
&& storage.getSessionsCount() == 0) {
long currentTime = System.currentTimeMillis();
if (currentTime > storage.getLastCloseTime() + delay) {
toClose.add(storage.getName());
Expand Down Expand Up @@ -420,7 +422,6 @@ public ODatabaseDocumentEmbedded openNoAuthenticate(String name, String user) {
synchronized (this) {
checkOpen();
OAbstractPaginatedStorage storage = getAndOpenStorage(name, config);
storage.incOnOpen();
embedded = newSessionInstance(storage, config, getOrCreateSharedContext(storage));
}
embedded.rebuildIndexes();
Expand Down Expand Up @@ -455,7 +456,6 @@ public ODatabaseDocumentEmbedded openNoAuthorization(String name) {
synchronized (this) {
checkOpen();
OAbstractPaginatedStorage storage = getAndOpenStorage(name, config);
storage.incOnOpen();
embedded = newSessionInstance(storage, config, getOrCreateSharedContext(storage));
}
embedded.rebuildIndexes();
Expand All @@ -480,8 +480,6 @@ public ODatabaseDocumentInternal open(
OAbstractPaginatedStorage storage = getAndOpenStorage(name, config);

embedded = newSessionInstance(storage, config, getOrCreateSharedContext(storage));

storage.incOnOpen();
}
embedded.rebuildIndexes();
embedded.internalOpen(user, password);
Expand All @@ -507,7 +505,6 @@ public ODatabaseDocumentInternal open(
String database = authenticationInfo.getDatabase().get();
OAbstractPaginatedStorage storage = getAndOpenStorage(database, config);
embedded = newSessionInstance(storage, config, getOrCreateSharedContext(storage));
storage.incOnOpen();
}
embedded.rebuildIndexes();
embedded.internalOpen(authenticationInfo);
Expand Down Expand Up @@ -570,7 +567,6 @@ public ODatabaseDocumentInternal poolOpen(
checkOpen();
OAbstractPaginatedStorage storage = getAndOpenStorage(name, pool.getConfig());
embedded = newPooledSessionInstance(pool, storage, getOrCreateSharedContext(storage));
storage.incOnOpen();
}
embedded.rebuildIndexes();
embedded.internalOpen(user, password);
Expand Down Expand Up @@ -693,9 +689,10 @@ public void create(
throw OException.wrapException(
new ODatabaseException("Cannot create database '" + name + "'"), e);
}
} else
} else {
throw new ODatabaseException(
"Cannot create new database '" + name + "' because it already exists");
}
}
embedded.callOnCreateListeners();
ODatabaseRecordThreadLocal.instance().remove();
Expand Down Expand Up @@ -772,9 +769,10 @@ public void restore(
throw OException.wrapException(
new ODatabaseException("Cannot restore database '" + name + "'"), e);
}
} else
} else {
throw new ODatabaseException(
"Cannot create new storage '" + name + "' because it already exists");
}
}
storage.restoreFromIncrementalBackup(path);
embedded.callOnCreateListeners();
Expand Down Expand Up @@ -842,7 +840,9 @@ public synchronized boolean exists(String name, String user, String password) {
if (storage == null) {
if (basePath != null) {
return OLocalPaginatedStorage.exists(Paths.get(buildName(name)));
} else return false;
} else {
return false;
}
}
return storage.exists();
}
Expand Down Expand Up @@ -886,6 +886,7 @@ public void drop(String name, String user, String password) {
}

protected interface DatabaseFound {

void found(String name);
}

Expand Down Expand Up @@ -952,7 +953,9 @@ public ODatabasePoolInternal cachedPool(

@Override
public void close() {
if (!open) return;
if (!open) {
return;
}
timeoutChecker.close();
timer.cancel();
securitySystem.shutdown();
Expand Down Expand Up @@ -986,12 +989,16 @@ public void close() {
}

public synchronized void preClose() {
if (!open) return;
if (!open) {
return;
}
this.sharedContexts.values().forEach(x -> x.getViewManager().close());
}

public synchronized void internalClose() {
if (!open) return;
if (!open) {
return;
}
open = false;
this.sharedContexts.values().forEach(x -> x.close());
final List<OAbstractPaginatedStorage> storagesCopy = new ArrayList<>(storages.values());
Expand Down Expand Up @@ -1034,7 +1041,7 @@ public void removePool(ODatabasePoolInternal pool) {
private static void scanDatabaseDirectory(final File directory, DatabaseFound found) {
if (directory.exists() && directory.isDirectory()) {
final File[] files = directory.listFiles();
if (files != null)
if (files != null) {
for (File db : files) {
if (db.isDirectory()) {
for (File cf : db.listFiles()) {
Expand All @@ -1049,6 +1056,7 @@ private static void scanDatabaseDirectory(final File directory, DatabaseFound fo
}
}
}
}
}
}

Expand Down Expand Up @@ -1095,13 +1103,16 @@ public synchronized void forceDatabaseClose(String iDatabaseName) {

public String getDatabasePath(String iDatabaseName) {
OAbstractPaginatedStorage storage = storages.get(iDatabaseName);
if (storage != null && storage instanceof OLocalPaginatedStorage)
if (storage != null && storage instanceof OLocalPaginatedStorage) {
return ((OLocalPaginatedStorage) storage).getStoragePath().toString();
}
return null;
}

protected void checkOpen() {
if (!open) throw new ODatabaseException("OrientDB Instance is closed");
if (!open) {
throw new ODatabaseException("OrientDB Instance is closed");
}
}

public boolean isOpen() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void open(

void close();

void close(boolean iForce, boolean onDelete);
void close(boolean iForce);

boolean isClosed();

Expand Down Expand Up @@ -192,7 +192,9 @@ boolean cleanOutRecord(

long getVersion();

/** @return Version of product release under which storage was created. */
/**
* @return Version of product release under which storage was created.
*/
String getCreatedAtVersion();

void synch();
Expand Down Expand Up @@ -239,7 +241,9 @@ boolean cleanOutRecord(

void setConflictStrategy(ORecordConflictStrategy iResolver);

/** @return Backup file name */
/**
* @return Backup file name
*/
String incrementalBackup(String backupDirectory, OCallable<Void, Void> started)
throws UnsupportedOperationException;

Expand Down
Loading

0 comments on commit a191162

Please sign in to comment.