Skip to content

Commit

Permalink
Fixed some NPE on auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Oct 16, 2015
1 parent a643909 commit 5129a3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
Expand Up @@ -84,7 +84,7 @@ public OChannelBinaryAsynchClient createNewResource(final String iKey, final Obj

@Override
public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs, final OChannelBinaryAsynchClient iValue) {
return true;
return iValue.isConnected();
}

});
Expand Down
Expand Up @@ -29,7 +29,6 @@
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.exception.OStorageException;
import com.orientechnologies.orient.core.serialization.OMemoryInputStream;
import com.orientechnologies.orient.core.sql.parser.OMatchStatement;
import com.orientechnologies.orient.enterprise.channel.OSocketFactory;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -67,7 +66,7 @@ public OChannelBinaryAsynchClient(final String remoteHost, final int remotePort,

public OChannelBinaryAsynchClient(final String remoteHost, final int remotePort, final String iDatabaseName,
final OContextConfiguration iConfig, final int protocolVersion, final ORemoteServerEventListener asynchEventListener)
throws IOException {
throws IOException {
super(OSocketFactory.instance(iConfig).createSocket(), iConfig);
try {

Expand Down Expand Up @@ -98,13 +97,12 @@ public OChannelBinaryAsynchClient(final String remoteHost, final int remotePort,

srvProtocolVersion = readShort();
} catch (IOException e) {
throw new ONetworkProtocolException("Cannot read protocol version from remote server " + socket.getRemoteSocketAddress()
+ ": " + e);
throw new ONetworkProtocolException(
"Cannot read protocol version from remote server " + socket.getRemoteSocketAddress() + ": " + e);
}

if (srvProtocolVersion != protocolVersion) {
OLogManager.instance().warn(
this,
OLogManager.instance().warn(this,
"The Client driver version is different than Server version: client=" + protocolVersion + ", server="
+ srvProtocolVersion
+ ". You could not use the full features of the newer version. Assure to have the same versions on both");
Expand Down Expand Up @@ -167,11 +165,11 @@ public void endRequest() throws IOException {
releaseWriteLock();
}

public byte[] beginResponse(final int iRequesterId, boolean token) throws IOException {
public byte[] beginResponse(final int iRequesterId, final boolean token) throws IOException {
return beginResponse(iRequesterId, timeout, token);
}

public byte[] beginResponse(final int iRequesterId, final long iTimeout, boolean token) throws IOException {
public byte[] beginResponse(final int iRequesterId, final long iTimeout, final boolean token) throws IOException {
try {
int unreadResponse = 0;
final long startClock = iTimeout > 0 ? System.currentTimeMillis() : 0;
Expand All @@ -187,8 +185,6 @@ else if (!getLockRead().tryAcquireLock(iTimeout, TimeUnit.MILLISECONDS))

if (!isConnected()) {
releaseReadLock();
readLock = false;

throw new IOException("Channel is closed");
}

Expand Down Expand Up @@ -417,13 +413,15 @@ protected int handleStatus(final byte iResult, final int iClientTxId) throws IOE
}

private void setReadResponseTimeout() throws SocketException {
if (socket != null && socket.isConnected() && !socket.isClosed())
socket.setSoTimeout(socketTimeout);
final Socket s = socket;
if (s != null && s.isConnected() && !s.isClosed())
s.setSoTimeout(socketTimeout);
}

private void setWaitResponseTimeout() throws SocketException {
if (socket != null)
socket.setSoTimeout(OGlobalConfiguration.NETWORK_REQUEST_TIMEOUT.getValueAsInteger());
final Socket s = socket;
if (s != null)
s.setSoTimeout(OGlobalConfiguration.NETWORK_REQUEST_TIMEOUT.getValueAsInteger());
}

private void throwSerializedException(final byte[] serializedException) throws IOException {
Expand Down Expand Up @@ -465,8 +463,7 @@ private void throwSerializedException(final byte[] serializedException) throws I
// WRAP IT

else
OLogManager.instance().error(
this,
OLogManager.instance().error(this,
"Error during exception serialization, serialized exception is not Throwable, exception type is "
+ (throwable != null ? throwable.getClass().getName() : "null"));
}
Expand Down
Expand Up @@ -675,8 +675,8 @@ protected void removeCluster() throws IOException {

final String clusterName = connection.database.getClusterNameById(id);
if (clusterName == null)
throw new IllegalArgumentException("Cluster " + id
+ " does not exist anymore. Refresh the db structure or just reconnect to the database");
throw new IllegalArgumentException(
"Cluster " + id + " does not exist anymore. Refresh the db structure or just reconnect to the database");

boolean result = connection.database.dropCluster(clusterName, true);

Expand Down Expand Up @@ -923,8 +923,9 @@ protected void sendError(final int iClientTxId, final Throwable t) throws IOExce

channel.writeByte(OChannelBinaryProtocol.RESPONSE_STATUS_ERROR);
channel.writeInt(iClientTxId);
if (!Boolean.FALSE.equals(tokenBased) && requestType != OChannelBinaryProtocol.REQUEST_CONNECT && requestType != OChannelBinaryProtocol.REQUEST_DB_OPEN && (connection != null && connection.data != null
|| connection.data.protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_32)){
if (tokenBased != null && Boolean.TRUE.equals(tokenBased) && requestType != OChannelBinaryProtocol.REQUEST_CONNECT
&& (requestType != OChannelBinaryProtocol.REQUEST_DB_OPEN || (connection != null && connection.data != null
&& connection.data.protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_32))) {
// TODO: Check if the token is expiring and if it is send a new token

if (token != null) {
Expand Down Expand Up @@ -1041,8 +1042,8 @@ protected void replicationDatabase() throws IOException {
} else if (operation.equals("config")) {
checkServerAccess("server.replication.config");

response = new ODocument().fromJSON(dManager.getDatabaseConfiguration((String) request.field("db")).serialize()
.toJSON("prettyPrint"));
response = new ODocument()
.fromJSON(dManager.getDatabaseConfiguration((String) request.field("db")).serialize().toJSON("prettyPrint"));

}

Expand Down Expand Up @@ -1919,7 +1920,8 @@ protected void beginResponse() {
protected void endResponse() throws IOException {
// resetting transaction state. Commands are stateless and connection should be cleared
// otherwise reused connection (connections pool) may lead to unpredicted errors
if (connection != null && connection.database != null && connection.database.activateOnCurrentThread().getTransaction() != null) {
if (connection != null && connection.database != null
&& connection.database.activateOnCurrentThread().getTransaction() != null) {
connection.database.activateOnCurrentThread();
connection.database.getTransaction().rollback();
}
Expand Down Expand Up @@ -2119,8 +2121,8 @@ private void ridBagSize() throws IOException {
final OSBTreeCollectionManager sbTreeCollectionManager = connection.database.getSbTreeCollectionManager();
final OSBTreeBonsai<OIdentifiable, Integer> tree = sbTreeCollectionManager.loadSBTree(collectionPointer);
try {
final Map<OIdentifiable, OSBTreeRidBag.Change> changes = OSBTreeRidBag.ChangeSerializationHelper.INSTANCE.deserializeChanges(
changeStream, 0);
final Map<OIdentifiable, OSBTreeRidBag.Change> changes = OSBTreeRidBag.ChangeSerializationHelper.INSTANCE
.deserializeChanges(changeStream, 0);

int realSize = tree.getRealBagSize(changes);

Expand Down Expand Up @@ -2175,8 +2177,8 @@ private void sbTreeBonsaiGetEntriesMajor() throws IOException {

private byte[] serializeSBTreeEntryCollection(List<Entry<OIdentifiable, Integer>> collection,
OBinarySerializer<OIdentifiable> keySerializer, OBinarySerializer<Integer> valueSerializer) {
byte[] stream = new byte[OIntegerSerializer.INT_SIZE + collection.size()
* (keySerializer.getFixedLength() + valueSerializer.getFixedLength())];
byte[] stream = new byte[OIntegerSerializer.INT_SIZE
+ collection.size() * (keySerializer.getFixedLength() + valueSerializer.getFixedLength())];
int offset = 0;

OIntegerSerializer.INSTANCE.serializeLiteral(collection.size(), stream, offset);
Expand Down

0 comments on commit 5129a3e

Please sign in to comment.