use of net.openhft.chronicle.wire.UnrecoverableTimeoutException in project Chronicle-Queue by OpenHFT.
the class SimpleStoreRecovery method recoverAndWriteHeader.
@Override
public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, @NotNull final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException {
Jvm.warn().on(getClass(), "Clearing an incomplete header so a header can be written");
wire.bytes().writeInt(0);
wire.pauser().reset();
try {
return wire.writeHeaderOfUnknownLength(timeoutMS, TimeUnit.MILLISECONDS, lastPosition, sequence);
} catch (@NotNull TimeoutException | EOFException e) {
throw new UnrecoverableTimeoutException(e);
}
}
use of net.openhft.chronicle.wire.UnrecoverableTimeoutException in project Chronicle-Queue by OpenHFT.
the class TimedStoreRecovery method recoverAndWriteHeader.
@Override
public long recoverAndWriteHeader(@NotNull Wire wire, long timeoutMS, final LongValue lastPosition, Sequence sequence) throws UnrecoverableTimeoutException, EOFException {
Bytes<?> bytes = wire.bytes();
long offset = bytes.writePosition();
int num = bytes.readVolatileInt(offset);
// header number is only updated after successful write
final long targetHeaderNumber = wire.headerNumber() + 1;
String msgStart = "Unable to write a header at header number: 0x" + Long.toHexString(targetHeaderNumber) + " position: " + offset;
if (Wires.isNotComplete(num)) {
// TODO Determine what the safe size should be.
int sizeToSkip = 32 << 10;
if (bytes instanceof MappedBytes) {
MappedBytes mb = (MappedBytes) bytes;
sizeToSkip = Maths.toUInt31(mb.mappedFile().overlapSize() / 2);
}
// pad to a 4 byte word.
sizeToSkip = (sizeToSkip + 3) & ~3;
sizeToSkip -= (int) (offset & 3);
// clearing the start of the data so the meta data will look like 4 zero values with no event names.
long pos = bytes.writePosition();
try {
bytes.writeSkip(4);
final String debugMessage = "!! Skipped due to recovery of locked header !! By thread " + Thread.currentThread().getName() + ", pid " + OS.getProcessId();
wire.getValueOut().text(debugMessage);
final StringWriter stackVisitor = new StringWriter();
new RuntimeException().printStackTrace(new PrintWriter(stackVisitor));
final String stackTrace = stackVisitor.toString();
// ensure there is enough space to record a stack trace for debugging purposes
if (debugMessage.length() + stackTrace.length() + 16 < sizeToSkip) {
wire.getValueOut().text(stackTrace);
}
wire.addPadding(Math.toIntExact(sizeToSkip + (pos + 4) - bytes.writePosition()));
} finally {
bytes.writePosition(pos);
}
int emptyMetaData = Wires.META_DATA | sizeToSkip;
if (bytes.compareAndSwapInt(offset, num, emptyMetaData)) {
warn().on(getClass(), msgStart + " switching to a corrupt meta data message");
bytes.writeSkip(sizeToSkip + 4);
} else {
int num2 = bytes.readVolatileInt(offset);
warn().on(getClass(), msgStart + " already set to " + Integer.toHexString(num2));
}
} else {
warn().on(getClass(), msgStart + " but message now exists.");
}
try {
return wire.writeHeaderOfUnknownLength(timeoutMS, TimeUnit.MILLISECONDS, lastPosition, sequence);
} catch (TimeoutException e) {
warn().on(getClass(), e);
// Could happen if another thread recovers, writes 2 messages but the second one is corrupt.
return recoverAndWriteHeader(wire, timeoutMS, lastPosition, sequence);
} catch (EOFException e) {
throw new AssertionError(e);
}
}
use of net.openhft.chronicle.wire.UnrecoverableTimeoutException in project Chronicle-Queue by OpenHFT.
the class SimpleStoreRecovery method recoverSecondaryAddress.
@Override
public long recoverSecondaryAddress(@NotNull LongArrayValues index2indexArr, int index2, @NotNull Callable<Long> action, long timeoutMS) throws UnrecoverableTimeoutException {
Jvm.warn().on(getClass(), "Timed out trying to get index2index[" + index2 + "]");
index2indexArr.setValueAt(index2, 0L);
try {
return action.call();
} catch (TimeoutException e) {
throw new UnrecoverableTimeoutException(e);
} catch (Exception e) {
throw Jvm.rethrow(e);
}
}
Aggregations