Skip to content

Commit

Permalink
Issue #7772 was fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii0lomakin committed Oct 24, 2017
1 parent fac159d commit 83a2b9b
Show file tree
Hide file tree
Showing 209 changed files with 627 additions and 612 deletions.
8 changes: 4 additions & 4 deletions client/src/main/java/com/orientechnologies/orient/client/remote/OSBTreeBonsaiRemote.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public OBonsaiCollectionPointer getCollectionPointer() {

@Override
public V get(K key) {
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying();
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.instance().get().getStorage().getUnderlying();

final byte[] keyStream = new byte[keySerializer.getObjectSize(key)];
keySerializer.serialize(key, keyStream, 0);
Expand Down Expand Up @@ -168,7 +168,7 @@ private boolean pushEntriesToListener(RangeResultListener<K, V> listener, List<M
private List<Map.Entry<K, V>> fetchEntriesMajor(final K key,final boolean inclusive) {
final byte[] keyStream = new byte[keySerializer.getObjectSize(key)];
keySerializer.serialize(key, keyStream, 0);
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying();
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.instance().get().getStorage().getUnderlying();
return storage.networkOperation(new OStorageRemoteOperation<List<Map.Entry<K, V>>>() {
@Override
public List<Map.Entry<K, V>> execute(final OChannelBinaryAsynchClient client, OStorageRemoteSession session) throws IOException {
Expand Down Expand Up @@ -215,7 +215,7 @@ public Collection<V> getValuesBetween(K keyFrom, boolean fromInclusive, K keyTo,

@Override
public K firstKey() {
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying();
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.instance().get().getStorage().getUnderlying();
return storage.networkOperation(new OStorageRemoteOperation<K>() {
@Override
public K execute(final OChannelBinaryAsynchClient client, OStorageRemoteSession session) throws IOException {
Expand Down Expand Up @@ -251,7 +251,7 @@ public void loadEntriesBetween(K keyFrom, boolean fromInclusive, K keyTo, boolea

@Override
public int getRealBagSize(final Map<K, OSBTreeRidBag.Change> changes) {
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying();
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.instance().get().getStorage().getUnderlying();
return storage.networkOperation(new OStorageRemoteOperation<Integer>() {
@Override
public Integer execute(OChannelBinaryAsynchClient client, OStorageRemoteSession session) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void onStartup() {
@Override
protected OSBTreeBonsaiRemote<OIdentifiable, Integer> createTree(final int clusterId) {
if (remoteCreationAllowed) {
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying();
final OStorageRemote storage = (OStorageRemote) ODatabaseRecordThreadLocal.instance().get().getStorage().getUnderlying();
return storage.networkOperation(new OStorageRemoteOperation<OSBTreeBonsaiRemote<OIdentifiable, Integer>>() {
@Override
public OSBTreeBonsaiRemote<OIdentifiable, Integer> execute(final OChannelBinaryAsynchClient client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public Boolean execute(final OChannelBinaryAsynchClient network, OStorageRemoteS
s.close(true, true);
}

ODatabaseRecordThreadLocal.INSTANCE.remove();
ODatabaseRecordThreadLocal.instance().remove();

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public Set<String> getClusterNames() {
public OStorageOperationResult<OPhysicalPosition> createRecord(final ORecordId iRid, final byte[] iContent,
final int iRecordVersion, final byte iRecordType, final int iMode, final ORecordCallback<Long> iCallback) {

final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.instance().get().getSbTreeCollectionManager();
ORecordCallback<OPhysicalPosition> realCallback = null;
if (iCallback != null) {
realCallback = new ORecordCallback<OPhysicalPosition>() {
Expand Down Expand Up @@ -663,7 +663,7 @@ public OStorageOperationResult<ORawBuffer> execute(OChannelBinaryAsynchClient ne
byte[] bytes = network.readBytes();
ORawBuffer buffer = new ORawBuffer(bytes, recVersion, type);

final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
final ODatabaseDocument database = ODatabaseRecordThreadLocal.instance().getIfDefined();
ORecord record;

while (network.readByte() == 2) {
Expand Down Expand Up @@ -722,7 +722,7 @@ public OStorageOperationResult<ORawBuffer> execute(OChannelBinaryAsynchClient ne
buffer = new ORawBuffer(bytes, recVersion, type);
}

final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
final ODatabaseDocument database = ODatabaseRecordThreadLocal.instance().getIfDefined();
ORecord record;
while (network.readByte() == 2) {
record = (ORecord) OChannelBinaryProtocol.readIdentifiable(network);
Expand Down Expand Up @@ -772,7 +772,7 @@ public void restoreFromIncrementalBackup(final String filePath) {

public OStorageOperationResult<Integer> updateRecord(final ORecordId iRid, final boolean updateContent, final byte[] iContent,
final int iVersion, final byte iRecordType, final int iMode, final ORecordCallback<Integer> iCallback) {
final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.instance().get().getSbTreeCollectionManager();

Integer resVersion = asyncNetworkOperation(new OStorageRemoteOperationWrite() {
@Override
Expand Down Expand Up @@ -1175,7 +1175,7 @@ public Object command(final OCommandRequestText iCommand) {
if (!(iCommand instanceof OSerializableStream))
throw new OCommandExecutionException("Cannot serialize the command to be executed to the server side.");
final boolean live = iCommand instanceof OLiveQuery;
final ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
final ODatabaseDocument database = ODatabaseRecordThreadLocal.instance().get();

return networkOperation(new OStorageRemoteOperation<Object>() {
@Override
Expand Down Expand Up @@ -1245,7 +1245,7 @@ public Object execute(final OChannelBinaryAsynchClient network, OStorageRemoteSe
OStorageRemote.this.asynchEventListener.unregisterLiveListener(token);
} else {
final OLiveResultListener listener = (OLiveResultListener) iCommand.getResultListener();
ODatabaseDocumentInternal current = ODatabaseRecordThreadLocal.INSTANCE.get();
ODatabaseDocumentInternal current = ODatabaseRecordThreadLocal.instance().get();
final ODatabaseDocument dbCopy = current.copy();
ORemoteConnectionPool pool = OStorageRemote.this.connectionManager.getPool(network.getServerURL());
OStorageRemote.this.asynchEventListener.registerLiveListener(pool, token, new OLiveResultListener() {
Expand Down Expand Up @@ -1432,7 +1432,7 @@ public Void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession se
updatedRecordsMap.clear();

if (collectionChanges != null)
updateCollection(collectionChanges, ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager());
updateCollection(collectionChanges, ODatabaseRecordThreadLocal.instance().get().getSbTreeCollectionManager());

} finally {
endResponse(network);
Expand Down Expand Up @@ -2557,7 +2557,7 @@ public Object call() throws Exception {
}

protected OStorageRemoteSession getCurrentSession() {
final ODatabaseDocumentTx db = (ODatabaseDocumentTx) ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
final ODatabaseDocumentTx db = (ODatabaseDocumentTx) ODatabaseRecordThreadLocal.instance().getIfDefined();
if (db == null)
return null;
OStorageRemoteSession session = (OStorageRemoteSession) ODatabaseDocumentTxInternal.getSessionMetadata(db);
Expand All @@ -2581,7 +2581,7 @@ public boolean isClosed() {

@Override
public OStorageRemote copy(final ODatabaseDocumentTx source, final ODatabaseDocumentTx dest) {
ODatabaseDocumentInternal origin = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
ODatabaseDocumentInternal origin = ODatabaseRecordThreadLocal.instance().getIfDefined();

final OStorageRemoteSession session = (OStorageRemoteSession) ODatabaseDocumentTxInternal.getSessionMetadata(source);
if (session != null) {
Expand All @@ -2597,7 +2597,7 @@ public OStorageRemote copy(final ODatabaseDocumentTx source, final ODatabaseDocu
} catch (IOException e) {
OLogManager.instance().error(this, "Can not open remote database", e);
} finally {
ODatabaseRecordThreadLocal.INSTANCE.set(origin);
ODatabaseRecordThreadLocal.instance().set(origin);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setUp() throws Exception {
@Test(enabled = false)
public void testCreateTree() throws Exception {
OSBTreeCollectionManagerRemote remoteManager = new OSBTreeCollectionManagerRemote(storageMock, networkSerializerMock);
ODatabaseRecordThreadLocal.INSTANCE.set(dbMock);
ODatabaseRecordThreadLocal.instance().set(dbMock);

when(dbMock.getStorage()).thenReturn(storageMock);
when(storageMock.getUnderlying()).thenReturn(storageMock);
Expand Down
2 changes: 2 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
com.sun.jna;resolution:=optional,
org.xerial.snappy;resolution:=optional,
sun.nio.ch;resolution:=optional,
com.orientechnologies.orient.server.distributed.impl;resolution:=optional,
com.orientechnologies.orient.server.distributed.impl.task;resolution:=optional,
*
</osgi.import>
<osgi.export>com.orientechnologies.orient.core.*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void log(final Object iRequester, final Level iLevel, String iMessage, fi
if (iMessage != null) {
try {
final ODatabaseDocumentInternal db =
ODatabaseRecordThreadLocal.INSTANCE != null ? ODatabaseRecordThreadLocal.INSTANCE.getIfDefined() : null;
ODatabaseRecordThreadLocal.instance() != null ? ODatabaseRecordThreadLocal.instance().getIfDefined() : null;
if (db != null && db.getStorage() != null && db.getStorage() instanceof OAbstractPaginatedStorage) {
final String dbName = db.getStorage().getName();
if (dbName != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public OLocalRecordCache() {

@Override
public void startup() {
ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().get();

profilerPrefix = "db." + db.getName() + ".cache.level1.";
profilerMetadataPrefix = "db.*.cache.level1.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class OCommandExecutorAbstract extends OBaseParser implements OC
protected OCommandContext context;

public static ODatabaseDocumentInternal getDatabase() {
return ODatabaseRecordThreadLocal.INSTANCE.get();
return ODatabaseRecordThreadLocal.instance().get();
}

public OCommandExecutorAbstract init(final OCommandRequestText iRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public <RET> RET execute(final Object... iArgs) {
OExecutionThreadLocal.INSTANCE.get().onAsyncReplicationOk = onAsyncReplicationOk;
OExecutionThreadLocal.INSTANCE.get().onAsyncReplicationError = onAsyncReplicationError;

return (RET) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().command(this);
return (RET) ODatabaseRecordThreadLocal.instance().get().getStorage().command(this);
}

public String getText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ODocumentEqualityWrapper {

public boolean equals(Object obj) {
if(obj instanceof ODocumentEqualityWrapper) {
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.instance().getIfDefined();
return ODocumentHelper.hasSameContentOf(internal, db, ((ODocumentEqualityWrapper)obj).internal, db, null);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Object executeInContext(final OCommandContext iContext, final Map<Object,

parserText = request.getText();

ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.instance().get();
final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(parserText);

db.checkSecurity(ORule.ResourceGeneric.FUNCTION, ORole.PERMISSION_READ, f.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public boolean isIdempotent() {
}

protected Object executeJsr223Script(final String language, final OCommandContext iContext, final Map<Object, Object> iArgs) {
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.instance().get();

final OScriptManager scriptManager = Orient.instance().getScriptManager();
CompiledScript compiledScript = request.getCompiledScript();
Expand Down Expand Up @@ -223,7 +223,7 @@ protected Object executeJsr223Script(final String language, final OCommandContex

// TODO: CREATE A REGULAR JSR223 SCRIPT IMPL
protected Object executeSQL() {
ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().getIfDefined();
try {

return executeSQLScript(parserText, db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public OPartitionedObjectPool.ObjectFactory<ScriptEngine> create(final String la
@Override
public ScriptEngine create() {
final ScriptEngine scriptEngine = scriptManager.getEngine(language);
final String library = scriptManager.getLibrary(ODatabaseRecordThreadLocal.INSTANCE.get(), language);
final String library = scriptManager.getLibrary(ODatabaseRecordThreadLocal.instance().get(), language);

if (library != null)
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public byte[] onUpdate(OStorage storage, final byte iRecordType, final ORecordId
final ODocument storedRecord = new ODocument(rid).fromStream(res.getResult().getBuffer());
final ODocument newRecord = new ODocument().fromStream(iRecordContent);

final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.INSTANCE.get();
final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.instance().get();
hasSameContent = ODocumentHelper.hasSameContentOf(storedRecord, currentDb, newRecord, currentDb, null, false);
} else {
// CHECK BYTE PER BYTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,38 @@
import com.orientechnologies.orient.core.OOrientListenerAbstract;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.metadata.schema.OClass;

import java.util.concurrent.atomic.AtomicReference;

public class ODatabaseRecordThreadLocal extends ThreadLocal<ODatabaseDocumentInternal> {

public static volatile ODatabaseRecordThreadLocal INSTANCE = new ODatabaseRecordThreadLocal();
private static final AtomicReference<ODatabaseRecordThreadLocal> INSTANCE = new AtomicReference<ODatabaseRecordThreadLocal>();

static {
final Orient inst = Orient.instance();
inst.registerListener(new OOrientListenerAbstract() {
@Override
public void onStartup() {
if (INSTANCE == null)
INSTANCE = new ODatabaseRecordThreadLocal();
}
public static ODatabaseRecordThreadLocal instance() {
final ODatabaseRecordThreadLocal dbInst = INSTANCE.get();

@Override
public void onShutdown() {
INSTANCE = null;
}
});
if (dbInst != null)
return dbInst;

//we can do that to avoid thread local memory leaks in containers
if (INSTANCE.get() == null) {
final Orient inst = Orient.instance();
inst.registerListener(new OOrientListenerAbstract() {
@Override
public void onStartup() {
}

@Override
public void onShutdown() {
INSTANCE.set(null);
}
});

INSTANCE.compareAndSet(null, new ODatabaseRecordThreadLocal());

}
return INSTANCE.get();
}

@Override
Expand All @@ -49,12 +62,12 @@ public ODatabaseDocumentInternal get() {
if (db == null) {
if (Orient.instance().getDatabaseThreadFactory() == null) {
throw new ODatabaseException(
"The database instance is not set in the current thread. Be sure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);");
"The database instance is not set in the current thread. Be sure to set it with: ODatabaseRecordThreadLocal.instance().set(db);");
} else {
db = Orient.instance().getDatabaseThreadFactory().getThreadDatabase();
if (db == null) {
throw new ODatabaseException(
"The database instance is not set in the current thread. Be sure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);");
"The database instance is not set in the current thread. Be sure to set it with: ODatabaseRecordThreadLocal.instance().set(db);");
} else {
set(db);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public void close() {
db.activateOnCurrentThread();
OStorage storage = db.getStorage();
storage.close();
ODatabaseRecordThreadLocal.INSTANCE.remove();
ODatabaseRecordThreadLocal.instance().remove();
}
}

Expand Down
Loading

0 comments on commit 83a2b9b

Please sign in to comment.