Share Hazelcast instance when possible.
- Give a name to Hazelcast instance so the instance can be retrived within the same JVM.
- Datastore client now tries to get the instance first to avoid
overhead added by running Hazelcast in client mode.
Change-Id: If8f4b8fec08e3ba81eb0a8a9a23f8a779058db50
diff --git a/src/main/java/net/onrc/onos/core/datagrid/HazelcastDatagrid.java b/src/main/java/net/onrc/onos/core/datagrid/HazelcastDatagrid.java
index 5758b38..8632669 100644
--- a/src/main/java/net/onrc/onos/core/datagrid/HazelcastDatagrid.java
+++ b/src/main/java/net/onrc/onos/core/datagrid/HazelcastDatagrid.java
@@ -30,6 +30,12 @@
*/
public class HazelcastDatagrid implements IFloodlightModule, IDatagridService {
static final Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class);
+
+ /**
+ * The name of Hazelcast instance in this JVM.
+ */
+ public static final String ONOS_HAZELCAST_INSTANCE = "ONOS_HazelcastInstance";
+
private IRestApiService restApi;
static final String HAZELCAST_CONFIG_FILE = "datagridConfig";
@@ -72,6 +78,10 @@
// XXX probably should throw some exception to kill ONOS instead.
}
}
+
+ // set the name of Hazelcast instance in this JVM.
+ hazelcastConfig.setInstanceName(ONOS_HAZELCAST_INSTANCE);
+
/*
hazelcastConfig.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1");
hazelcastConfig.setProperty(GroupProperties.PROP_OPERATION_THREAD_COUNT, "1");
diff --git a/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZClient.java b/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZClient.java
index 41d5853..bb74422 100644
--- a/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZClient.java
+++ b/src/main/java/net/onrc/onos/core/datastore/hazelcast/HZClient.java
@@ -4,6 +4,7 @@
import java.util.Collection;
import java.util.List;
+import net.onrc.onos.core.datagrid.HazelcastDatagrid;
import net.onrc.onos.core.datastore.IKVClient;
import net.onrc.onos.core.datastore.IKVTable;
import net.onrc.onos.core.datastore.IKVTable.IKVEntry;
@@ -32,6 +33,9 @@
import com.hazelcast.core.IAtomicLong;
import com.hazelcast.core.IMap;
+/**
+ * Hazelcast implementation of datastore IKVClient.
+ */
public final class HZClient implements IKVClient {
private static final Logger log = LoggerFactory.getLogger(HZClient.class);
@@ -39,9 +43,10 @@
private static final String MAP_PREFIX = "datastore://";
- // make this path configurable
private static final String BASE_CONFIG_FILENAME = System.getProperty("net.onrc.onos.core.datastore.hazelcast.baseConfig", "conf/hazelcast.xml");
private static final String HAZELCAST_DEFAULT_XML = "conf/hazelcast.default.xml";
+
+ // XXX Remove this mode at some point
private static boolean useClientMode = Boolean.parseBoolean(System.getProperty("net.onrc.onos.core.datastore.hazelcast.clientMode", "true"));
// Note: xml configuration will overwrite this value if present
@@ -55,11 +60,34 @@
return THE_INSTANCE;
}
+ /**
+ * Default constructor.
+ * <p/>
+ * Get or create the Hazelcast Instance to use for datastore.
+ */
private HZClient() {
- hazelcastInstance = getHZinstance(BASE_CONFIG_FILENAME);
+ // Try to get the existing HZ instance in JVM if possible.
+ HazelcastInstance instance = Hazelcast.getHazelcastInstanceByName(HazelcastDatagrid.ONOS_HAZELCAST_INSTANCE);
+ if (instance == null) {
+ log.error("Failed to get the Hazelcast instance in JVM. "
+ + "Probably DataStoreClient was requested before "
+ + "IDatagridService was started "
+ + "or running as part of unit tests. "
+ + "Creating instance on it's own.");
+ instance = getFallbackHZinstance(BASE_CONFIG_FILENAME);
+ }
+ hazelcastInstance = instance;
}
- private static HazelcastInstance getHZinstance(final String hazelcastConfigFileName) {
+ /**
+ * Get or create the hazelcast instance to use for datastore, when existing
+ * Hazelcast instance cannot be retrieved.
+ * <p/>
+ *
+ * @param hazelcastConfigFileName Hazelcast configuration to use when creating a
+ * @return HazelcastInstance to use for datastore
+ */
+ private static HazelcastInstance getFallbackHZinstance(final String hazelcastConfigFileName) {
Config baseHzConfig = null;
try {
baseHzConfig = new FileSystemXmlConfig(hazelcastConfigFileName);
@@ -86,6 +114,8 @@
}
HazelcastInstance instance = null;
+ // TODO Client mode should be removed at some point.
+ // we can get HZ instance used by ONOS using getHazelcastInstanceByName
if (useClientMode) {
log.info("Configuring Hazelcast datastore as Client mode");
ClientConfig clientConfig = new ClientConfig();