Search in sources :

Example 6 with OServerNetworkListener

use of com.orientechnologies.orient.server.network.OServerNetworkListener in project orientdb by orientechnologies.

the class OHazelcastPlugin method startup.

@Override
public void startup() {
    if (!enabled)
        return;
    Orient.instance().setRunningDistributed(true);
    OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.setValue(Integer.MAX_VALUE);
    OGlobalConfiguration.RID_BAG_SBTREEBONSAI_TO_EMBEDDED_THRESHOLD.setValue(Integer.MAX_VALUE);
    OGlobalConfiguration.STORAGE_TRACK_CHANGED_RECORDS_IN_WAL.setValue(true);
    // REGISTER TEMPORARY USER FOR REPLICATION PURPOSE
    serverInstance.addTemporaryUser(REPLICATOR_USER, "" + new SecureRandom().nextLong(), "*");
    super.startup();
    status = NODE_STATUS.STARTING;
    final String localNodeName = nodeName;
    activeNodes.clear();
    activeNodesNamesByUuid.clear();
    activeNodesUuidByName.clear();
    // CLOSE ALL CONNECTIONS TO THE SERVERS
    for (ORemoteServerController server : remoteServers.values()) server.close();
    remoteServers.clear();
    registeredNodeById.clear();
    registeredNodeByName.clear();
    try {
        hazelcastInstance = configureHazelcast();
        nodeUuid = hazelcastInstance.getCluster().getLocalMember().getUuid();
        final LifecycleService lifecycleService = hazelcastInstance.getLifecycleService();
        lifecycleService.addLifecycleListener(this);
        OLogManager.instance().info(this, "Starting distributed server '%s' (hzID=%s)...", localNodeName, nodeUuid);
        final long clusterTime = getClusterTime();
        final long deltaTime = System.currentTimeMillis() - clusterTime;
        OLogManager.instance().info(this, "Distributed cluster time=%s (delta from local node=%d)...", new Date(clusterTime), deltaTime);
        activeNodes.put(localNodeName, hazelcastInstance.getCluster().getLocalMember());
        activeNodesNamesByUuid.put(nodeUuid, localNodeName);
        activeNodesUuidByName.put(localNodeName, nodeUuid);
        configurationMap = new OHazelcastDistributedMap(this, hazelcastInstance);
        OServer.registerServerInstance(localNodeName, serverInstance);
        initRegisteredNodeIds();
        // PUBLISH CURRENT NODE NAME
        final ODocument nodeCfg = new ODocument();
        nodeCfg.setTrackingChanges(false);
        // REMOVE ANY PREVIOUS REGISTERED SERVER WITH THE SAME NODE NAME
        final Set<String> node2Remove = new HashSet<String>();
        for (Iterator<Map.Entry<String, Object>> it = configurationMap.getHazelcastMap().entrySet().iterator(); it.hasNext(); ) {
            final Map.Entry<String, Object> entry = it.next();
            if (entry.getKey().startsWith(CONFIG_NODE_PREFIX)) {
                final ODocument nCfg = (ODocument) entry.getValue();
                if (nodeName.equals(nCfg.field("name"))) {
                    // SAME NODE NAME: REMOVE IT
                    node2Remove.add(entry.getKey());
                }
            }
        }
        for (String n : node2Remove) configurationMap.getHazelcastMap().remove(n);
        nodeCfg.field("id", nodeId);
        nodeCfg.field("uuid", nodeUuid);
        nodeCfg.field("name", nodeName);
        ORecordInternal.setRecordSerializer(nodeCfg, ODatabaseDocumentTx.getDefaultSerializer());
        configurationMap.put(CONFIG_NODE_PREFIX + nodeUuid, nodeCfg);
        // REGISTER CURRENT NODES
        for (Member m : hazelcastInstance.getCluster().getMembers()) {
            if (!m.getUuid().equals(nodeUuid)) {
                boolean found = false;
                for (int retry = 0; retry < 10; ++retry) {
                    final String memberName = getNodeName(m, false);
                    if (memberName == null || memberName.startsWith("ext:")) {
                        // ACTIVE NODE IN HZ, BUT NOT YET REGISTERED, WAIT AND RETRY
                        Thread.sleep(1000);
                        continue;
                    }
                    found = true;
                    activeNodes.put(memberName, m);
                    activeNodesNamesByUuid.put(m.getUuid(), memberName);
                    activeNodesUuidByName.put(memberName, m.getUuid());
                    break;
                }
                if (!found)
                    ODistributedServerLog.warn(this, localNodeName, null, DIRECTION.NONE, "Cannot find configuration for member: %s, uuid", m, m.getUuid());
            }
        }
        assignLockManagerFromCluster();
        messageService = new ODistributedMessageServiceImpl(this);
        initSystemDatabase();
        ODistributedServerLog.info(this, localNodeName, null, DIRECTION.NONE, "Servers in cluster: %s", activeNodes.keySet());
        publishLocalNodeConfiguration();
        if (!configurationMap.containsKey(CONFIG_NODE_PREFIX + nodeUuid)) {
            // NODE NOT REGISTERED, FORCING SHUTTING DOWN
            ODistributedServerLog.error(this, localNodeName, null, DIRECTION.NONE, "Error on registering local node on cluster");
            throw new ODistributedStartupException("Error on registering local node on cluster");
        }
        // CONNECTS TO ALL THE AVAILABLE NODES
        for (String m : activeNodes.keySet()) if (!m.equals(nodeName))
            getRemoteServer(m);
        publishLocalNodeConfiguration();
        installNewDatabasesFromCluster();
        loadLocalDatabases();
        membershipListenerMapRegistration = configurationMap.getHazelcastMap().addEntryListener(this, true);
        membershipListenerRegistration = hazelcastInstance.getCluster().addMembershipListener(this);
        // REGISTER CURRENT MEMBERS
        setNodeStatus(NODE_STATUS.ONLINE);
        publishLocalNodeConfiguration();
        final long delay = OGlobalConfiguration.DISTRIBUTED_PUBLISH_NODE_STATUS_EVERY.getValueAsLong();
        if (delay > 0) {
            publishLocalNodeConfigurationTask = new TimerTask() {

                @Override
                public void run() {
                    publishLocalNodeConfiguration();
                }
            };
            Orient.instance().scheduleTask(publishLocalNodeConfigurationTask, delay, delay);
        }
        final long statsDelay = OGlobalConfiguration.DISTRIBUTED_DUMP_STATS_EVERY.getValueAsLong();
        if (statsDelay > 0) {
            haStatsTask = new TimerTask() {

                @Override
                public void run() {
                    dumpStats();
                }
            };
            Orient.instance().scheduleTask(haStatsTask, statsDelay, statsDelay);
        }
        final long healthChecker = OGlobalConfiguration.DISTRIBUTED_CHECK_HEALTH_EVERY.getValueAsLong();
        if (healthChecker > 0) {
            healthCheckerTask = new OClusterHealthChecker(this);
            Orient.instance().scheduleTask(healthCheckerTask, healthChecker, healthChecker);
        }
        for (OServerNetworkListener nl : serverInstance.getNetworkListeners()) nl.registerBeforeConnectNetworkEventListener(this);
        // WAIT ALL THE MESSAGES IN QUEUE ARE PROCESSED OR MAX 10 SECONDS
        waitStartupIsCompleted();
        signalListener = new OSignalHandler.OSignalListener() {

            @Override
            public void onSignal(final Signal signal) {
                if (signal.toString().trim().equalsIgnoreCase("SIGTRAP"))
                    dumpStats();
            }
        };
        Orient.instance().getSignalHandler().registerListener(signalListener);
    } catch (Exception e) {
        ODistributedServerLog.error(this, localNodeName, null, DIRECTION.NONE, "Error on starting distributed plugin", e);
        throw OException.wrapException(new ODistributedStartupException("Error on starting distributed plugin"), e);
    }
    dumpServersStatus();
}
Also used : Signal(sun.misc.Signal) OServerNetworkListener(com.orientechnologies.orient.server.network.OServerNetworkListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) SecureRandom(java.security.SecureRandom) OException(com.orientechnologies.common.exception.OException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) RetryableHazelcastException(com.hazelcast.spi.exception.RetryableHazelcastException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) FileNotFoundException(java.io.FileNotFoundException) OSignalHandler(com.orientechnologies.orient.core.OSignalHandler)

