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");
}
}
}
use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.
the class ODistributedDatabaseImpl method lockRecord.
@Override
public boolean lockRecord(final ORID rid, final ODistributedRequestId iRequestId, final long timeout) {
// TODO: IMPROVE THIS BY RECEIVING THE RECORD AS PARAMETER INSTEAD OF RELOADING IT
ORawBuffer originalRecord = null;
if (rid.isPersistent()) {
final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
if (db != null)
originalRecord = db.getStorage().getUnderlying().readRecord((ORecordId) rid, null, false, true, null).getResult();
}
final ODistributedLock lock = new ODistributedLock(iRequestId, originalRecord);
boolean newLock = true;
ODistributedLock currentLock = lockManager.putIfAbsent(rid, lock);
if (currentLock != null) {
if (iRequestId.equals(currentLock.reqId)) {
// SAME ID, ALREADY LOCKED
ODistributedServerLog.debug(this, localNodeName, null, DIRECTION.NONE, "Distributed transaction: %s locked record %s in database '%s' owned by %s (thread=%d)", iRequestId, rid, databaseName, currentLock.reqId, Thread.currentThread().getId());
currentLock = null;
newLock = false;
} else {
// TRY TO RE-LOCK IT UNTIL TIMEOUT IS EXPIRED
final long startTime = System.currentTimeMillis();
do {
try {
if (timeout > 0) {
if (!currentLock.lock.await(timeout, TimeUnit.MILLISECONDS))
continue;
} else
currentLock.lock.await();
currentLock = lockManager.putIfAbsent(rid, lock);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
} while (currentLock != null && (timeout == 0 || System.currentTimeMillis() - startTime < timeout));
}
}
//
if (ODistributedServerLog.isDebugEnabled())
if (currentLock == null) {
ODistributedServerLog.debug(this, localNodeName, null, DIRECTION.NONE, "Distributed transaction: %s locked record %s in database '%s' (thread=%d)", iRequestId, rid, databaseName, Thread.currentThread().getId());
} else {
ODistributedServerLog.debug(this, localNodeName, null, DIRECTION.NONE, "Distributed transaction: %s cannot lock record %s in database '%s' owned by %s (thread=%d)", iRequestId, rid, databaseName, currentLock.reqId, Thread.currentThread().getId());
}
if (currentLock != null)
throw new ODistributedRecordLockedException(manager.getLocalNodeName(), rid, currentLock.reqId, timeout);
return newLock;
}
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;
}
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;
}
use of com.orientechnologies.orient.core.storage.ORawBuffer in project orientdb by orientechnologies.
the class LocalPaginatedClusterTest method testUpdateManyBigRecords.
public void testUpdateManyBigRecords() throws IOException {
final int records = 5000;
long seed = System.currentTimeMillis();
Random mersenneTwisterFast = new Random(seed);
System.out.println("testUpdateManyBigRecords seed : " + seed);
Map<Long, byte[]> positionRecordMap = new HashMap<Long, byte[]>();
Set<Long> updatedPositions = new HashSet<Long>();
int recordVersion = 0;
recordVersion++;
recordVersion++;
for (int i = 0; i < records; i++) {
int recordSize = mersenneTwisterFast.nextInt(2 * OClusterPage.MAX_RECORD_SIZE) + OClusterPage.MAX_RECORD_SIZE + 1;
byte[] bigRecord = new byte[recordSize];
mersenneTwisterFast.nextBytes(bigRecord);
final OPhysicalPosition physicalPosition = paginatedCluster.createRecord(bigRecord, recordVersion, (byte) 2, null);
positionRecordMap.put(physicalPosition.clusterPosition, bigRecord);
}
int newRecordVersion = 0;
newRecordVersion = recordVersion;
newRecordVersion++;
for (long clusterPosition : positionRecordMap.keySet()) {
if (mersenneTwisterFast.nextBoolean()) {
int recordSize = mersenneTwisterFast.nextInt(2 * OClusterPage.MAX_RECORD_SIZE) + OClusterPage.MAX_RECORD_SIZE + 1;
byte[] bigRecord = new byte[recordSize];
mersenneTwisterFast.nextBytes(bigRecord);
paginatedCluster.updateRecord(clusterPosition, bigRecord, newRecordVersion, (byte) 3);
positionRecordMap.put(clusterPosition, bigRecord);
updatedPositions.add(clusterPosition);
}
}
for (Map.Entry<Long, byte[]> entry : positionRecordMap.entrySet()) {
ORawBuffer rawBuffer = paginatedCluster.readRecord(entry.getKey(), false);
Assert.assertNotNull(rawBuffer);
Assert.assertEquals(rawBuffer.buffer, entry.getValue());
if (updatedPositions.contains(entry.getKey())) {
Assert.assertEquals(rawBuffer.version, newRecordVersion);
Assert.assertEquals(rawBuffer.recordType, 3);
} else {
Assert.assertEquals(rawBuffer.version, recordVersion);
Assert.assertEquals(rawBuffer.recordType, 2);
}
}
}
Aggregations