@Test public void shouldDetectDuplicate() { oldEngine = new DuplicateEngineChecker( DEFAULT_DUPLICATE_ENGINE_TIMEOUT_IN_MS, THIS_DIR, true); oldEngine.check(); newEngine = new DuplicateEngineChecker( DEFAULT_DUPLICATE_ENGINE_TIMEOUT_IN_MS, THIS_DIR, true); assertCheckThrows(newEngine); }
public void check() { if (file.exists()) { mappedByteBuffer = IoUtil.mapExistingFile(file, FILE_NAME); try { buffer.wrap(mappedByteBuffer); final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder(); final EngineInformationDecoder engineInformationDecoder = new EngineInformationDecoder(); messageHeaderDecoder.wrap(buffer, 0); engineInformationDecoder.wrap( buffer, MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final long heartbeatTimeInMs = engineInformationDecoder.heartbeatTimeInMs(); final String otherRuntimeName = engineInformationDecoder.runtimeName(); final long latestEngineHeartbeatInMs = System.currentTimeMillis() - duplicateEngineTimeoutInMs; if (heartbeatTimeInMs > latestEngineHeartbeatInMs && errorIfDuplicateEngineDetected) { throw new IllegalStateException(String.format( "Error starting Engine a duplicate Artio Engine instance might be running [%s] produced " + "heartbeat at %d, less than %d ms ago.", otherRuntimeName, heartbeatTimeInMs, duplicateEngineTimeoutInMs)); } } finally { IoUtil.unmap(mappedByteBuffer); } } final File tempFile = new File(logFileDir, UUID.randomUUID().toString() + "-" + FILE_NAME); final byte[] thisRuntimeName = ManagementFactory.getRuntimeMXBean().getName().getBytes(StandardCharsets.UTF_8); final long length = MessageHeaderEncoder.ENCODED_LENGTH + EngineInformationEncoder.BLOCK_LENGTH + EngineInformationEncoder.runtimeNameHeaderLength() + thisRuntimeName.length; mappedByteBuffer = IoUtil.mapNewFile(tempFile, length); buffer.wrap(mappedByteBuffer); engineInformation .wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()) .putRuntimeName(thisRuntimeName, 0, thisRuntimeName.length); saveheartbeatTime(System.currentTimeMillis()); mappedByteBuffer.force(); try { Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING); } catch (final IOException e) { throw new IllegalStateException(e); } }
DuplicateEngineChecker implements Agent { public void check() { if (file.exists()) { mappedByteBuffer = IoUtil.mapExistingFile(file, FILE_NAME); try { buffer.wrap(mappedByteBuffer); final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder(); final EngineInformationDecoder engineInformationDecoder = new EngineInformationDecoder(); messageHeaderDecoder.wrap(buffer, 0); engineInformationDecoder.wrap( buffer, MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final long heartbeatTimeInMs = engineInformationDecoder.heartbeatTimeInMs(); final String otherRuntimeName = engineInformationDecoder.runtimeName(); final long latestEngineHeartbeatInMs = System.currentTimeMillis() - duplicateEngineTimeoutInMs; if (heartbeatTimeInMs > latestEngineHeartbeatInMs && errorIfDuplicateEngineDetected) { throw new IllegalStateException(String.format( "Error starting Engine a duplicate Artio Engine instance might be running [%s] produced " + "heartbeat at %d, less than %d ms ago.", otherRuntimeName, heartbeatTimeInMs, duplicateEngineTimeoutInMs)); } } finally { IoUtil.unmap(mappedByteBuffer); } } final File tempFile = new File(logFileDir, UUID.randomUUID().toString() + "-" + FILE_NAME); final byte[] thisRuntimeName = ManagementFactory.getRuntimeMXBean().getName().getBytes(StandardCharsets.UTF_8); final long length = MessageHeaderEncoder.ENCODED_LENGTH + EngineInformationEncoder.BLOCK_LENGTH + EngineInformationEncoder.runtimeNameHeaderLength() + thisRuntimeName.length; mappedByteBuffer = IoUtil.mapNewFile(tempFile, length); buffer.wrap(mappedByteBuffer); engineInformation .wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()) .putRuntimeName(thisRuntimeName, 0, thisRuntimeName.length); saveheartbeatTime(System.currentTimeMillis()); mappedByteBuffer.force(); try { Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING); } catch (final IOException e) { throw new IllegalStateException(e); } } }
DuplicateEngineChecker implements Agent { public void check() { if (file.exists()) { mappedByteBuffer = IoUtil.mapExistingFile(file, FILE_NAME); try { buffer.wrap(mappedByteBuffer); final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder(); final EngineInformationDecoder engineInformationDecoder = new EngineInformationDecoder(); messageHeaderDecoder.wrap(buffer, 0); engineInformationDecoder.wrap( buffer, MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final long heartbeatTimeInMs = engineInformationDecoder.heartbeatTimeInMs(); final String otherRuntimeName = engineInformationDecoder.runtimeName(); final long latestEngineHeartbeatInMs = System.currentTimeMillis() - duplicateEngineTimeoutInMs; if (heartbeatTimeInMs > latestEngineHeartbeatInMs && errorIfDuplicateEngineDetected) { throw new IllegalStateException(String.format( "Error starting Engine a duplicate Artio Engine instance might be running [%s] produced " + "heartbeat at %d, less than %d ms ago.", otherRuntimeName, heartbeatTimeInMs, duplicateEngineTimeoutInMs)); } } finally { IoUtil.unmap(mappedByteBuffer); } } final File tempFile = new File(logFileDir, UUID.randomUUID().toString() + "-" + FILE_NAME); final byte[] thisRuntimeName = ManagementFactory.getRuntimeMXBean().getName().getBytes(StandardCharsets.UTF_8); final long length = MessageHeaderEncoder.ENCODED_LENGTH + EngineInformationEncoder.BLOCK_LENGTH + EngineInformationEncoder.runtimeNameHeaderLength() + thisRuntimeName.length; mappedByteBuffer = IoUtil.mapNewFile(tempFile, length); buffer.wrap(mappedByteBuffer); engineInformation .wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()) .putRuntimeName(thisRuntimeName, 0, thisRuntimeName.length); saveheartbeatTime(System.currentTimeMillis()); mappedByteBuffer.force(); try { Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING); } catch (final IOException e) { throw new IllegalStateException(e); } } DuplicateEngineChecker(
        final long duplicateEngineTimeoutInMs, final String logFileDir, final boolean errorIfDuplicateEngineDetected); }