Example 7 with OServerNetworkListener

use of com.orientechnologies.orient.server.network.OServerNetworkListener in project orientdb by orientechnologies.

the class OHazelcastPlugin method shutdown.

@Override
public void shutdown() {
    if (!enabled)
        return;
    Orient.instance().getSignalHandler().unregisterListener(signalListener);
    for (OServerNetworkListener nl : serverInstance.getNetworkListeners()) nl.unregisterBeforeConnectNetworkEventListener(this);
    OLogManager.instance().warn(this, "Shutting down node '%s'...", nodeName);
    setNodeStatus(NODE_STATUS.SHUTTINGDOWN);
    try {
        final Set<String> databases = new HashSet<String>();
        if (hazelcastInstance.getLifecycleService().isRunning())
            for (Map.Entry<String, Object> entry : configurationMap.entrySet()) {
                if (entry.getKey().toString().startsWith(CONFIG_DBSTATUS_PREFIX)) {
                    final String nodeDb = entry.getKey().toString().substring(CONFIG_DBSTATUS_PREFIX.length());
                    if (nodeDb.startsWith(nodeName))
                        databases.add(entry.getKey());
                }
            }
        // PUT DATABASES AS NOT_AVAILABLE
        for (String k : databases) configurationMap.put(k, DB_STATUS.NOT_AVAILABLE);
    } catch (HazelcastInstanceNotActiveException e) {
    // HZ IS ALREADY DOWN, IGNORE IT
    }
    try {
        super.shutdown();
    } catch (HazelcastInstanceNotActiveException e) {
    // HZ IS ALREADY DOWN, IGNORE IT
    }
    if (membershipListenerRegistration != null) {
        try {
            hazelcastInstance.getCluster().removeMembershipListener(membershipListenerRegistration);
        } catch (HazelcastInstanceNotActiveException e) {
        // HZ IS ALREADY DOWN, IGNORE IT
        }
    }
    if (hazelcastInstance != null)
        try {
            hazelcastInstance.shutdown();
        } catch (Exception e) {
            OLogManager.instance().error(this, "Error on shutting down Hazelcast instance", e);
        } finally {
            hazelcastInstance = null;
        }
    OCallableUtils.executeIgnoringAnyExceptions(new OCallableNoParamNoReturn() {

        @Override
        public void call() {
            configurationMap.destroy();
        }
    });
    OCallableUtils.executeIgnoringAnyExceptions(new OCallableNoParamNoReturn() {

        @Override
        public void call() {
            configurationMap.getHazelcastMap().removeEntryListener(membershipListenerMapRegistration);
        }
    });
    setNodeStatus(NODE_STATUS.OFFLINE);
}
Also used : OCallableNoParamNoReturn(com.orientechnologies.common.util.OCallableNoParamNoReturn) OServerNetworkListener(com.orientechnologies.orient.server.network.OServerNetworkListener) OException(com.orientechnologies.common.exception.OException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) RetryableHazelcastException(com.hazelcast.spi.exception.RetryableHazelcastException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) FileNotFoundException(java.io.FileNotFoundException)

