Skip to content

Commit 437c275

Browse files
author
lvca
committed
1 parent d69200b commit 437c275

9 files changed

Lines changed: 114 additions & 33 deletions

File tree

client/src/main/java/com/orientechnologies/orient/client/remote/ORemoteConnectionManager.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void close() {
5656
connections.clear();
5757
}
5858

59-
public OChannelBinaryAsynchClient acquire(final String iServerURL, final OContextConfiguration clientConfiguration,
59+
public OChannelBinaryAsynchClient acquire(String iServerURL, final OContextConfiguration clientConfiguration,
6060
final Map<String, Object> iConfiguration, final ORemoteServerEventListener iListener) {
6161
OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(iServerURL);
6262
if (pool == null) {
@@ -89,7 +89,15 @@ public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs,
8989
}
9090
}
9191

92-
return pool.getResource(iServerURL, timeout, clientConfiguration, iConfiguration, iListener);
92+
try {
93+
// RETURN THE RESOURCE
94+
return pool.getResource(iServerURL, timeout, clientConfiguration, iConfiguration, iListener);
95+
} catch (Exception e) {
96+
// ERROR ON RETRIEVING THE INSTANCE FROM THE POOL
97+
OLogManager.instance().error(this, "Error on retrieving the connection from pool: " + iServerURL, e);
98+
connections.remove(iServerURL);
99+
}
100+
return null;
93101
}
94102

95103
public void release(final OChannelBinaryAsynchClient conn) {

client/src/main/java/com/orientechnologies/orient/client/remote/OStorageRemote.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.orientechnologies.common.concur.OTimeoutException;
1919
import com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException;
2020
import com.orientechnologies.common.exception.OException;
21+
import com.orientechnologies.common.io.OIOException;
2122
import com.orientechnologies.common.log.OLogManager;
2223
import com.orientechnologies.orient.client.remote.OStorageRemoteThreadLocal.OStorageRemoteSession;
2324
import com.orientechnologies.orient.core.OConstants;
@@ -58,7 +59,6 @@
5859
import com.orientechnologies.orient.core.storage.OStorageAbstract;
5960
import com.orientechnologies.orient.core.storage.OStorageOperationResult;
6061
import com.orientechnologies.orient.core.storage.OStorageProxy;
61-
import com.orientechnologies.orient.core.storage.impl.local.OStorageConfigurationSegment;
6262
import com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext;
6363
import com.orientechnologies.orient.core.tx.OTransaction;
6464
import com.orientechnologies.orient.core.tx.OTransactionAbstract;
@@ -1665,7 +1665,7 @@ protected void openRemoteDatabase() throws IOException {
16651665
}
16661666
} while (engine.getConnectionManager().getAvailableConnections(currentURL) > 0);
16671667

1668-
currentURL = removeServerURL(currentURL);
1668+
currentURL = useNewServerURL(currentURL);
16691669

16701670
} while (currentURL != null);
16711671

@@ -1675,13 +1675,22 @@ protected void openRemoteDatabase() throws IOException {
16751675
throw new OStorageException("Cannot create a connection to remote server address(es): " + serverURLs);
16761676
}
16771677

