use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OConflictResolverDatabaseRepairer method check.
private void check() throws Exception {
// OPEN THE DATABASE ONLY IF NEEDED
ODatabaseDocumentTx db = null;
try {
final int batchMax = OGlobalConfiguration.DISTRIBUTED_CONFLICT_RESOLVER_REPAIRER_BATCH.getValueAsInteger();
final List<ORecordId> rids = new ArrayList<ORecordId>(batchMax);
// REPAIR CLUSTER FIRST
for (Integer clusterId : clusters.keySet()) {
//repairCluster(db, clusterId);
}
clusters.clear();
// REPAIR RECORDS
for (ORecordId rid : records.keySet()) {
rids.add(rid);
if (rids.size() >= batchMax)
// REACHED MAXIMUM FOR BATCH
break;
}
if (!rids.isEmpty()) {
// REPAIR RECORDS IN BATCH
db = getDatabase();
if (repairRecords(db, rids)) {
// SUCCEED: REMOVE REPAIRED RECORDS
for (ORecordId rid : rids) records.remove(rid);
}
}
} finally {
if (db != null)
db.close();
}
}
use of com.orientechnologies.orient.core.id.ORecordId 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;
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OServerCommandPutIndex method execute.
@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: index/<database>/<index-name>/<key>[/<value>]");
iRequest.data.commandInfo = "Index put";
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
final OIndex<?> index = db.getMetadata().getIndexManager().getIndex(urlParts[2]);
if (index == null)
throw new IllegalArgumentException("Index name '" + urlParts[2] + "' not found");
final OIdentifiable record;
if (urlParts.length > 4)
// GET THE RECORD ID AS VALUE
record = new ORecordId(urlParts[4]);
else {
// GET THE REQUEST CONTENT AS DOCUMENT
if (iRequest.content == null || iRequest.content.length() == 0)
throw new IllegalArgumentException("Index's entry value is null");
record = new ODocument().fromJSON(iRequest.content);
}
final OIndexDefinition indexDefinition = index.getDefinition();
final Object key;
if (indexDefinition != null)
key = indexDefinition.createValue(urlParts[3]);
else
key = urlParts[3];
if (key == null)
throw new IllegalArgumentException("Invalid key value : " + urlParts[3]);
final boolean existent = record.getIdentity().isPersistent();
if (existent && record instanceof ORecord)
((ORecord) record).save();
index.put(key, record);
if (existent)
iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
else
iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class ONetworkProtocolBinary method readRecord.
protected void readRecord(final OClientConnection connection) throws IOException {
setDataCommandInfo(connection, "Load record");
if (!isConnectionAlive(connection))
return;
final ORecordId rid = channel.readRID();
final String fetchPlanString = channel.readString();
boolean ignoreCache = false;
ignoreCache = channel.readByte() == 1;
boolean loadTombstones = false;
loadTombstones = channel.readByte() > 0;
if (rid.getClusterId() == 0 && rid.getClusterPosition() == 0) {
// @COMPATIBILITY 0.9.25
// SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
OFetchHelper.checkFetchPlanValid(fetchPlanString);
beginResponse();
try {
sendOk(connection, clientTxId);
channel.writeByte((byte) 1);
final byte[] storageStream = connection.getDatabase().getStorage().callInLock(new Callable<byte[]>() {
@Override
public byte[] call() throws Exception {
return connection.getDatabase().getStorage().getConfiguration().toStream(connection.getData().protocolVersion);
}
}, false);
if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
channel.writeBytes(storageStream);
channel.writeVersion(0);
channel.writeByte(OBlob.RECORD_TYPE);
} else {
channel.writeByte(OBlob.RECORD_TYPE);
channel.writeVersion(0);
channel.writeBytes(storageStream);
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
} else {
final ORecord record = connection.getDatabase().load(rid, fetchPlanString, ignoreCache, loadTombstones, OStorage.LOCKING_STRATEGY.NONE);
beginResponse();
try {
sendOk(connection, clientTxId);
if (record != null) {
// HAS RECORD
channel.writeByte((byte) 1);
byte[] bytes = getRecordBytes(connection, record);
int length = trimCsvSerializedContent(connection, bytes);
if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
channel.writeBytes(bytes, length);
channel.writeVersion(record.getVersion());
channel.writeByte(ORecordInternal.getRecordType(record));
} else {
channel.writeByte(ORecordInternal.getRecordType(record));
channel.writeVersion(record.getVersion());
channel.writeBytes(bytes, length);
}
if (fetchPlanString.length() > 0) {
// PLAN
if (record instanceof ODocument) {
final OFetchPlan fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
final Set<ORecord> recordsToSend = new HashSet<ORecord>();
final ODocument doc = (ODocument) record;
final OFetchListener listener = new ORemoteFetchListener() {
@Override
protected void sendRecord(ORecord iLinked) {
recordsToSend.add(iLinked);
}
};
final OFetchContext context = new ORemoteFetchContext();
OFetchHelper.fetch(doc, doc, fetchPlan, listener, context, "");
// SEND RECORDS TO LOAD IN CLIENT CACHE
for (ORecord d : recordsToSend) {
if (d.getIdentity().isValid()) {
// CLIENT CACHE
channel.writeByte((byte) 2);
// RECORD. IT ISN'T PART OF THE RESULT SET
writeIdentifiable(connection, d);
}
}
}
}
}
// NO MORE RECORDS
channel.writeByte((byte) 0);
} finally {
endResponse(connection);
}
}
}
use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.
the class OAbstractRecordReplicatedTask method execute.
@Override
public final Object execute(final ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
final ODistributedDatabase ddb = iManager.getMessageService().getDatabase(database.getName());
ORecordId rid2Lock = rid;
if (!rid.isPersistent())
// CREATE A COPY TO MAINTAIN THE LOCK ON THE CLUSTER AVOIDING THE RID IS TRANSFORMED IN PERSISTENT. THIS ALLOWS TO HAVE
// PARALLEL TX BECAUSE NEW RID LOCKS THE ENTIRE CLUSTER.
rid2Lock = new ORecordId(rid.getClusterId(), -1l);
if (lockRecords) {
// TRY LOCKING RECORD
ddb.lockRecord(rid2Lock, requestId, OGlobalConfiguration.DISTRIBUTED_CRUD_TASK_SYNCH_TIMEOUT.getValueAsLong() / 2);
}
try {
return executeRecordTask(requestId, iServer, iManager, database);
} finally {
if (lockRecords)
// UNLOCK THE SINGLE OPERATION. IN TX WAIT FOR THE 2-PHASE COMMIT/ROLLBACK/FIX MESSAGE
ddb.unlockRecord(rid2Lock, requestId);
}
}
Aggregations