Search in sources :

Example 11 with ORawBuffer

use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.

the class OConflictResolverDatabaseRepairer method repairClusterAtBlocks.

private int repairClusterAtBlocks(final ODatabaseDocumentInternal db, final List<String> clusterNames, final int clusterId, final Map<String, Object> repairInfoResult) throws IOException {
    final OStorage storage = db.getStorage().getUnderlying();
    final long localEnd = storage.getClusterById(clusterId).getNextPosition() - 1;
    final int batchMax = OGlobalConfiguration.DISTRIBUTED_CONFLICT_RESOLVER_REPAIRER_BATCH.getValueAsInteger();
    int recordRepaired = 0;
    for (Map.Entry<String, Object> entry : repairInfoResult.entrySet()) {
        final String server = entry.getKey();
        final ODistributedServerManager.DB_STATUS status = dManager.getDatabaseStatus(server, databaseName);
        if (status != ODistributedServerManager.DB_STATUS.ONLINE) {
            ODistributedServerLog.debug(this, dManager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Cannot align missing records of cluster '%s' on server %s, because is not ONLINE (status=%s)", clusterNames.get(0), server, status);
            return 0;
        }
        final Object result = entry.getValue();
        if (result instanceof Long) {
            final long remoteEnd = (Long) result;
            ORepairClusterTask task = new ORepairClusterTask(clusterId);
            for (long pos = remoteEnd + 1; pos <= localEnd; ++pos) {
                final ORecordId rid = new ORecordId(clusterId, pos);
                final ORawBuffer rawRecord = storage.readRecord(rid, null, true, false, null).getResult();
                if (rawRecord == null)
                    continue;
                task.add(new OCreateRecordTask(rid, rawRecord.buffer, rawRecord.version, rawRecord.recordType));
                recordRepaired++;
                if (task.getTasks().size() > batchMax) {
                    // SEND BATCH OF CHANGES
                    final List<String> servers = new ArrayList<String>(1);
                    servers.add(server);
                    final ODistributedResponse response = dManager.sendRequest(databaseName, clusterNames, servers, task, dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
                    task = new ORepairClusterTask(clusterId);
                }
            }
            if (!task.getTasks().isEmpty()) {
                // SEND FINAL BATCH OF CHANGES
                final List<String> servers = new ArrayList<String>(1);
                servers.add(server);
                final ODistributedResponse response = dManager.sendRequest(databaseName, clusterNames, servers, task, dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
            }
            if (task.getTasks().size() == 0)
                ODistributedServerLog.debug(this, dManager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Auto repair aligned %d records of cluster '%s'", task.getTasks().size(), clusterNames.get(0));
            else
                ODistributedServerLog.info(this, dManager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Auto repair aligned %d records of cluster '%s'", task.getTasks().size(), clusterNames.get(0));
        }
    }
    return recordRepaired;
}
Also used : OStorage(com.orientechnologies.orient.core.storage.OStorage) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 12 with ORawBuffer

use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.

the class OStorageConfigurationSegment method load.

@Override
public OStorageConfiguration load(final Map<String, Object> iProperties) throws OSerializationException {
    try {
        initConfiguration();
        bindPropertiesToContext(iProperties);
        if (segment.getFile().exists())
            segment.open();
        else {
            segment.create(START_SIZE);
            // @COMPATIBILITY0.9.25
            // CHECK FOR OLD VERSION OF DATABASE
            final ORawBuffer rawRecord = storage.readRecord(CONFIG_RID, null, false, false, null).getResult();
            if (rawRecord != null)
                fromStream(rawRecord.buffer);
            update();
            return this;
        }
        final int size = segment.getFile().readInt(0);
        byte[] buffer = new byte[size];
        segment.getFile().read(OBinaryProtocol.SIZE_INT, buffer, size);
        fromStream(buffer);
    } catch (IOException e) {
        throw OException.wrapException(new OSerializationException("Cannot load database configuration. The database seems corrupted"), e);
    }
    return this;
}
Also used : ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) IOException(java.io.IOException)

Example 13 with ORawBuffer

use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.

the class OCreateRecordTask method executeRecordTask.

@Override
public Object executeRecordTask(final ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    if (ODistributedServerLog.isDebugEnabled())
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Creating record %s/%s v.%d reqId=%s...", database.getName(), rid.toString(), version, requestId);
    if (!rid.isPersistent())
        throw new ODistributedException("Record " + rid + " has not been saved on owner node first (temporary rid)");
    final OPaginatedCluster cluster = (OPaginatedCluster) ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getClusterById(rid.getClusterId());
    final OPaginatedCluster.RECORD_STATUS recordStatus = cluster.getRecordStatus(rid.getClusterPosition());
    switch(recordStatus) {
        case REMOVED:
            // RECYCLE THE RID AND OVERWRITE IT WITH THE NEW CONTENT
            ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().recyclePosition(rid, new byte[] {}, version, recordType);
            // CREATE A RECORD TO CALL ALL THE HOOKS (LIKE INDEXES FOR UNIQUE CONSTRAINTS)
            final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(recordType);
            ORecordInternal.fill(loadedRecordInstance, rid, version, content, true);
            loadedRecordInstance.save();
            return new OPlaceholder(rid, loadedRecordInstance.getVersion());
        case ALLOCATED:
        case PRESENT:
            final OStorageOperationResult<ORawBuffer> loadedRecord = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().readRecord(rid, null, true, false, null);
            if (loadedRecord.getResult() != null) {
                // ALREADY PRESENT
                record = forceUpdate(iManager, database, requestId, loadedRecord.getResult());
                return new OPlaceholder(record);
            }
        case NOT_EXISTENT:
            // try {
            ORecordId newRid;
            do {
                getRecord();
                if (clusterId > -1)
                    record.save(database.getClusterNameById(clusterId), true);
                else if (rid.getClusterId() != -1)
                    record.save(database.getClusterNameById(rid.getClusterId()), true);
                else
                    record.save();
                newRid = (ORecordId) record.getIdentity();
                if (newRid.getClusterPosition() >= rid.getClusterPosition())
                    break;
                // CREATE AN HOLE
                record.delete();
                record = null;
            } while (newRid.getClusterPosition() < rid.getClusterPosition());
            if (!rid.equals(newRid)) {
                ODistributedServerLog.warn(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Record %s has been saved with the RID %s instead of the expected %s reqId=%s", record, newRid, rid, requestId);
                // DELETE THE INVALID RECORD FIRST
                record.delete();
                throw new ODistributedException("Record " + rid + " has been saved with the different RID " + newRid + " on server " + iManager.getLocalNodeName());
            }
            ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> assigned new rid %s/%s v.%d reqId=%s", database.getName(), rid.toString(), record.getVersion(), requestId);
    }
    // IMPROVED TRANSPORT BY AVOIDING THE RECORD CONTENT, BUT JUST RID + VERSION
    return new OPlaceholder(record);
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 14 with ORawBuffer

use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.

the class OCreateRecordTask method getFixTask.

@Override
public ORemoteTask getFixTask(final ODistributedRequest iRequest, ORemoteTask iOriginalTask, final Object iBadResponse, final Object iGoodResponse, final String executorNode, final ODistributedServerManager dManager) {
    if (iBadResponse == null || iBadResponse instanceof Throwable)
        return null;
    final OPlaceholder badResult = (OPlaceholder) iBadResponse;
    final OPlaceholder goodResult = (OPlaceholder) iGoodResponse;
    ORemoteTask result = null;
    if (!badResult.equals(goodResult)) {
        // CREATE RECORD FAILED TO HAVE THE SAME RIDS. FORCE REALIGNING OF DATA CLUSTERS
        if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() < goodResult.getIdentity().getClusterPosition()) {
            final long minPos = Math.max(badResult.getIdentity().getClusterPosition() - 1, 0);
            for (long pos = minPos; pos < goodResult.getIdentity().getClusterPosition(); ++pos) {
                // UPDATE INTERMEDIATE RECORDS
                final ORecordId toUpdateRid = new ORecordId(goodResult.getIdentity().getClusterId(), pos);
                final ORecord toUpdateRecord;
                if (dManager.getLocalNodeName().equals(executorNode)) {
                    // SAME SERVER: LOAD THE RECORD FROM ANOTHER NODE
                    final ODistributedConfiguration dCfg = dManager.getDatabaseConfiguration(iRequest.getDatabaseName());
                    final List<String> nodes = dCfg.getServers(ODatabaseRecordThreadLocal.INSTANCE.get().getClusterNameById(clusterId), dManager.getLocalNodeName());
                    final ODistributedResponse response = dManager.sendRequest(iRequest.getDatabaseName(), null, nodes, new OReadRecordTask(toUpdateRid), dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
                    final ORawBuffer remoteReadRecord = (ORawBuffer) response.getPayload();
                    if (remoteReadRecord != null) {
                        toUpdateRecord = Orient.instance().getRecordFactoryManager().newInstance(recordType);
                        ORecordInternal.fill(toUpdateRecord, toUpdateRid, remoteReadRecord.version, remoteReadRecord.buffer, false);
                    } else
                        toUpdateRecord = null;
                } else
                    // LOAD IT LOCALLY
                    toUpdateRecord = toUpdateRid.getRecord();
                if (toUpdateRecord != null)
                    result = new OFixUpdateRecordTask(toUpdateRid, toUpdateRecord.toStream(), toUpdateRecord.getVersion(), ORecordInternal.getRecordType(toUpdateRecord));
            }
            // CREATE LAST RECORD
            result = new OCreateRecordTask((ORecordId) goodResult.getIdentity(), content, version, recordType);
        } else if (badResult.getIdentity().getClusterId() == goodResult.getIdentity().getClusterId() && badResult.getIdentity().getClusterPosition() > goodResult.getIdentity().getClusterPosition()) {
        } else
            // ANY OTHER CASE JUST DELETE IT
            result = new OFixCreateRecordTask(new ORecordId(badResult.getIdentity()), badResult.getVersion());
    }
    return result;
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) ORemoteTask(com.orientechnologies.orient.server.distributed.task.ORemoteTask) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 15 with ORawBuffer

use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.

the class OConsoleDatabaseApp method displayRawRecord.

@ConsoleCommand(description = "Display a record as raw bytes", onlineHelp = "Console-Command-Display-Raw-Record")
public void displayRawRecord(@ConsoleParameter(name = "rid", description = "The record id to display") final String iRecordId) throws IOException {
    checkForDatabase();
    ORecordId rid;
    if (iRecordId.indexOf(':') > -1)
        rid = new ORecordId(iRecordId);
    else {
        OIdentifiable rec = setCurrentRecord(Integer.parseInt(iRecordId));
        if (rec != null)
            rid = (ORecordId) rec.getIdentity();
        else
            return;
    }
    ORawBuffer record;
    ORecordId id = new ORecordId(rid);
    if (!(currentDatabase.getStorage() instanceof OLocalPaginatedStorage)) {
        record = currentDatabase.getStorage().readRecord(rid, null, false, false, null).getResult();
        if (record != null) {
            String content;
            if (Integer.parseInt(properties.get("maxBinaryDisplay")) < record.buffer.length)
                content = new String(Arrays.copyOf(record.buffer, Integer.parseInt(properties.get("maxBinaryDisplay"))));
            else
                content = new String(record.buffer);
            out.println("\nRaw record content. The size is " + record.buffer.length + " bytes, while settings force to print first " + content.length() + " bytes:\n\n" + content);
        }
    } else {
        final OLocalPaginatedStorage storage = (OLocalPaginatedStorage) currentDatabase.getStorage();
        final OPaginatedCluster cluster = (OPaginatedCluster) storage.getClusterById(id.getClusterId());
        if (cluster == null) {
            message("\n cluster with id %i does not exist", id.getClusterId());
            return;
        }
        message("\n\nLOW LEVEL CLUSTER INFO");
        final OPaginatedCluster.RECORD_STATUS status = cluster.getRecordStatus(id.getClusterPosition());
        message("\n status: %s", status);
        final OPaginatedClusterDebug debugInfo = cluster.readDebug(id.getClusterPosition());
        message("\n cluster fieldId: %d", debugInfo.fileId);
        message("\n cluster name: %s", cluster.getName());
        message("\n in cluster position: %d", debugInfo.clusterPosition);
        message("\n empty: %b", debugInfo.empty);
        message("\n contentSize: %d", debugInfo.contentSize);
        message("\n n-pages: %d", debugInfo.pages.size());
        message("\n\n +----------PAGE_ID---------------+------IN_PAGE_POSITION----------+---------IN_PAGE_SIZE-----------+----PAGE_CONTENT---->> ");
        for (OClusterPageDebug page : debugInfo.pages) {
            message("\n |%30d ", page.pageIndex);
            message(" |%30d ", page.inPagePosition);
            message(" |%30d ", page.inPageSize);
            message(" |%s", OBase64Utils.encodeBytes(page.content));
        }
        record = cluster.readRecord(id.getClusterPosition(), false);
    }
    if (record == null)
        throw new OSystemException("The record has been deleted");
    if ("ORecordSerializerBinary".equals(currentDatabase.getSerializer().toString())) {
        byte[] buff = record.getBuffer();
        ORecordSerializerBinaryDebug debugger = new ORecordSerializerBinaryDebug();
        ORecordSerializationDebug deserializeDebug = debugger.deserializeDebug(buff, currentDatabase);
        message("\n\nRECORD CONTENT INFO");
        message("\n class name: %s", deserializeDebug.className);
        message("\n fail on Reading: %b", deserializeDebug.readingFailure);
        message("\n fail position: %d", deserializeDebug.failPosition);
        if (deserializeDebug.readingException != null) {
            StringWriter writer = new StringWriter();
            deserializeDebug.readingException.printStackTrace(new PrintWriter(writer));
            message("\n Exception On Reading: %s", writer.getBuffer().toString());
        }
        message("\n number of properties : %d", deserializeDebug.properties.size());
        message("\n\n PROPERTIES");
        for (ORecordSerializationDebugProperty prop : deserializeDebug.properties) {
            message("\n  property name: %s", prop.name);
            message("\n  property type: %s", prop.type.name());
            message("\n  property globalId: %d", prop.globalId);
            message("\n  fail on reading: %b", prop.faildToRead);
            if (prop.faildToRead) {
                message("\n  failed on reading position: %b", prop.failPosition);
                StringWriter writer = new StringWriter();
                prop.readingException.printStackTrace(new PrintWriter(writer));
                message("\n  Exception on reading: %s", writer.getBuffer().toString());
            } else {
                if (prop.value instanceof ORidBag) {
                    message("\n  property value: ORidBug ");
                    ((ORidBag) prop.value).debugPrint(System.out);
                } else
                    message("\n  property value: %s", prop.value != null ? prop.value.toString() : "null");
            }
            message("\n");
        }
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OSystemException(com.orientechnologies.common.exception.OSystemException) OLocalPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.paginated.OLocalPaginatedStorage) ORecordSerializationDebugProperty(com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializationDebugProperty) ORecordSerializerBinaryDebug(com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializerBinaryDebug) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) OClusterPageDebug(com.orientechnologies.orient.core.storage.impl.local.paginated.OClusterPageDebug) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) OPaginatedClusterDebug(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedClusterDebug) ORecordSerializationDebug(com.orientechnologies.orient.core.serialization.serializer.record.binary.ORecordSerializationDebug) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Aggregations

ORawBuffer (com.orientechnologies.orient.core.storage.ORawBuffer)35 OPhysicalPosition (com.orientechnologies.orient.core.storage.OPhysicalPosition)23 ORecordId (com.orientechnologies.orient.core.id.ORecordId)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ORecord (com.orientechnologies.orient.core.record.ORecord)3 OStorage (com.orientechnologies.orient.core.storage.OStorage)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OPlaceholder (com.orientechnologies.orient.core.db.record.OPlaceholder)2 OCluster (com.orientechnologies.orient.core.storage.OCluster)2 OPaginatedCluster (com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)1 OSystemException (com.orientechnologies.common.exception.OSystemException)1 OStorageConfiguration (com.orientechnologies.orient.core.config.OStorageConfiguration)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OPaginatedClusterException (com.orientechnologies.orient.core.exception.OPaginatedClusterException)1