1678-
protected String removeServerURL(final String iUrl) {
1679-
serverURLs.remove(iUrl);
1678+
protected String useNewServerURL(final String iUrl) {
1679+
int pos = iUrl.indexOf('/');
1680+
if (pos >= iUrl.length() - 1)
1681+
// IGNORE ENDING /
1682+
pos = -1;
1683+
1684+
final String postFix = pos > -1 ? iUrl.substring(pos) : "";
1685+
final String url = pos > -1 ? iUrl.substring(0, pos) : iUrl;
1686+
1687+
// REMOVE INVALID URL
1688+
serverURLs.remove(url);
16801689

16811690
OLogManager.instance().debug(this, "Updated server list: %s...", serverURLs);
16821691

16831692
if (!serverURLs.isEmpty())
1684-
return serverURLs.get(0);
1693+
return serverURLs.get(0) + postFix;
16851694

16861695
return null;
16871696
}
@@ -1800,16 +1809,25 @@ protected String getCurrentServerURL() {
18001809

18011810
protected OChannelBinaryAsynchClient getAvailableNetwork(final String iCurrentURL) throws IOException {
18021811
OChannelBinaryAsynchClient network;
1812+
1813+
String lastURL = iCurrentURL;
18031814
do {
1804-
network = engine.getConnectionManager().acquire(iCurrentURL, clientConfiguration, connectionOptions, asynchEventListener);
1805-
if (!network.isConnected()) {
1815+
network = engine.getConnectionManager().acquire(lastURL, clientConfiguration, connectionOptions, asynchEventListener);
1816+
1817+
if (network == null) {
1818+
lastURL = useNewServerURL(lastURL);
1819+
if (lastURL == null) {
1820+
parseServerURLs();
1821+
throw new OIOException("Cannot open a connection to remote server: " + iCurrentURL);
1822+
}
1823+
} else if (!network.isConnected()) {
18061824
// DISCONNECTED NETWORK, GET ANOTHER ONE
1807-
OLogManager.instance().error(this, "Removing disconnected network channel '%s'...", iCurrentURL);
1825+
OLogManager.instance().error(this, "Removing disconnected network channel '%s'...", lastURL);
18081826
engine.getConnectionManager().remove(network);
18091827
network = null;
18101828
} else if (!network.tryLock()) {
18111829
// CANNOT LOCK IT, MAYBE HASN'T BE CORRECTLY UNLOCKED BY PREVIOUS USER
1812-
OLogManager.instance().error(this, "Removing locked network channel '%s'...", iCurrentURL);
1830+
OLogManager.instance().error(this, "Removing locked network channel '%s'...", lastURL);
18131831
engine.getConnectionManager().remove(network);
18141832
network = null;
18151833
}

distributed/src/main/java/com/orientechnologies/orient/server/hazelcast/OHazelcastPlugin.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public ODocument getLocalNodeConfiguration() {
287287
listeners.add(listenerCfg);
288288

289289
listenerCfg.put("protocol", listener.getProtocolType().getSimpleName());
290-
listenerCfg.put("listen", listener.getListeningAddress());
290+
listenerCfg.put("listen", listener.getListeningAddress(true));
291291
}
292292
nodeCfg.field("databases", getManagedDatabases());
293293

@@ -450,6 +450,9 @@ public ODocument getStats() {
450450
}
451451

452452
public String getNodeName(final Member iMember) {
453+
if (iMember == null)
454+
return "?";
455+
453456
final ODocument cfg = getNodeConfigurationById(iMember.getUuid());
454457
if (cfg != null)
455458
return cfg.field("name");
@@ -508,7 +511,11 @@ public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
508511
}
509512

510513
@Override
511-
public void entryAdded(EntryEvent<String, Object> iEvent) {
514+
public void entryAdded(final EntryEvent<String, Object> iEvent) {
515+
if( iEvent.getMember() == null )
516+
// IGNORE IT
517+
return;
518+
512519
final String key = iEvent.getKey();
513520
if (key.startsWith(CONFIG_NODE_PREFIX)) {
514521
if (!iEvent.getMember().equals(hazelcastInstance.getCluster().getLocalMember())) {
@@ -547,7 +554,7 @@ public void entryAdded(EntryEvent<String, Object> iEvent) {
547554
}
548555

549556
@Override
550-
public void entryUpdated(EntryEvent<String, Object> iEvent) {
557+
public void entryUpdated(final EntryEvent<String, Object> iEvent) {
551558
final String key = iEvent.getKey();
552559
final String eventNodeName = getNodeName(iEvent.getMember());
553560

@@ -582,7 +589,7 @@ public void entryUpdated(EntryEvent<String, Object> iEvent) {
582589
}
583590

584591
@Override
585-
public void entryRemoved(EntryEvent<String, Object> iEvent) {
592+
public void entryRemoved(final EntryEvent<String, Object> iEvent) {
586593
final String key = iEvent.getKey();
587594
if (key.startsWith(CONFIG_NODE_PREFIX)) {
588595
final String nName = getNodeName(iEvent.getMember());

distributed/src/test/java/com/orientechnologies/orient/server/distributed/ServerRun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String getServerId() {
5757
}
5858

5959
public String getBinaryProtocolAddress() {
60-
return server.getListenerByProtocol(ONetworkProtocolBinary.class).getListeningAddress();
60+
return server.getListenerByProtocol(ONetworkProtocolBinary.class).getListeningAddress(false);
6161
}
6262

6363
public void deleteNode() {

enterprise/src/main/java/com/orientechnologies/orient/enterprise/channel/OChannel.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.io.OutputStream;
31+
import java.net.Inet4Address;
32+
import java.net.InetAddress;
33+
import java.net.NetworkInterface;
3134
import java.net.Socket;
35+
import java.net.SocketException;
36+
import java.util.Enumeration;
3237
import java.util.concurrent.atomic.AtomicLong;
3338

3439
public abstract class OChannel extends OListenerManger<OChannelListener> {
@@ -77,6 +82,27 @@ public OChannel(final Socket iSocket, final OContextConfiguration iConfig) throw
7782
socket.setTcpNoDelay(true);
7883
}
7984

85+
public static String getLocalIpAddress(final boolean iFavoriteIp4) throws SocketException {
86+
String bestAddress = null;
87+
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
88+
while (interfaces.hasMoreElements()) {
89+
final NetworkInterface current = interfaces.nextElement();
90+
if (!current.isUp() || current.isLoopback() || current.isVirtual())
91+
continue;
92+
Enumeration<InetAddress> addresses = current.getInetAddresses();
93+
while (addresses.hasMoreElements()) {
94+
final InetAddress current_addr = addresses.nextElement();
95+
if (current_addr.isLoopbackAddress())
96+
continue;
97+
98+
if (bestAddress == null || (iFavoriteIp4 && current_addr instanceof Inet4Address))
99+
// FAVORITE IP4 ADDRESS
100+
bestAddress = current_addr.getHostAddress();
101+
}
102+
}
103+
return bestAddress;
104+
}
105+
80106
public void acquireWriteLock() {
81107
lockWrite.lock();
82108
}

server/src/main/java/com/orientechnologies/orient/server/distributed/task/ODeployDatabaseTask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
2424
import com.orientechnologies.orient.server.OServer;
2525
import com.orientechnologies.orient.server.distributed.ODistributedDatabaseChunk;
26+
import com.orientechnologies.orient.server.distributed.ODistributedException;
2627
import com.orientechnologies.orient.server.distributed.ODistributedServerLog;
2728
import com.orientechnologies.orient.server.distributed.ODistributedServerLog.DIRECTION;
2829
import com.orientechnologies.orient.server.distributed.ODistributedServerManager;
@@ -53,6 +54,8 @@ public Object execute(final OServer iServer, ODistributedServerManager iManager,
5354
throws Exception {
5455

5556
if (!getNodeSource().equals(iManager.getLocalNodeName())) {
57+
if (database == null)
58+
throw new ODistributedException("Database instance is null");
5659

5760
final String databaseName = database.getName();
5861

server/src/main/java/com/orientechnologies/orient/server/network/OServerNetworkListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.orientechnologies.orient.core.config.OContextConfiguration;
2121
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
2222
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
23+
import com.orientechnologies.orient.enterprise.channel.OChannel;
2324
import com.orientechnologies.orient.enterprise.channel.binary.ONetworkProtocolException;
2425
import com.orientechnologies.orient.server.OClientConnectionManager;
2526
import com.orientechnologies.orient.server.OServer;
@@ -237,12 +238,16 @@ public InetSocketAddress getInboundAddr() {
237238
return inboundAddr;
238239
}
239240

240-
public String getListeningAddress() {
241+
public String getListeningAddress(final boolean resolveMultiIfcWithLocal) {
241242
String address = serverSocket.getInetAddress().getHostAddress().toString();
242-
if (address.equals("0.0.0.0"))
243+
if (resolveMultiIfcWithLocal && address.equals("0.0.0.0"))
243244
try {
244245
address = InetAddress.getLocalHost().getHostAddress().toString();
245246
} catch (UnknownHostException e) {
247+
try {
248+
address = OChannel.getLocalIpAddress(true);
249+
} catch (Exception ex) {
250+
}
246251
}
247252
return address + ":" + serverSocket.getLocalPort();
248253
}

tests/src/test/resources/orientdb-server-config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parameter value="false" name="enabled"/>
1313
<parameter value="4h" name="delay"/>
1414
<parameter value="backup" name="target.directory"/>
15-
<parameter value="${DBNAME}-${DATE:yyyyMMddHHmmss}.json" name="target.fileName"/>
15+
<parameter value="${DBNAME}-${DATE:yyyyMMddHHmmss}.zip" name="target.fileName"/>
1616
<parameter value="" name="db.include"/>
1717
<parameter value="" name="db.exclude"/>
1818
</parameters>

tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.orientechnologies.common.console.annotation.ConsoleParameter;
2222
import com.orientechnologies.common.exception.OException;
2323
import com.orientechnologies.common.io.OFileUtils;
24+
import com.orientechnologies.common.io.OIOException;
2425
import com.orientechnologies.common.listener.OProgressListener;
2526
import com.orientechnologies.common.log.OLogManager;
2627
import com.orientechnologies.orient.client.remote.OEngineRemote;
@@ -208,7 +209,6 @@ public void connect(
208209

209210
currentDatabase.registerListener(new OConsoleDatabaseListener(this));
210211
currentDatabase.open(iUserName, iUserPassword);
211-
212212
currentDatabaseName = currentDatabase.getName();
213213
} else {
214214
// CONNECT TO REMOTE SERVER
@@ -220,6 +220,8 @@ public void connect(
220220
}
221221

222222
message("OK");
223+
224+
dumpDistributedConfiguration(false);
223225
}
224226

225227
@ConsoleCommand(aliases = { "close database" }, description = "Disconnect from the current database")
@@ -1075,11 +1077,7 @@ public void info() {
10751077
final OStorage stg = currentDatabase.getStorage();
10761078

10771079
if (stg instanceof OStorageRemoteThread) {
1078-
final ODocument clusterConfig = ((OStorageRemoteThread) stg).getClusterConfiguration();
1079-
if (clusterConfig != null)
1080-
message("\n\nCluster configuration: " + clusterConfig.toJSON("prettyPrint"));
1081-
else
1082-
message("\n\nCluster configuration: none");
1080+
dumpDistributedConfiguration(true);
10831081
} else if (stg instanceof OStorageLocal) {
10841082
final OStorageLocal localStorage = (OStorageLocal) stg;
10851083

@@ -1105,7 +1103,7 @@ public void listProperties() {
11051103

11061104
final OStorageConfiguration dbCfg = stg.getConfiguration();
11071105

1108-
message("\n\nDATABASE PROPERTIES:");
1106+
message("\n\nDATABASE PROPERTIES");
11091107

11101108
if (dbCfg.properties != null) {
11111109
message("\n--------------------------------+----------------------------------------------------+");
@@ -1173,7 +1171,7 @@ public void infoClass(@ConsoleParameter(name = "class-name", description = "The
11731171
}
11741172

11751173
if (cls.properties().size() > 0) {
1176-
message("\nProperties:");
1174+
message("\n\nPROPERTIES");
11771175
message("\n-------------------------------+-------------+-------------------------------+-----------+----------+----------+-----------+-----------+----------+");
11781176
message("\n NAME | TYPE | LINKED TYPE/CLASS | MANDATORY | READONLY | NOT NULL | MIN | MAX | COLLATE |");
11791177
message("\n-------------------------------+-------------+-------------------------------+-----------+----------+----------+-----------+-----------+----------+");
@@ -1192,7 +1190,7 @@ public void infoClass(@ConsoleParameter(name = "class-name", description = "The
11921190

11931191
final Set<OIndex<?>> indexes = cls.getClassIndexes();
11941192
if (!indexes.isEmpty()) {
1195-
message("\nIndexes (" + indexes.size() + " altogether):");
1193+
message("\n\nINDEXES (" + indexes.size() + " altogether)");
11961194
message("\n-------------------------------+----------------+");
11971195
message("\n NAME | PROPERTIES |");
11981196
message("\n-------------------------------+----------------+");
@@ -1219,7 +1217,7 @@ public void infoClass(@ConsoleParameter(name = "class-name", description = "The
12191217
@ConsoleCommand(description = "Display all indexes", aliases = { "indexes" })
12201218
public void listIndexes() {
12211219
if (currentDatabaseName != null) {
1222-
message("\n\nINDEXES:");
1220+
message("\n\nINDEXES");
12231221
message("\n----------------------------------------------+------------+-----------------------+----------------+------------+");
12241222
message("\n NAME | TYPE | CLASS | FIELDS | RECORDS |");
12251223
message("\n----------------------------------------------+------------+-----------------------+----------------+------------+");
@@ -1270,7 +1268,7 @@ public int compare(OIndex<?> o1, OIndex<?> o2) {
12701268
@ConsoleCommand(description = "Display all the configured clusters", aliases = { "clusters" })
12711269
public void listClusters() {
12721270
if (currentDatabaseName != null) {
1273-
message("\n\nCLUSTERS:");
1271+
message("\n\nCLUSTERS");
12741272
message("\n----------------------------------------------+-------+---------------------+---------+-----------------+");
12751273
message("\n NAME | ID | TYPE | DATASEG | RECORDS |");
12761274
message("\n----------------------------------------------+-------+---------------------+---------+-----------------+");
@@ -1294,7 +1292,9 @@ public void listClusters() {
12941292

12951293
message("\n %-45s| %5d | %-20s| %7d | %15d |", format(clusterName, 45), clusterId, clusterType,
12961294
cluster.getDataSegmentId(), count);
1297-
} catch (Exception ignored) {
1295+
} catch (Exception e) {
1296+
if (e instanceof OIOException)
1297+
break;
12981298
}
12991299
}
13001300
message("\n----------------------------------------------+-------+---------------------+---------+-----------------+");
@@ -1308,7 +1308,7 @@ public void listClusters() {
13081308
@ConsoleCommand(description = "Display all the configured classes", aliases = { "classes" })
13091309
public void listClasses() {
13101310
if (currentDatabaseName != null) {
1311-
message("\n\nCLASSES:");
1311+
message("\n\nCLASSES");
13121312
message("\n----------------------------------------------+------------------------------------+------------+----------------+");
13131313
message("\n NAME | SUPERCLASS | CLUSTERS | RECORDS |");
13141314
message("\n----------------------------------------------+------------------------------------+------------+----------------+");
@@ -1335,7 +1335,7 @@ public int compare(OClass o1, OClass o2) {
13351335
clusters.append(cls.getClusterIds()[i]);
13361336
}
13371337

1338-
count = currentDatabase.countClass(cls.getName());
1338+
count = currentDatabase.countClass(cls.getName(), false);
13391339
totalElements += count;
13401340

13411341
final String superClass = cls.getSuperClass() != null ? cls.getSuperClass().getName() : "";
@@ -1937,6 +1937,20 @@ public void onCompletition(final Object iTask, final boolean iSucceed) {
19371937
message(iSucceed ? "] Done." : " Error!");
19381938
}
19391939

1940+
protected void dumpDistributedConfiguration(final boolean iForce) {
1941+
if( currentDatabase == null )
1942+
return;
1943+
1944+
final OStorage stg = currentDatabase.getStorage();
1945+
if (stg instanceof OStorageRemoteThread) {
1946+
final ODocument distributedCfg = ((OStorageRemoteThread) stg).getClusterConfiguration();
1947+
if (distributedCfg != null && !distributedCfg.isEmpty()) {
1948+
message("\n\nDISTRIBUTED CONFIGURATION:\n" + distributedCfg.toJSON("prettyPrint"));
1949+
} else if (iForce)
1950+
message("\n\nDISTRIBUTED CONFIGURATION: none (OrientDB is running in standalone mode)");
1951+
}
1952+
}
1953+
19401954
@Override
19411955
protected boolean isCollectingCommands(final String iLine) {
19421956
return iLine.startsWith("js") || iLine.startsWith("script");

0 commit comments

Comments
 (0)