DuplicateEngineChecker implements Agent { public void check() { if (file.exists()) { mappedByteBuffer = IoUtil.mapExistingFile(file, FILE_NAME); try { buffer.wrap(mappedByteBuffer); final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder(); final EngineInformationDecoder engineInformationDecoder = new EngineInformationDecoder(); messageHeaderDecoder.wrap(buffer, 0); engineInformationDecoder.wrap( buffer, MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final long heartbeatTimeInMs = engineInformationDecoder.heartbeatTimeInMs(); final String otherRuntimeName = engineInformationDecoder.runtimeName(); final long latestEngineHeartbeatInMs = System.currentTimeMillis() - duplicateEngineTimeoutInMs; if (heartbeatTimeInMs > latestEngineHeartbeatInMs && errorIfDuplicateEngineDetected) { throw new IllegalStateException(String.format( "Error starting Engine a duplicate Artio Engine instance might be running [%s] produced " + "heartbeat at %d, less than %d ms ago.", otherRuntimeName, heartbeatTimeInMs, duplicateEngineTimeoutInMs)); } } finally { IoUtil.unmap(mappedByteBuffer); } } final File tempFile = new File(logFileDir, UUID.randomUUID().toString() + "-" + FILE_NAME); final byte[] thisRuntimeName = ManagementFactory.getRuntimeMXBean().getName().getBytes(StandardCharsets.UTF_8); final long length = MessageHeaderEncoder.ENCODED_LENGTH + EngineInformationEncoder.BLOCK_LENGTH + EngineInformationEncoder.runtimeNameHeaderLength() + thisRuntimeName.length; mappedByteBuffer = IoUtil.mapNewFile(tempFile, length); buffer.wrap(mappedByteBuffer); engineInformation .wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()) .putRuntimeName(thisRuntimeName, 0, thisRuntimeName.length); saveheartbeatTime(System.currentTimeMillis()); mappedByteBuffer.force(); try { Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING); } catch (final IOException e) { throw new IllegalStateException(e); } } DuplicateEngineChecker(
        final long duplicateEngineTimeoutInMs, final String logFileDir, final boolean errorIfDuplicateEngineDetected); void check(); int doWork(); String roleName(); void finalClose(); }
DuplicateEngineChecker implements Agent { public void check() { if (file.exists()) { mappedByteBuffer = IoUtil.mapExistingFile(file, FILE_NAME); try { buffer.wrap(mappedByteBuffer); final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder(); final EngineInformationDecoder engineInformationDecoder = new EngineInformationDecoder(); messageHeaderDecoder.wrap(buffer, 0); engineInformationDecoder.wrap( buffer, MessageHeaderDecoder.ENCODED_LENGTH, messageHeaderDecoder.blockLength(), messageHeaderDecoder.version()); final long heartbeatTimeInMs = engineInformationDecoder.heartbeatTimeInMs(); final String otherRuntimeName = engineInformationDecoder.runtimeName(); final long latestEngineHeartbeatInMs = System.currentTimeMillis() - duplicateEngineTimeoutInMs; if (heartbeatTimeInMs > latestEngineHeartbeatInMs && errorIfDuplicateEngineDetected) { throw new IllegalStateException(String.format( "Error starting Engine a duplicate Artio Engine instance might be running [%s] produced " + "heartbeat at %d, less than %d ms ago.", otherRuntimeName, heartbeatTimeInMs, duplicateEngineTimeoutInMs)); } } finally { IoUtil.unmap(mappedByteBuffer); } } final File tempFile = new File(logFileDir, UUID.randomUUID().toString() + "-" + FILE_NAME); final byte[] thisRuntimeName = ManagementFactory.getRuntimeMXBean().getName().getBytes(StandardCharsets.UTF_8); final long length = MessageHeaderEncoder.ENCODED_LENGTH + EngineInformationEncoder.BLOCK_LENGTH + EngineInformationEncoder.runtimeNameHeaderLength() + thisRuntimeName.length; mappedByteBuffer = IoUtil.mapNewFile(tempFile, length); buffer.wrap(mappedByteBuffer); engineInformation .wrapAndApplyHeader(buffer, 0, new MessageHeaderEncoder()) .putRuntimeName(thisRuntimeName, 0, thisRuntimeName.length); saveheartbeatTime(System.currentTimeMillis()); mappedByteBuffer.force(); try { Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING); } catch (final IOException e) { throw new IllegalStateException(e); } } DuplicateEngineChecker(
        final long duplicateEngineTimeoutInMs, final String logFileDir, final boolean errorIfDuplicateEngineDetected); void check(); int doWork(); String roleName(); void finalClose();  }