Search in sources :

Example 1 with ODistributedConfiguration

use of com.orientechnologies.orient.server.distributed.ODistributedConfiguration in project orientdb by orientechnologies.

the class OCommandExecutorSQLHAStatus method execute.

/**
   * Execute the command.
   */
public Object execute(final Map<Object, Object> iArgs) {
    final ODatabaseDocumentInternal database = getDatabase();
    database.checkSecurity(ORule.ResourceGeneric.SERVER, "status", ORole.PERMISSION_READ);
    final String dbUrl = database.getURL();
    final String path = dbUrl.substring(dbUrl.indexOf(":") + 1);
    final OServer serverInstance = OServer.getInstanceByPath(path);
    final OHazelcastPlugin dManager = (OHazelcastPlugin) serverInstance.getDistributedManager();
    if (dManager == null || !dManager.isEnabled())
        throw new OCommandExecutionException("OrientDB is not started in distributed mode");
    final String databaseName = database.getName();
    final ODistributedConfiguration cfg = dManager.getDatabaseConfiguration(databaseName);
    if (parsedStatement.outputText) {
        final StringBuilder output = new StringBuilder();
        if (parsedStatement.servers)
            output.append(ODistributedOutput.formatServerStatus(dManager, dManager.getClusterConfiguration()));
        if (parsedStatement.db)
            output.append(ODistributedOutput.formatClusterTable(dManager, databaseName, cfg, dManager.getAvailableNodes(databaseName)));
        if (parsedStatement.latency)
            output.append(ODistributedOutput.formatLatency(dManager, dManager.getClusterConfiguration()));
        if (parsedStatement.messages)
            output.append(ODistributedOutput.formatMessages(dManager, dManager.getClusterConfiguration()));
        return output.toString();
    }
    final ODocument output = new ODocument();
    if (parsedStatement.servers)
        output.field("servers", dManager.getClusterConfiguration(), OType.EMBEDDED);
    if (parsedStatement.db)
        output.field("database", cfg.getDocument(), OType.EMBEDDED);
    return output;
}
Also used : OServer(com.orientechnologies.orient.server.OServer) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OHazelcastPlugin(com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ODistributedConfiguration

use of com.orientechnologies.orient.server.distributed.ODistributedConfiguration in project orientdb by orientechnologies.

the class ODistributedOutput method getCompactServerStatus.

/**
   * Create a compact string with all the relevant information.
   *
   * @param manager
   * @param distribCfg
   * @return
   */
public static String getCompactServerStatus(final ODistributedServerManager manager, final ODocument distribCfg) {
    final StringBuilder buffer = new StringBuilder();
    final Collection<ODocument> members = distribCfg.field("members");
    if (members != null) {
        buffer.append(members.size());
        buffer.append(":[");
        int memberCount = 0;
        for (ODocument m : members) {
            if (m == null)
                continue;
            if (memberCount++ > 0)
                buffer.append(",");
            final String serverName = m.field("name");
            buffer.append(serverName);
            buffer.append(m.field("status"));
            final Collection<String> databases = m.field("databases");
            if (databases != null) {
                buffer.append("{");
                int dbCount = 0;
                for (String dbName : databases) {
                    final ODistributedConfiguration dbCfg = manager.getDatabaseConfiguration(dbName, false);
                    if (dbCfg == null)
                        continue;
                    if (dbCount++ > 0)
                        buffer.append(",");
                    buffer.append(dbName);
                    buffer.append("=");
                    buffer.append(manager.getDatabaseStatus(serverName, dbName));
                    buffer.append(" (");
                    buffer.append(dbCfg.getServerRole(serverName));
                    buffer.append(")");
                }
                buffer.append("}");
            }
        }
        buffer.append("]");
    }
    return buffer.toString();
}
Also used : ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with ODistributedConfiguration

use of com.orientechnologies.orient.server.distributed.ODistributedConfiguration in project orientdb by orientechnologies.

the class ODistributedOutput method formatServerStatus.

public static String formatServerStatus(final ODistributedServerManager manager, final ODocument distribCfg) {
    final List<OIdentifiable> rows = new ArrayList<OIdentifiable>();
    final Collection<ODocument> members = distribCfg.field("members");
    if (members != null)
        for (ODocument m : members) {
            if (m == null)
                continue;
            final ODocument serverRow = new ODocument();
            final String serverName = m.field("name");
            serverRow.field("Name", serverName + (manager.getLocalNodeName().equals(serverName) ? "*" : ""));
            serverRow.field("Status", m.field("status"));
            serverRow.field("Databases", (String) null);
            serverRow.field("Conns", m.field("connections"));
            final Date date = m.field("startedOn");
            if (date != null) {
                final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                if (sdf.format(date).equals(sdf.format(new Date())))
                    // TODAY, PUT ONLY THE HOUR
                    serverRow.field("StartedOn", new SimpleDateFormat("HH:mm:ss").format(date));
                else
                    // ANY OTHER DAY, PUT FULL DATE
                    serverRow.field("StartedOn", date);
            }
            final Collection<Map> listeners = m.field("listeners");
            if (listeners != null) {
                for (Map l : listeners) {
                    final String protocol = (String) l.get("protocol");
                    if (protocol.equals("ONetworkProtocolBinary")) {
                        serverRow.field("Binary", l.get("listen"));
                    } else if (protocol.equals("ONetworkProtocolHttpDb")) {
                        serverRow.field("HTTP", l.get("listen"));
                    }
                }
            }
            final Long usedMem = m.field("usedMemory");
            if (usedMem != null) {
                final long maxMem = m.field("maxMemory");
                serverRow.field("UsedMemory", String.format("%s/%s (%.2f%%)", OFileUtils.getSizeAsString(usedMem), OFileUtils.getSizeAsString(maxMem), ((float) usedMem / (float) maxMem) * 100));
            }
            rows.add(serverRow);
            final Collection<String> databases = m.field("databases");
            if (databases != null) {
                int serverNum = 0;
                for (String dbName : databases) {
                    final StringBuilder buffer = new StringBuilder();
                    final ODistributedConfiguration dbCfg = manager.getDatabaseConfiguration(dbName, false);
                    if (dbCfg == null)
                        continue;
                    buffer.append(dbName);
                    buffer.append("=");
                    buffer.append(manager.getDatabaseStatus(serverName, dbName));
                    buffer.append(" (");
                    buffer.append(dbCfg.getServerRole(serverName));
                    buffer.append(")");
                    if (serverNum++ == 0)
                        // ADD THE 1ST DB IT IN THE SERVER ROW
                        serverRow.field("Databases", buffer.toString());
                    else
                        // ADD IN A SEPARATE ROW
                        rows.add(new ODocument().field("Databases", buffer.toString()));
                }
            }
        }
    final StringBuilder buffer = new StringBuilder();
    final OTableFormatter table = new OTableFormatter(new OTableFormatter.OTableOutput() {

        @Override
        public void onMessage(final String text, final Object... args) {
            buffer.append(String.format(text, args));
        }
    });
    table.setColumnHidden("#");
    table.writeRecords(rows, -1);
    buffer.append("\n");
    return buffer.toString();
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTableFormatter(com.orientechnologies.orient.console.OTableFormatter) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration) SimpleDateFormat(java.text.SimpleDateFormat) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ODistributedConfiguration

use of com.orientechnologies.orient.server.distributed.ODistributedConfiguration in project orientdb by orientechnologies.

the class OLocalClusterWrapperStrategy method readConfiguration.

protected ODistributedConfiguration readConfiguration() {
    if (cls.isAbstract())
        throw new IllegalArgumentException("Cannot create a new instance of abstract class");
    final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
    final int[] clusterIds = cls.getClusterIds();
    final List<String> clusterNames = new ArrayList<String>(clusterIds.length);
    for (int c : clusterIds) clusterNames.add(db.getClusterNameById(c).toLowerCase());
    ODistributedConfiguration cfg = manager.getDatabaseConfiguration(databaseName);
    List<String> bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
    if (bestClusters.isEmpty()) {
        // REBALANCE THE CLUSTERS
        manager.reassignClustersOwnership(nodeName, databaseName, cfg.modify());
        cfg = manager.getDatabaseConfiguration(databaseName);
        bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
        if (bestClusters.isEmpty()) {
            // FILL THE MAP CLUSTER/SERVERS
            final StringBuilder buffer = new StringBuilder();
            for (String c : clusterNames) {
                if (buffer.length() > 0)
                    buffer.append(" ");
                buffer.append(" ");
                buffer.append(c);
                buffer.append(":");
                buffer.append(cfg.getServers(c, null));
            }
            ODistributedServerLog.warn(this, manager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Cannot find best cluster for class '%s'. Configured servers for clusters %s are %s (dCfgVersion=%d)", cls.getName(), clusterNames, buffer.toString(), cfg.getVersion());
            throw new ODatabaseException("Cannot find best cluster for class '" + cls.getName() + "' on server '" + nodeName + "'. ClusterStrategy=" + getName() + " dCfgVersion=" + cfg.getVersion());
        }
    }
    db.activateOnCurrentThread();
    final int[] newBestClusters = new int[bestClusters.size()];
    int i = 0;
    for (String c : bestClusters) newBestClusters[i++] = db.getClusterIdByName(c);
    this.localScopedClass = new OLocalScopedClass((OClassImpl) cls, newBestClusters);
    final ODistributedStorage storage = (ODistributedStorage) manager.getStorage(databaseName);
    lastVersion = storage.getConfigurationUpdated();
    return cfg;
}
Also used : ArrayList(java.util.ArrayList) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration)

Example 5 with ODistributedConfiguration

use of com.orientechnologies.orient.server.distributed.ODistributedConfiguration in project orientdb by orientechnologies.

the class DeleteAndLazarusScenarioTest method executeTest.

@Override
public void executeTest() throws Exception {
    /*
     * Test with writeQuorum = majority
     */
    banner("Test with writeQuorum = majority");
    ODatabaseDocumentTx dbServer1 = poolFactory.get(getDatabaseURL(serverInstance.get(0)), "admin", "admin").acquire();
    // changing configuration: readQuorum=2, autoDeploy=false
    System.out.print("\nChanging configuration (autoDeploy=false)...");
    ODocument cfg = null;
    ServerRun server = serverInstance.get(2);
    OHazelcastPlugin manager = (OHazelcastPlugin) server.getServerInstance().getDistributedManager();
    ODistributedConfiguration databaseConfiguration = manager.getDatabaseConfiguration(getDatabaseName());
    cfg = databaseConfiguration.getDocument();
    System.out.println("\nConfiguration updated.");
    // inserting record r1 and checking consistency on all the servers
    try {
        ODatabaseRecordThreadLocal.INSTANCE.set(dbServer1);
        System.out.print("Inserting record r1...");
        new ODocument("Person").fields("id", "R001", "firstName", "Luke", "lastName", "Skywalker").save();
        System.out.println("Done.");
    } catch (Exception e) {
        e.printStackTrace();
        fail("Record r1 not inserted!.");
    }
    waitForInsertedRecordPropagation("R001");
    System.out.print("Checking consistency for record r1...");
    ODocument r1onServer1 = retrieveRecord(getDatabaseURL(serverInstance.get(0)), "R001");
    ODocument r1onServer2 = retrieveRecord(getDatabaseURL(serverInstance.get(1)), "R001");
    ODocument r1onServer3 = retrieveRecord(getDatabaseURL(serverInstance.get(2)), "R001");
    final ORecordId r1Rid = (ORecordId) r1onServer1.getIdentity();
    assertEquals(r1onServer1.field("@version"), r1onServer2.field("@version"));
    assertEquals(r1onServer1.field("id"), r1onServer2.field("id"));
    assertEquals(r1onServer1.field("firstName"), r1onServer2.field("firstName"));
    assertEquals(r1onServer1.field("lastName"), r1onServer2.field("lastName"));
    assertEquals(r1onServer2.field("@version"), r1onServer3.field("@version"));
    assertEquals(r1onServer2.field("id"), r1onServer3.field("id"));
    assertEquals(r1onServer2.field("firstName"), r1onServer3.field("firstName"));
    assertEquals(r1onServer2.field("lastName"), r1onServer3.field("lastName"));
    System.out.println("\tDone.");
    // initial version of the record r1
    int initialVersion = r1onServer1.field("@version");
    // isolating server3
    System.out.println("Network fault on server3.\n");
    simulateServerFault(serverInstance.get(2), "net-fault");
    assertFalse(serverInstance.get(2).isActive());
    // updating r1 in r1* on server3
    banner("Updating r1* on server3 (isolated from the the cluster)");
    ODatabaseDocumentTx dbServer3 = null;
    try {
        r1onServer3 = retrieveRecord(getPlocalDatabaseURL(serverInstance.get(2)), "R001");
        dbServer3 = new ODatabaseDocumentTx(getPlocalDatabaseURL(serverInstance.get(2))).open("admin", "admin");
        r1onServer3.field("firstName", "Darth");
        r1onServer3.field("lastName", "Vader");
        r1onServer3.save();
        System.out.println(r1onServer3.getRecord().toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    // restarting server3
    serverInstance.get(2).startServer(getDistributedServerConfiguration(serverInstance.get(SERVERS - 1)));
    System.out.println("Server 3 restarted.");
    assertTrue(serverInstance.get(2).isActive());
    // reading r1* on server3
    dbServer3 = poolFactory.get(getDatabaseURL(serverInstance.get(2)), "admin", "admin").acquire();
    try {
        r1onServer3 = retrieveRecord(getPlocalDatabaseURL(serverInstance.get(2)), "R001");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        dbServer3.close();
    }
    // r1 was not modified both on server1 and server2
    r1onServer1 = retrieveRecord(getDatabaseURL(serverInstance.get(0)), "R001");
    r1onServer2 = retrieveRecord(getDatabaseURL(serverInstance.get(1)), "R001");
    assertEquals(1, r1onServer1.field("@version"));
    assertEquals("R001", r1onServer1.field("id"));
    assertEquals("Luke", r1onServer1.field("firstName"));
    assertEquals("Skywalker", r1onServer1.field("lastName"));
    assertEquals(r1onServer1.field("@version"), r1onServer2.field("@version"));
    assertEquals(r1onServer1.field("id"), r1onServer2.field("id"));
    assertEquals(r1onServer1.field("firstName"), r1onServer2.field("firstName"));
    assertEquals(r1onServer1.field("lastName"), r1onServer2.field("lastName"));
    // checking we have different values for r1* on server3
    assertEquals("R001", r1onServer3.field("id"));
    assertEquals("Darth", r1onServer3.field("firstName"));
    assertEquals("Vader", r1onServer3.field("lastName"));
    assertEquals(initialVersion + 1, r1onServer3.field("@version"));
    // shutdown server1
    System.out.println("Network fault on server1.\n");
    simulateServerFault(serverInstance.get(0), "net-fault");
    assertFalse(serverInstance.get(0).isActive());
    // delete request on server3 for r1*
    dbServer3 = poolFactory.get(getDatabaseURL(serverInstance.get(2)), "admin", "admin").acquire();
    try {
        dbServer3.command(new OCommandSQL("delete from Person where @rid=#27:0")).execute();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        dbServer3.close();
    }
    // restarting server1
    serverInstance.get(0).startServer(getDistributedServerConfiguration(serverInstance.get(0)));
    System.out.println("Server 1 restarted.");
    assertTrue(serverInstance.get(0).isActive());
    // r1 is still present both on server1 and server2
    r1onServer1 = retrieveRecord(getDatabaseURL(serverInstance.get(0)), "R001");
    r1onServer2 = retrieveRecord(getDatabaseURL(serverInstance.get(1)), "R001");
    assertEquals(1, r1onServer1.field("@version"));
    assertEquals("R001", r1onServer1.field("id"));
    assertEquals("Luke", r1onServer1.field("firstName"));
    assertEquals("Skywalker", r1onServer1.field("lastName"));
    assertEquals(r1onServer1.field("@version"), r1onServer2.field("@version"));
    assertEquals(r1onServer1.field("id"), r1onServer2.field("id"));
    assertEquals(r1onServer1.field("firstName"), r1onServer2.field("firstName"));
    assertEquals(r1onServer1.field("lastName"), r1onServer2.field("lastName"));
    // r1* is still present on server3
    r1onServer3 = retrieveRecord(getDatabaseURL(serverInstance.get(2)), "R001");
    assertEquals(2, r1onServer3.field("@version"));
    assertEquals("R001", r1onServer3.field("id"));
    assertEquals("Darth", r1onServer3.field("firstName"));
    assertEquals("Vader", r1onServer3.field("lastName"));
    // delete request on server1 for r1
    dbServer1 = poolFactory.get(getRemoteDatabaseURL(serverInstance.get(0)), "admin", "admin").acquire();
    try {
        Integer result = dbServer1.command(new OCommandSQL("delete from " + r1Rid)).execute();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        dbServer1.close();
    }
    // r1 is no more present neither on server1, server2 nor server3
    r1onServer1 = retrieveRecord(getDatabaseURL(serverInstance.get(0)), "R001", true, new OCallable<ODocument, ODocument>() {

        @Override
        public ODocument call(ODocument doc) {
            assertEquals(MISSING_DOCUMENT, doc);
            return null;
        }
    });
    r1onServer2 = retrieveRecord(getDatabaseURL(serverInstance.get(1)), "R001", true, new OCallable<ODocument, ODocument>() {

        @Override
        public ODocument call(ODocument doc) {
            assertEquals(MISSING_DOCUMENT, doc);
            return null;
        }
    });
    r1onServer3 = retrieveRecord(getDatabaseURL(serverInstance.get(2)), "R001", true, new OCallable<ODocument, ODocument>() {

        @Override
        public ODocument call(ODocument doc) {
            assertEquals(MISSING_DOCUMENT, doc);
            return null;
        }
    });
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OCallable(com.orientechnologies.common.util.OCallable) ServerRun(com.orientechnologies.orient.server.distributed.ServerRun) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OHazelcastPlugin(com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODistributedConfiguration (com.orientechnologies.orient.server.distributed.ODistributedConfiguration)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OHazelcastPlugin (com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin)2 ArrayList (java.util.ArrayList)2 OCallable (com.orientechnologies.common.util.OCallable)1 OTableFormatter (com.orientechnologies.orient.console.OTableFormatter)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClassImpl (com.orientechnologies.orient.core.metadata.schema.OClassImpl)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 OServer (com.orientechnologies.orient.server.OServer)1 ServerRun (com.orientechnologies.orient.server.distributed.ServerRun)1 SimpleDateFormat (java.text.SimpleDateFormat)1 List (java.util.List)1 Map (java.util.Map)1