Example 8 with OServerNetworkListener

use of com.orientechnologies.orient.server.network.OServerNetworkListener in project orientdb by orientechnologies.

the class OServer method deinit.

protected boolean deinit() {
    if (!running)
        return false;
    try {
        running = false;
        OLogManager.instance().info(this, "OrientDB Server is shutting down...");
        if (shutdownHook != null)
            shutdownHook.cancel();
        Orient.instance().getProfiler().unregisterHookValue("system.databases");
        for (OServerLifecycleListener l : lifecycleListeners) l.onBeforeDeactivate();
        lock.lock();
        try {
            if (networkListeners.size() > 0) {
                // SHUTDOWN LISTENERS
                OLogManager.instance().info(this, "Shutting down listeners:");
                // SHUTDOWN LISTENERS
                for (OServerNetworkListener l : networkListeners) {
                    OLogManager.instance().info(this, "- %s", l);
                    try {
                        l.shutdown();
                    } catch (Throwable e) {
                        OLogManager.instance().error(this, "Error during shutdown of listener %s.", e, l);
                    }
                }
            }
            if (networkProtocols.size() > 0) {
                // PROTOCOL SHUTDOWN
                OLogManager.instance().info(this, "Shutting down protocols");
                networkProtocols.clear();
            }
            for (OServerLifecycleListener l : lifecycleListeners) try {
                l.onAfterDeactivate();
            } catch (Exception e) {
                OLogManager.instance().error(this, "Error during deactivation of server lifecycle listener %s", e, l);
            }
            rejectRequests = true;
            clientConnectionManager.shutdown();
            if (pluginManager != null)
                pluginManager.shutdown();
            if (serverSecurity != null)
                serverSecurity.shutdown();
            networkListeners.clear();
        } finally {
            lock.unlock();
        }
        if (shutdownEngineOnExit && !Orient.isRegisterDatabaseByPath())
            try {
                OLogManager.instance().info(this, "Shutting down databases:");
                Orient.instance().shutdown();
            } catch (Throwable e) {
                OLogManager.instance().error(this, "Error during OrientDB shutdown", e);
            }
    } finally {
        OLogManager.instance().info(this, "OrientDB Server shutdown complete\n");
        OLogManager.instance().flush();
    }
    return true;
}
Also used : OServerNetworkListener(com.orientechnologies.orient.server.network.OServerNetworkListener) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException)

Aggregations

OServerNetworkListener (com.orientechnologies.orient.server.network.OServerNetworkListener)8 IOException (java.io.IOException)4 OException (com.orientechnologies.common.exception.OException)3 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)3 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)3 RetryableHazelcastException (com.hazelcast.spi.exception.RetryableHazelcastException)2 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)2 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 FileNotFoundException (java.io.FileNotFoundException)2 OCallable (com.orientechnologies.common.util.OCallable)1 OCallableNoParamNoReturn (com.orientechnologies.common.util.OCallableNoParamNoReturn)1 OSignalHandler (com.orientechnologies.orient.core.OSignalHandler)1 OSecurityException (com.orientechnologies.orient.core.exception.OSecurityException)1 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)1 OClusterPositionMap (com.orientechnologies.orient.core.storage.impl.local.paginated.OClusterPositionMap)1 OServerCommandPostCommandGraph (com.orientechnologies.orient.graph.server.command.OServerCommandPostCommandGraph)1 OServer (com.orientechnologies.orient.server.OServer)1 OServerUserConfiguration (com.orientechnologies.orient.server.config.OServerUserConfiguration)1 OServerSocketFactory (com.orientechnologies.orient.server.network.OServerSocketFactory)1